Skip to content

Commit 0c55444

Browse files
Update with Devin's proposed changes to prevent inflation from unrelated commits.
1 parent 8314626 commit 0c55444

2 files changed

Lines changed: 18 additions & 39 deletions

File tree

‎change-proneness-ranker/src/main/java/org/hjug/git/GitLogReader.java‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -136,17 +136,6 @@ public ScmLogInfo fileLog(String path) throws GitAPIException, IOException {
136136
CommitWalkStats stats =
137137
walkCommits(git.log().add(branchId).addPath(path).call());
138138

139-
if (stats.commitCount == 0 && stats.truncatedByMissingObjects) {
140-
// Shallow or partial clone: the path-filtered history is unreadable, but a
141-
// pathless walk only needs commit objects, so fall back to the total commit
142-
// history as a degraded change-proneness signal.
143-
log.warn(
144-
"No commits could be read for {} due to missing Git objects; "
145-
+ "falling back to the total repository commit count",
146-
path);
147-
stats = walkCommits(git.log().add(branchId).call());
148-
}
149-
150139
if (stats.commitCount == 0) {
151140
return new ScmLogInfo(path, null, stats.earliestCommit, stats.earliestCommit, stats.commitCount);
152141
}
@@ -176,7 +165,6 @@ private static CommitWalkStats walkCommits(Iterable<RevCommit> revCommits) {
176165
} catch (RevWalkException e) {
177166
// JGit wraps checked exceptions thrown mid-walk in a RevWalkException.
178167
if (isCausedByMissingObject(e)) {
179-
stats.truncatedByMissingObjects = true;
180168
log.warn(
181169
"Missing Git object while reading history (shallow or partial clone?); "
182170
+ "reporting the {} commit(s) that could be read. Cause: {}",
@@ -204,7 +192,6 @@ private static class CommitWalkStats {
204192
int commitCount = 0;
205193
int earliestCommit = Integer.MAX_VALUE;
206194
int mostRecentCommit = 0;
207-
boolean truncatedByMissingObjects = false;
208195
}
209196

210197
// based on https://stackoverflow.com/questions/27361538/how-to-show-changes-between-commits-with-jgit

‎change-proneness-ranker/src/test/java/org/hjug/git/GitLogReaderTest.java‎

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -125,25 +125,17 @@ void testFileLogRethrowsNonMissingObjectWalkFailures() throws Exception {
125125
}
126126

127127
/**
128-
* A Git whose successive log commands return the given iterables; the underlying
128+
* A Git whose log command returns the given iterable; the underlying
129129
* repository is real so that HEAD resolution works.
130130
*/
131-
@SafeVarargs
132-
private final Git gitWithLogs(Iterable<RevCommit>... iterables) throws IOException, GitAPIException {
131+
private final Git gitWithLogs(Iterable<RevCommit> revCommits) throws IOException, GitAPIException {
133132
Git mockGit = mock(Git.class);
134133
when(mockGit.getRepository()).thenReturn(repository);
135-
List<LogCommand> logCommands = new ArrayList<>();
136-
for (Iterable<RevCommit> revCommits : iterables) {
137-
LogCommand logCommand = mock(LogCommand.class);
138-
when(logCommand.add(any(ObjectId.class))).thenReturn(logCommand);
139-
when(logCommand.addPath(anyString())).thenReturn(logCommand);
140-
when(logCommand.call()).thenReturn(revCommits);
141-
logCommands.add(logCommand);
142-
}
143-
when(mockGit.log())
144-
.thenReturn(
145-
logCommands.get(0),
146-
logCommands.subList(1, logCommands.size()).toArray(new LogCommand[0]));
134+
LogCommand logCommand = mock(LogCommand.class);
135+
when(logCommand.add(any(ObjectId.class))).thenReturn(logCommand);
136+
when(logCommand.addPath(anyString())).thenReturn(logCommand);
137+
when(logCommand.call()).thenReturn(revCommits);
138+
when(mockGit.log()).thenReturn(logCommand);
147139
return mockGit;
148140
}
149141

@@ -173,7 +165,8 @@ private ObjectId headId() throws IOException {
173165
}
174166

175167
@Test
176-
void testFileLogFallsBackToTotalCommitCountWhenFilteredWalkYieldsNothingDueToMissingObjects() throws Exception {
168+
void testFileLogReturnsOnlyVerifiedPartialPathHistoryWhenFilteredWalkYieldsNothingDueToMissingObjects()
169+
throws Exception {
177170
String attributeHandler = "AttributeHandler.java";
178171
InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(attributeHandler);
179172
writeFile(attributeHandler, convertInputStreamToString(resourceAsStream));
@@ -183,21 +176,20 @@ void testFileLogFallsBackToTotalCommitCountWhenFilteredWalkYieldsNothingDueToMis
183176
Iterable<RevCommit> failingFilteredWalk = walkYieldingThenThrowing(
184177
Collections.emptyList(),
185178
new RevWalkException(new MissingObjectException(headId(), Constants.OBJ_TREE)));
186-
Iterable<RevCommit> pathlessWalk = Collections.singletonList(onlyCommit);
187179

188-
GitLogReader gitLogReader = new GitLogReader(gitWithLogs(failingFilteredWalk, pathlessWalk));
180+
GitLogReader gitLogReader = new GitLogReader(gitWithLogs(failingFilteredWalk));
189181

190182
ScmLogInfo scmLogInfo = Assertions.assertDoesNotThrow(() -> gitLogReader.fileLog(attributeHandler));
191183

192-
Assertions.assertEquals(1, scmLogInfo.getCommitCount());
193-
Assertions.assertEquals(onlyCommit.getCommitTime(), scmLogInfo.getEarliestCommit());
194-
Assertions.assertEquals(onlyCommit.getCommitTime(), scmLogInfo.getMostRecentCommit());
184+
// When the filtered walk yields nothing due to missing objects, return only the verified partial results
185+
// (which in this case is zero commits) rather than falling back to total repository history
186+
Assertions.assertEquals(0, scmLogInfo.getCommitCount());
195187
}
196188

197189
@Test
198-
void testFileLogWithMissingTreeFallsBackToTotalCommitCount() throws Exception {
190+
void testFileLogReturnsOnlyVerifiedPartialPathHistoryWithMissingTree() throws Exception {
199191
// Simulates a shallow clone whose tree objects are missing: the filtered walk
200-
// yields nothing, so fileLog falls back to the total (tree-independent) history.
192+
// may yield zero results due to missing objects, but should not fall back to total history.
201193
GitLogReader gitLogReader = new GitLogReader(git);
202194

203195
String attributeHandler = "AttributeHandler.java";
@@ -218,9 +210,9 @@ void testFileLogWithMissingTreeFallsBackToTotalCommitCount() throws Exception {
218210

219211
ScmLogInfo scmLogInfo = Assertions.assertDoesNotThrow(() -> gitLogReader.fileLog(attributeHandler));
220212

221-
Assertions.assertEquals(2, scmLogInfo.getCommitCount());
222-
Assertions.assertEquals(firstCommit.getCommitTime(), scmLogInfo.getEarliestCommit());
223-
Assertions.assertEquals(secondCommit.getCommitTime(), scmLogInfo.getMostRecentCommit());
213+
// With missing tree objects, the filtered walk may yield zero verified partial results
214+
// rather than falling back to total repository history
215+
Assertions.assertEquals(0, scmLogInfo.getCommitCount());
224216
}
225217

226218
@Test

0 commit comments

Comments
 (0)