Browse Source

Merge branch 'stable-4.2'

* stable-4.2:
  FileTreeIteratorTest: Open Git and RevWalk in try-with-resource
  RebaseCommandTest: Open RevWalk in try-with-resource
  PullCommandTest: Open RevWalk in try-with-resource
  BlameGeneratorTest: Create Git instances in try-with-resource
  Remove unnecessary suppression of deprecation warnings in tests
  DiffEntryTest: Open Git and TreeWalk in try-with-resource
  DiffCommandTest: Open Git and RevWalk in try-with-resource
  CommitCommandTest: Open Git and TreeWalk in try-with-resource

Change-Id: Ic886ec9b1a4b3b46f9fa14188b4df832ce36cfa6
Signed-off-by: David Pursehouse <david.pursehouse@sonymobile.com>
stable-4.3
David Pursehouse 9 years ago
parent
commit
27a6a52654
  1. 526
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitCommandTest.java
  2. 283
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/DiffCommandTest.java
  3. 33
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PullCommandTest.java
  4. 378
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RebaseCommandTest.java
  5. 237
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/blame/BlameGeneratorTest.java
  6. 533
      org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/DiffEntryTest.java
  7. 1
      org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/T0003_BasicTest.java
  8. 1
      org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/IndexDiffTest.java
  9. 1
      org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/ObjectWalkTest.java
  10. 399
      org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/FileTreeIteratorTest.java

526
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitCommandTest.java

@ -186,297 +186,302 @@ public class CommitCommandTest extends RepositoryTestCase {
@Test @Test
public void commitNewSubmodule() throws Exception { public void commitNewSubmodule() throws Exception {
Git git = new Git(db); try (Git git = new Git(db)) {
writeTrashFile("file.txt", "content"); writeTrashFile("file.txt", "content");
git.add().addFilepattern("file.txt").call(); git.add().addFilepattern("file.txt").call();
RevCommit commit = git.commit().setMessage("create file").call(); RevCommit commit = git.commit().setMessage("create file").call();
SubmoduleAddCommand command = new SubmoduleAddCommand(db); SubmoduleAddCommand command = new SubmoduleAddCommand(db);
String path = "sub"; String path = "sub";
command.setPath(path); command.setPath(path);
String uri = db.getDirectory().toURI().toString(); String uri = db.getDirectory().toURI().toString();
command.setURI(uri); command.setURI(uri);
Repository repo = command.call(); Repository repo = command.call();
assertNotNull(repo); assertNotNull(repo);
addRepoToClose(repo); addRepoToClose(repo);
SubmoduleWalk generator = SubmoduleWalk.forIndex(db); SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
assertTrue(generator.next()); assertTrue(generator.next());
assertEquals(path, generator.getPath()); assertEquals(path, generator.getPath());
assertEquals(commit, generator.getObjectId()); assertEquals(commit, generator.getObjectId());
assertEquals(uri, generator.getModulesUrl()); assertEquals(uri, generator.getModulesUrl());
assertEquals(path, generator.getModulesPath()); assertEquals(path, generator.getModulesPath());
assertEquals(uri, generator.getConfigUrl()); assertEquals(uri, generator.getConfigUrl());
Repository subModRepo = generator.getRepository(); Repository subModRepo = generator.getRepository();
assertNotNull(subModRepo); assertNotNull(subModRepo);
subModRepo.close(); subModRepo.close();
assertEquals(commit, repo.resolve(Constants.HEAD)); assertEquals(commit, repo.resolve(Constants.HEAD));
RevCommit submoduleCommit = git.commit().setMessage("submodule add") RevCommit submoduleCommit = git.commit().setMessage("submodule add")
.setOnly(path).call(); .setOnly(path).call();
assertNotNull(submoduleCommit); assertNotNull(submoduleCommit);
TreeWalk walk = new TreeWalk(db); try (TreeWalk walk = new TreeWalk(db)) {
walk.addTree(commit.getTree()); walk.addTree(commit.getTree());
walk.addTree(submoduleCommit.getTree()); walk.addTree(submoduleCommit.getTree());
walk.setFilter(TreeFilter.ANY_DIFF); walk.setFilter(TreeFilter.ANY_DIFF);
List<DiffEntry> diffs = DiffEntry.scan(walk); List<DiffEntry> diffs = DiffEntry.scan(walk);
assertEquals(1, diffs.size()); assertEquals(1, diffs.size());
DiffEntry subDiff = diffs.get(0); DiffEntry subDiff = diffs.get(0);
assertEquals(FileMode.MISSING, subDiff.getOldMode()); assertEquals(FileMode.MISSING, subDiff.getOldMode());
assertEquals(FileMode.GITLINK, subDiff.getNewMode()); assertEquals(FileMode.GITLINK, subDiff.getNewMode());
assertEquals(ObjectId.zeroId(), subDiff.getOldId().toObjectId()); assertEquals(ObjectId.zeroId(), subDiff.getOldId().toObjectId());
assertEquals(commit, subDiff.getNewId().toObjectId()); assertEquals(commit, subDiff.getNewId().toObjectId());
assertEquals(path, subDiff.getNewPath()); assertEquals(path, subDiff.getNewPath());
}
}
} }
@Test @Test
public void commitSubmoduleUpdate() throws Exception { public void commitSubmoduleUpdate() throws Exception {
Git git = new Git(db); try (Git git = new Git(db)) {
writeTrashFile("file.txt", "content"); writeTrashFile("file.txt", "content");
git.add().addFilepattern("file.txt").call(); git.add().addFilepattern("file.txt").call();
RevCommit commit = git.commit().setMessage("create file").call(); RevCommit commit = git.commit().setMessage("create file").call();
writeTrashFile("file.txt", "content2"); writeTrashFile("file.txt", "content2");
git.add().addFilepattern("file.txt").call(); git.add().addFilepattern("file.txt").call();
RevCommit commit2 = git.commit().setMessage("edit file").call(); RevCommit commit2 = git.commit().setMessage("edit file").call();
SubmoduleAddCommand command = new SubmoduleAddCommand(db); SubmoduleAddCommand command = new SubmoduleAddCommand(db);
String path = "sub"; String path = "sub";
command.setPath(path); command.setPath(path);
String uri = db.getDirectory().toURI().toString(); String uri = db.getDirectory().toURI().toString();
command.setURI(uri); command.setURI(uri);
Repository repo = command.call(); Repository repo = command.call();
assertNotNull(repo); assertNotNull(repo);
addRepoToClose(repo); addRepoToClose(repo);
SubmoduleWalk generator = SubmoduleWalk.forIndex(db); SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
assertTrue(generator.next()); assertTrue(generator.next());
assertEquals(path, generator.getPath()); assertEquals(path, generator.getPath());
assertEquals(commit2, generator.getObjectId()); assertEquals(commit2, generator.getObjectId());
assertEquals(uri, generator.getModulesUrl()); assertEquals(uri, generator.getModulesUrl());
assertEquals(path, generator.getModulesPath()); assertEquals(path, generator.getModulesPath());
assertEquals(uri, generator.getConfigUrl()); assertEquals(uri, generator.getConfigUrl());
Repository subModRepo = generator.getRepository(); Repository subModRepo = generator.getRepository();
assertNotNull(subModRepo); assertNotNull(subModRepo);
subModRepo.close(); subModRepo.close();
assertEquals(commit2, repo.resolve(Constants.HEAD)); assertEquals(commit2, repo.resolve(Constants.HEAD));
RevCommit submoduleAddCommit = git.commit().setMessage("submodule add") RevCommit submoduleAddCommit = git.commit().setMessage("submodule add")
.setOnly(path).call(); .setOnly(path).call();
assertNotNull(submoduleAddCommit); assertNotNull(submoduleAddCommit);
RefUpdate update = repo.updateRef(Constants.HEAD); RefUpdate update = repo.updateRef(Constants.HEAD);
update.setNewObjectId(commit); update.setNewObjectId(commit);
assertEquals(Result.FORCED, update.forceUpdate()); assertEquals(Result.FORCED, update.forceUpdate());
RevCommit submoduleEditCommit = git.commit() RevCommit submoduleEditCommit = git.commit()
.setMessage("submodule add").setOnly(path).call(); .setMessage("submodule add").setOnly(path).call();
assertNotNull(submoduleEditCommit); assertNotNull(submoduleEditCommit);
TreeWalk walk = new TreeWalk(db); try (TreeWalk walk = new TreeWalk(db)) {
walk.addTree(submoduleAddCommit.getTree()); walk.addTree(submoduleAddCommit.getTree());
walk.addTree(submoduleEditCommit.getTree()); walk.addTree(submoduleEditCommit.getTree());
walk.setFilter(TreeFilter.ANY_DIFF); walk.setFilter(TreeFilter.ANY_DIFF);
List<DiffEntry> diffs = DiffEntry.scan(walk); List<DiffEntry> diffs = DiffEntry.scan(walk);
assertEquals(1, diffs.size()); assertEquals(1, diffs.size());
DiffEntry subDiff = diffs.get(0); DiffEntry subDiff = diffs.get(0);
assertEquals(FileMode.GITLINK, subDiff.getOldMode()); assertEquals(FileMode.GITLINK, subDiff.getOldMode());
assertEquals(FileMode.GITLINK, subDiff.getNewMode()); assertEquals(FileMode.GITLINK, subDiff.getNewMode());
assertEquals(commit2, subDiff.getOldId().toObjectId()); assertEquals(commit2, subDiff.getOldId().toObjectId());
assertEquals(commit, subDiff.getNewId().toObjectId()); assertEquals(commit, subDiff.getNewId().toObjectId());
assertEquals(path, subDiff.getNewPath()); assertEquals(path, subDiff.getNewPath());
assertEquals(path, subDiff.getOldPath()); assertEquals(path, subDiff.getOldPath());
}
}
} }
@Test @Test
public void commitUpdatesSmudgedEntries() throws Exception { public void commitUpdatesSmudgedEntries() throws Exception {
Git git = new Git(db); try (Git git = new Git(db)) {
File file1 = writeTrashFile("file1.txt", "content1");
File file1 = writeTrashFile("file1.txt", "content1"); assertTrue(file1.setLastModified(file1.lastModified() - 5000));
assertTrue(file1.setLastModified(file1.lastModified() - 5000)); File file2 = writeTrashFile("file2.txt", "content2");
File file2 = writeTrashFile("file2.txt", "content2"); assertTrue(file2.setLastModified(file2.lastModified() - 5000));
assertTrue(file2.setLastModified(file2.lastModified() - 5000)); File file3 = writeTrashFile("file3.txt", "content3");
File file3 = writeTrashFile("file3.txt", "content3"); assertTrue(file3.setLastModified(file3.lastModified() - 5000));
assertTrue(file3.setLastModified(file3.lastModified() - 5000));
assertNotNull(git.add().addFilepattern("file1.txt")
assertNotNull(git.add().addFilepattern("file1.txt") .addFilepattern("file2.txt").addFilepattern("file3.txt").call());
.addFilepattern("file2.txt").addFilepattern("file3.txt").call()); RevCommit commit = git.commit().setMessage("add files").call();
RevCommit commit = git.commit().setMessage("add files").call(); assertNotNull(commit);
assertNotNull(commit);
DirCache cache = DirCache.read(db.getIndexFile(), db.getFS());
DirCache cache = DirCache.read(db.getIndexFile(), db.getFS()); int file1Size = cache.getEntry("file1.txt").getLength();
int file1Size = cache.getEntry("file1.txt").getLength(); int file2Size = cache.getEntry("file2.txt").getLength();
int file2Size = cache.getEntry("file2.txt").getLength(); int file3Size = cache.getEntry("file3.txt").getLength();
int file3Size = cache.getEntry("file3.txt").getLength(); ObjectId file2Id = cache.getEntry("file2.txt").getObjectId();
ObjectId file2Id = cache.getEntry("file2.txt").getObjectId(); ObjectId file3Id = cache.getEntry("file3.txt").getObjectId();
ObjectId file3Id = cache.getEntry("file3.txt").getObjectId(); assertTrue(file1Size > 0);
assertTrue(file1Size > 0); assertTrue(file2Size > 0);
assertTrue(file2Size > 0); assertTrue(file3Size > 0);
assertTrue(file3Size > 0);
// Smudge entries
// Smudge entries cache = DirCache.lock(db.getIndexFile(), db.getFS());
cache = DirCache.lock(db.getIndexFile(), db.getFS()); cache.getEntry("file1.txt").setLength(0);
cache.getEntry("file1.txt").setLength(0); cache.getEntry("file2.txt").setLength(0);
cache.getEntry("file2.txt").setLength(0); cache.getEntry("file3.txt").setLength(0);
cache.getEntry("file3.txt").setLength(0); cache.write();
cache.write(); assertTrue(cache.commit());
assertTrue(cache.commit());
// Verify entries smudged
// Verify entries smudged cache = DirCache.read(db.getIndexFile(), db.getFS());
cache = DirCache.read(db.getIndexFile(), db.getFS()); assertEquals(0, cache.getEntry("file1.txt").getLength());
assertEquals(0, cache.getEntry("file1.txt").getLength()); assertEquals(0, cache.getEntry("file2.txt").getLength());
assertEquals(0, cache.getEntry("file2.txt").getLength()); assertEquals(0, cache.getEntry("file3.txt").getLength());
assertEquals(0, cache.getEntry("file3.txt").getLength());
long indexTime = db.getIndexFile().lastModified();
long indexTime = db.getIndexFile().lastModified(); db.getIndexFile().setLastModified(indexTime - 5000);
db.getIndexFile().setLastModified(indexTime - 5000);
write(file1, "content4");
write(file1, "content4"); assertTrue(file1.setLastModified(file1.lastModified() + 2500));
assertTrue(file1.setLastModified(file1.lastModified() + 2500)); assertNotNull(git.commit().setMessage("edit file").setOnly("file1.txt")
assertNotNull(git.commit().setMessage("edit file").setOnly("file1.txt") .call());
.call());
cache = db.readDirCache();
cache = db.readDirCache(); assertEquals(file1Size, cache.getEntry("file1.txt").getLength());
assertEquals(file1Size, cache.getEntry("file1.txt").getLength()); assertEquals(file2Size, cache.getEntry("file2.txt").getLength());
assertEquals(file2Size, cache.getEntry("file2.txt").getLength()); assertEquals(file3Size, cache.getEntry("file3.txt").getLength());
assertEquals(file3Size, cache.getEntry("file3.txt").getLength()); assertEquals(file2Id, cache.getEntry("file2.txt").getObjectId());
assertEquals(file2Id, cache.getEntry("file2.txt").getObjectId()); assertEquals(file3Id, cache.getEntry("file3.txt").getObjectId());
assertEquals(file3Id, cache.getEntry("file3.txt").getObjectId()); }
} }
@Test @Test
public void commitIgnoresSmudgedEntryWithDifferentId() throws Exception { public void commitIgnoresSmudgedEntryWithDifferentId() throws Exception {
Git git = new Git(db); try (Git git = new Git(db)) {
File file1 = writeTrashFile("file1.txt", "content1");
File file1 = writeTrashFile("file1.txt", "content1"); assertTrue(file1.setLastModified(file1.lastModified() - 5000));
assertTrue(file1.setLastModified(file1.lastModified() - 5000)); File file2 = writeTrashFile("file2.txt", "content2");
File file2 = writeTrashFile("file2.txt", "content2"); assertTrue(file2.setLastModified(file2.lastModified() - 5000));
assertTrue(file2.setLastModified(file2.lastModified() - 5000));
assertNotNull(git.add().addFilepattern("file1.txt")
assertNotNull(git.add().addFilepattern("file1.txt") .addFilepattern("file2.txt").call());
.addFilepattern("file2.txt").call()); RevCommit commit = git.commit().setMessage("add files").call();
RevCommit commit = git.commit().setMessage("add files").call(); assertNotNull(commit);
assertNotNull(commit);
DirCache cache = DirCache.read(db.getIndexFile(), db.getFS());
DirCache cache = DirCache.read(db.getIndexFile(), db.getFS()); int file1Size = cache.getEntry("file1.txt").getLength();
int file1Size = cache.getEntry("file1.txt").getLength(); int file2Size = cache.getEntry("file2.txt").getLength();
int file2Size = cache.getEntry("file2.txt").getLength(); assertTrue(file1Size > 0);
assertTrue(file1Size > 0); assertTrue(file2Size > 0);
assertTrue(file2Size > 0);
writeTrashFile("file2.txt", "content3");
writeTrashFile("file2.txt", "content3"); assertNotNull(git.add().addFilepattern("file2.txt").call());
assertNotNull(git.add().addFilepattern("file2.txt").call()); writeTrashFile("file2.txt", "content4");
writeTrashFile("file2.txt", "content4");
// Smudge entries
// Smudge entries cache = DirCache.lock(db.getIndexFile(), db.getFS());
cache = DirCache.lock(db.getIndexFile(), db.getFS()); cache.getEntry("file1.txt").setLength(0);
cache.getEntry("file1.txt").setLength(0); cache.getEntry("file2.txt").setLength(0);
cache.getEntry("file2.txt").setLength(0); cache.write();
cache.write(); assertTrue(cache.commit());
assertTrue(cache.commit());
// Verify entries smudged
// Verify entries smudged cache = db.readDirCache();
cache = db.readDirCache(); assertEquals(0, cache.getEntry("file1.txt").getLength());
assertEquals(0, cache.getEntry("file1.txt").getLength()); assertEquals(0, cache.getEntry("file2.txt").getLength());
assertEquals(0, cache.getEntry("file2.txt").getLength());
long indexTime = db.getIndexFile().lastModified();
long indexTime = db.getIndexFile().lastModified(); db.getIndexFile().setLastModified(indexTime - 5000);
db.getIndexFile().setLastModified(indexTime - 5000);
write(file1, "content5");
write(file1, "content5"); assertTrue(file1.setLastModified(file1.lastModified() + 1000));
assertTrue(file1.setLastModified(file1.lastModified() + 1000));
assertNotNull(git.commit().setMessage("edit file").setOnly("file1.txt")
assertNotNull(git.commit().setMessage("edit file").setOnly("file1.txt") .call());
.call());
cache = db.readDirCache();
cache = db.readDirCache(); assertEquals(file1Size, cache.getEntry("file1.txt").getLength());
assertEquals(file1Size, cache.getEntry("file1.txt").getLength()); assertEquals(0, cache.getEntry("file2.txt").getLength());
assertEquals(0, cache.getEntry("file2.txt").getLength()); }
} }
@Test @Test
public void commitAfterSquashMerge() throws Exception { public void commitAfterSquashMerge() throws Exception {
Git git = new Git(db); try (Git git = new Git(db)) {
writeTrashFile("file1", "file1");
writeTrashFile("file1", "file1"); git.add().addFilepattern("file1").call();
git.add().addFilepattern("file1").call(); RevCommit first = git.commit().setMessage("initial commit").call();
RevCommit first = git.commit().setMessage("initial commit").call();
assertTrue(new File(db.getWorkTree(), "file1").exists()); assertTrue(new File(db.getWorkTree(), "file1").exists());
createBranch(first, "refs/heads/branch1"); createBranch(first, "refs/heads/branch1");
checkoutBranch("refs/heads/branch1"); checkoutBranch("refs/heads/branch1");
writeTrashFile("file2", "file2"); writeTrashFile("file2", "file2");
git.add().addFilepattern("file2").call(); git.add().addFilepattern("file2").call();
git.commit().setMessage("second commit").call(); git.commit().setMessage("second commit").call();
assertTrue(new File(db.getWorkTree(), "file2").exists()); assertTrue(new File(db.getWorkTree(), "file2").exists());
checkoutBranch("refs/heads/master"); checkoutBranch("refs/heads/master");
MergeResult result = git.merge() MergeResult result = git.merge()
.include(db.exactRef("refs/heads/branch1")) .include(db.exactRef("refs/heads/branch1"))
.setSquash(true) .setSquash(true)
.call(); .call();
assertTrue(new File(db.getWorkTree(), "file1").exists()); assertTrue(new File(db.getWorkTree(), "file1").exists());
assertTrue(new File(db.getWorkTree(), "file2").exists()); assertTrue(new File(db.getWorkTree(), "file2").exists());
assertEquals(MergeResult.MergeStatus.FAST_FORWARD_SQUASHED, assertEquals(MergeResult.MergeStatus.FAST_FORWARD_SQUASHED,
result.getMergeStatus()); result.getMergeStatus());
// comment not set, should be inferred from SQUASH_MSG // comment not set, should be inferred from SQUASH_MSG
RevCommit squashedCommit = git.commit().call(); RevCommit squashedCommit = git.commit().call();
assertEquals(1, squashedCommit.getParentCount()); assertEquals(1, squashedCommit.getParentCount());
assertNull(db.readSquashCommitMsg()); assertNull(db.readSquashCommitMsg());
assertEquals("commit: Squashed commit of the following:", db assertEquals("commit: Squashed commit of the following:", db
.getReflogReader(Constants.HEAD).getLastEntry().getComment()); .getReflogReader(Constants.HEAD).getLastEntry().getComment());
assertEquals("commit: Squashed commit of the following:", db assertEquals("commit: Squashed commit of the following:", db
.getReflogReader(db.getBranch()).getLastEntry().getComment()); .getReflogReader(db.getBranch()).getLastEntry().getComment());
}
} }
@Test(expected = WrongRepositoryStateException.class) @Test(expected = WrongRepositoryStateException.class)
public void commitAmendOnInitialShouldFail() throws Exception { public void commitAmendOnInitialShouldFail() throws Exception {
Git git = new Git(db); try (Git git = new Git(db)) {
git.commit().setAmend(true).setMessage("initial commit").call(); git.commit().setAmend(true).setMessage("initial commit").call();
}
} }
@Test @Test
public void commitAmendWithoutAuthorShouldSetOriginalAuthorAndAuthorTime() public void commitAmendWithoutAuthorShouldSetOriginalAuthorAndAuthorTime()
throws Exception { throws Exception {
Git git = new Git(db); try (Git git = new Git(db)) {
writeTrashFile("file1", "file1");
writeTrashFile("file1", "file1"); git.add().addFilepattern("file1").call();
git.add().addFilepattern("file1").call();
final String authorName = "First Author"; final String authorName = "First Author";
final String authorEmail = "author@example.org"; final String authorEmail = "author@example.org";
final Date authorDate = new Date(1349621117000L); final Date authorDate = new Date(1349621117000L);
PersonIdent firstAuthor = new PersonIdent(authorName, authorEmail, PersonIdent firstAuthor = new PersonIdent(authorName, authorEmail,
authorDate, TimeZone.getTimeZone("UTC")); authorDate, TimeZone.getTimeZone("UTC"));
git.commit().setMessage("initial commit").setAuthor(firstAuthor).call(); git.commit().setMessage("initial commit").setAuthor(firstAuthor).call();
RevCommit amended = git.commit().setAmend(true) RevCommit amended = git.commit().setAmend(true)
.setMessage("amend commit").call(); .setMessage("amend commit").call();
PersonIdent amendedAuthor = amended.getAuthorIdent(); PersonIdent amendedAuthor = amended.getAuthorIdent();
assertEquals(authorName, amendedAuthor.getName()); assertEquals(authorName, amendedAuthor.getName());
assertEquals(authorEmail, amendedAuthor.getEmailAddress()); assertEquals(authorEmail, amendedAuthor.getEmailAddress());
assertEquals(authorDate.getTime(), amendedAuthor.getWhen().getTime()); assertEquals(authorDate.getTime(), amendedAuthor.getWhen().getTime());
}
} }
@Test @Test
public void commitAmendWithAuthorShouldUseIt() throws Exception { public void commitAmendWithAuthorShouldUseIt() throws Exception {
Git git = new Git(db); try (Git git = new Git(db)) {
writeTrashFile("file1", "file1");
writeTrashFile("file1", "file1"); git.add().addFilepattern("file1").call();
git.add().addFilepattern("file1").call(); git.commit().setMessage("initial commit").call();
git.commit().setMessage("initial commit").call();
RevCommit amended = git.commit().setAmend(true) RevCommit amended = git.commit().setAmend(true)
.setAuthor("New Author", "newauthor@example.org") .setAuthor("New Author", "newauthor@example.org")
.setMessage("amend commit").call(); .setMessage("amend commit").call();
PersonIdent amendedAuthor = amended.getAuthorIdent(); PersonIdent amendedAuthor = amended.getAuthorIdent();
assertEquals("New Author", amendedAuthor.getName()); assertEquals("New Author", amendedAuthor.getName());
assertEquals("newauthor@example.org", amendedAuthor.getEmailAddress()); assertEquals("newauthor@example.org", amendedAuthor.getEmailAddress());
}
} }
@Test @Test
@ -532,18 +537,19 @@ public class CommitCommandTest extends RepositoryTestCase {
+ "[unmerged2, mode:100644, stage:3]", + "[unmerged2, mode:100644, stage:3]",
indexState(0)); indexState(0));
Git git = new Git(db); try (Git git = new Git(db)) {
RevCommit commit = git.commit().setOnly("unmerged1") RevCommit commit = git.commit().setOnly("unmerged1")
.setMessage("Only one file").call(); .setMessage("Only one file").call();
assertEquals("[other, mode:100644]" + "[unmerged1, mode:100644]" assertEquals("[other, mode:100644]" + "[unmerged1, mode:100644]"
+ "[unmerged2, mode:100644, stage:1]" + "[unmerged2, mode:100644, stage:1]"
+ "[unmerged2, mode:100644, stage:2]" + "[unmerged2, mode:100644, stage:2]"
+ "[unmerged2, mode:100644, stage:3]", + "[unmerged2, mode:100644, stage:3]",
indexState(0)); indexState(0));
try (TreeWalk walk = TreeWalk.forPath(db, "unmerged1", commit.getTree())) { try (TreeWalk walk = TreeWalk.forPath(db, "unmerged1", commit.getTree())) {
assertEquals(FileMode.REGULAR_FILE, walk.getFileMode(0)); assertEquals(FileMode.REGULAR_FILE, walk.getFileMode(0));
}
} }
} }

283
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/DiffCommandTest.java

@ -70,32 +70,33 @@ public class DiffCommandTest extends RepositoryTestCase {
File folder = new File(db.getWorkTree(), "folder"); File folder = new File(db.getWorkTree(), "folder");
folder.mkdir(); folder.mkdir();
write(new File(folder, "folder.txt"), "folder"); write(new File(folder, "folder.txt"), "folder");
Git git = new Git(db); try (Git git = new Git(db)) {
git.add().addFilepattern(".").call(); git.add().addFilepattern(".").call();
git.commit().setMessage("Initial commit").call(); git.commit().setMessage("Initial commit").call();
write(new File(folder, "folder.txt"), "folder change"); write(new File(folder, "folder.txt"), "folder change");
OutputStream out = new ByteArrayOutputStream(); OutputStream out = new ByteArrayOutputStream();
List<DiffEntry> entries = git.diff().setOutputStream(out).call(); List<DiffEntry> entries = git.diff().setOutputStream(out).call();
assertEquals(1, entries.size()); assertEquals(1, entries.size());
assertEquals(ChangeType.MODIFY, entries.get(0) assertEquals(ChangeType.MODIFY, entries.get(0)
.getChangeType()); .getChangeType());
assertEquals("folder/folder.txt", entries.get(0) assertEquals("folder/folder.txt", entries.get(0)
.getOldPath()); .getOldPath());
assertEquals("folder/folder.txt", entries.get(0) assertEquals("folder/folder.txt", entries.get(0)
.getNewPath()); .getNewPath());
String actual = out.toString(); String actual = out.toString();
String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n" String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n"
+ "index 0119635..95c4c65 100644\n" + "index 0119635..95c4c65 100644\n"
+ "--- a/folder/folder.txt\n" + "--- a/folder/folder.txt\n"
+ "+++ b/folder/folder.txt\n" + "+++ b/folder/folder.txt\n"
+ "@@ -1 +1 @@\n" + "@@ -1 +1 @@\n"
+ "-folder\n" + "-folder\n"
+ "\\ No newline at end of file\n" + "\\ No newline at end of file\n"
+ "+folder change\n" + "+folder change\n"
+ "\\ No newline at end of file\n"; + "\\ No newline at end of file\n";
assertEquals(expected.toString(), actual); assertEquals(expected.toString(), actual);
}
} }
@Test @Test
@ -103,33 +104,34 @@ public class DiffCommandTest extends RepositoryTestCase {
write(new File(db.getWorkTree(), "test.txt"), "test"); write(new File(db.getWorkTree(), "test.txt"), "test");
File folder = new File(db.getWorkTree(), "folder"); File folder = new File(db.getWorkTree(), "folder");
folder.mkdir(); folder.mkdir();
Git git = new Git(db); try (Git git = new Git(db)) {
git.add().addFilepattern(".").call(); git.add().addFilepattern(".").call();
git.commit().setMessage("Initial commit").call(); git.commit().setMessage("Initial commit").call();
write(new File(folder, "folder.txt"), "folder"); write(new File(folder, "folder.txt"), "folder");
git.add().addFilepattern(".").call(); git.add().addFilepattern(".").call();
OutputStream out = new ByteArrayOutputStream(); OutputStream out = new ByteArrayOutputStream();
List<DiffEntry> entries = git.diff().setOutputStream(out) List<DiffEntry> entries = git.diff().setOutputStream(out)
.setCached(true).call(); .setCached(true).call();
assertEquals(1, entries.size()); assertEquals(1, entries.size());
assertEquals(ChangeType.ADD, entries.get(0) assertEquals(ChangeType.ADD, entries.get(0)
.getChangeType()); .getChangeType());
assertEquals("/dev/null", entries.get(0) assertEquals("/dev/null", entries.get(0)
.getOldPath()); .getOldPath());
assertEquals("folder/folder.txt", entries.get(0) assertEquals("folder/folder.txt", entries.get(0)
.getNewPath()); .getNewPath());
String actual = out.toString(); String actual = out.toString();
String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n" String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n"
+ "new file mode 100644\n" + "new file mode 100644\n"
+ "index 0000000..0119635\n" + "index 0000000..0119635\n"
+ "--- /dev/null\n" + "--- /dev/null\n"
+ "+++ b/folder/folder.txt\n" + "+++ b/folder/folder.txt\n"
+ "@@ -0,0 +1 @@\n" + "@@ -0,0 +1 @@\n"
+ "+folder\n" + "+folder\n"
+ "\\ No newline at end of file\n"; + "\\ No newline at end of file\n";
assertEquals(expected.toString(), actual); assertEquals(expected.toString(), actual);
}
} }
@Test @Test
@ -138,107 +140,109 @@ public class DiffCommandTest extends RepositoryTestCase {
File folder = new File(db.getWorkTree(), "folder"); File folder = new File(db.getWorkTree(), "folder");
folder.mkdir(); folder.mkdir();
write(new File(folder, "folder.txt"), "folder"); write(new File(folder, "folder.txt"), "folder");
Git git = new Git(db); try (Git git = new Git(db)) {
git.add().addFilepattern(".").call(); git.add().addFilepattern(".").call();
git.commit().setMessage("Initial commit").call(); git.commit().setMessage("Initial commit").call();
write(new File(folder, "folder.txt"), "folder change"); write(new File(folder, "folder.txt"), "folder change");
git.add().addFilepattern(".").call(); git.add().addFilepattern(".").call();
git.commit().setMessage("second commit").call(); git.commit().setMessage("second commit").call();
write(new File(folder, "folder.txt"), "second folder change"); write(new File(folder, "folder.txt"), "second folder change");
git.add().addFilepattern(".").call(); git.add().addFilepattern(".").call();
git.commit().setMessage("third commit").call(); git.commit().setMessage("third commit").call();
// bad filter // bad filter
DiffCommand diff = git.diff().setShowNameAndStatusOnly(true) DiffCommand diff = git.diff().setShowNameAndStatusOnly(true)
.setPathFilter(PathFilter.create("test.txt")) .setPathFilter(PathFilter.create("test.txt"))
.setOldTree(getTreeIterator("HEAD^^")) .setOldTree(getTreeIterator("HEAD^^"))
.setNewTree(getTreeIterator("HEAD^")); .setNewTree(getTreeIterator("HEAD^"));
List<DiffEntry> entries = diff.call(); List<DiffEntry> entries = diff.call();
assertEquals(0, entries.size()); assertEquals(0, entries.size());
// no filter, two commits // no filter, two commits
OutputStream out = new ByteArrayOutputStream(); OutputStream out = new ByteArrayOutputStream();
diff = git.diff().setOutputStream(out) diff = git.diff().setOutputStream(out)
.setOldTree(getTreeIterator("HEAD^^")) .setOldTree(getTreeIterator("HEAD^^"))
.setNewTree(getTreeIterator("HEAD^")); .setNewTree(getTreeIterator("HEAD^"));
entries = diff.call(); entries = diff.call();
assertEquals(1, entries.size()); assertEquals(1, entries.size());
assertEquals(ChangeType.MODIFY, entries.get(0).getChangeType()); assertEquals(ChangeType.MODIFY, entries.get(0).getChangeType());
assertEquals("folder/folder.txt", entries.get(0).getOldPath()); assertEquals("folder/folder.txt", entries.get(0).getOldPath());
assertEquals("folder/folder.txt", entries.get(0).getNewPath()); assertEquals("folder/folder.txt", entries.get(0).getNewPath());
String actual = out.toString(); String actual = out.toString();
String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n" String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n"
+ "index 0119635..95c4c65 100644\n" + "index 0119635..95c4c65 100644\n"
+ "--- a/folder/folder.txt\n" + "--- a/folder/folder.txt\n"
+ "+++ b/folder/folder.txt\n" + "+++ b/folder/folder.txt\n"
+ "@@ -1 +1 @@\n" + "@@ -1 +1 @@\n"
+ "-folder\n" + "-folder\n"
+ "\\ No newline at end of file\n" + "\\ No newline at end of file\n"
+ "+folder change\n" + "+folder change\n"
+ "\\ No newline at end of file\n"; + "\\ No newline at end of file\n";
assertEquals(expected.toString(), actual); assertEquals(expected.toString(), actual);
}
} }
@Test @Test
public void testDiffWithPrefixes() throws Exception { public void testDiffWithPrefixes() throws Exception {
write(new File(db.getWorkTree(), "test.txt"), "test"); write(new File(db.getWorkTree(), "test.txt"), "test");
Git git = new Git(db); try (Git git = new Git(db)) {
git.add().addFilepattern(".").call(); git.add().addFilepattern(".").call();
git.commit().setMessage("Initial commit").call(); git.commit().setMessage("Initial commit").call();
write(new File(db.getWorkTree(), "test.txt"), "test change"); write(new File(db.getWorkTree(), "test.txt"), "test change");
OutputStream out = new ByteArrayOutputStream(); OutputStream out = new ByteArrayOutputStream();
git.diff().setOutputStream(out).setSourcePrefix("old/") git.diff().setOutputStream(out).setSourcePrefix("old/")
.setDestinationPrefix("new/") .setDestinationPrefix("new/").call();
.call();
String actual = out.toString();
String actual = out.toString(); String expected = "diff --git old/test.txt new/test.txt\n"
String expected = "diff --git old/test.txt new/test.txt\n" + "index 30d74d2..4dba797 100644\n" + "--- old/test.txt\n"
+ "index 30d74d2..4dba797 100644\n" + "--- old/test.txt\n" + "+++ new/test.txt\n" + "@@ -1 +1 @@\n" + "-test\n"
+ "+++ new/test.txt\n" + "@@ -1 +1 @@\n" + "-test\n" + "\\ No newline at end of file\n" + "+test change\n"
+ "\\ No newline at end of file\n" + "+test change\n" + "\\ No newline at end of file\n";
+ "\\ No newline at end of file\n"; assertEquals(expected.toString(), actual);
assertEquals(expected.toString(), actual); }
} }
@Test @Test
public void testDiffWithNegativeLineCount() throws Exception { public void testDiffWithNegativeLineCount() throws Exception {
write(new File(db.getWorkTree(), "test.txt"), write(new File(db.getWorkTree(), "test.txt"),
"0\n1\n2\n3\n4\n5\n6\n7\n8\n9"); "0\n1\n2\n3\n4\n5\n6\n7\n8\n9");
Git git = new Git(db); try (Git git = new Git(db)) {
git.add().addFilepattern(".").call(); git.add().addFilepattern(".").call();
git.commit().setMessage("Initial commit").call(); git.commit().setMessage("Initial commit").call();
write(new File(db.getWorkTree(), "test.txt"), write(new File(db.getWorkTree(), "test.txt"),
"0\n1\n2\n3\n4a\n5\n6\n7\n8\n9"); "0\n1\n2\n3\n4a\n5\n6\n7\n8\n9");
OutputStream out = new ByteArrayOutputStream(); OutputStream out = new ByteArrayOutputStream();
git.diff().setOutputStream(out).setContextLines(1) git.diff().setOutputStream(out).setContextLines(1).call();
.call();
String actual = out.toString();
String actual = out.toString(); String expected = "diff --git a/test.txt b/test.txt\n"
String expected = "diff --git a/test.txt b/test.txt\n" + "index f55b5c9..c5ec8fd 100644\n" + "--- a/test.txt\n"
+ "index f55b5c9..c5ec8fd 100644\n" + "--- a/test.txt\n" + "+++ b/test.txt\n" + "@@ -4,3 +4,3 @@\n" + " 3\n" + "-4\n"
+ "+++ b/test.txt\n" + "@@ -4,3 +4,3 @@\n" + " 3\n" + "-4\n" + "+4a\n" + " 5\n";
+ "+4a\n" + " 5\n"; assertEquals(expected.toString(), actual);
assertEquals(expected.toString(), actual); }
} }
@Test @Test
public void testNoOutputStreamSet() throws Exception { public void testNoOutputStreamSet() throws Exception {
File file = writeTrashFile("test.txt", "a"); File file = writeTrashFile("test.txt", "a");
assertTrue(file.setLastModified(file.lastModified() - 5000)); assertTrue(file.setLastModified(file.lastModified() - 5000));
Git git = new Git(db); try (Git git = new Git(db)) {
git.add().addFilepattern(".").call(); git.add().addFilepattern(".").call();
write(file, "b"); write(file, "b");
List<DiffEntry> diffs = git.diff().call(); List<DiffEntry> diffs = git.diff().call();
assertNotNull(diffs); assertNotNull(diffs);
assertEquals(1, diffs.size()); assertEquals(1, diffs.size());
DiffEntry diff = diffs.get(0); DiffEntry diff = diffs.get(0);
assertEquals(ChangeType.MODIFY, diff.getChangeType()); assertEquals(ChangeType.MODIFY, diff.getChangeType());
assertEquals("test.txt", diff.getOldPath()); assertEquals("test.txt", diff.getOldPath());
assertEquals("test.txt", diff.getNewPath()); assertEquals("test.txt", diff.getNewPath());
}
} }
private AbstractTreeIterator getTreeIterator(String name) private AbstractTreeIterator getTreeIterator(String name)
@ -247,8 +251,9 @@ public class DiffCommandTest extends RepositoryTestCase {
if (id == null) if (id == null)
throw new IllegalArgumentException(name); throw new IllegalArgumentException(name);
final CanonicalTreeParser p = new CanonicalTreeParser(); final CanonicalTreeParser p = new CanonicalTreeParser();
try (ObjectReader or = db.newObjectReader()) { try (ObjectReader or = db.newObjectReader();
p.reset(or, new RevWalk(db).parseTree(id)); RevWalk rw = new RevWalk(db)) {
p.reset(or, rw.parseTree(id));
return p; return p;
} }
} }

33
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PullCommandTest.java

@ -140,11 +140,12 @@ public class PullCommandTest extends RepositoryTestCase {
ObjectId[] mergedCommits = mergeResult.getMergedCommits(); ObjectId[] mergedCommits = mergeResult.getMergedCommits();
assertEquals(targetCommit.getId(), mergedCommits[0]); assertEquals(targetCommit.getId(), mergedCommits[0]);
assertEquals(sourceCommit.getId(), mergedCommits[1]); assertEquals(sourceCommit.getId(), mergedCommits[1]);
RevCommit mergeCommit = new RevWalk(dbTarget).parseCommit(mergeResult try (RevWalk rw = new RevWalk(dbTarget)) {
.getNewHead()); RevCommit mergeCommit = rw.parseCommit(mergeResult.getNewHead());
String message = "Merge branch 'master' of " String message = "Merge branch 'master' of "
+ db.getWorkTree().getAbsolutePath(); + db.getWorkTree().getAbsolutePath();
assertEquals(message, mergeCommit.getShortMessage()); assertEquals(message, mergeCommit.getShortMessage());
}
} }
@Test @Test
@ -259,11 +260,12 @@ public class PullCommandTest extends RepositoryTestCase {
ObjectId[] mergedCommits = mergeResult.getMergedCommits(); ObjectId[] mergedCommits = mergeResult.getMergedCommits();
assertEquals(targetCommit.getId(), mergedCommits[0]); assertEquals(targetCommit.getId(), mergedCommits[0]);
assertEquals(sourceCommit.getId(), mergedCommits[1]); assertEquals(sourceCommit.getId(), mergedCommits[1]);
RevCommit mergeCommit = new RevWalk(dbTarget).parseCommit(mergeResult try (RevWalk rw = new RevWalk(dbTarget)) {
.getNewHead()); RevCommit mergeCommit = rw.parseCommit(mergeResult.getNewHead());
String message = "Merge branch 'other' of " String message = "Merge branch 'other' of "
+ db.getWorkTree().getAbsolutePath(); + db.getWorkTree().getAbsolutePath();
assertEquals(message, mergeCommit.getShortMessage()); assertEquals(message, mergeCommit.getShortMessage());
}
} }
@Test @Test
@ -293,11 +295,12 @@ public class PullCommandTest extends RepositoryTestCase {
ObjectId[] mergedCommits = mergeResult.getMergedCommits(); ObjectId[] mergedCommits = mergeResult.getMergedCommits();
assertEquals(targetCommit.getId(), mergedCommits[0]); assertEquals(targetCommit.getId(), mergedCommits[0]);
assertEquals(sourceCommit.getId(), mergedCommits[1]); assertEquals(sourceCommit.getId(), mergedCommits[1]);
RevCommit mergeCommit = new RevWalk(dbTarget).parseCommit(mergeResult try (RevWalk rw = new RevWalk(dbTarget)) {
.getNewHead()); RevCommit mergeCommit = rw.parseCommit(mergeResult.getNewHead());
String message = "Merge branch 'other' of " String message = "Merge branch 'other' of "
+ db.getWorkTree().getAbsolutePath() + " into other"; + db.getWorkTree().getAbsolutePath() + " into other";
assertEquals(message, mergeCommit.getShortMessage()); assertEquals(message, mergeCommit.getShortMessage());
}
} }
private enum TestPullMode { private enum TestPullMode {

378
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(); RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
assertEquals(Status.OK, res.getStatus()); 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"))); rw.markStart(rw.parseCommit(db.resolve("refs/heads/topic")));
assertDerivedFrom(rw.next(), e); assertDerivedFrom(rw.next(), e);
assertDerivedFrom(rw.next(), d); assertDerivedFrom(rw.next(), d);
assertDerivedFrom(rw.next(), c); assertDerivedFrom(rw.next(), c);
assertEquals(b, rw.next()); assertEquals(b, rw.next());
assertEquals(a, rw.next()); assertEquals(a, rw.next());
}
List<ReflogEntry> headLog = db.getReflogReader(Constants.HEAD) List<ReflogEntry> headLog = db.getReflogReader(Constants.HEAD)
.getReverseEntries(); .getReverseEntries();
@ -354,8 +355,6 @@ public class RebaseCommandTest extends RepositoryTestCase {
*/ */
private void doTestRebasePreservingMerges(boolean testConflict) private void doTestRebasePreservingMerges(boolean testConflict)
throws Exception { throws Exception {
RevWalk rw = new RevWalk(db);
// create file1 on master // create file1 on master
writeTrashFile(FILE1, FILE1); writeTrashFile(FILE1, FILE1);
git.add().addFilepattern(FILE1).call(); git.add().addFilepattern(FILE1).call();
@ -409,7 +408,9 @@ public class RebaseCommandTest extends RepositoryTestCase {
f = git.commit().setMessage("commit f").call(); f = git.commit().setMessage("commit f").call();
} else { } else {
assertEquals(MergeStatus.MERGED, result.getMergeStatus()); assertEquals(MergeStatus.MERGED, result.getMergeStatus());
f = rw.parseCommit(result.getNewHead()); try (RevWalk rw = new RevWalk(db)) {
f = rw.parseCommit(result.getNewHead());
}
} }
RebaseResult res = git.rebase().setUpstream("refs/heads/master") RebaseResult res = git.rebase().setUpstream("refs/heads/master")
@ -453,23 +454,25 @@ public class RebaseCommandTest extends RepositoryTestCase {
assertEquals("file2", read("file2")); assertEquals("file2", read("file2"));
assertEquals("more change", read("file3")); assertEquals("more change", read("file3"));
rw.markStart(rw.parseCommit(db.resolve("refs/heads/topic"))); try (RevWalk rw = new RevWalk(db)) {
RevCommit newF = rw.next(); rw.markStart(rw.parseCommit(db.resolve("refs/heads/topic")));
assertDerivedFrom(newF, f); RevCommit newF = rw.next();
assertEquals(2, newF.getParentCount()); assertDerivedFrom(newF, f);
RevCommit newD = rw.next(); assertEquals(2, newF.getParentCount());
assertDerivedFrom(newD, d); RevCommit newD = rw.next();
if (testConflict) assertDerivedFrom(newD, d);
assertEquals("d new", readFile("conflict", newD)); if (testConflict)
RevCommit newE = rw.next(); assertEquals("d new", readFile("conflict", newD));
assertDerivedFrom(newE, e); RevCommit newE = rw.next();
if (testConflict) assertDerivedFrom(newE, e);
assertEquals("e new", readFile("conflict", newE)); if (testConflict)
assertEquals(newD, newF.getParent(0)); assertEquals("e new", readFile("conflict", newE));
assertEquals(newE, newF.getParent(1)); assertEquals(newD, newF.getParent(0));
assertDerivedFrom(rw.next(), c); assertEquals(newE, newF.getParent(1));
assertEquals(b, rw.next()); assertDerivedFrom(rw.next(), c);
assertEquals(a, rw.next()); assertEquals(b, rw.next());
assertEquals(a, rw.next());
}
} }
private String readFile(String path, RevCommit commit) throws IOException { private String readFile(String path, RevCommit commit) throws IOException {
@ -517,88 +520,89 @@ public class RebaseCommandTest extends RepositoryTestCase {
*/ */
private void doTestRebasePreservingMergesWithUnrelatedSide( private void doTestRebasePreservingMergesWithUnrelatedSide(
boolean testConflict) throws Exception { boolean testConflict) throws Exception {
RevWalk rw = new RevWalk(db); try (RevWalk rw = new RevWalk(db)) {
rw.sort(RevSort.TOPO); rw.sort(RevSort.TOPO);
writeTrashFile(FILE1, FILE1); writeTrashFile(FILE1, FILE1);
git.add().addFilepattern(FILE1).call(); git.add().addFilepattern(FILE1).call();
RevCommit a = git.commit().setMessage("commit a").call(); RevCommit a = git.commit().setMessage("commit a").call();
writeTrashFile("file2", "blah"); writeTrashFile("file2", "blah");
git.add().addFilepattern("file2").call(); git.add().addFilepattern("file2").call();
RevCommit b = git.commit().setMessage("commit b").call(); RevCommit b = git.commit().setMessage("commit b").call();
// create a topic branch
createBranch(b, "refs/heads/topic");
checkoutBranch("refs/heads/topic");
writeTrashFile("file3", "more changess");
writeTrashFile(FILE1, "preparing conflict");
git.add().addFilepattern("file3").addFilepattern(FILE1).call();
RevCommit c = git.commit().setMessage("commit c").call();
createBranch(a, "refs/heads/side");
checkoutBranch("refs/heads/side");
writeTrashFile("conflict", "e");
writeTrashFile(FILE1, FILE1 + "\n" + "line 2");
git.add().addFilepattern(".").call();
RevCommit e = git.commit().setMessage("commit e").call();
// switch back to topic and merge in side, creating d // create a topic branch
checkoutBranch("refs/heads/topic"); createBranch(b, "refs/heads/topic");
MergeResult result = git.merge().include(e) checkoutBranch("refs/heads/topic");
.setStrategy(MergeStrategy.RESOLVE).call();
assertEquals(MergeStatus.CONFLICTING, result.getMergeStatus()); writeTrashFile("file3", "more changess");
assertEquals(result.getConflicts().keySet(), writeTrashFile(FILE1, "preparing conflict");
Collections.singleton(FILE1)); git.add().addFilepattern("file3").addFilepattern(FILE1).call();
writeTrashFile(FILE1, "merge resolution"); RevCommit c = git.commit().setMessage("commit c").call();
git.add().addFilepattern(FILE1).call();
RevCommit d = git.commit().setMessage("commit d").call();
RevCommit f = commitFile("file2", "new content two", "topic"); createBranch(a, "refs/heads/side");
checkoutBranch("refs/heads/side");
writeTrashFile("conflict", "e");
writeTrashFile(FILE1, FILE1 + "\n" + "line 2");
git.add().addFilepattern(".").call();
RevCommit e = git.commit().setMessage("commit e").call();
checkoutBranch("refs/heads/master"); // switch back to topic and merge in side, creating d
writeTrashFile("fileg", "fileg"); checkoutBranch("refs/heads/topic");
if (testConflict) MergeResult result = git.merge().include(e)
writeTrashFile("conflict", "g"); .setStrategy(MergeStrategy.RESOLVE).call();
git.add().addFilepattern(".").call();
RevCommit g = git.commit().setMessage("commit g").call();
checkoutBranch("refs/heads/topic"); assertEquals(MergeStatus.CONFLICTING, result.getMergeStatus());
RebaseResult res = git.rebase().setUpstream("refs/heads/master") assertEquals(result.getConflicts().keySet(),
.setPreserveMerges(true).call(); Collections.singleton(FILE1));
if (testConflict) { writeTrashFile(FILE1, "merge resolution");
assertEquals(Status.STOPPED, res.getStatus()); git.add().addFilepattern(FILE1).call();
assertEquals(Collections.singleton("conflict"), git.status().call() RevCommit d = git.commit().setMessage("commit d").call();
.getConflicting());
// resolve RevCommit f = commitFile("file2", "new content two", "topic");
writeTrashFile("conflict", "e");
git.add().addFilepattern("conflict").call(); checkoutBranch("refs/heads/master");
res = git.rebase().setOperation(Operation.CONTINUE).call(); writeTrashFile("fileg", "fileg");
if (testConflict)
writeTrashFile("conflict", "g");
git.add().addFilepattern(".").call();
RevCommit g = git.commit().setMessage("commit g").call();
checkoutBranch("refs/heads/topic");
RebaseResult res = git.rebase().setUpstream("refs/heads/master")
.setPreserveMerges(true).call();
if (testConflict) {
assertEquals(Status.STOPPED, res.getStatus());
assertEquals(Collections.singleton("conflict"), git.status().call()
.getConflicting());
// resolve
writeTrashFile("conflict", "e");
git.add().addFilepattern("conflict").call();
res = git.rebase().setOperation(Operation.CONTINUE).call();
}
assertEquals(Status.OK, res.getStatus());
assertEquals("merge resolution", read(FILE1));
assertEquals("new content two", read("file2"));
assertEquals("more changess", read("file3"));
assertEquals("fileg", read("fileg"));
rw.markStart(rw.parseCommit(db.resolve("refs/heads/topic")));
RevCommit newF = rw.next();
assertDerivedFrom(newF, f);
RevCommit newD = rw.next();
assertDerivedFrom(newD, d);
assertEquals(2, newD.getParentCount());
RevCommit newC = rw.next();
assertDerivedFrom(newC, c);
RevCommit newE = rw.next();
assertEquals(e, newE);
assertEquals(newC, newD.getParent(0));
assertEquals(e, newD.getParent(1));
assertEquals(g, rw.next());
assertEquals(b, rw.next());
assertEquals(a, rw.next());
} }
assertEquals(Status.OK, res.getStatus());
assertEquals("merge resolution", read(FILE1));
assertEquals("new content two", read("file2"));
assertEquals("more changess", read("file3"));
assertEquals("fileg", read("fileg"));
rw.markStart(rw.parseCommit(db.resolve("refs/heads/topic")));
RevCommit newF = rw.next();
assertDerivedFrom(newF, f);
RevCommit newD = rw.next();
assertDerivedFrom(newD, d);
assertEquals(2, newD.getParentCount());
RevCommit newC = rw.next();
assertDerivedFrom(newC, c);
RevCommit newE = rw.next();
assertEquals(e, newE);
assertEquals(newC, newD.getParent(0));
assertEquals(e, newD.getParent(1));
assertEquals(g, rw.next());
assertEquals(b, rw.next());
assertEquals(a, rw.next());
} }
@Test @Test
@ -687,8 +691,10 @@ public class RebaseCommandTest extends RepositoryTestCase {
checkFile(theFile, "1master\n2\n3\ntopic\n"); checkFile(theFile, "1master\n2\n3\ntopic\n");
// our old branch should be checked out again // our old branch should be checked out again
assertEquals("refs/heads/topic", db.getFullBranch()); assertEquals("refs/heads/topic", db.getFullBranch());
assertEquals(lastMasterChange, new RevWalk(db).parseCommit( try (RevWalk rw = new RevWalk(db)) {
db.resolve(Constants.HEAD)).getParent(0)); assertEquals(lastMasterChange, rw.parseCommit(
db.resolve(Constants.HEAD)).getParent(0));
}
assertEquals(origHead, db.readOrigHead()); assertEquals(origHead, db.readOrigHead());
List<ReflogEntry> headLog = db.getReflogReader(Constants.HEAD) List<ReflogEntry> headLog = db.getReflogReader(Constants.HEAD)
.getReverseEntries(); .getReverseEntries();
@ -737,8 +743,10 @@ public class RebaseCommandTest extends RepositoryTestCase {
RebaseResult res = git.rebase().setUpstream("refs/heads/master").call(); RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
assertEquals(Status.OK, res.getStatus()); assertEquals(Status.OK, res.getStatus());
checkFile(theFile, "1master\n2\n3\ntopic\n"); checkFile(theFile, "1master\n2\n3\ntopic\n");
assertEquals(lastMasterChange, new RevWalk(db).parseCommit( try (RevWalk rw = new RevWalk(db)) {
db.resolve(Constants.HEAD)).getParent(0)); assertEquals(lastMasterChange, rw.parseCommit(
db.resolve(Constants.HEAD)).getParent(0));
}
List<ReflogEntry> headLog = db.getReflogReader(Constants.HEAD) List<ReflogEntry> headLog = db.getReflogReader(Constants.HEAD)
.getReverseEntries(); .getReverseEntries();
@ -785,8 +793,10 @@ public class RebaseCommandTest extends RepositoryTestCase {
// our old branch should be checked out again // our old branch should be checked out again
assertEquals("refs/heads/file3", db.getFullBranch()); assertEquals("refs/heads/file3", db.getFullBranch());
assertEquals(addFile2, new RevWalk(db).parseCommit( try (RevWalk rw = new RevWalk(db)) {
db.resolve(Constants.HEAD)).getParent(0)); assertEquals(addFile2, rw.parseCommit(
db.resolve(Constants.HEAD)).getParent(0));
}
checkoutBranch("refs/heads/file2"); checkoutBranch("refs/heads/file2");
assertTrue(new File(db.getWorkTree(), FILE1).exists()); assertTrue(new File(db.getWorkTree(), FILE1).exists());
@ -846,9 +856,10 @@ public class RebaseCommandTest extends RepositoryTestCase {
assertEquals(res.getStatus(), Status.ABORTED); assertEquals(res.getStatus(), Status.ABORTED);
assertEquals("refs/heads/topic", db.getFullBranch()); assertEquals("refs/heads/topic", db.getFullBranch());
checkFile(FILE1, "1topic", "2", "3", "topic4"); checkFile(FILE1, "1topic", "2", "3", "topic4");
RevWalk rw = new RevWalk(db); try (RevWalk rw = new RevWalk(db)) {
assertEquals(lastTopicCommit, rw assertEquals(lastTopicCommit,
.parseCommit(db.resolve(Constants.HEAD))); rw.parseCommit(db.resolve(Constants.HEAD)));
}
assertEquals(RepositoryState.SAFE, db.getRepositoryState()); assertEquals(RepositoryState.SAFE, db.getRepositoryState());
// rebase- dir in .git must be deleted // rebase- dir in .git must be deleted
@ -909,9 +920,10 @@ public class RebaseCommandTest extends RepositoryTestCase {
assertEquals(res.getStatus(), Status.ABORTED); assertEquals(res.getStatus(), Status.ABORTED);
assertEquals(lastTopicCommit.getName(), db.getFullBranch()); assertEquals(lastTopicCommit.getName(), db.getFullBranch());
checkFile(FILE1, "1topic", "2", "3", "topic4"); checkFile(FILE1, "1topic", "2", "3", "topic4");
RevWalk rw = new RevWalk(db); try (RevWalk rw = new RevWalk(db)) {
assertEquals(lastTopicCommit, assertEquals(lastTopicCommit,
rw.parseCommit(db.resolve(Constants.HEAD))); rw.parseCommit(db.resolve(Constants.HEAD)));
}
assertEquals(RepositoryState.SAFE, db.getRepositoryState()); assertEquals(RepositoryState.SAFE, db.getRepositoryState());
// rebase- dir in .git must be deleted // rebase- dir in .git must be deleted
@ -966,11 +978,12 @@ public class RebaseCommandTest extends RepositoryTestCase {
assertEquals(RepositoryState.SAFE, db.getRepositoryState()); assertEquals(RepositoryState.SAFE, db.getRepositoryState());
ObjectId headId = db.resolve(Constants.HEAD); ObjectId headId = db.resolve(Constants.HEAD);
RevWalk rw = new RevWalk(db); try (RevWalk rw = new RevWalk(db)) {
RevCommit rc = rw.parseCommit(headId); RevCommit rc = rw.parseCommit(headId);
RevCommit parent = rw.parseCommit(rc.getParent(0)); RevCommit parent = rw.parseCommit(rc.getParent(0));
assertEquals("change file1 in topic\n\nThis is conflicting", parent assertEquals("change file1 in topic\n\nThis is conflicting", parent
.getFullMessage()); .getFullMessage());
}
} }
@Test @Test
@ -1017,9 +1030,10 @@ public class RebaseCommandTest extends RepositoryTestCase {
git.rebase().setOperation(Operation.SKIP).call(); git.rebase().setOperation(Operation.SKIP).call();
ObjectId headId = db.resolve(Constants.HEAD); ObjectId headId = db.resolve(Constants.HEAD);
RevWalk rw = new RevWalk(db); try (RevWalk rw = new RevWalk(db)) {
RevCommit rc = rw.parseCommit(headId); RevCommit rc = rw.parseCommit(headId);
assertEquals("change file1 in master", rc.getFullMessage()); assertEquals("change file1 in master", rc.getFullMessage());
}
} }
@Test @Test
@ -1308,10 +1322,11 @@ public class RebaseCommandTest extends RepositoryTestCase {
git.rebase().setOperation(Operation.SKIP).call(); git.rebase().setOperation(Operation.SKIP).call();
ObjectId headId = db.resolve(Constants.HEAD); ObjectId headId = db.resolve(Constants.HEAD);
RevWalk rw = new RevWalk(db); try (RevWalk rw = new RevWalk(db)) {
RevCommit rc = rw.parseCommit(headId); RevCommit rc = rw.parseCommit(headId);
RevCommit parent = rw.parseCommit(rc.getParent(0)); RevCommit parent = rw.parseCommit(rc.getParent(0));
assertEquals("A different commit message", parent.getFullMessage()); assertEquals("A different commit message", parent.getFullMessage());
}
} }
private RevCommit writeFileAndCommit(String fileName, String commitMessage, private RevCommit writeFileAndCommit(String fileName, String commitMessage,
@ -1420,9 +1435,10 @@ public class RebaseCommandTest extends RepositoryTestCase {
res = git.rebase().setOperation(Operation.ABORT).call(); res = git.rebase().setOperation(Operation.ABORT).call();
assertEquals(res.getStatus(), Status.ABORTED); assertEquals(res.getStatus(), Status.ABORTED);
assertEquals("refs/heads/topic", db.getFullBranch()); 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(conflicting, rw.parseCommit(db.resolve(Constants.HEAD)));
assertEquals(RepositoryState.SAFE, db.getRepositoryState()); assertEquals(RepositoryState.SAFE, db.getRepositoryState());
}
// rebase- dir in .git must be deleted // rebase- dir in .git must be deleted
assertFalse(new File(db.getDirectory(), "rebase-merge").exists()); assertFalse(new File(db.getDirectory(), "rebase-merge").exists());
@ -2286,14 +2302,15 @@ public class RebaseCommandTest extends RepositoryTestCase {
assertEquals(RebaseResult.Status.OK, res2.getStatus()); assertEquals(RebaseResult.Status.OK, res2.getStatus());
ObjectId headId = db.resolve(Constants.HEAD); ObjectId headId = db.resolve(Constants.HEAD);
RevWalk rw = new RevWalk(db); try (RevWalk rw = new RevWalk(db)) {
RevCommit rc = rw.parseCommit(headId); RevCommit rc = rw.parseCommit(headId);
ObjectId head1Id = db.resolve(Constants.HEAD + "~1"); ObjectId head1Id = db.resolve(Constants.HEAD + "~1");
RevCommit rc1 = rw.parseCommit(head1Id); RevCommit rc1 = rw.parseCommit(head1Id);
assertEquals(rc.getFullMessage(), c4.getFullMessage()); assertEquals(rc.getFullMessage(), c4.getFullMessage());
assertEquals(rc1.getFullMessage(), c2.getFullMessage()); assertEquals(rc1.getFullMessage(), c2.getFullMessage());
}
} }
@Test @Test
@ -2643,15 +2660,16 @@ public class RebaseCommandTest extends RepositoryTestCase {
} }
}).call(); }).call();
RevWalk walk = new RevWalk(db); try (RevWalk walk = new RevWalk(db)) {
ObjectId headId = db.resolve(Constants.HEAD); ObjectId headId = db.resolve(Constants.HEAD);
RevCommit headCommit = walk.parseCommit(headId); RevCommit headCommit = walk.parseCommit(headId);
assertEquals(headCommit.getFullMessage(), assertEquals(headCommit.getFullMessage(),
"update file2 on master\nnew line"); "update file2 on master\nnew line");
ObjectId head2Id = db.resolve(Constants.HEAD + "^1"); ObjectId head2Id = db.resolve(Constants.HEAD + "^1");
RevCommit head1Commit = walk.parseCommit(head2Id); RevCommit head1Commit = walk.parseCommit(head2Id);
assertEquals("changed", head1Commit.getFullMessage()); assertEquals("changed", head1Commit.getFullMessage());
}
} }
@Test @Test
@ -2722,17 +2740,18 @@ public class RebaseCommandTest extends RepositoryTestCase {
} }
}).call(); }).call();
RevWalk walk = new RevWalk(db); try (RevWalk walk = new RevWalk(db)) {
ObjectId headId = db.resolve(Constants.HEAD); ObjectId headId = db.resolve(Constants.HEAD);
RevCommit headCommit = walk.parseCommit(headId); RevCommit headCommit = walk.parseCommit(headId);
assertEquals(headCommit.getFullMessage(), assertEquals(headCommit.getFullMessage(),
"update file2 on master\nnew line"); "update file2 on master\nnew line");
ObjectId head2Id = db.resolve(Constants.HEAD + "^1"); ObjectId head2Id = db.resolve(Constants.HEAD + "^1");
RevCommit head1Commit = walk.parseCommit(head2Id); RevCommit head1Commit = walk.parseCommit(head2Id);
assertEquals( assertEquals(
"Add file1\nnew line\nAdd file2\nnew line\nupdated file1 on master\nnew line", "Add file1\nnew line\nAdd file2\nnew line\nupdated file1 on master\nnew line",
head1Commit.getFullMessage()); head1Commit.getFullMessage());
}
} }
@Test @Test
@ -2804,15 +2823,16 @@ public class RebaseCommandTest extends RepositoryTestCase {
} }
}).call(); }).call();
RevWalk walk = new RevWalk(db); try (RevWalk walk = new RevWalk(db)) {
ObjectId headId = db.resolve(Constants.HEAD); ObjectId headId = db.resolve(Constants.HEAD);
RevCommit headCommit = walk.parseCommit(headId); RevCommit headCommit = walk.parseCommit(headId);
assertEquals(headCommit.getFullMessage(), assertEquals(headCommit.getFullMessage(),
"update file2 on master\nnew line"); "update file2 on master\nnew line");
ObjectId head2Id = db.resolve(Constants.HEAD + "^1"); ObjectId head2Id = db.resolve(Constants.HEAD + "^1");
RevCommit head1Commit = walk.parseCommit(head2Id); RevCommit head1Commit = walk.parseCommit(head2Id);
assertEquals("changed", head1Commit.getFullMessage()); assertEquals("changed", head1Commit.getFullMessage());
}
} }
@Test @Test
@ -2855,16 +2875,17 @@ public class RebaseCommandTest extends RepositoryTestCase {
} }
}).call(); }).call();
RevWalk walk = new RevWalk(db); try (RevWalk walk = new RevWalk(db)) {
ObjectId headId = db.resolve(Constants.HEAD); ObjectId headId = db.resolve(Constants.HEAD);
RevCommit headCommit = walk.parseCommit(headId); RevCommit headCommit = walk.parseCommit(headId);
assertEquals("update file2 on master\nnew line", assertEquals("update file2 on master\nnew line",
headCommit.getFullMessage()); headCommit.getFullMessage());
ObjectId head1Id = db.resolve(Constants.HEAD + "^1"); ObjectId head1Id = db.resolve(Constants.HEAD + "^1");
RevCommit head1Commit = walk.parseCommit(head1Id); RevCommit head1Commit = walk.parseCommit(head1Id);
assertEquals("Add file2\nnew line", assertEquals("Add file2\nnew line",
head1Commit.getFullMessage()); head1Commit.getFullMessage());
}
} }
@Test @Test
@ -2903,11 +2924,12 @@ public class RebaseCommandTest extends RepositoryTestCase {
} }
}).call(); }).call();
RevWalk walk = new RevWalk(db); try (RevWalk walk = new RevWalk(db)) {
ObjectId headId = db.resolve(Constants.HEAD); ObjectId headId = db.resolve(Constants.HEAD);
RevCommit headCommit = walk.parseCommit(headId); RevCommit headCommit = walk.parseCommit(headId);
assertEquals("Add file2", assertEquals("Add file2",
headCommit.getFullMessage()); headCommit.getFullMessage());
}
} }
@Test(expected = InvalidRebaseStepException.class) @Test(expected = InvalidRebaseStepException.class)

237
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/blame/BlameGeneratorTest.java

@ -58,141 +58,142 @@ import org.junit.Test;
public class BlameGeneratorTest extends RepositoryTestCase { public class BlameGeneratorTest extends RepositoryTestCase {
@Test @Test
public void testBoundLineDelete() throws Exception { public void testBoundLineDelete() throws Exception {
Git git = new Git(db); try (Git git = new Git(db)) {
String[] content1 = new String[] { "first", "second" };
String[] content1 = new String[] { "first", "second" }; writeTrashFile("file.txt", join(content1));
writeTrashFile("file.txt", join(content1)); git.add().addFilepattern("file.txt").call();
git.add().addFilepattern("file.txt").call(); RevCommit c1 = git.commit().setMessage("create file").call();
RevCommit c1 = git.commit().setMessage("create file").call();
String[] content2 = new String[] { "third", "first", "second" };
String[] content2 = new String[] { "third", "first", "second" }; writeTrashFile("file.txt", join(content2));
writeTrashFile("file.txt", join(content2)); git.add().addFilepattern("file.txt").call();
git.add().addFilepattern("file.txt").call(); RevCommit c2 = git.commit().setMessage("create file").call();
RevCommit c2 = git.commit().setMessage("create file").call();
try (BlameGenerator generator = new BlameGenerator(db, "file.txt")) {
try (BlameGenerator generator = new BlameGenerator(db, "file.txt")) { generator.push(null, db.resolve(Constants.HEAD));
generator.push(null, db.resolve(Constants.HEAD)); assertEquals(3, generator.getResultContents().size());
assertEquals(3, generator.getResultContents().size());
assertTrue(generator.next());
assertTrue(generator.next()); assertEquals(c2, generator.getSourceCommit());
assertEquals(c2, generator.getSourceCommit()); assertEquals(1, generator.getRegionLength());
assertEquals(1, generator.getRegionLength()); assertEquals(0, generator.getResultStart());
assertEquals(0, generator.getResultStart()); assertEquals(1, generator.getResultEnd());
assertEquals(1, generator.getResultEnd()); assertEquals(0, generator.getSourceStart());
assertEquals(0, generator.getSourceStart()); assertEquals(1, generator.getSourceEnd());
assertEquals(1, generator.getSourceEnd()); assertEquals("file.txt", generator.getSourcePath());
assertEquals("file.txt", generator.getSourcePath());
assertTrue(generator.next());
assertTrue(generator.next()); assertEquals(c1, generator.getSourceCommit());
assertEquals(c1, generator.getSourceCommit()); assertEquals(2, generator.getRegionLength());
assertEquals(2, generator.getRegionLength()); assertEquals(1, generator.getResultStart());
assertEquals(1, generator.getResultStart()); assertEquals(3, generator.getResultEnd());
assertEquals(3, generator.getResultEnd()); assertEquals(0, generator.getSourceStart());
assertEquals(0, generator.getSourceStart()); assertEquals(2, generator.getSourceEnd());
assertEquals(2, generator.getSourceEnd()); assertEquals("file.txt", generator.getSourcePath());
assertEquals("file.txt", generator.getSourcePath());
assertFalse(generator.next());
assertFalse(generator.next()); }
} }
} }
@Test @Test
public void testRenamedBoundLineDelete() throws Exception { public void testRenamedBoundLineDelete() throws Exception {
Git git = new Git(db); try (Git git = new Git(db)) {
final String FILENAME_1 = "subdir/file1.txt"; final String FILENAME_1 = "subdir/file1.txt";
final String FILENAME_2 = "subdir/file2.txt"; final String FILENAME_2 = "subdir/file2.txt";
String[] content1 = new String[] { "first", "second" }; String[] content1 = new String[] { "first", "second" };
writeTrashFile(FILENAME_1, join(content1)); writeTrashFile(FILENAME_1, join(content1));
git.add().addFilepattern(FILENAME_1).call(); git.add().addFilepattern(FILENAME_1).call();
RevCommit c1 = git.commit().setMessage("create file1").call(); RevCommit c1 = git.commit().setMessage("create file1").call();
// rename it // rename it
writeTrashFile(FILENAME_2, join(content1)); writeTrashFile(FILENAME_2, join(content1));
git.add().addFilepattern(FILENAME_2).call(); git.add().addFilepattern(FILENAME_2).call();
deleteTrashFile(FILENAME_1); deleteTrashFile(FILENAME_1);
git.rm().addFilepattern(FILENAME_1).call(); git.rm().addFilepattern(FILENAME_1).call();
git.commit().setMessage("rename file1.txt to file2.txt").call(); git.commit().setMessage("rename file1.txt to file2.txt").call();
// and change the new file // and change the new file
String[] content2 = new String[] { "third", "first", "second" }; String[] content2 = new String[] { "third", "first", "second" };
writeTrashFile(FILENAME_2, join(content2)); writeTrashFile(FILENAME_2, join(content2));
git.add().addFilepattern(FILENAME_2).call(); git.add().addFilepattern(FILENAME_2).call();
RevCommit c2 = git.commit().setMessage("change file2").call(); RevCommit c2 = git.commit().setMessage("change file2").call();
try (BlameGenerator generator = new BlameGenerator(db, FILENAME_2)) { try (BlameGenerator generator = new BlameGenerator(db, FILENAME_2)) {
generator.push(null, db.resolve(Constants.HEAD)); generator.push(null, db.resolve(Constants.HEAD));
assertEquals(3, generator.getResultContents().size()); assertEquals(3, generator.getResultContents().size());
assertTrue(generator.next()); assertTrue(generator.next());
assertEquals(c2, generator.getSourceCommit()); assertEquals(c2, generator.getSourceCommit());
assertEquals(1, generator.getRegionLength()); assertEquals(1, generator.getRegionLength());
assertEquals(0, generator.getResultStart()); assertEquals(0, generator.getResultStart());
assertEquals(1, generator.getResultEnd()); assertEquals(1, generator.getResultEnd());
assertEquals(0, generator.getSourceStart()); assertEquals(0, generator.getSourceStart());
assertEquals(1, generator.getSourceEnd()); assertEquals(1, generator.getSourceEnd());
assertEquals(FILENAME_2, generator.getSourcePath()); assertEquals(FILENAME_2, generator.getSourcePath());
assertTrue(generator.next()); assertTrue(generator.next());
assertEquals(c1, generator.getSourceCommit()); assertEquals(c1, generator.getSourceCommit());
assertEquals(2, generator.getRegionLength()); assertEquals(2, generator.getRegionLength());
assertEquals(1, generator.getResultStart()); assertEquals(1, generator.getResultStart());
assertEquals(3, generator.getResultEnd()); assertEquals(3, generator.getResultEnd());
assertEquals(0, generator.getSourceStart()); assertEquals(0, generator.getSourceStart());
assertEquals(2, generator.getSourceEnd()); assertEquals(2, generator.getSourceEnd());
assertEquals(FILENAME_1, generator.getSourcePath()); assertEquals(FILENAME_1, generator.getSourcePath());
assertFalse(generator.next()); assertFalse(generator.next());
} }
// and test again with other BlameGenerator API: // and test again with other BlameGenerator API:
try (BlameGenerator generator = new BlameGenerator(db, FILENAME_2)) { try (BlameGenerator generator = new BlameGenerator(db, FILENAME_2)) {
generator.push(null, db.resolve(Constants.HEAD)); generator.push(null, db.resolve(Constants.HEAD));
BlameResult result = generator.computeBlameResult(); BlameResult result = generator.computeBlameResult();
assertEquals(3, result.getResultContents().size()); assertEquals(3, result.getResultContents().size());
assertEquals(c2, result.getSourceCommit(0)); assertEquals(c2, result.getSourceCommit(0));
assertEquals(FILENAME_2, result.getSourcePath(0)); assertEquals(FILENAME_2, result.getSourcePath(0));
assertEquals(c1, result.getSourceCommit(1)); assertEquals(c1, result.getSourceCommit(1));
assertEquals(FILENAME_1, result.getSourcePath(1)); assertEquals(FILENAME_1, result.getSourcePath(1));
assertEquals(c1, result.getSourceCommit(2)); assertEquals(c1, result.getSourceCommit(2));
assertEquals(FILENAME_1, result.getSourcePath(2)); assertEquals(FILENAME_1, result.getSourcePath(2));
}
} }
} }
@Test @Test
public void testLinesAllDeletedShortenedWalk() throws Exception { public void testLinesAllDeletedShortenedWalk() throws Exception {
Git git = new Git(db); try (Git git = new Git(db)) {
String[] content1 = new String[] { "first", "second", "third" };
String[] content1 = new String[] { "first", "second", "third" };
writeTrashFile("file.txt", join(content1)); writeTrashFile("file.txt", join(content1));
git.add().addFilepattern("file.txt").call(); git.add().addFilepattern("file.txt").call();
git.commit().setMessage("create file").call(); git.commit().setMessage("create file").call();
String[] content2 = new String[] { "" }; String[] content2 = new String[] { "" };
writeTrashFile("file.txt", join(content2)); writeTrashFile("file.txt", join(content2));
git.add().addFilepattern("file.txt").call(); git.add().addFilepattern("file.txt").call();
git.commit().setMessage("create file").call(); git.commit().setMessage("create file").call();
writeTrashFile("file.txt", join(content1)); writeTrashFile("file.txt", join(content1));
git.add().addFilepattern("file.txt").call(); git.add().addFilepattern("file.txt").call();
RevCommit c3 = git.commit().setMessage("create file").call(); RevCommit c3 = git.commit().setMessage("create file").call();
try (BlameGenerator generator = new BlameGenerator(db, "file.txt")) { try (BlameGenerator generator = new BlameGenerator(db, "file.txt")) {
generator.push(null, db.resolve(Constants.HEAD)); generator.push(null, db.resolve(Constants.HEAD));
assertEquals(3, generator.getResultContents().size()); assertEquals(3, generator.getResultContents().size());
assertTrue(generator.next()); assertTrue(generator.next());
assertEquals(c3, generator.getSourceCommit()); assertEquals(c3, generator.getSourceCommit());
assertEquals(0, generator.getResultStart()); assertEquals(0, generator.getResultStart());
assertEquals(3, generator.getResultEnd()); assertEquals(3, generator.getResultEnd());
assertFalse(generator.next()); assertFalse(generator.next());
}
} }
} }

533
org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/DiffEntryTest.java

@ -77,260 +77,268 @@ public class DiffEntryTest extends RepositoryTestCase {
public void shouldListAddedFileInInitialCommit() throws Exception { public void shouldListAddedFileInInitialCommit() throws Exception {
// given // given
writeTrashFile("a.txt", "content"); writeTrashFile("a.txt", "content");
Git git = new Git(db); try (Git git = new Git(db);
git.add().addFilepattern("a.txt").call(); TreeWalk walk = new TreeWalk(db)) {
RevCommit c = git.commit().setMessage("initial commit").call(); git.add().addFilepattern("a.txt").call();
RevCommit c = git.commit().setMessage("initial commit").call();
// when
TreeWalk walk = new TreeWalk(db); // when
walk.addTree(new EmptyTreeIterator()); walk.addTree(new EmptyTreeIterator());
walk.addTree(c.getTree()); walk.addTree(c.getTree());
List<DiffEntry> result = DiffEntry.scan(walk); List<DiffEntry> result = DiffEntry.scan(walk);
// then // then
assertThat(result, notNullValue()); assertThat(result, notNullValue());
assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1))); assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1)));
DiffEntry entry = result.get(0); DiffEntry entry = result.get(0);
assertThat(entry.getChangeType(), is(ChangeType.ADD)); assertThat(entry.getChangeType(), is(ChangeType.ADD));
assertThat(entry.getNewPath(), is("a.txt")); assertThat(entry.getNewPath(), is("a.txt"));
assertThat(entry.getOldPath(), is(DEV_NULL)); assertThat(entry.getOldPath(), is(DEV_NULL));
}
} }
@Test @Test
public void shouldListAddedFileBetweenTwoCommits() throws Exception { public void shouldListAddedFileBetweenTwoCommits() throws Exception {
// given // given
Git git = new Git(db); try (Git git = new Git(db);
RevCommit c1 = git.commit().setMessage("initial commit").call(); TreeWalk walk = new TreeWalk(db)) {
writeTrashFile("a.txt", "content"); RevCommit c1 = git.commit().setMessage("initial commit").call();
git.add().addFilepattern("a.txt").call(); writeTrashFile("a.txt", "content");
RevCommit c2 = git.commit().setMessage("second commit").call(); git.add().addFilepattern("a.txt").call();
RevCommit c2 = git.commit().setMessage("second commit").call();
// when
TreeWalk walk = new TreeWalk(db); // when
walk.addTree(c1.getTree()); walk.addTree(c1.getTree());
walk.addTree(c2.getTree()); walk.addTree(c2.getTree());
List<DiffEntry> result = DiffEntry.scan(walk); List<DiffEntry> result = DiffEntry.scan(walk);
// then // then
assertThat(result, notNullValue()); assertThat(result, notNullValue());
assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1))); assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1)));
DiffEntry entry = result.get(0); DiffEntry entry = result.get(0);
assertThat(entry.getChangeType(), is(ChangeType.ADD)); assertThat(entry.getChangeType(), is(ChangeType.ADD));
assertThat(entry.getNewPath(), is("a.txt")); assertThat(entry.getNewPath(), is("a.txt"));
assertThat(entry.getOldPath(), is(DEV_NULL)); assertThat(entry.getOldPath(), is(DEV_NULL));
}
} }
@Test @Test
public void shouldListModificationBetweenTwoCommits() throws Exception { public void shouldListModificationBetweenTwoCommits() throws Exception {
// given // given
Git git = new Git(db); try (Git git = new Git(db);
File file = writeTrashFile("a.txt", "content"); TreeWalk walk = new TreeWalk(db)) {
git.add().addFilepattern("a.txt").call(); File file = writeTrashFile("a.txt", "content");
RevCommit c1 = git.commit().setMessage("initial commit").call(); git.add().addFilepattern("a.txt").call();
write(file, "new content"); RevCommit c1 = git.commit().setMessage("initial commit").call();
RevCommit c2 = git.commit().setAll(true).setMessage("second commit") write(file, "new content");
.call(); RevCommit c2 = git.commit().setAll(true).setMessage("second commit")
.call();
// when
TreeWalk walk = new TreeWalk(db); // when
walk.addTree(c1.getTree()); walk.addTree(c1.getTree());
walk.addTree(c2.getTree()); walk.addTree(c2.getTree());
List<DiffEntry> result = DiffEntry.scan(walk); List<DiffEntry> result = DiffEntry.scan(walk);
// then // then
assertThat(result, notNullValue()); assertThat(result, notNullValue());
assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1))); assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1)));
DiffEntry entry = result.get(0); DiffEntry entry = result.get(0);
assertThat(entry.getChangeType(), is(ChangeType.MODIFY)); assertThat(entry.getChangeType(), is(ChangeType.MODIFY));
assertThat(entry.getNewPath(), is("a.txt")); assertThat(entry.getNewPath(), is("a.txt"));
}
} }
@Test @Test
public void shouldListDeletionBetweenTwoCommits() throws Exception { public void shouldListDeletionBetweenTwoCommits() throws Exception {
// given // given
Git git = new Git(db); try (Git git = new Git(db);
File file = writeTrashFile("a.txt", "content"); TreeWalk walk = new TreeWalk(db)) {
git.add().addFilepattern("a.txt").call(); File file = writeTrashFile("a.txt", "content");
RevCommit c1 = git.commit().setMessage("initial commit").call(); git.add().addFilepattern("a.txt").call();
delete(file); RevCommit c1 = git.commit().setMessage("initial commit").call();
RevCommit c2 = git.commit().setAll(true).setMessage("delete a.txt") delete(file);
.call(); RevCommit c2 = git.commit().setAll(true).setMessage("delete a.txt")
.call();
// when
TreeWalk walk = new TreeWalk(db); // when
walk.addTree(c1.getTree()); walk.addTree(c1.getTree());
walk.addTree(c2.getTree()); walk.addTree(c2.getTree());
List<DiffEntry> result = DiffEntry.scan(walk); List<DiffEntry> result = DiffEntry.scan(walk);
// then // then
assertThat(result, notNullValue()); assertThat(result, notNullValue());
assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1))); assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1)));
DiffEntry entry = result.get(0); DiffEntry entry = result.get(0);
assertThat(entry.getOldPath(), is("a.txt")); assertThat(entry.getOldPath(), is("a.txt"));
assertThat(entry.getNewPath(), is(DEV_NULL)); assertThat(entry.getNewPath(), is(DEV_NULL));
assertThat(entry.getChangeType(), is(ChangeType.DELETE)); assertThat(entry.getChangeType(), is(ChangeType.DELETE));
}
} }
@Test @Test
public void shouldListModificationInDirWithoutModifiedTrees() public void shouldListModificationInDirWithoutModifiedTrees()
throws Exception { throws Exception {
// given // given
Git git = new Git(db); try (Git git = new Git(db);
File tree = new File(new File(db.getWorkTree(), "a"), "b"); TreeWalk walk = new TreeWalk(db)) {
FileUtils.mkdirs(tree); File tree = new File(new File(db.getWorkTree(), "a"), "b");
File file = new File(tree, "c.txt"); FileUtils.mkdirs(tree);
FileUtils.createNewFile(file); File file = new File(tree, "c.txt");
write(file, "content"); FileUtils.createNewFile(file);
git.add().addFilepattern("a").call(); write(file, "content");
RevCommit c1 = git.commit().setMessage("initial commit").call(); git.add().addFilepattern("a").call();
write(file, "new line"); RevCommit c1 = git.commit().setMessage("initial commit").call();
RevCommit c2 = git.commit().setAll(true).setMessage("second commit") write(file, "new line");
.call(); RevCommit c2 = git.commit().setAll(true).setMessage("second commit")
.call();
// when
TreeWalk walk = new TreeWalk(db); // when
walk.addTree(c1.getTree()); walk.addTree(c1.getTree());
walk.addTree(c2.getTree()); walk.addTree(c2.getTree());
walk.setRecursive(true); walk.setRecursive(true);
List<DiffEntry> result = DiffEntry.scan(walk); List<DiffEntry> result = DiffEntry.scan(walk);
// then // then
assertThat(result, notNullValue()); assertThat(result, notNullValue());
assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1))); assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1)));
DiffEntry entry = result.get(0); DiffEntry entry = result.get(0);
assertThat(entry.getChangeType(), is(ChangeType.MODIFY)); assertThat(entry.getChangeType(), is(ChangeType.MODIFY));
assertThat(entry.getNewPath(), is("a/b/c.txt")); assertThat(entry.getNewPath(), is("a/b/c.txt"));
}
} }
@Test @Test
public void shouldListModificationInDirWithModifiedTrees() throws Exception { public void shouldListModificationInDirWithModifiedTrees() throws Exception {
// given // given
Git git = new Git(db); try (Git git = new Git(db);
File tree = new File(new File(db.getWorkTree(), "a"), "b"); TreeWalk walk = new TreeWalk(db)) {
FileUtils.mkdirs(tree); File tree = new File(new File(db.getWorkTree(), "a"), "b");
File file = new File(tree, "c.txt"); FileUtils.mkdirs(tree);
FileUtils.createNewFile(file); File file = new File(tree, "c.txt");
write(file, "content"); FileUtils.createNewFile(file);
git.add().addFilepattern("a").call(); write(file, "content");
RevCommit c1 = git.commit().setMessage("initial commit").call(); git.add().addFilepattern("a").call();
write(file, "new line"); RevCommit c1 = git.commit().setMessage("initial commit").call();
RevCommit c2 = git.commit().setAll(true).setMessage("second commit") write(file, "new line");
.call(); RevCommit c2 = git.commit().setAll(true).setMessage("second commit")
.call();
// when
TreeWalk walk = new TreeWalk(db); // when
walk.addTree(c1.getTree()); walk.addTree(c1.getTree());
walk.addTree(c2.getTree()); walk.addTree(c2.getTree());
List<DiffEntry> result = DiffEntry.scan(walk, true); List<DiffEntry> result = DiffEntry.scan(walk, true);
// then // then
assertThat(result, notNullValue()); assertThat(result, notNullValue());
assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(3))); assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(3)));
DiffEntry entry = result.get(0); DiffEntry entry = result.get(0);
assertThat(entry.getChangeType(), is(ChangeType.MODIFY)); assertThat(entry.getChangeType(), is(ChangeType.MODIFY));
assertThat(entry.getNewPath(), is("a")); assertThat(entry.getNewPath(), is("a"));
entry = result.get(1); entry = result.get(1);
assertThat(entry.getChangeType(), is(ChangeType.MODIFY)); assertThat(entry.getChangeType(), is(ChangeType.MODIFY));
assertThat(entry.getNewPath(), is("a/b")); assertThat(entry.getNewPath(), is("a/b"));
entry = result.get(2); entry = result.get(2);
assertThat(entry.getChangeType(), is(ChangeType.MODIFY)); assertThat(entry.getChangeType(), is(ChangeType.MODIFY));
assertThat(entry.getNewPath(), is("a/b/c.txt")); assertThat(entry.getNewPath(), is("a/b/c.txt"));
}
} }
@Test @Test
public void shouldListChangesInWorkingTree() throws Exception { public void shouldListChangesInWorkingTree() throws Exception {
// given // given
writeTrashFile("a.txt", "content"); writeTrashFile("a.txt", "content");
Git git = new Git(db); try (Git git = new Git(db);
git.add().addFilepattern("a.txt").call(); TreeWalk walk = new TreeWalk(db)) {
RevCommit c = git.commit().setMessage("initial commit").call(); git.add().addFilepattern("a.txt").call();
writeTrashFile("b.txt", "new line"); RevCommit c = git.commit().setMessage("initial commit").call();
writeTrashFile("b.txt", "new line");
// when
TreeWalk walk = new TreeWalk(db); // when
walk.addTree(c.getTree()); walk.addTree(c.getTree());
walk.addTree(new FileTreeIterator(db)); walk.addTree(new FileTreeIterator(db));
List<DiffEntry> result = DiffEntry.scan(walk, true); List<DiffEntry> result = DiffEntry.scan(walk, true);
// then // then
assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1))); assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1)));
DiffEntry entry = result.get(0); DiffEntry entry = result.get(0);
assertThat(entry.getChangeType(), is(ChangeType.ADD)); assertThat(entry.getChangeType(), is(ChangeType.ADD));
assertThat(entry.getNewPath(), is("b.txt")); assertThat(entry.getNewPath(), is("b.txt"));
}
} }
@Test @Test
public void shouldMarkEntriesWhenGivenMarkTreeFilter() throws Exception { public void shouldMarkEntriesWhenGivenMarkTreeFilter() throws Exception {
// given // given
Git git = new Git(db); try (Git git = new Git(db);
RevCommit c1 = git.commit().setMessage("initial commit").call(); TreeWalk walk = new TreeWalk(db)) {
FileUtils.mkdir(new File(db.getWorkTree(), "b")); RevCommit c1 = git.commit().setMessage("initial commit").call();
writeTrashFile("a.txt", "a"); FileUtils.mkdir(new File(db.getWorkTree(), "b"));
writeTrashFile("b/1.txt", "b1"); writeTrashFile("a.txt", "a");
writeTrashFile("b/2.txt", "b2"); writeTrashFile("b/1.txt", "b1");
writeTrashFile("c.txt", "c"); writeTrashFile("b/2.txt", "b2");
git.add().addFilepattern("a.txt").addFilepattern("b") writeTrashFile("c.txt", "c");
.addFilepattern("c.txt").call(); git.add().addFilepattern("a.txt").addFilepattern("b")
RevCommit c2 = git.commit().setMessage("second commit").call(); .addFilepattern("c.txt").call();
TreeFilter filterA = PathFilterGroup.createFromStrings("a.txt"); RevCommit c2 = git.commit().setMessage("second commit").call();
TreeFilter filterB = PathFilterGroup.createFromStrings("b"); TreeFilter filterA = PathFilterGroup.createFromStrings("a.txt");
TreeFilter filterB2 = PathFilterGroup.createFromStrings("b/2.txt"); TreeFilter filterB = PathFilterGroup.createFromStrings("b");
TreeFilter filterB2 = PathFilterGroup.createFromStrings("b/2.txt");
// when
TreeWalk walk = new TreeWalk(db); // when
walk.addTree(c1.getTree()); walk.addTree(c1.getTree());
walk.addTree(c2.getTree()); walk.addTree(c2.getTree());
List<DiffEntry> result = DiffEntry.scan(walk, true, new TreeFilter[] { List<DiffEntry> result = DiffEntry.scan(walk, true, new TreeFilter[] {
filterA, filterB, filterB2 }); filterA, filterB, filterB2 });
// then // then
assertThat(result, notNullValue()); assertThat(result, notNullValue());
assertEquals(5, result.size()); assertEquals(5, result.size());
DiffEntry entryA = result.get(0); DiffEntry entryA = result.get(0);
DiffEntry entryB = result.get(1); DiffEntry entryB = result.get(1);
DiffEntry entryB1 = result.get(2); DiffEntry entryB1 = result.get(2);
DiffEntry entryB2 = result.get(3); DiffEntry entryB2 = result.get(3);
DiffEntry entryC = result.get(4); DiffEntry entryC = result.get(4);
assertThat(entryA.getNewPath(), is("a.txt")); assertThat(entryA.getNewPath(), is("a.txt"));
assertTrue(entryA.isMarked(0)); assertTrue(entryA.isMarked(0));
assertFalse(entryA.isMarked(1)); assertFalse(entryA.isMarked(1));
assertFalse(entryA.isMarked(2)); assertFalse(entryA.isMarked(2));
assertEquals(1, entryA.getTreeFilterMarks()); assertEquals(1, entryA.getTreeFilterMarks());
assertThat(entryB.getNewPath(), is("b")); assertThat(entryB.getNewPath(), is("b"));
assertFalse(entryB.isMarked(0)); assertFalse(entryB.isMarked(0));
assertTrue(entryB.isMarked(1)); assertTrue(entryB.isMarked(1));
assertTrue(entryB.isMarked(2)); assertTrue(entryB.isMarked(2));
assertEquals(6, entryB.getTreeFilterMarks()); assertEquals(6, entryB.getTreeFilterMarks());
assertThat(entryB1.getNewPath(), is("b/1.txt")); assertThat(entryB1.getNewPath(), is("b/1.txt"));
assertFalse(entryB1.isMarked(0)); assertFalse(entryB1.isMarked(0));
assertTrue(entryB1.isMarked(1)); assertTrue(entryB1.isMarked(1));
assertFalse(entryB1.isMarked(2)); assertFalse(entryB1.isMarked(2));
assertEquals(2, entryB1.getTreeFilterMarks()); assertEquals(2, entryB1.getTreeFilterMarks());
assertThat(entryB2.getNewPath(), is("b/2.txt")); assertThat(entryB2.getNewPath(), is("b/2.txt"));
assertFalse(entryB2.isMarked(0)); assertFalse(entryB2.isMarked(0));
assertTrue(entryB2.isMarked(1)); assertTrue(entryB2.isMarked(1));
assertTrue(entryB2.isMarked(2)); assertTrue(entryB2.isMarked(2));
assertEquals(6, entryB2.getTreeFilterMarks()); assertEquals(6, entryB2.getTreeFilterMarks());
assertThat(entryC.getNewPath(), is("c.txt")); assertThat(entryC.getNewPath(), is("c.txt"));
assertFalse(entryC.isMarked(0)); assertFalse(entryC.isMarked(0));
assertFalse(entryC.isMarked(1)); assertFalse(entryC.isMarked(1));
assertFalse(entryC.isMarked(2)); assertFalse(entryC.isMarked(2));
assertEquals(0, entryC.getTreeFilterMarks()); assertEquals(0, entryC.getTreeFilterMarks());
}
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
@ -339,9 +347,10 @@ public class DiffEntryTest extends RepositoryTestCase {
// given - we don't need anything here // given - we don't need anything here
// when // when
TreeWalk walk = new TreeWalk(db); try (TreeWalk walk = new TreeWalk(db)) {
walk.addTree(new EmptyTreeIterator()); walk.addTree(new EmptyTreeIterator());
DiffEntry.scan(walk); DiffEntry.scan(walk);
}
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
@ -350,11 +359,12 @@ public class DiffEntryTest extends RepositoryTestCase {
// given - we don't need anything here // given - we don't need anything here
// when // when
TreeWalk walk = new TreeWalk(db); try (TreeWalk walk = new TreeWalk(db)) {
walk.addTree(new EmptyTreeIterator()); walk.addTree(new EmptyTreeIterator());
walk.addTree(new EmptyTreeIterator()); walk.addTree(new EmptyTreeIterator());
walk.addTree(new EmptyTreeIterator()); walk.addTree(new EmptyTreeIterator());
DiffEntry.scan(walk); DiffEntry.scan(walk);
}
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
@ -363,46 +373,47 @@ public class DiffEntryTest extends RepositoryTestCase {
// given - we don't need anything here // given - we don't need anything here
// when // when
TreeWalk walk = new TreeWalk(db); try (TreeWalk walk = new TreeWalk(db)) {
walk.addTree(new EmptyTreeIterator()); walk.addTree(new EmptyTreeIterator());
walk.addTree(new EmptyTreeIterator()); walk.addTree(new EmptyTreeIterator());
walk.setRecursive(true); walk.setRecursive(true);
DiffEntry.scan(walk, true); DiffEntry.scan(walk, true);
}
} }
@Test @Test
public void shouldReportFileModeChange() throws Exception { public void shouldReportFileModeChange() throws Exception {
writeTrashFile("a.txt", "content"); writeTrashFile("a.txt", "content");
Git git = new Git(db); try (Git git = new Git(db);
git.add().addFilepattern("a.txt").call(); TreeWalk walk = new TreeWalk(db)) {
RevCommit c1 = git.commit().setMessage("initial commit").call(); git.add().addFilepattern("a.txt").call();
DirCache cache = db.lockDirCache(); RevCommit c1 = git.commit().setMessage("initial commit").call();
DirCacheEditor editor = cache.editor(); DirCache cache = db.lockDirCache();
final TreeWalk walk = new TreeWalk(db); DirCacheEditor editor = cache.editor();
walk.addTree(c1.getTree()); walk.addTree(c1.getTree());
walk.setRecursive(true); walk.setRecursive(true);
assertTrue(walk.next()); assertTrue(walk.next());
editor.add(new PathEdit("a.txt") { editor.add(new PathEdit("a.txt") {
public void apply(DirCacheEntry ent) {
public void apply(DirCacheEntry ent) { ent.setFileMode(FileMode.EXECUTABLE_FILE);
ent.setFileMode(FileMode.EXECUTABLE_FILE); ent.setObjectId(walk.getObjectId(0));
ent.setObjectId(walk.getObjectId(0)); }
} });
}); assertTrue(editor.commit());
assertTrue(editor.commit()); RevCommit c2 = git.commit().setMessage("second commit").call();
RevCommit c2 = git.commit().setMessage("second commit").call(); walk.reset();
walk.reset(); walk.addTree(c1.getTree());
walk.addTree(c1.getTree()); walk.addTree(c2.getTree());
walk.addTree(c2.getTree()); List<DiffEntry> diffs = DiffEntry.scan(walk, false);
List<DiffEntry> diffs = DiffEntry.scan(walk, false); assertEquals(1, diffs.size());
assertEquals(1, diffs.size()); DiffEntry diff = diffs.get(0);
DiffEntry diff = diffs.get(0); assertEquals(ChangeType.MODIFY,diff.getChangeType());
assertEquals(ChangeType.MODIFY,diff.getChangeType()); assertEquals(diff.getOldId(), diff.getNewId());
assertEquals(diff.getOldId(), diff.getNewId()); assertEquals("a.txt", diff.getOldPath());
assertEquals("a.txt", diff.getOldPath()); assertEquals(diff.getOldPath(), diff.getNewPath());
assertEquals(diff.getOldPath(), diff.getNewPath()); assertEquals(FileMode.EXECUTABLE_FILE, diff.getNewMode());
assertEquals(FileMode.EXECUTABLE_FILE, diff.getNewMode()); assertEquals(FileMode.REGULAR_FILE, diff.getOldMode());
assertEquals(FileMode.REGULAR_FILE, diff.getOldMode()); }
} }
} }

1
org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/T0003_BasicTest.java

@ -84,7 +84,6 @@ import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase;
import org.eclipse.jgit.util.FileUtils; import org.eclipse.jgit.util.FileUtils;
import org.junit.Test; import org.junit.Test;
@SuppressWarnings("deprecation")
public class T0003_BasicTest extends SampleDataRepositoryTestCase { public class T0003_BasicTest extends SampleDataRepositoryTestCase {
@Test @Test

1
org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/IndexDiffTest.java

@ -77,7 +77,6 @@ import org.eclipse.jgit.treewalk.FileTreeIterator;
import org.eclipse.jgit.util.IO; import org.eclipse.jgit.util.IO;
import org.junit.Test; import org.junit.Test;
@SuppressWarnings("deprecation")
public class IndexDiffTest extends RepositoryTestCase { public class IndexDiffTest extends RepositoryTestCase {
static PathEdit add(final Repository db, final File workdir, static PathEdit add(final Repository db, final File workdir,

1
org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/ObjectWalkTest.java

@ -53,7 +53,6 @@ import org.eclipse.jgit.lib.ObjectInserter;
import org.eclipse.jgit.lib.TreeFormatter; import org.eclipse.jgit.lib.TreeFormatter;
import org.junit.Test; import org.junit.Test;
@SuppressWarnings("deprecation")
public class ObjectWalkTest extends RevWalkTestCase { public class ObjectWalkTest extends RevWalkTestCase {
protected ObjectWalk objw; protected ObjectWalk objw;

399
org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/FileTreeIteratorTest.java

@ -255,10 +255,11 @@ public class FileTreeIteratorTest extends RepositoryTestCase {
@Test @Test
public void testDirCacheMatchingId() throws Exception { public void testDirCacheMatchingId() throws Exception {
File f = writeTrashFile("file", "content"); File f = writeTrashFile("file", "content");
Git git = new Git(db); try (Git git = new Git(db)) {
writeTrashFile("file", "content"); writeTrashFile("file", "content");
fsTick(f); fsTick(f);
git.add().addFilepattern("file").call(); git.add().addFilepattern("file").call();
}
DirCacheEntry dce = db.readDirCache().getEntry("file"); DirCacheEntry dce = db.readDirCache().getEntry("file");
TreeWalk tw = new TreeWalk(db); TreeWalk tw = new TreeWalk(db);
FileTreeIterator fti = new FileTreeIterator(trash, db.getFS(), db FileTreeIterator fti = new FileTreeIterator(trash, db.getFS(), db
@ -282,11 +283,12 @@ public class FileTreeIteratorTest extends RepositoryTestCase {
@Test @Test
public void testIsModifiedSymlinkAsFile() throws Exception { public void testIsModifiedSymlinkAsFile() throws Exception {
writeTrashFile("symlink", "content"); writeTrashFile("symlink", "content");
Git git = new Git(db); try (Git git = new Git(db)) {
db.getConfig().setString(ConfigConstants.CONFIG_CORE_SECTION, null, db.getConfig().setString(ConfigConstants.CONFIG_CORE_SECTION, null,
ConfigConstants.CONFIG_KEY_SYMLINKS, "false"); ConfigConstants.CONFIG_KEY_SYMLINKS, "false");
git.add().addFilepattern("symlink").call(); git.add().addFilepattern("symlink").call();
git.commit().setMessage("commit").call(); git.commit().setMessage("commit").call();
}
// Modify previously committed DirCacheEntry and write it back to disk // Modify previously committed DirCacheEntry and write it back to disk
DirCacheEntry dce = db.readDirCache().getEntry("symlink"); DirCacheEntry dce = db.readDirCache().getEntry("symlink");
@ -305,20 +307,21 @@ public class FileTreeIteratorTest extends RepositoryTestCase {
@Test @Test
public void testIsModifiedFileSmudged() throws Exception { public void testIsModifiedFileSmudged() throws Exception {
File f = writeTrashFile("file", "content"); File f = writeTrashFile("file", "content");
Git git = new Git(db); try (Git git = new Git(db)) {
// The idea of this test is to check the smudged handling // The idea of this test is to check the smudged handling
// Hopefully fsTick will make sure our entry gets smudged // Hopefully fsTick will make sure our entry gets smudged
fsTick(f); fsTick(f);
writeTrashFile("file", "content"); writeTrashFile("file", "content");
long lastModified = f.lastModified(); long lastModified = f.lastModified();
git.add().addFilepattern("file").call(); git.add().addFilepattern("file").call();
writeTrashFile("file", "conten2"); writeTrashFile("file", "conten2");
f.setLastModified(lastModified); f.setLastModified(lastModified);
// We cannot trust this to go fast enough on // We cannot trust this to go fast enough on
// a system with less than one-second lastModified // a system with less than one-second lastModified
// resolution, so we force the index to have the // resolution, so we force the index to have the
// same timestamp as the file we look at. // same timestamp as the file we look at.
db.getIndexFile().setLastModified(lastModified); db.getIndexFile().setLastModified(lastModified);
}
DirCacheEntry dce = db.readDirCache().getEntry("file"); DirCacheEntry dce = db.readDirCache().getEntry("file");
FileTreeIterator fti = new FileTreeIterator(trash, db.getFS(), db FileTreeIterator fti = new FileTreeIterator(trash, db.getFS(), db
.getConfig().get(WorkingTreeOptions.KEY)); .getConfig().get(WorkingTreeOptions.KEY));
@ -334,198 +337,204 @@ public class FileTreeIteratorTest extends RepositoryTestCase {
@Test @Test
public void submoduleHeadMatchesIndex() throws Exception { public void submoduleHeadMatchesIndex() throws Exception {
Git git = new Git(db); try (Git git = new Git(db);
writeTrashFile("file.txt", "content"); TreeWalk walk = new TreeWalk(db)) {
git.add().addFilepattern("file.txt").call(); writeTrashFile("file.txt", "content");
final RevCommit id = git.commit().setMessage("create file").call(); git.add().addFilepattern("file.txt").call();
final String path = "sub"; final RevCommit id = git.commit().setMessage("create file").call();
DirCache cache = db.lockDirCache(); final String path = "sub";
DirCacheEditor editor = cache.editor(); DirCache cache = db.lockDirCache();
editor.add(new PathEdit(path) { DirCacheEditor editor = cache.editor();
editor.add(new PathEdit(path) {
public void apply(DirCacheEntry ent) {
ent.setFileMode(FileMode.GITLINK); public void apply(DirCacheEntry ent) {
ent.setObjectId(id); ent.setFileMode(FileMode.GITLINK);
} ent.setObjectId(id);
}); }
editor.commit(); });
editor.commit();
Git.cloneRepository().setURI(db.getDirectory().toURI().toString())
.setDirectory(new File(db.getWorkTree(), path)).call() Git.cloneRepository().setURI(db.getDirectory().toURI().toString())
.getRepository().close(); .setDirectory(new File(db.getWorkTree(), path)).call()
.getRepository().close();
TreeWalk walk = new TreeWalk(db);
DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache()); DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
FileTreeIterator workTreeIter = new FileTreeIterator(db); FileTreeIterator workTreeIter = new FileTreeIterator(db);
walk.addTree(indexIter); walk.addTree(indexIter);
walk.addTree(workTreeIter); walk.addTree(workTreeIter);
walk.setFilter(PathFilter.create(path)); walk.setFilter(PathFilter.create(path));
assertTrue(walk.next()); assertTrue(walk.next());
assertTrue(indexIter.idEqual(workTreeIter)); assertTrue(indexIter.idEqual(workTreeIter));
}
} }
@Test @Test
public void submoduleWithNoGitDirectory() throws Exception { public void submoduleWithNoGitDirectory() throws Exception {
Git git = new Git(db); try (Git git = new Git(db);
writeTrashFile("file.txt", "content"); TreeWalk walk = new TreeWalk(db)) {
git.add().addFilepattern("file.txt").call(); writeTrashFile("file.txt", "content");
final RevCommit id = git.commit().setMessage("create file").call(); git.add().addFilepattern("file.txt").call();
final String path = "sub"; final RevCommit id = git.commit().setMessage("create file").call();
DirCache cache = db.lockDirCache(); final String path = "sub";
DirCacheEditor editor = cache.editor(); DirCache cache = db.lockDirCache();
editor.add(new PathEdit(path) { DirCacheEditor editor = cache.editor();
editor.add(new PathEdit(path) {
public void apply(DirCacheEntry ent) {
ent.setFileMode(FileMode.GITLINK); public void apply(DirCacheEntry ent) {
ent.setObjectId(id); ent.setFileMode(FileMode.GITLINK);
} ent.setObjectId(id);
}); }
editor.commit(); });
editor.commit();
File submoduleRoot = new File(db.getWorkTree(), path);
assertTrue(submoduleRoot.mkdir()); File submoduleRoot = new File(db.getWorkTree(), path);
assertTrue(new File(submoduleRoot, Constants.DOT_GIT).mkdir()); assertTrue(submoduleRoot.mkdir());
assertTrue(new File(submoduleRoot, Constants.DOT_GIT).mkdir());
TreeWalk walk = new TreeWalk(db);
DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache()); DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
FileTreeIterator workTreeIter = new FileTreeIterator(db); FileTreeIterator workTreeIter = new FileTreeIterator(db);
walk.addTree(indexIter); walk.addTree(indexIter);
walk.addTree(workTreeIter); walk.addTree(workTreeIter);
walk.setFilter(PathFilter.create(path)); walk.setFilter(PathFilter.create(path));
assertTrue(walk.next()); assertTrue(walk.next());
assertFalse(indexIter.idEqual(workTreeIter)); assertFalse(indexIter.idEqual(workTreeIter));
assertEquals(ObjectId.zeroId(), workTreeIter.getEntryObjectId()); assertEquals(ObjectId.zeroId(), workTreeIter.getEntryObjectId());
}
} }
@Test @Test
public void submoduleWithNoHead() throws Exception { public void submoduleWithNoHead() throws Exception {
Git git = new Git(db); try (Git git = new Git(db);
writeTrashFile("file.txt", "content"); TreeWalk walk = new TreeWalk(db)) {
git.add().addFilepattern("file.txt").call(); writeTrashFile("file.txt", "content");
final RevCommit id = git.commit().setMessage("create file").call(); git.add().addFilepattern("file.txt").call();
final String path = "sub"; final RevCommit id = git.commit().setMessage("create file").call();
DirCache cache = db.lockDirCache(); final String path = "sub";
DirCacheEditor editor = cache.editor(); DirCache cache = db.lockDirCache();
editor.add(new PathEdit(path) { DirCacheEditor editor = cache.editor();
editor.add(new PathEdit(path) {
public void apply(DirCacheEntry ent) {
ent.setFileMode(FileMode.GITLINK); public void apply(DirCacheEntry ent) {
ent.setObjectId(id); ent.setFileMode(FileMode.GITLINK);
} ent.setObjectId(id);
}); }
editor.commit(); });
editor.commit();
assertNotNull(Git.init().setDirectory(new File(db.getWorkTree(), path))
.call().getRepository()); assertNotNull(Git.init().setDirectory(new File(db.getWorkTree(), path))
.call().getRepository());
TreeWalk walk = new TreeWalk(db);
DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache()); DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
FileTreeIterator workTreeIter = new FileTreeIterator(db); FileTreeIterator workTreeIter = new FileTreeIterator(db);
walk.addTree(indexIter); walk.addTree(indexIter);
walk.addTree(workTreeIter); walk.addTree(workTreeIter);
walk.setFilter(PathFilter.create(path)); walk.setFilter(PathFilter.create(path));
assertTrue(walk.next()); assertTrue(walk.next());
assertFalse(indexIter.idEqual(workTreeIter)); assertFalse(indexIter.idEqual(workTreeIter));
assertEquals(ObjectId.zeroId(), workTreeIter.getEntryObjectId()); assertEquals(ObjectId.zeroId(), workTreeIter.getEntryObjectId());
}
} }
@Test @Test
public void submoduleDirectoryIterator() throws Exception { public void submoduleDirectoryIterator() throws Exception {
Git git = new Git(db); try (Git git = new Git(db);
writeTrashFile("file.txt", "content"); TreeWalk walk = new TreeWalk(db)) {
git.add().addFilepattern("file.txt").call(); writeTrashFile("file.txt", "content");
final RevCommit id = git.commit().setMessage("create file").call(); git.add().addFilepattern("file.txt").call();
final String path = "sub"; final RevCommit id = git.commit().setMessage("create file").call();
DirCache cache = db.lockDirCache(); final String path = "sub";
DirCacheEditor editor = cache.editor(); DirCache cache = db.lockDirCache();
editor.add(new PathEdit(path) { DirCacheEditor editor = cache.editor();
editor.add(new PathEdit(path) {
public void apply(DirCacheEntry ent) {
ent.setFileMode(FileMode.GITLINK); public void apply(DirCacheEntry ent) {
ent.setObjectId(id); ent.setFileMode(FileMode.GITLINK);
} ent.setObjectId(id);
}); }
editor.commit(); });
editor.commit();
Git.cloneRepository().setURI(db.getDirectory().toURI().toString())
.setDirectory(new File(db.getWorkTree(), path)).call() Git.cloneRepository().setURI(db.getDirectory().toURI().toString())
.getRepository().close(); .setDirectory(new File(db.getWorkTree(), path)).call()
.getRepository().close();
TreeWalk walk = new TreeWalk(db);
DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache()); DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
FileTreeIterator workTreeIter = new FileTreeIterator(db.getWorkTree(), FileTreeIterator workTreeIter = new FileTreeIterator(db.getWorkTree(),
db.getFS(), db.getConfig().get(WorkingTreeOptions.KEY)); db.getFS(), db.getConfig().get(WorkingTreeOptions.KEY));
walk.addTree(indexIter); walk.addTree(indexIter);
walk.addTree(workTreeIter); walk.addTree(workTreeIter);
walk.setFilter(PathFilter.create(path)); walk.setFilter(PathFilter.create(path));
assertTrue(walk.next()); assertTrue(walk.next());
assertTrue(indexIter.idEqual(workTreeIter)); assertTrue(indexIter.idEqual(workTreeIter));
}
} }
@Test @Test
public void submoduleNestedWithHeadMatchingIndex() throws Exception { public void submoduleNestedWithHeadMatchingIndex() throws Exception {
Git git = new Git(db); try (Git git = new Git(db);
writeTrashFile("file.txt", "content"); TreeWalk walk = new TreeWalk(db)) {
git.add().addFilepattern("file.txt").call(); writeTrashFile("file.txt", "content");
final RevCommit id = git.commit().setMessage("create file").call(); git.add().addFilepattern("file.txt").call();
final String path = "sub/dir1/dir2"; final RevCommit id = git.commit().setMessage("create file").call();
DirCache cache = db.lockDirCache(); final String path = "sub/dir1/dir2";
DirCacheEditor editor = cache.editor(); DirCache cache = db.lockDirCache();
editor.add(new PathEdit(path) { DirCacheEditor editor = cache.editor();
editor.add(new PathEdit(path) {
public void apply(DirCacheEntry ent) {
ent.setFileMode(FileMode.GITLINK); public void apply(DirCacheEntry ent) {
ent.setObjectId(id); ent.setFileMode(FileMode.GITLINK);
} ent.setObjectId(id);
}); }
editor.commit(); });
editor.commit();
Git.cloneRepository().setURI(db.getDirectory().toURI().toString())
.setDirectory(new File(db.getWorkTree(), path)).call() Git.cloneRepository().setURI(db.getDirectory().toURI().toString())
.getRepository().close(); .setDirectory(new File(db.getWorkTree(), path)).call()
.getRepository().close();
TreeWalk walk = new TreeWalk(db);
DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache()); DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
FileTreeIterator workTreeIter = new FileTreeIterator(db); FileTreeIterator workTreeIter = new FileTreeIterator(db);
walk.addTree(indexIter); walk.addTree(indexIter);
walk.addTree(workTreeIter); walk.addTree(workTreeIter);
walk.setFilter(PathFilter.create(path)); walk.setFilter(PathFilter.create(path));
assertTrue(walk.next()); assertTrue(walk.next());
assertTrue(indexIter.idEqual(workTreeIter)); assertTrue(indexIter.idEqual(workTreeIter));
}
} }
@Test @Test
public void idOffset() throws Exception { public void idOffset() throws Exception {
Git git = new Git(db); try (Git git = new Git(db);
writeTrashFile("fileAinfsonly", "A"); TreeWalk tw = new TreeWalk(db)) {
File fileBinindex = writeTrashFile("fileBinindex", "B"); writeTrashFile("fileAinfsonly", "A");
fsTick(fileBinindex); File fileBinindex = writeTrashFile("fileBinindex", "B");
git.add().addFilepattern("fileBinindex").call(); fsTick(fileBinindex);
writeTrashFile("fileCinfsonly", "C"); git.add().addFilepattern("fileBinindex").call();
TreeWalk tw = new TreeWalk(db); writeTrashFile("fileCinfsonly", "C");
DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache()); DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
FileTreeIterator workTreeIter = new FileTreeIterator(db); FileTreeIterator workTreeIter = new FileTreeIterator(db);
tw.addTree(indexIter); tw.addTree(indexIter);
tw.addTree(workTreeIter); tw.addTree(workTreeIter);
workTreeIter.setDirCacheIterator(tw, 0); workTreeIter.setDirCacheIterator(tw, 0);
assertEntry("d46c305e85b630558ee19cc47e73d2e5c8c64cdc", "a,", tw); assertEntry("d46c305e85b630558ee19cc47e73d2e5c8c64cdc", "a,", tw);
assertEntry("58ee403f98538ec02409538b3f80adf610accdec", "a,b", tw); assertEntry("58ee403f98538ec02409538b3f80adf610accdec", "a,b", tw);
assertEntry("0000000000000000000000000000000000000000", "a", tw); assertEntry("0000000000000000000000000000000000000000", "a", tw);
assertEntry("b8d30ff397626f0f1d3538d66067edf865e201d6", "a0b", tw); assertEntry("b8d30ff397626f0f1d3538d66067edf865e201d6", "a0b", tw);
// The reason for adding this test. Check that the id is correct for // The reason for adding this test. Check that the id is correct for
// mixed // mixed
assertEntry("8c7e5a667f1b771847fe88c01c3de34413a1b220", assertEntry("8c7e5a667f1b771847fe88c01c3de34413a1b220",
"fileAinfsonly", tw); "fileAinfsonly", tw);
assertEntry("7371f47a6f8bd23a8fa1a8b2a9479cdd76380e54", "fileBinindex", assertEntry("7371f47a6f8bd23a8fa1a8b2a9479cdd76380e54", "fileBinindex",
tw); tw);
assertEntry("96d80cd6c4e7158dbebd0849f4fb7ce513e5828c", assertEntry("96d80cd6c4e7158dbebd0849f4fb7ce513e5828c",
"fileCinfsonly", tw); "fileCinfsonly", tw);
assertFalse(tw.next()); assertFalse(tw.next());
}
} }
private static void assertEntry(String sha1string, String path, TreeWalk tw) private static void assertEntry(String sha1string, String path, TreeWalk tw)

Loading…
Cancel
Save