|
1 | 1 | package org.hjug.git; |
2 | 2 |
|
3 | 3 | import static java.nio.charset.StandardCharsets.UTF_8; |
| 4 | +import static org.mockito.ArgumentMatchers.any; |
| 5 | +import static org.mockito.ArgumentMatchers.anyString; |
| 6 | +import static org.mockito.Mockito.mock; |
| 7 | +import static org.mockito.Mockito.when; |
4 | 8 |
|
5 | 9 | import java.io.*; |
6 | 10 | import java.util.*; |
7 | 11 | import org.eclipse.jgit.api.Git; |
| 12 | +import org.eclipse.jgit.api.LogCommand; |
8 | 13 | import org.eclipse.jgit.api.errors.GitAPIException; |
| 14 | +import org.eclipse.jgit.errors.MissingObjectException; |
| 15 | +import org.eclipse.jgit.errors.RevWalkException; |
| 16 | +import org.eclipse.jgit.lib.Constants; |
| 17 | +import org.eclipse.jgit.lib.ObjectId; |
9 | 18 | import org.eclipse.jgit.lib.Repository; |
10 | 19 | import org.eclipse.jgit.revwalk.RevCommit; |
11 | 20 | import org.junit.jupiter.api.AfterEach; |
@@ -66,6 +75,183 @@ void testFileLog() throws IOException, GitAPIException, InterruptedException { |
66 | 75 | Assertions.assertEquals(secondCommit.getCommitTime(), scmLogInfo.getMostRecentCommit()); |
67 | 76 | } |
68 | 77 |
|
| 78 | + @Test |
| 79 | + void testFileLogReturnsPartialResultsWhenWalkFailsMidIteration() throws Exception { |
| 80 | + // A missing tree encountered mid-walk (e.g. in a partial clone) must not |
| 81 | + // discard the commits that were already read: fileLog should return the |
| 82 | + // partial results gathered before the walk failed. |
| 83 | + String attributeHandler = "AttributeHandler.java"; |
| 84 | + InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(attributeHandler); |
| 85 | + String contents = convertInputStreamToString(resourceAsStream); |
| 86 | + |
| 87 | + writeFile(attributeHandler, contents); |
| 88 | + git.add().addFilepattern(".").call(); |
| 89 | + git.commit().setMessage("message").call(); |
| 90 | + |
| 91 | + Thread.sleep(1000); |
| 92 | + |
| 93 | + writeFile(attributeHandler, contents + "\n// second revision\n"); |
| 94 | + git.add().addFilepattern(".").call(); |
| 95 | + RevCommit newestCommit = git.commit().setMessage("message").call(); |
| 96 | + |
| 97 | + Iterable<RevCommit> failingWalk = walkYieldingThenThrowing( |
| 98 | + List.of(newestCommit), new RevWalkException(new MissingObjectException(headId(), Constants.OBJ_TREE))); |
| 99 | + |
| 100 | + GitLogReader gitLogReader = new GitLogReader(gitWithLogs(failingWalk)); |
| 101 | + |
| 102 | + ScmLogInfo scmLogInfo = Assertions.assertDoesNotThrow(() -> gitLogReader.fileLog(attributeHandler)); |
| 103 | + |
| 104 | + Assertions.assertEquals(1, scmLogInfo.getCommitCount()); |
| 105 | + Assertions.assertEquals(newestCommit.getCommitTime(), scmLogInfo.getEarliestCommit()); |
| 106 | + Assertions.assertEquals(newestCommit.getCommitTime(), scmLogInfo.getMostRecentCommit()); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + void testFileLogRethrowsNonMissingObjectWalkFailures() throws Exception { |
| 111 | + // Guard against silencing unrelated walk failures: only missing objects |
| 112 | + // should be tolerated, anything else must still propagate. |
| 113 | + String attributeHandler = "AttributeHandler.java"; |
| 114 | + InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(attributeHandler); |
| 115 | + writeFile(attributeHandler, convertInputStreamToString(resourceAsStream)); |
| 116 | + git.add().addFilepattern(".").call(); |
| 117 | + git.commit().setMessage("message").call(); |
| 118 | + |
| 119 | + Iterable<RevCommit> failingWalk = |
| 120 | + walkYieldingThenThrowing(Collections.emptyList(), new RevWalkException(new IOException("disk I/O"))); |
| 121 | + |
| 122 | + GitLogReader gitLogReader = new GitLogReader(gitWithLogs(failingWalk)); |
| 123 | + |
| 124 | + Assertions.assertThrows(RevWalkException.class, () -> gitLogReader.fileLog("AttributeHandler.java")); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * A Git whose successive log commands return the given iterables; the underlying |
| 129 | + * repository is real so that HEAD resolution works. |
| 130 | + */ |
| 131 | + @SafeVarargs |
| 132 | + private final Git gitWithLogs(Iterable<RevCommit>... iterables) throws IOException, GitAPIException { |
| 133 | + Git mockGit = mock(Git.class); |
| 134 | + 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])); |
| 147 | + return mockGit; |
| 148 | + } |
| 149 | + |
| 150 | + /** An iterable that yields the given commits and then fails, mimicking a truncated walk. */ |
| 151 | + private static Iterable<RevCommit> walkYieldingThenThrowing(List<RevCommit> commits, RuntimeException failure) { |
| 152 | + return () -> new Iterator<RevCommit>() { |
| 153 | + private final Iterator<RevCommit> delegate = commits.iterator(); |
| 154 | + |
| 155 | + @Override |
| 156 | + public boolean hasNext() { |
| 157 | + return true; |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public RevCommit next() { |
| 162 | + if (delegate.hasNext()) { |
| 163 | + return delegate.next(); |
| 164 | + } |
| 165 | + throw failure; |
| 166 | + } |
| 167 | + }; |
| 168 | + } |
| 169 | + |
| 170 | + private ObjectId headId() throws IOException { |
| 171 | + ObjectId head = repository.resolve("HEAD"); |
| 172 | + return head == null ? ObjectId.fromString("1111111111111111111111111111111111111111") : head; |
| 173 | + } |
| 174 | + |
| 175 | + @Test |
| 176 | + void testFileLogFallsBackToTotalCommitCountWhenFilteredWalkYieldsNothingDueToMissingObjects() throws Exception { |
| 177 | + String attributeHandler = "AttributeHandler.java"; |
| 178 | + InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(attributeHandler); |
| 179 | + writeFile(attributeHandler, convertInputStreamToString(resourceAsStream)); |
| 180 | + git.add().addFilepattern(".").call(); |
| 181 | + RevCommit onlyCommit = git.commit().setMessage("message").call(); |
| 182 | + |
| 183 | + Iterable<RevCommit> failingFilteredWalk = walkYieldingThenThrowing( |
| 184 | + Collections.emptyList(), |
| 185 | + new RevWalkException(new MissingObjectException(headId(), Constants.OBJ_TREE))); |
| 186 | + Iterable<RevCommit> pathlessWalk = Collections.singletonList(onlyCommit); |
| 187 | + |
| 188 | + GitLogReader gitLogReader = new GitLogReader(gitWithLogs(failingFilteredWalk, pathlessWalk)); |
| 189 | + |
| 190 | + ScmLogInfo scmLogInfo = Assertions.assertDoesNotThrow(() -> gitLogReader.fileLog(attributeHandler)); |
| 191 | + |
| 192 | + Assertions.assertEquals(1, scmLogInfo.getCommitCount()); |
| 193 | + Assertions.assertEquals(onlyCommit.getCommitTime(), scmLogInfo.getEarliestCommit()); |
| 194 | + Assertions.assertEquals(onlyCommit.getCommitTime(), scmLogInfo.getMostRecentCommit()); |
| 195 | + } |
| 196 | + |
| 197 | + @Test |
| 198 | + void testFileLogWithMissingTreeFallsBackToTotalCommitCount() throws Exception { |
| 199 | + // 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. |
| 201 | + GitLogReader gitLogReader = new GitLogReader(git); |
| 202 | + |
| 203 | + String attributeHandler = "AttributeHandler.java"; |
| 204 | + InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(attributeHandler); |
| 205 | + String contents = convertInputStreamToString(resourceAsStream); |
| 206 | + |
| 207 | + writeFile(attributeHandler, contents); |
| 208 | + git.add().addFilepattern(".").call(); |
| 209 | + RevCommit firstCommit = git.commit().setMessage("message").call(); |
| 210 | + |
| 211 | + Thread.sleep(1000); |
| 212 | + |
| 213 | + writeFile(attributeHandler, contents + "\n// second revision\n"); |
| 214 | + git.add().addFilepattern(".").call(); |
| 215 | + RevCommit secondCommit = git.commit().setMessage("message").call(); |
| 216 | + |
| 217 | + deleteLooseObject(secondCommit.getTree()); |
| 218 | + |
| 219 | + ScmLogInfo scmLogInfo = Assertions.assertDoesNotThrow(() -> gitLogReader.fileLog(attributeHandler)); |
| 220 | + |
| 221 | + Assertions.assertEquals(2, scmLogInfo.getCommitCount()); |
| 222 | + Assertions.assertEquals(firstCommit.getCommitTime(), scmLogInfo.getEarliestCommit()); |
| 223 | + Assertions.assertEquals(secondCommit.getCommitTime(), scmLogInfo.getMostRecentCommit()); |
| 224 | + } |
| 225 | + |
| 226 | + @Test |
| 227 | + void testFileLogDoesNotFabricateHistoryForFileNeverCommitted() throws Exception { |
| 228 | + // An empty filtered walk with NO missing objects means the file simply has no |
| 229 | + // history: the total commit count must NOT be substituted. |
| 230 | + GitLogReader gitLogReader = new GitLogReader(git); |
| 231 | + |
| 232 | + String attributeHandler = "AttributeHandler.java"; |
| 233 | + InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(attributeHandler); |
| 234 | + writeFile(attributeHandler, convertInputStreamToString(resourceAsStream)); |
| 235 | + git.add().addFilepattern(".").call(); |
| 236 | + git.commit().setMessage("message").call(); |
| 237 | + |
| 238 | + ScmLogInfo scmLogInfo = Assertions.assertDoesNotThrow(() -> gitLogReader.fileLog("NeverCommitted.java")); |
| 239 | + |
| 240 | + Assertions.assertEquals(0, scmLogInfo.getCommitCount()); |
| 241 | + } |
| 242 | + |
| 243 | + private void deleteLooseObject(ObjectId objectId) throws IOException { |
| 244 | + String objectName = objectId.getName(); |
| 245 | + File looseObject = new File( |
| 246 | + new File(repository.getDirectory(), "objects"), |
| 247 | + objectName.substring(0, 2) + '/' + objectName.substring(2)); |
| 248 | + org.junit.jupiter.api.Assumptions.assumeTrue( |
| 249 | + looseObject.exists(), "loose object " + objectName + " should exist in a fresh repository"); |
| 250 | + if (!looseObject.delete()) { |
| 251 | + throw new IOException("Unable to delete loose object " + looseObject); |
| 252 | + } |
| 253 | + } |
| 254 | + |
69 | 255 | @Test |
70 | 256 | void testWalkFirstCommit() throws IOException, GitAPIException { |
71 | 257 | GitLogReader gitLogReader = new GitLogReader(git); |
|
0 commit comments