diff --git a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/RepositoryTestCase.java b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/RepositoryTestCase.java index c649eb908..0db71f7cb 100644 --- a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/RepositoryTestCase.java +++ b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/RepositoryTestCase.java @@ -401,8 +401,7 @@ public abstract class RepositoryTestCase extends LocalDiskRepositoryTestCase { * @return the created commit */ protected RevCommit commitFile(String filename, String contents, String branch) { - try { - Git git = new Git(db); + try (Git git = new Git(db)) { Repository repo = git.getRepository(); String originalBranch = repo.getFullBranch(); boolean empty = repo.resolve(Constants.HEAD) == null; @@ -443,8 +442,10 @@ public abstract class RepositoryTestCase extends LocalDiskRepositoryTestCase { final int stage, final String content) { final DirCacheEntry entry = new DirCacheEntry(path, stage); entry.setFileMode(mode); - entry.setObjectId(new ObjectInserter.Formatter().idFor( - Constants.OBJ_BLOB, Constants.encode(content))); + try (ObjectInserter.Formatter formatter = new ObjectInserter.Formatter()) { + entry.setObjectId(formatter.idFor( + Constants.OBJ_BLOB, Constants.encode(content))); + } return entry; } diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/RepoTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/RepoTest.java index 0eee771d2..ba62383b3 100644 --- a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/RepoTest.java +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/RepoTest.java @@ -74,28 +74,32 @@ public class RepoTest extends CLIRepositoryTestCase { super.setUp(); defaultDb = createWorkRepository(); - Git git = new Git(defaultDb); - JGitTestUtil.writeTrashFile(defaultDb, "hello.txt", "world"); - git.add().addFilepattern("hello.txt").call(); - git.commit().setMessage("Initial commit").call(); + try (Git git = new Git(defaultDb)) { + JGitTestUtil.writeTrashFile(defaultDb, "hello.txt", "world"); + git.add().addFilepattern("hello.txt").call(); + git.commit().setMessage("Initial commit").call(); + } notDefaultDb = createWorkRepository(); - git = new Git(notDefaultDb); - JGitTestUtil.writeTrashFile(notDefaultDb, "world.txt", "hello"); - git.add().addFilepattern("world.txt").call(); - git.commit().setMessage("Initial commit").call(); + try (Git git = new Git(notDefaultDb)) { + JGitTestUtil.writeTrashFile(notDefaultDb, "world.txt", "hello"); + git.add().addFilepattern("world.txt").call(); + git.commit().setMessage("Initial commit").call(); + } groupADb = createWorkRepository(); - git = new Git(groupADb); - JGitTestUtil.writeTrashFile(groupADb, "a.txt", "world"); - git.add().addFilepattern("a.txt").call(); - git.commit().setMessage("Initial commit").call(); + try (Git git = new Git(groupADb)) { + JGitTestUtil.writeTrashFile(groupADb, "a.txt", "world"); + git.add().addFilepattern("a.txt").call(); + git.commit().setMessage("Initial commit").call(); + } groupBDb = createWorkRepository(); - git = new Git(groupBDb); - JGitTestUtil.writeTrashFile(groupBDb, "b.txt", "world"); - git.add().addFilepattern("b.txt").call(); - git.commit().setMessage("Initial commit").call(); + try (Git git = new Git(groupBDb)) { + JGitTestUtil.writeTrashFile(groupBDb, "b.txt", "world"); + git.add().addFilepattern("b.txt").call(); + git.commit().setMessage("Initial commit").call(); + } resolveRelativeUris(); } diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/TextHashFunctions.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/TextHashFunctions.java index 062f4e7a1..28d92aeca 100644 --- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/TextHashFunctions.java +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/TextHashFunctions.java @@ -286,25 +286,25 @@ class TextHashFunctions extends TextBuiltin { else rb.findGitDir(dir); - Repository db = rb.build(); + Repository repo = rb.build(); try { - run(db); + run(repo); } finally { - db.close(); + repo.close(); } } } - private void run(Repository db) throws Exception { + private void run(Repository repo) throws Exception { List all = init(); long fileCnt = 0; long lineCnt = 0; - try (ObjectReader or = db.newObjectReader(); + try (ObjectReader or = repo.newObjectReader(); RevWalk rw = new RevWalk(or); TreeWalk tw = new TreeWalk(or)) { final MutableObjectId id = new MutableObjectId(); - tw.reset(rw.parseTree(db.resolve(Constants.HEAD))); + tw.reset(rw.parseTree(repo.resolve(Constants.HEAD))); tw.setRecursive(true); while (tw.next()) { @@ -341,7 +341,7 @@ class TextHashFunctions extends TextBuiltin { } } - File directory = db.getDirectory(); + File directory = repo.getDirectory(); if (directory != null) { String name = directory.getName(); File parent = directory.getParentFile(); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ResetCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ResetCommandTest.java index 66f25e8e5..40d8458ef 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ResetCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ResetCommandTest.java @@ -463,24 +463,24 @@ public class ResetCommandTest extends RepositoryTestCase { @Test public void testHardResetAfterSquashMerge() throws Exception { - Git g = new Git(db); + git = new Git(db); writeTrashFile("file1", "file1"); - g.add().addFilepattern("file1").call(); - RevCommit first = g.commit().setMessage("initial commit").call(); + git.add().addFilepattern("file1").call(); + RevCommit first = git.commit().setMessage("initial commit").call(); assertTrue(new File(db.getWorkTree(), "file1").exists()); createBranch(first, "refs/heads/branch1"); checkoutBranch("refs/heads/branch1"); writeTrashFile("file2", "file2"); - g.add().addFilepattern("file2").call(); - g.commit().setMessage("second commit").call(); + git.add().addFilepattern("file2").call(); + git.commit().setMessage("second commit").call(); assertTrue(new File(db.getWorkTree(), "file2").exists()); checkoutBranch("refs/heads/master"); - MergeResult result = g.merge() + MergeResult result = git.merge() .include(db.exactRef("refs/heads/branch1")) .setSquash(true) .call(); @@ -489,7 +489,7 @@ public class ResetCommandTest extends RepositoryTestCase { result.getMergeStatus()); assertNotNull(db.readSquashCommitMsg()); - assertSameAsHead(g.reset().setMode(ResetType.HARD) + assertSameAsHead(git.reset().setMode(ResetType.HARD) .setRef(first.getName()).call()); assertNull(db.readSquashCommitMsg()); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RevertCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RevertCommandTest.java index 060168c67..ea631045c 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RevertCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RevertCommandTest.java @@ -74,303 +74,309 @@ public class RevertCommandTest extends RepositoryTestCase { @Test public void testRevert() throws IOException, JGitInternalException, GitAPIException { - Git git = new Git(db); + try (Git git = new Git(db)) { + writeTrashFile("a", "first line\nsec. line\nthird line\n"); + git.add().addFilepattern("a").call(); + git.commit().setMessage("create a").call(); + + writeTrashFile("b", "content\n"); + git.add().addFilepattern("b").call(); + git.commit().setMessage("create b").call(); + + writeTrashFile("a", "first line\nsec. line\nthird line\nfourth line\n"); + git.add().addFilepattern("a").call(); + git.commit().setMessage("enlarged a").call(); + + writeTrashFile("a", + "first line\nsecond line\nthird line\nfourth line\n"); + git.add().addFilepattern("a").call(); + RevCommit fixingA = git.commit().setMessage("fixed a").call(); + + writeTrashFile("b", "first line\n"); + git.add().addFilepattern("b").call(); + git.commit().setMessage("fixed b").call(); + + git.revert().include(fixingA).call(); + + assertEquals(RepositoryState.SAFE, db.getRepositoryState()); + + assertTrue(new File(db.getWorkTree(), "b").exists()); + checkFile(new File(db.getWorkTree(), "a"), + "first line\nsec. line\nthird line\nfourth line\n"); + Iterator history = git.log().call().iterator(); + RevCommit revertCommit = history.next(); + String expectedMessage = "Revert \"fixed a\"\n\n" + + "This reverts commit " + fixingA.getId().getName() + ".\n"; + assertEquals(expectedMessage, revertCommit.getFullMessage()); + assertEquals("fixed b", history.next().getFullMessage()); + assertEquals("fixed a", history.next().getFullMessage()); + assertEquals("enlarged a", history.next().getFullMessage()); + assertEquals("create b", history.next().getFullMessage()); + assertEquals("create a", history.next().getFullMessage()); + assertFalse(history.hasNext()); - writeTrashFile("a", "first line\nsec. line\nthird line\n"); - git.add().addFilepattern("a").call(); - git.commit().setMessage("create a").call(); - - writeTrashFile("b", "content\n"); - git.add().addFilepattern("b").call(); - git.commit().setMessage("create b").call(); - - writeTrashFile("a", "first line\nsec. line\nthird line\nfourth line\n"); - git.add().addFilepattern("a").call(); - git.commit().setMessage("enlarged a").call(); - - writeTrashFile("a", - "first line\nsecond line\nthird line\nfourth line\n"); - git.add().addFilepattern("a").call(); - RevCommit fixingA = git.commit().setMessage("fixed a").call(); - - writeTrashFile("b", "first line\n"); - git.add().addFilepattern("b").call(); - git.commit().setMessage("fixed b").call(); - - git.revert().include(fixingA).call(); - - assertEquals(RepositoryState.SAFE, db.getRepositoryState()); - - assertTrue(new File(db.getWorkTree(), "b").exists()); - checkFile(new File(db.getWorkTree(), "a"), - "first line\nsec. line\nthird line\nfourth line\n"); - Iterator history = git.log().call().iterator(); - RevCommit revertCommit = history.next(); - String expectedMessage = "Revert \"fixed a\"\n\n" - + "This reverts commit " + fixingA.getId().getName() + ".\n"; - assertEquals(expectedMessage, revertCommit.getFullMessage()); - assertEquals("fixed b", history.next().getFullMessage()); - assertEquals("fixed a", history.next().getFullMessage()); - assertEquals("enlarged a", history.next().getFullMessage()); - assertEquals("create b", history.next().getFullMessage()); - assertEquals("create a", history.next().getFullMessage()); - assertFalse(history.hasNext()); - - ReflogReader reader = db.getReflogReader(Constants.HEAD); - assertTrue(reader.getLastEntry().getComment() - .startsWith("revert: Revert \"")); - reader = db.getReflogReader(db.getBranch()); - assertTrue(reader.getLastEntry().getComment() - .startsWith("revert: Revert \"")); + ReflogReader reader = db.getReflogReader(Constants.HEAD); + assertTrue(reader.getLastEntry().getComment() + .startsWith("revert: Revert \"")); + reader = db.getReflogReader(db.getBranch()); + assertTrue(reader.getLastEntry().getComment() + .startsWith("revert: Revert \"")); + } } @Test public void testRevertMultiple() throws IOException, JGitInternalException, GitAPIException { - Git git = new Git(db); - - writeTrashFile("a", "first\n"); - git.add().addFilepattern("a").call(); - git.commit().setMessage("add first").call(); + try (Git git = new Git(db)) { + writeTrashFile("a", "first\n"); + git.add().addFilepattern("a").call(); + git.commit().setMessage("add first").call(); + + writeTrashFile("a", "first\nsecond\n"); + git.add().addFilepattern("a").call(); + RevCommit secondCommit = git.commit().setMessage("add second").call(); + + writeTrashFile("a", "first\nsecond\nthird\n"); + git.add().addFilepattern("a").call(); + RevCommit thirdCommit = git.commit().setMessage("add third").call(); + + git.revert().include(thirdCommit).include(secondCommit).call(); + + assertEquals(RepositoryState.SAFE, db.getRepositoryState()); + + checkFile(new File(db.getWorkTree(), "a"), "first\n"); + Iterator history = git.log().call().iterator(); + RevCommit revertCommit = history.next(); + String expectedMessage = "Revert \"add second\"\n\n" + + "This reverts commit " + + secondCommit.getId().getName() + ".\n"; + assertEquals(expectedMessage, revertCommit.getFullMessage()); + revertCommit = history.next(); + expectedMessage = "Revert \"add third\"\n\n" + + "This reverts commit " + thirdCommit.getId().getName() + + ".\n"; + assertEquals(expectedMessage, revertCommit.getFullMessage()); + assertEquals("add third", history.next().getFullMessage()); + assertEquals("add second", history.next().getFullMessage()); + assertEquals("add first", history.next().getFullMessage()); + assertFalse(history.hasNext()); - writeTrashFile("a", "first\nsecond\n"); - git.add().addFilepattern("a").call(); - RevCommit secondCommit = git.commit().setMessage("add second").call(); - - writeTrashFile("a", "first\nsecond\nthird\n"); - git.add().addFilepattern("a").call(); - RevCommit thirdCommit = git.commit().setMessage("add third").call(); - - git.revert().include(thirdCommit).include(secondCommit).call(); - - assertEquals(RepositoryState.SAFE, db.getRepositoryState()); - - checkFile(new File(db.getWorkTree(), "a"), "first\n"); - Iterator history = git.log().call().iterator(); - RevCommit revertCommit = history.next(); - String expectedMessage = "Revert \"add second\"\n\n" - + "This reverts commit " - + secondCommit.getId().getName() + ".\n"; - assertEquals(expectedMessage, revertCommit.getFullMessage()); - revertCommit = history.next(); - expectedMessage = "Revert \"add third\"\n\n" - + "This reverts commit " + thirdCommit.getId().getName() - + ".\n"; - assertEquals(expectedMessage, revertCommit.getFullMessage()); - assertEquals("add third", history.next().getFullMessage()); - assertEquals("add second", history.next().getFullMessage()); - assertEquals("add first", history.next().getFullMessage()); - assertFalse(history.hasNext()); - - ReflogReader reader = db.getReflogReader(Constants.HEAD); - assertTrue(reader.getLastEntry().getComment() - .startsWith("revert: Revert \"")); - reader = db.getReflogReader(db.getBranch()); - assertTrue(reader.getLastEntry().getComment() - .startsWith("revert: Revert \"")); + ReflogReader reader = db.getReflogReader(Constants.HEAD); + assertTrue(reader.getLastEntry().getComment() + .startsWith("revert: Revert \"")); + reader = db.getReflogReader(db.getBranch()); + assertTrue(reader.getLastEntry().getComment() + .startsWith("revert: Revert \"")); + } } @Test public void testRevertMultipleWithFail() throws IOException, JGitInternalException, GitAPIException { - Git git = new Git(db); - - writeTrashFile("a", "first\n"); - git.add().addFilepattern("a").call(); - git.commit().setMessage("add first").call(); - - writeTrashFile("a", "first\nsecond\n"); - git.add().addFilepattern("a").call(); - RevCommit secondCommit = git.commit().setMessage("add second").call(); - - writeTrashFile("a", "first\nsecond\nthird\n"); - git.add().addFilepattern("a").call(); - git.commit().setMessage("add third").call(); + try (Git git = new Git(db)) { + writeTrashFile("a", "first\n"); + git.add().addFilepattern("a").call(); + git.commit().setMessage("add first").call(); + + writeTrashFile("a", "first\nsecond\n"); + git.add().addFilepattern("a").call(); + RevCommit secondCommit = git.commit().setMessage("add second").call(); + + writeTrashFile("a", "first\nsecond\nthird\n"); + git.add().addFilepattern("a").call(); + git.commit().setMessage("add third").call(); + + writeTrashFile("a", "first\nsecond\nthird\nfourth\n"); + git.add().addFilepattern("a").call(); + RevCommit fourthCommit = git.commit().setMessage("add fourth").call(); + + git.revert().include(fourthCommit).include(secondCommit).call(); + + // not SAFE because it failed + assertEquals(RepositoryState.REVERTING, db.getRepositoryState()); + + checkFile(new File(db.getWorkTree(), "a"), "first\n" + + "<<<<<<< master\n" + "second\n" + "third\n" + "=======\n" + + ">>>>>>> " + secondCommit.getId().abbreviate(7).name() + + " add second\n"); + Iterator history = git.log().call().iterator(); + RevCommit revertCommit = history.next(); + String expectedMessage = "Revert \"add fourth\"\n\n" + + "This reverts commit " + fourthCommit.getId().getName() + + ".\n"; + assertEquals(expectedMessage, revertCommit.getFullMessage()); + assertEquals("add fourth", history.next().getFullMessage()); + assertEquals("add third", history.next().getFullMessage()); + assertEquals("add second", history.next().getFullMessage()); + assertEquals("add first", history.next().getFullMessage()); + assertFalse(history.hasNext()); - writeTrashFile("a", "first\nsecond\nthird\nfourth\n"); - git.add().addFilepattern("a").call(); - RevCommit fourthCommit = git.commit().setMessage("add fourth").call(); - - git.revert().include(fourthCommit).include(secondCommit).call(); - - // not SAFE because it failed - assertEquals(RepositoryState.REVERTING, db.getRepositoryState()); - - checkFile(new File(db.getWorkTree(), "a"), "first\n" - + "<<<<<<< master\n" + "second\n" + "third\n" + "=======\n" - + ">>>>>>> " + secondCommit.getId().abbreviate(7).name() - + " add second\n"); - Iterator history = git.log().call().iterator(); - RevCommit revertCommit = history.next(); - String expectedMessage = "Revert \"add fourth\"\n\n" - + "This reverts commit " + fourthCommit.getId().getName() - + ".\n"; - assertEquals(expectedMessage, revertCommit.getFullMessage()); - assertEquals("add fourth", history.next().getFullMessage()); - assertEquals("add third", history.next().getFullMessage()); - assertEquals("add second", history.next().getFullMessage()); - assertEquals("add first", history.next().getFullMessage()); - assertFalse(history.hasNext()); - - ReflogReader reader = db.getReflogReader(Constants.HEAD); - assertTrue(reader.getLastEntry().getComment() - .startsWith("revert: Revert \"")); - reader = db.getReflogReader(db.getBranch()); - assertTrue(reader.getLastEntry().getComment() - .startsWith("revert: Revert \"")); + ReflogReader reader = db.getReflogReader(Constants.HEAD); + assertTrue(reader.getLastEntry().getComment() + .startsWith("revert: Revert \"")); + reader = db.getReflogReader(db.getBranch()); + assertTrue(reader.getLastEntry().getComment() + .startsWith("revert: Revert \"")); + } } @Test public void testRevertDirtyIndex() throws Exception { - Git git = new Git(db); - RevCommit sideCommit = prepareRevert(git); + try (Git git = new Git(db)) { + RevCommit sideCommit = prepareRevert(git); - // modify and add file a - writeTrashFile("a", "a(modified)"); - git.add().addFilepattern("a").call(); - // do not commit + // modify and add file a + writeTrashFile("a", "a(modified)"); + git.add().addFilepattern("a").call(); + // do not commit - doRevertAndCheckResult(git, sideCommit, - MergeFailureReason.DIRTY_INDEX); + doRevertAndCheckResult(git, sideCommit, + MergeFailureReason.DIRTY_INDEX); + } } @Test public void testRevertDirtyWorktree() throws Exception { - Git git = new Git(db); - RevCommit sideCommit = prepareRevert(git); + try (Git git = new Git(db)) { + RevCommit sideCommit = prepareRevert(git); - // modify file a - writeTrashFile("a", "a(modified)"); - // do not add and commit + // modify file a + writeTrashFile("a", "a(modified)"); + // do not add and commit - doRevertAndCheckResult(git, sideCommit, - MergeFailureReason.DIRTY_WORKTREE); + doRevertAndCheckResult(git, sideCommit, + MergeFailureReason.DIRTY_WORKTREE); + } } @Test public void testRevertConflictResolution() throws Exception { - Git git = new Git(db); - RevCommit sideCommit = prepareRevert(git); - - RevertCommand revert = git.revert(); - RevCommit newHead = revert.include(sideCommit.getId()).call(); - assertNull(newHead); - MergeResult result = revert.getFailingResult(); - assertEquals(MergeStatus.CONFLICTING, result.getMergeStatus()); - assertTrue(new File(db.getDirectory(), Constants.MERGE_MSG).exists()); - assertEquals("Revert \"" + sideCommit.getShortMessage() - + "\"\n\nThis reverts commit " + sideCommit.getId().getName() - + ".\n\nConflicts:\n\ta\n", - db.readMergeCommitMsg()); - assertTrue(new File(db.getDirectory(), Constants.REVERT_HEAD) - .exists()); - assertEquals(sideCommit.getId(), db.readRevertHead()); - assertEquals(RepositoryState.REVERTING, db.getRepositoryState()); - - // Resolve - writeTrashFile("a", "a"); - git.add().addFilepattern("a").call(); - - assertEquals(RepositoryState.REVERTING_RESOLVED, - db.getRepositoryState()); - - git.commit().setOnly("a").setMessage("resolve").call(); - - assertEquals(RepositoryState.SAFE, db.getRepositoryState()); + try (Git git = new Git(db)) { + RevCommit sideCommit = prepareRevert(git); + + RevertCommand revert = git.revert(); + RevCommit newHead = revert.include(sideCommit.getId()).call(); + assertNull(newHead); + MergeResult result = revert.getFailingResult(); + assertEquals(MergeStatus.CONFLICTING, result.getMergeStatus()); + assertTrue(new File(db.getDirectory(), Constants.MERGE_MSG).exists()); + assertEquals("Revert \"" + sideCommit.getShortMessage() + + "\"\n\nThis reverts commit " + sideCommit.getId().getName() + + ".\n\nConflicts:\n\ta\n", + db.readMergeCommitMsg()); + assertTrue(new File(db.getDirectory(), Constants.REVERT_HEAD) + .exists()); + assertEquals(sideCommit.getId(), db.readRevertHead()); + assertEquals(RepositoryState.REVERTING, db.getRepositoryState()); + + // Resolve + writeTrashFile("a", "a"); + git.add().addFilepattern("a").call(); + + assertEquals(RepositoryState.REVERTING_RESOLVED, + db.getRepositoryState()); + + git.commit().setOnly("a").setMessage("resolve").call(); + + assertEquals(RepositoryState.SAFE, db.getRepositoryState()); + } } @Test public void testRevertkConflictReset() throws Exception { - Git git = new Git(db); + try (Git git = new Git(db)) { + RevCommit sideCommit = prepareRevert(git); - RevCommit sideCommit = prepareRevert(git); + RevertCommand revert = git.revert(); + RevCommit newHead = revert.include(sideCommit.getId()).call(); + assertNull(newHead); + MergeResult result = revert.getFailingResult(); - RevertCommand revert = git.revert(); - RevCommit newHead = revert.include(sideCommit.getId()).call(); - assertNull(newHead); - MergeResult result = revert.getFailingResult(); + assertEquals(MergeStatus.CONFLICTING, result.getMergeStatus()); + assertEquals(RepositoryState.REVERTING, db.getRepositoryState()); + assertTrue(new File(db.getDirectory(), Constants.REVERT_HEAD) + .exists()); - assertEquals(MergeStatus.CONFLICTING, result.getMergeStatus()); - assertEquals(RepositoryState.REVERTING, db.getRepositoryState()); - assertTrue(new File(db.getDirectory(), Constants.REVERT_HEAD) - .exists()); + git.reset().setMode(ResetType.MIXED).setRef("HEAD").call(); - git.reset().setMode(ResetType.MIXED).setRef("HEAD").call(); - - assertEquals(RepositoryState.SAFE, db.getRepositoryState()); - assertFalse(new File(db.getDirectory(), Constants.REVERT_HEAD) - .exists()); + assertEquals(RepositoryState.SAFE, db.getRepositoryState()); + assertFalse(new File(db.getDirectory(), Constants.REVERT_HEAD) + .exists()); + } } @Test public void testRevertOverExecutableChangeOnNonExectuableFileSystem() throws Exception { - Git git = new Git(db); - File file = writeTrashFile("test.txt", "a"); - assertNotNull(git.add().addFilepattern("test.txt").call()); - assertNotNull(git.commit().setMessage("commit1").call()); + try (Git git = new Git(db)) { + File file = writeTrashFile("test.txt", "a"); + assertNotNull(git.add().addFilepattern("test.txt").call()); + assertNotNull(git.commit().setMessage("commit1").call()); - assertNotNull(git.checkout().setCreateBranch(true).setName("a").call()); + assertNotNull(git.checkout().setCreateBranch(true).setName("a").call()); - writeTrashFile("test.txt", "b"); - assertNotNull(git.add().addFilepattern("test.txt").call()); - RevCommit commit2 = git.commit().setMessage("commit2").call(); - assertNotNull(commit2); + writeTrashFile("test.txt", "b"); + assertNotNull(git.add().addFilepattern("test.txt").call()); + RevCommit commit2 = git.commit().setMessage("commit2").call(); + assertNotNull(commit2); - assertNotNull(git.checkout().setName(Constants.MASTER).call()); + assertNotNull(git.checkout().setName(Constants.MASTER).call()); - DirCache cache = db.lockDirCache(); - cache.getEntry("test.txt").setFileMode(FileMode.EXECUTABLE_FILE); - cache.write(); - assertTrue(cache.commit()); - cache.unlock(); + DirCache cache = db.lockDirCache(); + cache.getEntry("test.txt").setFileMode(FileMode.EXECUTABLE_FILE); + cache.write(); + assertTrue(cache.commit()); + cache.unlock(); - assertNotNull(git.commit().setMessage("commit3").call()); + assertNotNull(git.commit().setMessage("commit3").call()); - db.getFS().setExecute(file, false); - git.getRepository() - .getConfig() - .setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, - ConfigConstants.CONFIG_KEY_FILEMODE, false); + db.getFS().setExecute(file, false); + git.getRepository() + .getConfig() + .setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, + ConfigConstants.CONFIG_KEY_FILEMODE, false); - RevertCommand revert = git.revert(); - RevCommit newHead = revert.include(commit2).call(); - assertNotNull(newHead); + RevertCommand revert = git.revert(); + RevCommit newHead = revert.include(commit2).call(); + assertNotNull(newHead); + } } @Test public void testRevertConflictMarkers() throws Exception { - Git git = new Git(db); - RevCommit sideCommit = prepareRevert(git); - - RevertCommand revert = git.revert(); - RevCommit newHead = revert.include(sideCommit.getId()) - .call(); - assertNull(newHead); - MergeResult result = revert.getFailingResult(); - assertEquals(MergeStatus.CONFLICTING, result.getMergeStatus()); - - String expected = "<<<<<<< master\na(latest)\n=======\na\n>>>>>>> ca96c31 second master\n"; - checkFile(new File(db.getWorkTree(), "a"), expected); + try (Git git = new Git(db)) { + RevCommit sideCommit = prepareRevert(git); + + RevertCommand revert = git.revert(); + RevCommit newHead = revert.include(sideCommit.getId()) + .call(); + assertNull(newHead); + MergeResult result = revert.getFailingResult(); + assertEquals(MergeStatus.CONFLICTING, result.getMergeStatus()); + + String expected = "<<<<<<< master\na(latest)\n=======\na\n>>>>>>> ca96c31 second master\n"; + checkFile(new File(db.getWorkTree(), "a"), expected); + } } @Test public void testRevertOurCommitName() throws Exception { - Git git = new Git(db); - RevCommit sideCommit = prepareRevert(git); - - RevertCommand revert = git.revert(); - RevCommit newHead = revert.include(sideCommit.getId()) - .setOurCommitName("custom name").call(); - assertNull(newHead); - MergeResult result = revert.getFailingResult(); - assertEquals(MergeStatus.CONFLICTING, result.getMergeStatus()); - - String expected = "<<<<<<< custom name\na(latest)\n=======\na\n>>>>>>> ca96c31 second master\n"; - checkFile(new File(db.getWorkTree(), "a"), expected); + try (Git git = new Git(db)) { + RevCommit sideCommit = prepareRevert(git); + + RevertCommand revert = git.revert(); + RevCommit newHead = revert.include(sideCommit.getId()) + .setOurCommitName("custom name").call(); + assertNull(newHead); + MergeResult result = revert.getFailingResult(); + assertEquals(MergeStatus.CONFLICTING, result.getMergeStatus()); + + String expected = "<<<<<<< custom name\na(latest)\n=======\na\n>>>>>>> ca96c31 second master\n"; + checkFile(new File(db.getWorkTree(), "a"), expected); + } } private RevCommit prepareRevert(final Git git) throws Exception { diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/StatusCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/StatusCommandTest.java index c70604ecb..f3ac65ca0 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/StatusCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/StatusCommandTest.java @@ -61,109 +61,111 @@ public class StatusCommandTest extends RepositoryTestCase { @Test public void testEmptyStatus() throws NoWorkTreeException, GitAPIException { - Git git = new Git(db); - - Status stat = git.status().call(); - assertEquals(0, stat.getAdded().size()); - assertEquals(0, stat.getChanged().size()); - assertEquals(0, stat.getMissing().size()); - assertEquals(0, stat.getModified().size()); - assertEquals(0, stat.getRemoved().size()); - assertEquals(0, stat.getUntracked().size()); + try (Git git = new Git(db)) { + Status stat = git.status().call(); + assertEquals(0, stat.getAdded().size()); + assertEquals(0, stat.getChanged().size()); + assertEquals(0, stat.getMissing().size()); + assertEquals(0, stat.getModified().size()); + assertEquals(0, stat.getRemoved().size()); + assertEquals(0, stat.getUntracked().size()); + } } @Test public void testDifferentStates() throws IOException, NoFilepatternException, GitAPIException { - Git git = new Git(db); - writeTrashFile("a", "content of a"); - writeTrashFile("b", "content of b"); - writeTrashFile("c", "content of c"); - git.add().addFilepattern("a").addFilepattern("b").call(); - Status stat = git.status().call(); - assertEquals(Sets.of("a", "b"), stat.getAdded()); - assertEquals(0, stat.getChanged().size()); - assertEquals(0, stat.getMissing().size()); - assertEquals(0, stat.getModified().size()); - assertEquals(0, stat.getRemoved().size()); - assertEquals(Sets.of("c"), stat.getUntracked()); - git.commit().setMessage("initial").call(); - - writeTrashFile("a", "modified content of a"); - writeTrashFile("b", "modified content of b"); - writeTrashFile("d", "content of d"); - git.add().addFilepattern("a").addFilepattern("d").call(); - writeTrashFile("a", "again modified content of a"); - stat = git.status().call(); - assertEquals(Sets.of("d"), stat.getAdded()); - assertEquals(Sets.of("a"), stat.getChanged()); - assertEquals(0, stat.getMissing().size()); - assertEquals(Sets.of("b", "a"), stat.getModified()); - assertEquals(0, stat.getRemoved().size()); - assertEquals(Sets.of("c"), stat.getUntracked()); - git.add().addFilepattern(".").call(); - git.commit().setMessage("second").call(); - - stat = git.status().call(); - assertEquals(0, stat.getAdded().size()); - assertEquals(0, stat.getChanged().size()); - assertEquals(0, stat.getMissing().size()); - assertEquals(0, stat.getModified().size()); - assertEquals(0, stat.getRemoved().size()); - assertEquals(0, stat.getUntracked().size()); - - deleteTrashFile("a"); - assertFalse(new File(git.getRepository().getWorkTree(), "a").exists()); - git.add().addFilepattern("a").setUpdate(true).call(); - writeTrashFile("a", "recreated content of a"); - stat = git.status().call(); - assertEquals(0, stat.getAdded().size()); - assertEquals(0, stat.getChanged().size()); - assertEquals(0, stat.getMissing().size()); - assertEquals(0, stat.getModified().size()); - assertEquals(Sets.of("a"), stat.getRemoved()); - assertEquals(Sets.of("a"), stat.getUntracked()); - git.commit().setMessage("t").call(); - - writeTrashFile("sub/a", "sub-file"); - stat = git.status().call(); - assertEquals(1, stat.getUntrackedFolders().size()); - assertTrue(stat.getUntrackedFolders().contains("sub")); + try (Git git = new Git(db)) { + writeTrashFile("a", "content of a"); + writeTrashFile("b", "content of b"); + writeTrashFile("c", "content of c"); + git.add().addFilepattern("a").addFilepattern("b").call(); + Status stat = git.status().call(); + assertEquals(Sets.of("a", "b"), stat.getAdded()); + assertEquals(0, stat.getChanged().size()); + assertEquals(0, stat.getMissing().size()); + assertEquals(0, stat.getModified().size()); + assertEquals(0, stat.getRemoved().size()); + assertEquals(Sets.of("c"), stat.getUntracked()); + git.commit().setMessage("initial").call(); + + writeTrashFile("a", "modified content of a"); + writeTrashFile("b", "modified content of b"); + writeTrashFile("d", "content of d"); + git.add().addFilepattern("a").addFilepattern("d").call(); + writeTrashFile("a", "again modified content of a"); + stat = git.status().call(); + assertEquals(Sets.of("d"), stat.getAdded()); + assertEquals(Sets.of("a"), stat.getChanged()); + assertEquals(0, stat.getMissing().size()); + assertEquals(Sets.of("b", "a"), stat.getModified()); + assertEquals(0, stat.getRemoved().size()); + assertEquals(Sets.of("c"), stat.getUntracked()); + git.add().addFilepattern(".").call(); + git.commit().setMessage("second").call(); + + stat = git.status().call(); + assertEquals(0, stat.getAdded().size()); + assertEquals(0, stat.getChanged().size()); + assertEquals(0, stat.getMissing().size()); + assertEquals(0, stat.getModified().size()); + assertEquals(0, stat.getRemoved().size()); + assertEquals(0, stat.getUntracked().size()); + + deleteTrashFile("a"); + assertFalse(new File(git.getRepository().getWorkTree(), "a").exists()); + git.add().addFilepattern("a").setUpdate(true).call(); + writeTrashFile("a", "recreated content of a"); + stat = git.status().call(); + assertEquals(0, stat.getAdded().size()); + assertEquals(0, stat.getChanged().size()); + assertEquals(0, stat.getMissing().size()); + assertEquals(0, stat.getModified().size()); + assertEquals(Sets.of("a"), stat.getRemoved()); + assertEquals(Sets.of("a"), stat.getUntracked()); + git.commit().setMessage("t").call(); + + writeTrashFile("sub/a", "sub-file"); + stat = git.status().call(); + assertEquals(1, stat.getUntrackedFolders().size()); + assertTrue(stat.getUntrackedFolders().contains("sub")); + } } @Test public void testDifferentStatesWithPaths() throws IOException, NoFilepatternException, GitAPIException { - Git git = new Git(db); - writeTrashFile("a", "content of a"); - writeTrashFile("D/b", "content of b"); - writeTrashFile("D/c", "content of c"); - writeTrashFile("D/D/d", "content of d"); - git.add().addFilepattern(".").call(); - - writeTrashFile("a", "new content of a"); - writeTrashFile("D/b", "new content of b"); - writeTrashFile("D/D/d", "new content of d"); - - - // filter on an not existing path - Status stat = git.status().addPath("x").call(); - assertEquals(0, stat.getModified().size()); - - // filter on an existing file - stat = git.status().addPath("a").call(); - assertEquals(Sets.of("a"), stat.getModified()); - - // filter on an existing folder - stat = git.status().addPath("D").call(); - assertEquals(Sets.of("D/b", "D/D/d"), stat.getModified()); - - // filter on an existing folder and file - stat = git.status().addPath("D/D").addPath("a").call(); - assertEquals(Sets.of("a", "D/D/d"), stat.getModified()); - - // do not filter at all - stat = git.status().call(); - assertEquals(Sets.of("a", "D/b", "D/D/d"), stat.getModified()); + try (Git git = new Git(db)) { + writeTrashFile("a", "content of a"); + writeTrashFile("D/b", "content of b"); + writeTrashFile("D/c", "content of c"); + writeTrashFile("D/D/d", "content of d"); + git.add().addFilepattern(".").call(); + + writeTrashFile("a", "new content of a"); + writeTrashFile("D/b", "new content of b"); + writeTrashFile("D/D/d", "new content of d"); + + + // filter on an not existing path + Status stat = git.status().addPath("x").call(); + assertEquals(0, stat.getModified().size()); + + // filter on an existing file + stat = git.status().addPath("a").call(); + assertEquals(Sets.of("a"), stat.getModified()); + + // filter on an existing folder + stat = git.status().addPath("D").call(); + assertEquals(Sets.of("D/b", "D/D/d"), stat.getModified()); + + // filter on an existing folder and file + stat = git.status().addPath("D/D").addPath("a").call(); + assertEquals(Sets.of("a", "D/D/d"), stat.getModified()); + + // do not filter at all + stat = git.status().call(); + assertEquals(Sets.of("a", "D/b", "D/D/d"), stat.getModified()); + } } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/DiffFormatterTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/DiffFormatterTest.java index 81a4c6a60..638a16375 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/DiffFormatterTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/DiffFormatterTest.java @@ -259,6 +259,68 @@ public class DiffFormatterTest extends RepositoryTestCase { assertEquals(1, hh.toEditList().size()); } + @Test + public void testCreateFileHeader_AddGitLink() throws Exception { + ObjectId adId = blob("a\nd\n"); + DiffEntry ent = DiffEntry.add("FOO", adId); + ent.newMode = FileMode.GITLINK; + FileHeader fh = df.toFileHeader(ent); + + String diffHeader = "diff --git a/FOO b/FOO\n" // + + "new file mode " + GITLINK + "\n" + + "index " + + ObjectId.zeroId().abbreviate(8).name() + + ".." + + adId.abbreviate(8).name() + "\n" // + + "--- /dev/null\n"// + + "+++ b/FOO\n"; + assertEquals(diffHeader, RawParseUtils.decode(fh.getBuffer())); + + assertEquals(1, fh.getHunks().size()); + HunkHeader hh = fh.getHunks().get(0); + + EditList el = hh.toEditList(); + assertEquals(1, el.size()); + + Edit e = el.get(0); + assertEquals(0, e.getBeginA()); + assertEquals(0, e.getEndA()); + assertEquals(0, e.getBeginB()); + assertEquals(1, e.getEndB()); + assertEquals(Edit.Type.INSERT, e.getType()); + } + + @Test + public void testCreateFileHeader_DeleteGitLink() throws Exception { + ObjectId adId = blob("a\nd\n"); + DiffEntry ent = DiffEntry.delete("FOO", adId); + ent.oldMode = FileMode.GITLINK; + FileHeader fh = df.toFileHeader(ent); + + String diffHeader = "diff --git a/FOO b/FOO\n" // + + "deleted file mode " + GITLINK + "\n" + + "index " + + adId.abbreviate(8).name() + + ".." + + ObjectId.zeroId().abbreviate(8).name() + "\n" // + + "--- a/FOO\n"// + + "+++ /dev/null\n"; + assertEquals(diffHeader, RawParseUtils.decode(fh.getBuffer())); + + assertEquals(1, fh.getHunks().size()); + HunkHeader hh = fh.getHunks().get(0); + + EditList el = hh.toEditList(); + assertEquals(1, el.size()); + + Edit e = el.get(0); + assertEquals(0, e.getBeginA()); + assertEquals(1, e.getEndA()); + assertEquals(0, e.getBeginB()); + assertEquals(0, e.getEndB()); + assertEquals(Edit.Type.DELETE, e.getType()); + } + @Test public void testCreateFileHeaderWithoutIndexLine() throws Exception { DiffEntry m = DiffEntry.modify(PATH_A); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/T0003_BasicTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/T0003_BasicTest.java index cb8726be5..b6ad22bbe 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/T0003_BasicTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/T0003_BasicTest.java @@ -354,14 +354,14 @@ public class T0003_BasicTest extends SampleDataRepositoryTestCase { @Test public void test007_Open() throws IOException { - final FileRepository db2 = new FileRepository(db.getDirectory()); - assertEquals(db.getDirectory(), db2.getDirectory()); - assertEquals(db.getObjectDatabase().getDirectory(), db2 - .getObjectDatabase().getDirectory()); - assertNotSame(db.getConfig(), db2.getConfig()); + try (final FileRepository db2 = new FileRepository(db.getDirectory())) { + assertEquals(db.getDirectory(), db2.getDirectory()); + assertEquals(db.getObjectDatabase().getDirectory(), db2 + .getObjectDatabase().getDirectory()); + assertNotSame(db.getConfig(), db2.getConfig()); + } } - @SuppressWarnings("unused") @Test public void test008_FailOnWrongVersion() throws IOException { final File cfg = new File(db.getDirectory(), Constants.CONFIG); @@ -370,8 +370,7 @@ public class T0003_BasicTest extends SampleDataRepositoryTestCase { + badvers + "\n"; write(cfg, configStr); - try { - new FileRepository(db.getDirectory()); + try (FileRepository unused = new FileRepository(db.getDirectory())) { fail("incorrectly opened a bad repository"); } catch (IllegalArgumentException ioe) { assertNotNull(ioe.getMessage()); @@ -533,9 +532,10 @@ public class T0003_BasicTest extends SampleDataRepositoryTestCase { public void test025_computeSha1NoStore() throws IOException { byte[] data = "test025 some data, more than 16 bytes to get good coverage" .getBytes("ISO-8859-1"); - final ObjectId id = new ObjectInserter.Formatter().idFor( - Constants.OBJ_BLOB, data); - assertEquals("4f561df5ecf0dfbd53a0dc0f37262fef075d9dde", id.name()); + try (ObjectInserter.Formatter formatter = new ObjectInserter.Formatter()) { + final ObjectId id = formatter.idFor(Constants.OBJ_BLOB, data); + assertEquals("4f561df5ecf0dfbd53a0dc0f37262fef075d9dde", id.name()); + } } @Test diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryResolveTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryResolveTest.java index c9ea28640..1d2a4e9ab 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryResolveTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryResolveTest.java @@ -267,35 +267,37 @@ public class RepositoryResolveTest extends SampleDataRepositoryTestCase { @Test public void resolveExprSimple() throws Exception { - Git git = new Git(db); - writeTrashFile("file.txt", "content"); - git.add().addFilepattern("file.txt").call(); - git.commit().setMessage("create file").call(); - assertEquals("master", db.simplify("master")); - assertEquals("refs/heads/master", db.simplify("refs/heads/master")); - assertEquals("HEAD", db.simplify("HEAD")); + try (Git git = new Git(db)) { + writeTrashFile("file.txt", "content"); + git.add().addFilepattern("file.txt").call(); + git.commit().setMessage("create file").call(); + assertEquals("master", db.simplify("master")); + assertEquals("refs/heads/master", db.simplify("refs/heads/master")); + assertEquals("HEAD", db.simplify("HEAD")); + } } @Test public void resolveUpstream() throws Exception { - Git git = new Git(db); - writeTrashFile("file.txt", "content"); - git.add().addFilepattern("file.txt").call(); - RevCommit c1 = git.commit().setMessage("create file").call(); - writeTrashFile("file2.txt", "content"); - RefUpdate updateRemoteRef = db.updateRef("refs/remotes/origin/main"); - updateRemoteRef.setNewObjectId(c1); - updateRemoteRef.update(); - db.getConfig().setString("branch", "master", "remote", "origin"); - db.getConfig() - .setString("branch", "master", "merge", "refs/heads/main"); - db.getConfig().setString("remote", "origin", "url", - "git://example.com/here"); - db.getConfig().setString("remote", "origin", "fetch", - "+refs/heads/*:refs/remotes/origin/*"); - git.add().addFilepattern("file2.txt").call(); - git.commit().setMessage("create file").call(); - assertEquals("refs/remotes/origin/main", db.simplify("@{upstream}")); + try (Git git = new Git(db)) { + writeTrashFile("file.txt", "content"); + git.add().addFilepattern("file.txt").call(); + RevCommit c1 = git.commit().setMessage("create file").call(); + writeTrashFile("file2.txt", "content"); + RefUpdate updateRemoteRef = db.updateRef("refs/remotes/origin/main"); + updateRemoteRef.setNewObjectId(c1); + updateRemoteRef.update(); + db.getConfig().setString("branch", "master", "remote", "origin"); + db.getConfig() + .setString("branch", "master", "merge", "refs/heads/main"); + db.getConfig().setString("remote", "origin", "url", + "git://example.com/here"); + db.getConfig().setString("remote", "origin", "fetch", + "+refs/heads/*:refs/remotes/origin/*"); + git.add().addFilepattern("file2.txt").call(); + git.commit().setMessage("create file").call(); + assertEquals("refs/remotes/origin/main", db.simplify("@{upstream}")); + } } @Test diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/ResolveMergerTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/ResolveMergerTest.java index 674619f0d..55bb93acf 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/ResolveMergerTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/ResolveMergerTest.java @@ -88,35 +88,36 @@ public class ResolveMergerTest extends RepositoryTestCase { file = new File(folder1, "file2.txt"); write(file, "folder1--file2.txt"); - Git git = new Git(db); - git.add().addFilepattern(folder1.getName()).call(); - RevCommit base = git.commit().setMessage("adding folder").call(); - - recursiveDelete(folder1); - git.rm().addFilepattern("folder1/file1.txt") - .addFilepattern("folder1/file2.txt").call(); - RevCommit other = git.commit() - .setMessage("removing folders on 'other'").call(); - - git.checkout().setName(base.name()).call(); - - file = new File(db.getWorkTree(), "unrelated.txt"); - write(file, "unrelated"); - - git.add().addFilepattern("unrelated.txt").call(); - RevCommit head = git.commit().setMessage("Adding another file").call(); - - // Untracked file to cause failing path for delete() of folder1 - // but that's ok. - file = new File(folder1, "file3.txt"); - write(file, "folder1--file3.txt"); - - ResolveMerger merger = (ResolveMerger) strategy.newMerger(db, false); - merger.setCommitNames(new String[] { "BASE", "HEAD", "other" }); - merger.setWorkingTreeIterator(new FileTreeIterator(db)); - boolean ok = merger.merge(head.getId(), other.getId()); - assertTrue(ok); - assertTrue(file.exists()); + try (Git git = new Git(db)) { + git.add().addFilepattern(folder1.getName()).call(); + RevCommit base = git.commit().setMessage("adding folder").call(); + + recursiveDelete(folder1); + git.rm().addFilepattern("folder1/file1.txt") + .addFilepattern("folder1/file2.txt").call(); + RevCommit other = git.commit() + .setMessage("removing folders on 'other'").call(); + + git.checkout().setName(base.name()).call(); + + file = new File(db.getWorkTree(), "unrelated.txt"); + write(file, "unrelated"); + + git.add().addFilepattern("unrelated.txt").call(); + RevCommit head = git.commit().setMessage("Adding another file").call(); + + // Untracked file to cause failing path for delete() of folder1 + // but that's ok. + file = new File(folder1, "file3.txt"); + write(file, "folder1--file3.txt"); + + ResolveMerger merger = (ResolveMerger) strategy.newMerger(db, false); + merger.setCommitNames(new String[] { "BASE", "HEAD", "other" }); + merger.setWorkingTreeIterator(new FileTreeIterator(db)); + boolean ok = merger.merge(head.getId(), other.getId()); + assertTrue(ok); + assertTrue(file.exists()); + } } /** diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/SquashMessageFormatterTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/SquashMessageFormatterTest.java index cddbbd528..7a2586dca 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/SquashMessageFormatterTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/SquashMessageFormatterTest.java @@ -73,17 +73,18 @@ public class SquashMessageFormatterTest extends SampleDataRepositoryTestCase { @Test public void testCommit() throws Exception { - Git git = new Git(db); - revCommit = git.commit().setMessage("squash_me").call(); + try (Git git = new Git(db)) { + revCommit = git.commit().setMessage("squash_me").call(); - Ref master = db.exactRef("refs/heads/master"); - String message = msgFormatter.format(Arrays.asList(revCommit), master); - assertEquals( - "Squashed commit of the following:\n\ncommit " - + revCommit.getName() + "\nAuthor: " - + revCommit.getAuthorIdent().getName() + " <" - + revCommit.getAuthorIdent().getEmailAddress() - + ">\nDate: " + dateFormatter.formatDate(author) - + "\n\n\tsquash_me\n", message); + Ref master = db.exactRef("refs/heads/master"); + String message = msgFormatter.format(Arrays.asList(revCommit), master); + assertEquals( + "Squashed commit of the following:\n\ncommit " + + revCommit.getName() + "\nAuthor: " + + revCommit.getAuthorIdent().getName() + " <" + + revCommit.getAuthorIdent().getEmailAddress() + + ">\nDate: " + dateFormatter.formatDate(author) + + "\n\n\tsquash_me\n", message); + } } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevCommitListTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevCommitListTest.java index 5ec6eb308..4d75322f4 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevCommitListTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevCommitListTest.java @@ -57,14 +57,15 @@ public class RevCommitListTest extends RepositoryTestCase { private RevCommitList list; public void setup(int count) throws Exception { - Git git = new Git(db); - for (int i = 0; i < count; i++) - git.commit().setCommitter(committer).setAuthor(author) - .setMessage("commit " + i).call(); - list = new RevCommitList(); - RevWalk w = new RevWalk(db); - w.markStart(w.lookupCommit(db.resolve(Constants.HEAD))); - list.source(w); + try (Git git = new Git(db); + RevWalk w = new RevWalk(db);) { + for (int i = 0; i < count; i++) + git.commit().setCommitter(committer).setAuthor(author) + .setMessage("commit " + i).call(); + list = new RevCommitList(); + w.markStart(w.lookupCommit(db.resolve(Constants.HEAD))); + list.source(w); + } } @Test @@ -107,17 +108,18 @@ public class RevCommitListTest extends RepositoryTestCase { public void testFillToCommit() throws Exception { setup(3); - RevWalk w = new RevWalk(db); - w.markStart(w.lookupCommit(db.resolve(Constants.HEAD))); + try (RevWalk w = new RevWalk(db)) { + w.markStart(w.lookupCommit(db.resolve(Constants.HEAD))); - w.next(); - RevCommit c = w.next(); - assertNotNull("should have found 2. commit", c); + w.next(); + RevCommit c = w.next(); + assertNotNull("should have found 2. commit", c); - list.fillTo(c, 5); - assertEquals(2, list.size()); - assertEquals("commit 1", list.get(1).getFullMessage()); - assertNull(list.get(3)); + list.fillTo(c, 5); + assertEquals(2, list.size()); + assertEquals("commit 1", list.get(1).getFullMessage()); + assertNull(list.get(3)); + } } @Test diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevCommitParseTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevCommitParseTest.java index 885c1b5b2..1a158425c 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevCommitParseTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevCommitParseTest.java @@ -452,9 +452,10 @@ public class RevCommitParseTest extends RepositoryTestCase { @Test public void testParse_PublicParseMethod() throws UnsupportedEncodingException { - ObjectInserter.Formatter fmt = new ObjectInserter.Formatter(); CommitBuilder src = new CommitBuilder(); - src.setTreeId(fmt.idFor(Constants.OBJ_TREE, new byte[] {})); + try (ObjectInserter.Formatter fmt = new ObjectInserter.Formatter()) { + src.setTreeId(fmt.idFor(Constants.OBJ_TREE, new byte[] {})); + } src.setAuthor(author); src.setCommitter(committer); src.setMessage("Test commit\n\nThis is a test.\n"); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevObjectTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevObjectTest.java index a768bef19..95e7ca6d5 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevObjectTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevObjectTest.java @@ -73,9 +73,12 @@ public class RevObjectTest extends RevWalkTestCase { assertTrue(a1.equals((Object) a1)); assertFalse(a1.equals("")); - final RevWalk rw2 = new RevWalk(db); - final RevCommit a2 = rw2.parseCommit(a1); - final RevCommit b2 = rw2.parseCommit(b1); + final RevCommit a2; + final RevCommit b2; + try (final RevWalk rw2 = new RevWalk(db)) { + a2 = rw2.parseCommit(a1); + b2 = rw2.parseCommit(b1); + } assertNotSame(a1, a2); assertNotSame(b1, b2); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevTagParseTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevTagParseTest.java index 82505caf2..f97043b7e 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevTagParseTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevTagParseTest.java @@ -463,10 +463,11 @@ public class RevTagParseTest extends RepositoryTestCase { @Test public void testParse_PublicParseMethod() throws CorruptObjectException { - ObjectInserter.Formatter fmt = new ObjectInserter.Formatter(); TagBuilder src = new TagBuilder(); - src.setObjectId(fmt.idFor(Constants.OBJ_TREE, new byte[] {}), - Constants.OBJ_TREE); + try (ObjectInserter.Formatter fmt = new ObjectInserter.Formatter()) { + src.setObjectId(fmt.idFor(Constants.OBJ_TREE, new byte[] {}), + Constants.OBJ_TREE); + } src.setTagger(committer); src.setTag("a.test"); src.setMessage("Test tag\n\nThis is a test.\n"); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleAddTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleAddTest.java index b13c4cd80..a131e5e63 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleAddTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleAddTest.java @@ -119,36 +119,37 @@ public class SubmoduleAddTest extends RepositoryTestCase { @Test public void addSubmodule() throws Exception { - Git git = new Git(db); - writeTrashFile("file.txt", "content"); - git.add().addFilepattern("file.txt").call(); - RevCommit commit = git.commit().setMessage("create file").call(); + try (Git git = new Git(db)) { + writeTrashFile("file.txt", "content"); + git.add().addFilepattern("file.txt").call(); + RevCommit commit = git.commit().setMessage("create file").call(); - SubmoduleAddCommand command = new SubmoduleAddCommand(db); - String path = "sub"; - command.setPath(path); - String uri = db.getDirectory().toURI().toString(); - command.setURI(uri); - Repository repo = command.call(); - assertNotNull(repo); - ObjectId subCommit = repo.resolve(Constants.HEAD); - repo.close(); + SubmoduleAddCommand command = new SubmoduleAddCommand(db); + String path = "sub"; + command.setPath(path); + String uri = db.getDirectory().toURI().toString(); + command.setURI(uri); + Repository repo = command.call(); + assertNotNull(repo); + ObjectId subCommit = repo.resolve(Constants.HEAD); + repo.close(); - SubmoduleWalk generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - assertEquals(path, generator.getPath()); - assertEquals(commit, generator.getObjectId()); - assertEquals(uri, generator.getModulesUrl()); - assertEquals(path, generator.getModulesPath()); - assertEquals(uri, generator.getConfigUrl()); - Repository subModRepo = generator.getRepository(); - assertNotNull(subModRepo); - assertEquals(subCommit, commit); - subModRepo.close(); + SubmoduleWalk generator = SubmoduleWalk.forIndex(db); + assertTrue(generator.next()); + assertEquals(path, generator.getPath()); + assertEquals(commit, generator.getObjectId()); + assertEquals(uri, generator.getModulesUrl()); + assertEquals(path, generator.getModulesPath()); + assertEquals(uri, generator.getConfigUrl()); + Repository subModRepo = generator.getRepository(); + assertNotNull(subModRepo); + assertEquals(subCommit, commit); + subModRepo.close(); - Status status = Git.wrap(db).status().call(); - assertTrue(status.getAdded().contains(Constants.DOT_GIT_MODULES)); - assertTrue(status.getAdded().contains(path)); + Status status = Git.wrap(db).status().call(); + assertTrue(status.getAdded().contains(Constants.DOT_GIT_MODULES)); + assertTrue(status.getAdded().contains(path)); + } } @Test @@ -182,45 +183,47 @@ public class SubmoduleAddTest extends RepositoryTestCase { @Test public void addSubmoduleWithRelativeUri() throws Exception { - Git git = new Git(db); - writeTrashFile("file.txt", "content"); - git.add().addFilepattern("file.txt").call(); - RevCommit commit = git.commit().setMessage("create file").call(); + try (Git git = new Git(db)) { + writeTrashFile("file.txt", "content"); + git.add().addFilepattern("file.txt").call(); + RevCommit commit = git.commit().setMessage("create file").call(); - SubmoduleAddCommand command = new SubmoduleAddCommand(db); - String path = "sub"; - String uri = "./.git"; - command.setPath(path); - command.setURI(uri); - Repository repo = command.call(); - assertNotNull(repo); - addRepoToClose(repo); + SubmoduleAddCommand command = new SubmoduleAddCommand(db); + String path = "sub"; + String uri = "./.git"; + command.setPath(path); + command.setURI(uri); + Repository repo = command.call(); + assertNotNull(repo); + addRepoToClose(repo); - SubmoduleWalk generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - assertEquals(path, generator.getPath()); - assertEquals(commit, generator.getObjectId()); - assertEquals(uri, generator.getModulesUrl()); - assertEquals(path, generator.getModulesPath()); - String fullUri = db.getDirectory().getAbsolutePath(); - if (File.separatorChar == '\\') - fullUri = fullUri.replace('\\', '/'); - assertEquals(fullUri, generator.getConfigUrl()); - Repository subModRepo = generator.getRepository(); - assertNotNull(subModRepo); - assertEquals( - fullUri, - subModRepo - .getConfig() - .getString(ConfigConstants.CONFIG_REMOTE_SECTION, - Constants.DEFAULT_REMOTE_NAME, - ConfigConstants.CONFIG_KEY_URL)); - subModRepo.close(); - assertEquals(commit, repo.resolve(Constants.HEAD)); + SubmoduleWalk generator = SubmoduleWalk.forIndex(db); + assertTrue(generator.next()); + assertEquals(path, generator.getPath()); + assertEquals(commit, generator.getObjectId()); + assertEquals(uri, generator.getModulesUrl()); + assertEquals(path, generator.getModulesPath()); + String fullUri = db.getDirectory().getAbsolutePath(); + if (File.separatorChar == '\\') { + fullUri = fullUri.replace('\\', '/'); + } + assertEquals(fullUri, generator.getConfigUrl()); + Repository subModRepo = generator.getRepository(); + assertNotNull(subModRepo); + assertEquals( + fullUri, + subModRepo + .getConfig() + .getString(ConfigConstants.CONFIG_REMOTE_SECTION, + Constants.DEFAULT_REMOTE_NAME, + ConfigConstants.CONFIG_KEY_URL)); + subModRepo.close(); + assertEquals(commit, repo.resolve(Constants.HEAD)); - Status status = Git.wrap(db).status().call(); - assertTrue(status.getAdded().contains(Constants.DOT_GIT_MODULES)); - assertTrue(status.getAdded().contains(path)); + Status status = Git.wrap(db).status().call(); + assertTrue(status.getAdded().contains(Constants.DOT_GIT_MODULES)); + assertTrue(status.getAdded().contains(path)); + } } @Test @@ -237,31 +240,32 @@ public class SubmoduleAddTest extends RepositoryTestCase { path1, ConfigConstants.CONFIG_KEY_URL, url1); modulesConfig.save(); - Git git = new Git(db); - writeTrashFile("file.txt", "content"); - git.add().addFilepattern("file.txt").call(); - assertNotNull(git.commit().setMessage("create file").call()); + try (Git git = new Git(db)) { + writeTrashFile("file.txt", "content"); + git.add().addFilepattern("file.txt").call(); + assertNotNull(git.commit().setMessage("create file").call()); - SubmoduleAddCommand command = new SubmoduleAddCommand(db); - command.setPath(path2); - String url2 = db.getDirectory().toURI().toString(); - command.setURI(url2); - Repository r = command.call(); - assertNotNull(r); - addRepoToClose(r); + SubmoduleAddCommand command = new SubmoduleAddCommand(db); + command.setPath(path2); + String url2 = db.getDirectory().toURI().toString(); + command.setURI(url2); + Repository r = command.call(); + assertNotNull(r); + addRepoToClose(r); - modulesConfig.load(); - assertEquals(path1, modulesConfig.getString( - ConfigConstants.CONFIG_SUBMODULE_SECTION, path1, - ConfigConstants.CONFIG_KEY_PATH)); - assertEquals(url1, modulesConfig.getString( - ConfigConstants.CONFIG_SUBMODULE_SECTION, path1, - ConfigConstants.CONFIG_KEY_URL)); - assertEquals(path2, modulesConfig.getString( - ConfigConstants.CONFIG_SUBMODULE_SECTION, path2, - ConfigConstants.CONFIG_KEY_PATH)); - assertEquals(url2, modulesConfig.getString( - ConfigConstants.CONFIG_SUBMODULE_SECTION, path2, - ConfigConstants.CONFIG_KEY_URL)); + modulesConfig.load(); + assertEquals(path1, modulesConfig.getString( + ConfigConstants.CONFIG_SUBMODULE_SECTION, path1, + ConfigConstants.CONFIG_KEY_PATH)); + assertEquals(url1, modulesConfig.getString( + ConfigConstants.CONFIG_SUBMODULE_SECTION, path1, + ConfigConstants.CONFIG_KEY_URL)); + assertEquals(path2, modulesConfig.getString( + ConfigConstants.CONFIG_SUBMODULE_SECTION, path2, + ConfigConstants.CONFIG_KEY_PATH)); + assertEquals(url2, modulesConfig.getString( + ConfigConstants.CONFIG_SUBMODULE_SECTION, path2, + ConfigConstants.CONFIG_KEY_URL)); + } } } \ No newline at end of file diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/symlinks/SymlinksTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/symlinks/SymlinksTest.java index 3ddc3ded8..274fa539c 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/symlinks/SymlinksTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/symlinks/SymlinksTest.java @@ -75,26 +75,27 @@ public class SymlinksTest extends RepositoryTestCase { */ @Test public void fileModeTestFileThenSymlink() throws Exception { - Git git = new Git(db); - writeTrashFile("a", "Hello world a"); - writeTrashFile("b", "Hello world b"); - git.add().addFilepattern(".").call(); - git.commit().setMessage("add files a & b").call(); - Ref branch_1 = git.branchCreate().setName("branch_1").call(); - git.rm().addFilepattern("a").call(); - FileUtils.createSymLink(new File(db.getWorkTree(), "a"), "b"); - git.add().addFilepattern("a").call(); - git.commit().setMessage("add symlink a").call(); + try (Git git = new Git(db)) { + writeTrashFile("a", "Hello world a"); + writeTrashFile("b", "Hello world b"); + git.add().addFilepattern(".").call(); + git.commit().setMessage("add files a & b").call(); + Ref branch_1 = git.branchCreate().setName("branch_1").call(); + git.rm().addFilepattern("a").call(); + FileUtils.createSymLink(new File(db.getWorkTree(), "a"), "b"); + git.add().addFilepattern("a").call(); + git.commit().setMessage("add symlink a").call(); - FileEntry entry = new FileTreeIterator.FileEntry(new File( - db.getWorkTree(), "a"), db.getFS()); - assertEquals(FileMode.SYMLINK, entry.getMode()); + FileEntry entry = new FileTreeIterator.FileEntry(new File( + db.getWorkTree(), "a"), db.getFS()); + assertEquals(FileMode.SYMLINK, entry.getMode()); - git.checkout().setName(branch_1.getName()).call(); + git.checkout().setName(branch_1.getName()).call(); - entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"), - db.getFS()); - assertEquals(FileMode.REGULAR_FILE, entry.getMode()); + entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"), + db.getFS()); + assertEquals(FileMode.REGULAR_FILE, entry.getMode()); + } } /** @@ -108,26 +109,27 @@ public class SymlinksTest extends RepositoryTestCase { */ @Test public void fileModeTestSymlinkThenFile() throws Exception { - Git git = new Git(db); - writeTrashFile("b", "Hello world b"); - FileUtils.createSymLink(new File(db.getWorkTree(), "a"), "b"); - git.add().addFilepattern(".").call(); - git.commit().setMessage("add file b & symlink a").call(); - Ref branch_1 = git.branchCreate().setName("branch_1").call(); - git.rm().addFilepattern("a").call(); - writeTrashFile("a", "Hello world a"); - git.add().addFilepattern("a").call(); - git.commit().setMessage("add file a").call(); + try (Git git = new Git(db)) { + writeTrashFile("b", "Hello world b"); + FileUtils.createSymLink(new File(db.getWorkTree(), "a"), "b"); + git.add().addFilepattern(".").call(); + git.commit().setMessage("add file b & symlink a").call(); + Ref branch_1 = git.branchCreate().setName("branch_1").call(); + git.rm().addFilepattern("a").call(); + writeTrashFile("a", "Hello world a"); + git.add().addFilepattern("a").call(); + git.commit().setMessage("add file a").call(); - FileEntry entry = new FileTreeIterator.FileEntry(new File( - db.getWorkTree(), "a"), db.getFS()); - assertEquals(FileMode.REGULAR_FILE, entry.getMode()); + FileEntry entry = new FileTreeIterator.FileEntry(new File( + db.getWorkTree(), "a"), db.getFS()); + assertEquals(FileMode.REGULAR_FILE, entry.getMode()); - git.checkout().setName(branch_1.getName()).call(); + git.checkout().setName(branch_1.getName()).call(); - entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"), - db.getFS()); - assertEquals(FileMode.SYMLINK, entry.getMode()); + entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"), + db.getFS()); + assertEquals(FileMode.SYMLINK, entry.getMode()); + } } /** @@ -141,27 +143,28 @@ public class SymlinksTest extends RepositoryTestCase { */ @Test public void fileModeTestFolderThenSymlink() throws Exception { - Git git = new Git(db); - FileUtils.mkdirs(new File(db.getWorkTree(), "a")); - writeTrashFile("a/b", "Hello world b"); - writeTrashFile("c", "Hello world c"); - git.add().addFilepattern(".").call(); - git.commit().setMessage("add folder a").call(); - Ref branch_1 = git.branchCreate().setName("branch_1").call(); - git.rm().addFilepattern("a").call(); - FileUtils.createSymLink(new File(db.getWorkTree(), "a"), "c"); - git.add().addFilepattern("a").call(); - git.commit().setMessage("add symlink a").call(); + try (Git git = new Git(db)) { + FileUtils.mkdirs(new File(db.getWorkTree(), "a")); + writeTrashFile("a/b", "Hello world b"); + writeTrashFile("c", "Hello world c"); + git.add().addFilepattern(".").call(); + git.commit().setMessage("add folder a").call(); + Ref branch_1 = git.branchCreate().setName("branch_1").call(); + git.rm().addFilepattern("a").call(); + FileUtils.createSymLink(new File(db.getWorkTree(), "a"), "c"); + git.add().addFilepattern("a").call(); + git.commit().setMessage("add symlink a").call(); - FileEntry entry = new FileTreeIterator.FileEntry(new File( - db.getWorkTree(), "a"), db.getFS()); - assertEquals(FileMode.SYMLINK, entry.getMode()); + FileEntry entry = new FileTreeIterator.FileEntry(new File( + db.getWorkTree(), "a"), db.getFS()); + assertEquals(FileMode.SYMLINK, entry.getMode()); - git.checkout().setName(branch_1.getName()).call(); + git.checkout().setName(branch_1.getName()).call(); - entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"), - db.getFS()); - assertEquals(FileMode.TREE, entry.getMode()); + entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"), + db.getFS()); + assertEquals(FileMode.TREE, entry.getMode()); + } } /** @@ -175,27 +178,28 @@ public class SymlinksTest extends RepositoryTestCase { */ @Test public void fileModeTestSymlinkThenFolder() throws Exception { - Git git = new Git(db); - writeTrashFile("c", "Hello world c"); - FileUtils.createSymLink(new File(db.getWorkTree(), "a"), "c"); - git.add().addFilepattern(".").call(); - git.commit().setMessage("add symlink a").call(); - Ref branch_1 = git.branchCreate().setName("branch_1").call(); - git.rm().addFilepattern("a").call(); - FileUtils.mkdirs(new File(db.getWorkTree(), "a")); - writeTrashFile("a/b", "Hello world b"); - git.add().addFilepattern("a").call(); - git.commit().setMessage("add folder a").call(); + try (Git git = new Git(db)) { + writeTrashFile("c", "Hello world c"); + FileUtils.createSymLink(new File(db.getWorkTree(), "a"), "c"); + git.add().addFilepattern(".").call(); + git.commit().setMessage("add symlink a").call(); + Ref branch_1 = git.branchCreate().setName("branch_1").call(); + git.rm().addFilepattern("a").call(); + FileUtils.mkdirs(new File(db.getWorkTree(), "a")); + writeTrashFile("a/b", "Hello world b"); + git.add().addFilepattern("a").call(); + git.commit().setMessage("add folder a").call(); - FileEntry entry = new FileTreeIterator.FileEntry(new File( - db.getWorkTree(), "a"), db.getFS()); - assertEquals(FileMode.TREE, entry.getMode()); + FileEntry entry = new FileTreeIterator.FileEntry(new File( + db.getWorkTree(), "a"), db.getFS()); + assertEquals(FileMode.TREE, entry.getMode()); - git.checkout().setName(branch_1.getName()).call(); + git.checkout().setName(branch_1.getName()).call(); - entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"), - db.getFS()); - assertEquals(FileMode.SYMLINK, entry.getMode()); + entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"), + db.getFS()); + assertEquals(FileMode.SYMLINK, entry.getMode()); + } } /** @@ -209,24 +213,25 @@ public class SymlinksTest extends RepositoryTestCase { */ @Test public void fileModeTestMissingThenSymlink() throws Exception { - Git git = new Git(db); - writeTrashFile("b", "Hello world b"); - git.add().addFilepattern(".").call(); - RevCommit commit1 = git.commit().setMessage("add file b").call(); - Ref branch_1 = git.branchCreate().setName("branch_1").call(); - FileUtils.createSymLink(new File(db.getWorkTree(), "a"), "b"); - git.add().addFilepattern("a").call(); - RevCommit commit2 = git.commit().setMessage("add symlink a").call(); + try (Git git = new Git(db); + TreeWalk tw = new TreeWalk(db);) { + writeTrashFile("b", "Hello world b"); + git.add().addFilepattern(".").call(); + RevCommit commit1 = git.commit().setMessage("add file b").call(); + Ref branch_1 = git.branchCreate().setName("branch_1").call(); + FileUtils.createSymLink(new File(db.getWorkTree(), "a"), "b"); + git.add().addFilepattern("a").call(); + RevCommit commit2 = git.commit().setMessage("add symlink a").call(); - git.checkout().setName(branch_1.getName()).call(); + git.checkout().setName(branch_1.getName()).call(); - TreeWalk tw = new TreeWalk(db); - tw.addTree(commit1.getTree()); - tw.addTree(commit2.getTree()); - List scan = DiffEntry.scan(tw); - assertEquals(1, scan.size()); - assertEquals(FileMode.SYMLINK, scan.get(0).getNewMode()); - assertEquals(FileMode.MISSING, scan.get(0).getOldMode()); + tw.addTree(commit1.getTree()); + tw.addTree(commit2.getTree()); + List scan = DiffEntry.scan(tw); + assertEquals(1, scan.size()); + assertEquals(FileMode.SYMLINK, scan.get(0).getNewMode()); + assertEquals(FileMode.MISSING, scan.get(0).getOldMode()); + } } /** @@ -240,97 +245,101 @@ public class SymlinksTest extends RepositoryTestCase { */ @Test public void fileModeTestSymlinkThenMissing() throws Exception { - Git git = new Git(db); - writeTrashFile("b", "Hello world b"); - FileUtils.createSymLink(new File(db.getWorkTree(), "a"), "b"); - git.add().addFilepattern(".").call(); - RevCommit commit1 = git.commit().setMessage("add file b & symlink a") - .call(); - Ref branch_1 = git.branchCreate().setName("branch_1").call(); - git.rm().addFilepattern("a").call(); - RevCommit commit2 = git.commit().setMessage("delete symlink a").call(); + try (Git git = new Git(db); + TreeWalk tw = new TreeWalk(db);) { + writeTrashFile("b", "Hello world b"); + FileUtils.createSymLink(new File(db.getWorkTree(), "a"), "b"); + git.add().addFilepattern(".").call(); + RevCommit commit1 = git.commit().setMessage("add file b & symlink a") + .call(); + Ref branch_1 = git.branchCreate().setName("branch_1").call(); + git.rm().addFilepattern("a").call(); + RevCommit commit2 = git.commit().setMessage("delete symlink a").call(); - git.checkout().setName(branch_1.getName()).call(); + git.checkout().setName(branch_1.getName()).call(); - TreeWalk tw = new TreeWalk(db); - tw.addTree(commit1.getTree()); - tw.addTree(commit2.getTree()); - List scan = DiffEntry.scan(tw); - assertEquals(1, scan.size()); - assertEquals(FileMode.MISSING, scan.get(0).getNewMode()); - assertEquals(FileMode.SYMLINK, scan.get(0).getOldMode()); + tw.addTree(commit1.getTree()); + tw.addTree(commit2.getTree()); + List scan = DiffEntry.scan(tw); + assertEquals(1, scan.size()); + assertEquals(FileMode.MISSING, scan.get(0).getNewMode()); + assertEquals(FileMode.SYMLINK, scan.get(0).getOldMode()); + } } @Test public void createSymlinkAfterTarget() throws Exception { - Git git = new Git(db); - writeTrashFile("a", "start"); - git.add().addFilepattern("a").call(); - RevCommit base = git.commit().setMessage("init").call(); - writeTrashFile("target", "someData"); - FileUtils.createSymLink(new File(db.getWorkTree(), "link"), "target"); - git.add().addFilepattern("target").addFilepattern("link").call(); - git.commit().setMessage("add target").call(); - assertEquals(4, db.getWorkTree().list().length); // self-check - git.checkout().setName(base.name()).call(); - assertEquals(2, db.getWorkTree().list().length); // self-check - git.checkout().setName("master").call(); - assertEquals(4, db.getWorkTree().list().length); - String data = read(new File(db.getWorkTree(), "target")); - assertEquals(8, new File(db.getWorkTree(), "target").length()); - assertEquals("someData", data); - data = read(new File(db.getWorkTree(), "link")); - assertEquals("target", - FileUtils.readSymLink(new File(db.getWorkTree(), "link"))); - assertEquals("someData", data); + try (Git git = new Git(db)) { + writeTrashFile("a", "start"); + git.add().addFilepattern("a").call(); + RevCommit base = git.commit().setMessage("init").call(); + writeTrashFile("target", "someData"); + FileUtils.createSymLink(new File(db.getWorkTree(), "link"), "target"); + git.add().addFilepattern("target").addFilepattern("link").call(); + git.commit().setMessage("add target").call(); + assertEquals(4, db.getWorkTree().list().length); // self-check + git.checkout().setName(base.name()).call(); + assertEquals(2, db.getWorkTree().list().length); // self-check + git.checkout().setName("master").call(); + assertEquals(4, db.getWorkTree().list().length); + String data = read(new File(db.getWorkTree(), "target")); + assertEquals(8, new File(db.getWorkTree(), "target").length()); + assertEquals("someData", data); + data = read(new File(db.getWorkTree(), "link")); + assertEquals("target", + FileUtils.readSymLink(new File(db.getWorkTree(), "link"))); + assertEquals("someData", data); + } } @Test public void createFileSymlinkBeforeTarget() throws Exception { - Git git = new Git(db); - writeTrashFile("a", "start"); - git.add().addFilepattern("a").call(); - RevCommit base = git.commit().setMessage("init").call(); - writeTrashFile("target", "someData"); - FileUtils.createSymLink(new File(db.getWorkTree(), "tlink"), "target"); - git.add().addFilepattern("target").addFilepattern("tlink").call(); - git.commit().setMessage("add target").call(); - assertEquals(4, db.getWorkTree().list().length); // self-check - git.checkout().setName(base.name()).call(); - assertEquals(2, db.getWorkTree().list().length); // self-check - git.checkout().setName("master").call(); - assertEquals(4, db.getWorkTree().list().length); - String data = read(new File(db.getWorkTree(), "target")); - assertEquals(8, new File(db.getWorkTree(), "target").length()); - assertEquals("someData", data); - data = read(new File(db.getWorkTree(), "tlink")); - assertEquals("target", - FileUtils.readSymLink(new File(db.getWorkTree(), "tlink"))); - assertEquals("someData", data); + try (Git git = new Git(db)) { + writeTrashFile("a", "start"); + git.add().addFilepattern("a").call(); + RevCommit base = git.commit().setMessage("init").call(); + writeTrashFile("target", "someData"); + FileUtils.createSymLink(new File(db.getWorkTree(), "tlink"), "target"); + git.add().addFilepattern("target").addFilepattern("tlink").call(); + git.commit().setMessage("add target").call(); + assertEquals(4, db.getWorkTree().list().length); // self-check + git.checkout().setName(base.name()).call(); + assertEquals(2, db.getWorkTree().list().length); // self-check + git.checkout().setName("master").call(); + assertEquals(4, db.getWorkTree().list().length); + String data = read(new File(db.getWorkTree(), "target")); + assertEquals(8, new File(db.getWorkTree(), "target").length()); + assertEquals("someData", data); + data = read(new File(db.getWorkTree(), "tlink")); + assertEquals("target", + FileUtils.readSymLink(new File(db.getWorkTree(), "tlink"))); + assertEquals("someData", data); + } } @Test public void createDirSymlinkBeforeTarget() throws Exception { - Git git = new Git(db); - writeTrashFile("a", "start"); - git.add().addFilepattern("a").call(); - RevCommit base = git.commit().setMessage("init").call(); - FileUtils.createSymLink(new File(db.getWorkTree(), "link"), "target"); - FileUtils.mkdir(new File(db.getWorkTree(), "target")); - writeTrashFile("target/file", "someData"); - git.add().addFilepattern("target").addFilepattern("link").call(); - git.commit().setMessage("add target").call(); - assertEquals(4, db.getWorkTree().list().length); // self-check - git.checkout().setName(base.name()).call(); - assertEquals(2, db.getWorkTree().list().length); // self-check - git.checkout().setName("master").call(); - assertEquals(4, db.getWorkTree().list().length); - String data = read(new File(db.getWorkTree(), "target/file")); - assertEquals(8, new File(db.getWorkTree(), "target/file").length()); - assertEquals("someData", data); - data = read(new File(db.getWorkTree(), "link/file")); - assertEquals("target", - FileUtils.readSymLink(new File(db.getWorkTree(), "link"))); - assertEquals("someData", data); + try (Git git = new Git(db)) { + writeTrashFile("a", "start"); + git.add().addFilepattern("a").call(); + RevCommit base = git.commit().setMessage("init").call(); + FileUtils.createSymLink(new File(db.getWorkTree(), "link"), "target"); + FileUtils.mkdir(new File(db.getWorkTree(), "target")); + writeTrashFile("target/file", "someData"); + git.add().addFilepattern("target").addFilepattern("link").call(); + git.commit().setMessage("add target").call(); + assertEquals(4, db.getWorkTree().list().length); // self-check + git.checkout().setName(base.name()).call(); + assertEquals(2, db.getWorkTree().list().length); // self-check + git.checkout().setName("master").call(); + assertEquals(4, db.getWorkTree().list().length); + String data = read(new File(db.getWorkTree(), "target/file")); + assertEquals(8, new File(db.getWorkTree(), "target/file").length()); + assertEquals("someData", data); + data = read(new File(db.getWorkTree(), "link/file")); + assertEquals("target", + FileUtils.readSymLink(new File(db.getWorkTree(), "link"))); + assertEquals("someData", data); + } } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/TreeWalkJava7Test.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/TreeWalkJava7Test.java index 1328b38e6..ba8f194e3 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/TreeWalkJava7Test.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/TreeWalkJava7Test.java @@ -60,13 +60,14 @@ public class TreeWalkJava7Test extends RepositoryTestCase { assertTrue(fs.supportsSymlinks()); writeTrashFile("target/data", "targetdata"); fs.createSymLink(new File(trash, "link"), "target"); - TreeWalk tw = new TreeWalk(db); - tw.setRecursive(true); - tw.addTree(new FileTreeIterator(db)); - assertTrue(tw.next()); - assertEquals("link", tw.getPathString()); - assertTrue(tw.next()); - assertEquals("target/data", tw.getPathString()); - assertFalse(tw.next()); + try (TreeWalk tw = new TreeWalk(db)) { + tw.setRecursive(true); + tw.addTree(new FileTreeIterator(db)); + assertTrue(tw.next()); + assertEquals("link", tw.getPathString()); + assertTrue(tw.next()); + assertEquals("target/data", tw.getPathString()); + assertFalse(tw.next()); + } } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/filter/TreeFilterTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/filter/TreeFilterTest.java index 09007e7cb..c3423b6fb 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/filter/TreeFilterTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/filter/TreeFilterTest.java @@ -55,9 +55,10 @@ import org.junit.Test; public class TreeFilterTest extends RepositoryTestCase { @Test public void testALL_IncludesAnything() throws Exception { - final TreeWalk tw = new TreeWalk(db); - tw.addTree(new EmptyTreeIterator()); - assertTrue(TreeFilter.ALL.include(tw)); + try (final TreeWalk tw = new TreeWalk(db)) { + tw.addTree(new EmptyTreeIterator()); + assertTrue(TreeFilter.ALL.include(tw)); + } } @Test @@ -72,16 +73,18 @@ public class TreeFilterTest extends RepositoryTestCase { @Test public void testNotALL_IncludesNothing() throws Exception { - final TreeWalk tw = new TreeWalk(db); - tw.addTree(new EmptyTreeIterator()); - assertFalse(TreeFilter.ALL.negate().include(tw)); + try (final TreeWalk tw = new TreeWalk(db)) { + tw.addTree(new EmptyTreeIterator()); + assertFalse(TreeFilter.ALL.negate().include(tw)); + } } @Test public void testANY_DIFF_IncludesSingleTreeCase() throws Exception { - final TreeWalk tw = new TreeWalk(db); - tw.addTree(new EmptyTreeIterator()); - assertTrue(TreeFilter.ANY_DIFF.include(tw)); + try (final TreeWalk tw = new TreeWalk(db)) { + tw.addTree(new EmptyTreeIterator()); + assertTrue(TreeFilter.ANY_DIFF.include(tw)); + } } @Test diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffFormatter.java b/org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffFormatter.java index 4c0ed386e..fc701f3a5 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffFormatter.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffFormatter.java @@ -666,6 +666,9 @@ public class DiffFormatter implements AutoCloseable { } private static byte[] writeGitLinkText(AbbreviatedObjectId id) { + if (id.toObjectId().equals(ObjectId.zeroId())) { + return EMPTY; + } return encodeASCII("Subproject commit " + id.name() //$NON-NLS-1$ + "\n"); //$NON-NLS-1$ } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoProject.java b/org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoProject.java index 915066d58..f6d1209cb 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoProject.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoProject.java @@ -252,10 +252,10 @@ public class RepoProject implements Comparable { /** * Add a bunch of copyfile configurations. * - * @param copyfiles + * @param copyFiles */ - public void addCopyFiles(Collection copyfiles) { - this.copyfiles.addAll(copyfiles); + public void addCopyFiles(Collection copyFiles) { + this.copyfiles.addAll(copyFiles); } /** @@ -288,13 +288,13 @@ public class RepoProject implements Comparable { /** * Check if this sub repo is an ancestor of the given path. * - * @param path + * @param thatPath * path to be checked to see if it is within this repository * @return true if this sub repo is an ancestor of the given path. * @since 4.2 */ - public boolean isAncestorOf(String path) { - return path.startsWith(getPathWithSlash()); + public boolean isAncestorOf(String thatPath) { + return thatPath.startsWith(getPathWithSlash()); } @Override