diff options
author | Thomas Wolf <twolf@apache.org> | 2023-04-20 21:13:59 +0200 |
---|---|---|
committer | Matthias Sohn <matthias.sohn@sap.com> | 2023-04-28 17:04:47 -0400 |
commit | 3ed4cdda6b99f812258ca50bd77447a28d9d4596 (patch) | |
tree | a2e42d4924d4d157843178deb0f0741395885807 /org.eclipse.jgit.test | |
parent | 45de4fa2cb638a8e20fa27b3d88435a697bb9df8 (diff) | |
download | jgit-3ed4cdda6b99f812258ca50bd77447a28d9d4596.tar.gz jgit-3ed4cdda6b99f812258ca50bd77447a28d9d4596.zip |
AddCommand: ability to switch off renormalization
JGit's AddCommand always renormalizes tracked files. C git does so only
on git add --renormalize. Especially for git add . and the JGit
equivalent git.add().addFilepattern(".").call() this can make a big
difference if there are many files, or large files.
Add a "renormalize" option to AddCommand. To maintain compatibility with
existing uses, this option is "true" by default, and the behavior of
AddCommand is as it has always been in JGit.
If set to "false", use an IndexDiffFilter (in addition to a path filter,
if any). This skips any unchanged files (that are not racily clean) from
content checks. Note that changes in CRLF settings or in filters will be
ignored for such files if renormalize == false.
Add the "--renormalize" option to the Add command in the JGit command
line program. For the command-line program, the default is as in C git:
renormalize is off by default and enabled only if the option is given.
Note that --renormalize implies --update in the command line program, as
in C git. In AddCommand, the two settings are independent.
Additionally, avoid opening input streams unnecessarily in
WorkingTreeIterator.getEntryContentLength() and fix some bogus
indentation.
Add a simple test that adds 1000 files of 10kB in 10 directories twice
and that fails if the second invocation (without any changes) with
renormalize=false is not significantly faster.
Locally, I observe for that second invocation
* git.add().addFilepattern(".").call() ~660ms
* git.add().addFilepattern(".").setRenormalize(false).call() ~16ms
Bug: 494323
Change-Id: I30f9d518563fa55d7058a48c27c425f3b60aeb4c
Signed-off-by: Thomas Wolf <twolf@apache.org>
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 |