diff options
Diffstat (limited to 'org.eclipse.jgit.test')
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java | 72 | ||||
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FilterCommandsTest.java | 31 |
2 files changed, 92 insertions, 11 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java index 57661a7eca..db2d5d1404 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java @@ -17,12 +17,16 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.junit.Assume.assumeTrue; +import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStream; import java.io.PrintWriter; import java.nio.file.Files; import java.util.Set; +import java.util.concurrent.TimeUnit; import org.eclipse.jgit.api.ResetCommand.ResetType; import org.eclipse.jgit.api.errors.FilterFailedException; @@ -825,7 +829,7 @@ public class AddCommandTest extends RepositoryTestCase { } @Test - public void testAddWholeRepo() throws Exception { + public void testAddWholeRepo() throws Exception { FileUtils.mkdir(new File(db.getWorkTree(), "sub")); File file = new File(db.getWorkTree(), "sub/a.txt"); FileUtils.createNewFile(file); @@ -848,6 +852,72 @@ public class AddCommandTest extends RepositoryTestCase { } } + @Test + public void testAddAllNoRenormalize() throws Exception { + final int nOfFiles = 1000; + final int filesPerDir = nOfFiles / 10; + final int fileSizeInBytes = 10_000; + assertTrue(nOfFiles > 0); + assertTrue(filesPerDir > 0); + File dir = null; + File lastFile = null; + for (int i = 0; i < nOfFiles; i++) { + if (i % filesPerDir == 0) { + dir = new File(db.getWorkTree(), "dir" + (i / filesPerDir)); + FileUtils.mkdir(dir); + } + lastFile = new File(dir, "file" + i); + try (OutputStream out = new BufferedOutputStream( + new FileOutputStream(lastFile))) { + for (int b = 0; b < fileSizeInBytes; b++) { + out.write('a' + (b % 26)); + if (((b + 1) % 70) == 0) { + out.write('\n'); + } + } + } + } + // Help null pointer analysis. + assert lastFile != null; + // Wait a bit. If entries are "racily clean", we'll recompute + // hashes from the disk files, and then the second add is also slow. + // We want to test the normal case. + fsTick(lastFile); + try (Git git = new Git(db)) { + long start = System.nanoTime(); + git.add().addFilepattern(".").call(); + long initialElapsed = System.nanoTime() - start; + assertEquals("Unexpected number on index entries", nOfFiles, + db.readDirCache().getEntryCount()); + start = System.nanoTime(); + git.add().addFilepattern(".").setRenormalize(false).call(); + long secondElapsed = System.nanoTime() - start; + assertEquals("Unexpected number on index entries", nOfFiles, + db.readDirCache().getEntryCount()); + // Fail the test if the second add all was not significantly faster. + // A factor of 4 is rather generous. The speed-up depends on the + // file system and OS caching and is hard to predict. + assertTrue( + "Second add all was too slow; initial took " + + TimeUnit.NANOSECONDS.toMillis(initialElapsed) + + ", second took " + + TimeUnit.NANOSECONDS.toMillis(secondElapsed), + secondElapsed * 4 <= initialElapsed); + // Change one file. The index should be updated even if + // renormalize==false. It doesn't matter what kind of change we do. + final String newData = "Hello"; + Files.writeString(lastFile.toPath(), newData); + git.add().addFilepattern(".").setRenormalize(false).call(); + DirCache dc = db.readDirCache(); + DirCacheEntry e = dc.getEntry(lastFile.getParentFile().getName() + + '/' + lastFile.getName()); + String blob = new String(db + .open(e.getObjectId(), Constants.OBJ_BLOB).getCachedBytes(), + UTF_8); + assertEquals("Unexpected index content", newData, blob); + } + } + // the same three cases as in testAddWithParameterUpdate // file a exists in workdir and in index -> added // file b exists not in workdir but in index -> unchanged diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FilterCommandsTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FilterCommandsTest.java index 89d31c3e8f..0513da2d15 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FilterCommandsTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FilterCommandsTest.java @@ -100,8 +100,7 @@ public class FilterCommandsTest extends RepositoryTestCase { } @Test - public void testBuiltinCleanFilter() - throws IOException, GitAPIException { + public void testBuiltinCleanFilter() throws Exception { String builtinCommandName = "jgit://builtin/test/clean"; FilterCommandRegistry.register(builtinCommandName, new TestCommandFactory('c')); @@ -113,28 +112,40 @@ public class FilterCommandsTest extends RepositoryTestCase { git.add().addFilepattern(".gitattributes").call(); git.commit().setMessage("add filter").call(); - writeTrashFile("Test.txt", "Hello again"); + File testFile = writeTrashFile("Test.txt", "Hello again"); + // Wait a little bit to ensure that the call with setRenormalize(false) + // below doesn't consider the file "racily clean". + fsTick(testFile); git.add().addFilepattern("Test.txt").call(); assertEquals( - "[.gitattributes, mode:100644, content:*.txt filter=test][Test.txt, mode:100644, content:cHceclclcoc cacgcacicn]", + "[.gitattributes, mode:100644, content:*.txt filter=test]" + + "[Test.txt, mode:100644, content:cHceclclcoc cacgcacicn]", indexState(CONTENT)); writeTrashFile("Test.bin", "Hello again"); git.add().addFilepattern("Test.bin").call(); assertEquals( - "[.gitattributes, mode:100644, content:*.txt filter=test][Test.bin, mode:100644, content:Hello again][Test.txt, mode:100644, content:cHceclclcoc cacgcacicn]", + "[.gitattributes, mode:100644, content:*.txt filter=test]" + + "[Test.bin, mode:100644, content:Hello again]" + + "[Test.txt, mode:100644, content:cHceclclcoc cacgcacicn]", indexState(CONTENT)); config.setString("filter", "test", "clean", null); config.save(); - git.add().addFilepattern("Test.txt").call(); - assertEquals( - "[.gitattributes, mode:100644, content:*.txt filter=test][Test.bin, mode:100644, content:Hello again][Test.txt, mode:100644, content:Hello again]", + git.add().addFilepattern("Test.txt").setRenormalize(false).call(); + assertEquals("No index update expected with renormalize==false", + "[.gitattributes, mode:100644, content:*.txt filter=test]" + + "[Test.bin, mode:100644, content:Hello again]" + + "[Test.txt, mode:100644, content:cHceclclcoc cacgcacicn]", indexState(CONTENT)); - config.setString("filter", "test", "clean", null); - config.save(); + git.add().addFilepattern("Test.txt").call(); + assertEquals("Index update expected with renormalize==true", + "[.gitattributes, mode:100644, content:*.txt filter=test]" + + "[Test.bin, mode:100644, content:Hello again]" + + "[Test.txt, mode:100644, content:Hello again]", + indexState(CONTENT)); } @Test |