Browse Source

RebaseCommandTest: Open RevWalk in try-with-resource

Change-Id: If7b7a90171d1349d634a84f135471ebac29e3210
Signed-off-by: David Pursehouse <david.pursehouse@sonymobile.com>
stable-4.2
David Pursehouse 9 years ago
parent
commit
92a8e0184c
  1. 64
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RebaseCommandTest.java

64
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RebaseCommandTest.java

@ -288,13 +288,14 @@ public class RebaseCommandTest extends RepositoryTestCase {
RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
assertEquals(Status.OK, res.getStatus());
RevWalk rw = new RevWalk(db);
try (RevWalk rw = new RevWalk(db)) {
rw.markStart(rw.parseCommit(db.resolve("refs/heads/topic")));
assertDerivedFrom(rw.next(), e);
assertDerivedFrom(rw.next(), d);
assertDerivedFrom(rw.next(), c);
assertEquals(b, rw.next());
assertEquals(a, rw.next());
}
List<ReflogEntry> headLog = db.getReflogReader(Constants.HEAD)
.getReverseEntries();
@ -354,8 +355,6 @@ public class RebaseCommandTest extends RepositoryTestCase {
*/
private void doTestRebasePreservingMerges(boolean testConflict)
throws Exception {
RevWalk rw = new RevWalk(db);
// create file1 on master
writeTrashFile(FILE1, FILE1);
git.add().addFilepattern(FILE1).call();
@ -409,8 +408,10 @@ public class RebaseCommandTest extends RepositoryTestCase {
f = git.commit().setMessage("commit f").call();
} else {
assertEquals(MergeStatus.MERGED, result.getMergeStatus());
try (RevWalk rw = new RevWalk(db)) {
f = rw.parseCommit(result.getNewHead());
}
}
RebaseResult res = git.rebase().setUpstream("refs/heads/master")
.setPreserveMerges(true).call();
@ -453,6 +454,7 @@ public class RebaseCommandTest extends RepositoryTestCase {
assertEquals("file2", read("file2"));
assertEquals("more change", read("file3"));
try (RevWalk rw = new RevWalk(db)) {
rw.markStart(rw.parseCommit(db.resolve("refs/heads/topic")));
RevCommit newF = rw.next();
assertDerivedFrom(newF, f);
@ -471,6 +473,7 @@ public class RebaseCommandTest extends RepositoryTestCase {
assertEquals(b, rw.next());
assertEquals(a, rw.next());
}
}
private String readFile(String path, RevCommit commit) throws IOException {
try (TreeWalk walk = TreeWalk.forPath(db, path, commit.getTree())) {
@ -517,7 +520,7 @@ public class RebaseCommandTest extends RepositoryTestCase {
*/
private void doTestRebasePreservingMergesWithUnrelatedSide(
boolean testConflict) throws Exception {
RevWalk rw = new RevWalk(db);
try (RevWalk rw = new RevWalk(db)) {
rw.sort(RevSort.TOPO);
writeTrashFile(FILE1, FILE1);
@ -600,6 +603,7 @@ public class RebaseCommandTest extends RepositoryTestCase {
assertEquals(b, rw.next());
assertEquals(a, rw.next());
}
}
@Test
public void testRebaseParentOntoHeadShouldBeUptoDate() throws Exception {
@ -687,8 +691,10 @@ public class RebaseCommandTest extends RepositoryTestCase {
checkFile(theFile, "1master\n2\n3\ntopic\n");
// our old branch should be checked out again
assertEquals("refs/heads/topic", db.getFullBranch());
assertEquals(lastMasterChange, new RevWalk(db).parseCommit(
try (RevWalk rw = new RevWalk(db)) {
assertEquals(lastMasterChange, rw.parseCommit(
db.resolve(Constants.HEAD)).getParent(0));
}
assertEquals(origHead, db.readOrigHead());
List<ReflogEntry> headLog = db.getReflogReader(Constants.HEAD)
.getReverseEntries();
@ -737,8 +743,10 @@ public class RebaseCommandTest extends RepositoryTestCase {
RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
assertEquals(Status.OK, res.getStatus());
checkFile(theFile, "1master\n2\n3\ntopic\n");
assertEquals(lastMasterChange, new RevWalk(db).parseCommit(
try (RevWalk rw = new RevWalk(db)) {
assertEquals(lastMasterChange, rw.parseCommit(
db.resolve(Constants.HEAD)).getParent(0));
}
List<ReflogEntry> headLog = db.getReflogReader(Constants.HEAD)
.getReverseEntries();
@ -785,8 +793,10 @@ public class RebaseCommandTest extends RepositoryTestCase {
// our old branch should be checked out again
assertEquals("refs/heads/file3", db.getFullBranch());
assertEquals(addFile2, new RevWalk(db).parseCommit(
try (RevWalk rw = new RevWalk(db)) {
assertEquals(addFile2, rw.parseCommit(
db.resolve(Constants.HEAD)).getParent(0));
}
checkoutBranch("refs/heads/file2");
assertTrue(new File(db.getWorkTree(), FILE1).exists());
@ -846,9 +856,10 @@ public class RebaseCommandTest extends RepositoryTestCase {
assertEquals(res.getStatus(), Status.ABORTED);
assertEquals("refs/heads/topic", db.getFullBranch());
checkFile(FILE1, "1topic", "2", "3", "topic4");
RevWalk rw = new RevWalk(db);
assertEquals(lastTopicCommit, rw
.parseCommit(db.resolve(Constants.HEAD)));
try (RevWalk rw = new RevWalk(db)) {
assertEquals(lastTopicCommit,
rw.parseCommit(db.resolve(Constants.HEAD)));
}
assertEquals(RepositoryState.SAFE, db.getRepositoryState());
// rebase- dir in .git must be deleted
@ -909,9 +920,10 @@ public class RebaseCommandTest extends RepositoryTestCase {
assertEquals(res.getStatus(), Status.ABORTED);
assertEquals(lastTopicCommit.getName(), db.getFullBranch());
checkFile(FILE1, "1topic", "2", "3", "topic4");
RevWalk rw = new RevWalk(db);
try (RevWalk rw = new RevWalk(db)) {
assertEquals(lastTopicCommit,
rw.parseCommit(db.resolve(Constants.HEAD)));
}
assertEquals(RepositoryState.SAFE, db.getRepositoryState());
// rebase- dir in .git must be deleted
@ -966,12 +978,13 @@ public class RebaseCommandTest extends RepositoryTestCase {
assertEquals(RepositoryState.SAFE, db.getRepositoryState());
ObjectId headId = db.resolve(Constants.HEAD);
RevWalk rw = new RevWalk(db);
try (RevWalk rw = new RevWalk(db)) {
RevCommit rc = rw.parseCommit(headId);
RevCommit parent = rw.parseCommit(rc.getParent(0));
assertEquals("change file1 in topic\n\nThis is conflicting", parent
.getFullMessage());
}
}
@Test
public void testStopOnConflictAndContinueWithNoDeltaToMaster()
@ -1017,10 +1030,11 @@ public class RebaseCommandTest extends RepositoryTestCase {
git.rebase().setOperation(Operation.SKIP).call();
ObjectId headId = db.resolve(Constants.HEAD);
RevWalk rw = new RevWalk(db);
try (RevWalk rw = new RevWalk(db)) {
RevCommit rc = rw.parseCommit(headId);
assertEquals("change file1 in master", rc.getFullMessage());
}
}
@Test
public void testStopOnConflictAndFailContinueIfFileIsDirty()
@ -1308,11 +1322,12 @@ public class RebaseCommandTest extends RepositoryTestCase {
git.rebase().setOperation(Operation.SKIP).call();
ObjectId headId = db.resolve(Constants.HEAD);
RevWalk rw = new RevWalk(db);
try (RevWalk rw = new RevWalk(db)) {
RevCommit rc = rw.parseCommit(headId);
RevCommit parent = rw.parseCommit(rc.getParent(0));
assertEquals("A different commit message", parent.getFullMessage());
}
}
private RevCommit writeFileAndCommit(String fileName, String commitMessage,
String... lines) throws Exception {
@ -1420,9 +1435,10 @@ public class RebaseCommandTest extends RepositoryTestCase {
res = git.rebase().setOperation(Operation.ABORT).call();
assertEquals(res.getStatus(), Status.ABORTED);
assertEquals("refs/heads/topic", db.getFullBranch());
RevWalk rw = new RevWalk(db);
try (RevWalk rw = new RevWalk(db)) {
assertEquals(conflicting, rw.parseCommit(db.resolve(Constants.HEAD)));
assertEquals(RepositoryState.SAFE, db.getRepositoryState());
}
// rebase- dir in .git must be deleted
assertFalse(new File(db.getDirectory(), "rebase-merge").exists());
@ -2286,7 +2302,7 @@ public class RebaseCommandTest extends RepositoryTestCase {
assertEquals(RebaseResult.Status.OK, res2.getStatus());
ObjectId headId = db.resolve(Constants.HEAD);
RevWalk rw = new RevWalk(db);
try (RevWalk rw = new RevWalk(db)) {
RevCommit rc = rw.parseCommit(headId);
ObjectId head1Id = db.resolve(Constants.HEAD + "~1");
@ -2295,6 +2311,7 @@ public class RebaseCommandTest extends RepositoryTestCase {
assertEquals(rc.getFullMessage(), c4.getFullMessage());
assertEquals(rc1.getFullMessage(), c2.getFullMessage());
}
}
@Test
public void testParseRewordCommand() throws Exception {
@ -2643,7 +2660,7 @@ public class RebaseCommandTest extends RepositoryTestCase {
}
}).call();
RevWalk walk = new RevWalk(db);
try (RevWalk walk = new RevWalk(db)) {
ObjectId headId = db.resolve(Constants.HEAD);
RevCommit headCommit = walk.parseCommit(headId);
assertEquals(headCommit.getFullMessage(),
@ -2653,6 +2670,7 @@ public class RebaseCommandTest extends RepositoryTestCase {
RevCommit head1Commit = walk.parseCommit(head2Id);
assertEquals("changed", head1Commit.getFullMessage());
}
}
@Test
public void testRebaseInteractiveMultipleSquash() throws Exception {
@ -2722,7 +2740,7 @@ public class RebaseCommandTest extends RepositoryTestCase {
}
}).call();
RevWalk walk = new RevWalk(db);
try (RevWalk walk = new RevWalk(db)) {
ObjectId headId = db.resolve(Constants.HEAD);
RevCommit headCommit = walk.parseCommit(headId);
assertEquals(headCommit.getFullMessage(),
@ -2734,6 +2752,7 @@ public class RebaseCommandTest extends RepositoryTestCase {
"Add file1\nnew line\nAdd file2\nnew line\nupdated file1 on master\nnew line",
head1Commit.getFullMessage());
}
}
@Test
public void testRebaseInteractiveMixedSquashAndFixup() throws Exception {
@ -2804,7 +2823,7 @@ public class RebaseCommandTest extends RepositoryTestCase {
}
}).call();
RevWalk walk = new RevWalk(db);
try (RevWalk walk = new RevWalk(db)) {
ObjectId headId = db.resolve(Constants.HEAD);
RevCommit headCommit = walk.parseCommit(headId);
assertEquals(headCommit.getFullMessage(),
@ -2814,6 +2833,7 @@ public class RebaseCommandTest extends RepositoryTestCase {
RevCommit head1Commit = walk.parseCommit(head2Id);
assertEquals("changed", head1Commit.getFullMessage());
}
}
@Test
public void testRebaseInteractiveSingleFixup() throws Exception {
@ -2855,7 +2875,7 @@ public class RebaseCommandTest extends RepositoryTestCase {
}
}).call();
RevWalk walk = new RevWalk(db);
try (RevWalk walk = new RevWalk(db)) {
ObjectId headId = db.resolve(Constants.HEAD);
RevCommit headCommit = walk.parseCommit(headId);
assertEquals("update file2 on master\nnew line",
@ -2866,6 +2886,7 @@ public class RebaseCommandTest extends RepositoryTestCase {
assertEquals("Add file2\nnew line",
head1Commit.getFullMessage());
}
}
@Test
public void testRebaseInteractiveFixupWithBlankLines() throws Exception {
@ -2903,12 +2924,13 @@ public class RebaseCommandTest extends RepositoryTestCase {
}
}).call();
RevWalk walk = new RevWalk(db);
try (RevWalk walk = new RevWalk(db)) {
ObjectId headId = db.resolve(Constants.HEAD);
RevCommit headCommit = walk.parseCommit(headId);
assertEquals("Add file2",
headCommit.getFullMessage());
}
}
@Test(expected = InvalidRebaseStepException.class)
public void testRebaseInteractiveFixupFirstCommitShouldFail()

Loading…
Cancel
Save