CommitCommandTest: Open Git and TreeWalk in try-with-resource
Change-Id: I65a6fd7028e209c300d992c2756100c09ab4dc19 Signed-off-by: David Pursehouse <david.pursehouse@sonymobile.com>
This commit is contained in:
parent
281dcf8956
commit
ff90192a05
|
@ -186,7 +186,7 @@ public boolean isCaseSensitive() {
|
|||
|
||||
@Test
|
||||
public void commitNewSubmodule() throws Exception {
|
||||
Git git = new Git(db);
|
||||
try (Git git = new Git(db)) {
|
||||
writeTrashFile("file.txt", "content");
|
||||
git.add().addFilepattern("file.txt").call();
|
||||
RevCommit commit = git.commit().setMessage("create file").call();
|
||||
|
@ -215,7 +215,7 @@ public void commitNewSubmodule() throws Exception {
|
|||
RevCommit submoduleCommit = git.commit().setMessage("submodule add")
|
||||
.setOnly(path).call();
|
||||
assertNotNull(submoduleCommit);
|
||||
TreeWalk walk = new TreeWalk(db);
|
||||
try (TreeWalk walk = new TreeWalk(db)) {
|
||||
walk.addTree(commit.getTree());
|
||||
walk.addTree(submoduleCommit.getTree());
|
||||
walk.setFilter(TreeFilter.ANY_DIFF);
|
||||
|
@ -228,10 +228,12 @@ public void commitNewSubmodule() throws Exception {
|
|||
assertEquals(commit, subDiff.getNewId().toObjectId());
|
||||
assertEquals(path, subDiff.getNewPath());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void commitSubmoduleUpdate() throws Exception {
|
||||
Git git = new Git(db);
|
||||
try (Git git = new Git(db)) {
|
||||
writeTrashFile("file.txt", "content");
|
||||
git.add().addFilepattern("file.txt").call();
|
||||
RevCommit commit = git.commit().setMessage("create file").call();
|
||||
|
@ -271,7 +273,7 @@ public void commitSubmoduleUpdate() throws Exception {
|
|||
RevCommit submoduleEditCommit = git.commit()
|
||||
.setMessage("submodule add").setOnly(path).call();
|
||||
assertNotNull(submoduleEditCommit);
|
||||
TreeWalk walk = new TreeWalk(db);
|
||||
try (TreeWalk walk = new TreeWalk(db)) {
|
||||
walk.addTree(submoduleAddCommit.getTree());
|
||||
walk.addTree(submoduleEditCommit.getTree());
|
||||
walk.setFilter(TreeFilter.ANY_DIFF);
|
||||
|
@ -285,11 +287,12 @@ public void commitSubmoduleUpdate() throws Exception {
|
|||
assertEquals(path, subDiff.getNewPath());
|
||||
assertEquals(path, subDiff.getOldPath());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void commitUpdatesSmudgedEntries() throws Exception {
|
||||
Git git = new Git(db);
|
||||
|
||||
try (Git git = new Git(db)) {
|
||||
File file1 = writeTrashFile("file1.txt", "content1");
|
||||
assertTrue(file1.setLastModified(file1.lastModified() - 5000));
|
||||
File file2 = writeTrashFile("file2.txt", "content2");
|
||||
|
@ -341,11 +344,11 @@ public void commitUpdatesSmudgedEntries() throws Exception {
|
|||
assertEquals(file2Id, cache.getEntry("file2.txt").getObjectId());
|
||||
assertEquals(file3Id, cache.getEntry("file3.txt").getObjectId());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void commitIgnoresSmudgedEntryWithDifferentId() throws Exception {
|
||||
Git git = new Git(db);
|
||||
|
||||
try (Git git = new Git(db)) {
|
||||
File file1 = writeTrashFile("file1.txt", "content1");
|
||||
assertTrue(file1.setLastModified(file1.lastModified() - 5000));
|
||||
File file2 = writeTrashFile("file2.txt", "content2");
|
||||
|
@ -391,11 +394,11 @@ public void commitIgnoresSmudgedEntryWithDifferentId() throws Exception {
|
|||
assertEquals(file1Size, cache.getEntry("file1.txt").getLength());
|
||||
assertEquals(0, cache.getEntry("file2.txt").getLength());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void commitAfterSquashMerge() throws Exception {
|
||||
Git git = new Git(db);
|
||||
|
||||
try (Git git = new Git(db)) {
|
||||
writeTrashFile("file1", "file1");
|
||||
git.add().addFilepattern("file1").call();
|
||||
RevCommit first = git.commit().setMessage("initial commit").call();
|
||||
|
@ -431,18 +434,19 @@ public void commitAfterSquashMerge() throws Exception {
|
|||
assertEquals("commit: Squashed commit of the following:", db
|
||||
.getReflogReader(db.getBranch()).getLastEntry().getComment());
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = WrongRepositoryStateException.class)
|
||||
public void commitAmendOnInitialShouldFail() throws Exception {
|
||||
Git git = new Git(db);
|
||||
try (Git git = new Git(db)) {
|
||||
git.commit().setAmend(true).setMessage("initial commit").call();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void commitAmendWithoutAuthorShouldSetOriginalAuthorAndAuthorTime()
|
||||
throws Exception {
|
||||
Git git = new Git(db);
|
||||
|
||||
try (Git git = new Git(db)) {
|
||||
writeTrashFile("file1", "file1");
|
||||
git.add().addFilepattern("file1").call();
|
||||
|
||||
|
@ -461,11 +465,11 @@ public void commitAmendWithoutAuthorShouldSetOriginalAuthorAndAuthorTime()
|
|||
assertEquals(authorEmail, amendedAuthor.getEmailAddress());
|
||||
assertEquals(authorDate.getTime(), amendedAuthor.getWhen().getTime());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void commitAmendWithAuthorShouldUseIt() throws Exception {
|
||||
Git git = new Git(db);
|
||||
|
||||
try (Git git = new Git(db)) {
|
||||
writeTrashFile("file1", "file1");
|
||||
git.add().addFilepattern("file1").call();
|
||||
git.commit().setMessage("initial commit").call();
|
||||
|
@ -478,6 +482,7 @@ public void commitAmendWithAuthorShouldUseIt() throws Exception {
|
|||
assertEquals("New Author", amendedAuthor.getName());
|
||||
assertEquals("newauthor@example.org", amendedAuthor.getEmailAddress());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void commitEmptyCommits() throws Exception {
|
||||
|
@ -532,7 +537,7 @@ public void commitOnlyShouldCommitUnmergedPathAndNotAffectOthers()
|
|||
+ "[unmerged2, mode:100644, stage:3]",
|
||||
indexState(0));
|
||||
|
||||
Git git = new Git(db);
|
||||
try (Git git = new Git(db)) {
|
||||
RevCommit commit = git.commit().setOnly("unmerged1")
|
||||
.setMessage("Only one file").call();
|
||||
|
||||
|
@ -546,6 +551,7 @@ public void commitOnlyShouldCommitUnmergedPathAndNotAffectOthers()
|
|||
assertEquals(FileMode.REGULAR_FILE, walk.getFileMode(0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void commitOnlyShouldHandleIgnored() throws Exception {
|
||||
|
|
Loading…
Reference in New Issue