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
public void commitNewSubmodule() throws Exception {
Git git = new Git(db);
writeTrashFile("file.txt", "content");
git.add().addFilepattern("file.txt").call();
RevCommit commit = git.commit().setMessage("create file").call();
SubmoduleAddCommand command = new SubmoduleAddCommand(db);
String path = "sub";
command.setPath(path);
String uri = db.getDirectory().toURI().toString();
command.setURI(uri);
Repository repo = command.call();
assertNotNull(repo);
addRepoToClose(repo);
SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
assertTrue(generator.next());
assertEquals(path, generator.getPath());
assertEquals(commit, generator.getObjectId());
assertEquals(uri, generator.getModulesUrl());
assertEquals(path, generator.getModulesPath());
assertEquals(uri, generator.getConfigUrl());
Repository subModRepo = generator.getRepository();
assertNotNull(subModRepo);
subModRepo.close();
assertEquals(commit, repo.resolve(Constants.HEAD));
RevCommit submoduleCommit = git.commit().setMessage("submodule add")
.setOnly(path).call();
assertNotNull(submoduleCommit);
TreeWalk walk = new TreeWalk(db);
walk.addTree(commit.getTree());
walk.addTree(submoduleCommit.getTree());
walk.setFilter(TreeFilter.ANY_DIFF);
List<DiffEntry> diffs = DiffEntry.scan(walk);
assertEquals(1, diffs.size());
DiffEntry subDiff = diffs.get(0);
assertEquals(FileMode.MISSING, subDiff.getOldMode());
assertEquals(FileMode.GITLINK, subDiff.getNewMode());
assertEquals(ObjectId.zeroId(), subDiff.getOldId().toObjectId());
assertEquals(commit, subDiff.getNewId().toObjectId());
assertEquals(path, subDiff.getNewPath());
try (Git git = new Git(db)) {
writeTrashFile("file.txt", "content");
git.add().addFilepattern("file.txt").call();
RevCommit commit = git.commit().setMessage("create file").call();
SubmoduleAddCommand command = new SubmoduleAddCommand(db);
String path = "sub";
command.setPath(path);
String uri = db.getDirectory().toURI().toString();
command.setURI(uri);
Repository repo = command.call();
assertNotNull(repo);
addRepoToClose(repo);
SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
assertTrue(generator.next());
assertEquals(path, generator.getPath());
assertEquals(commit, generator.getObjectId());
assertEquals(uri, generator.getModulesUrl());
assertEquals(path, generator.getModulesPath());
assertEquals(uri, generator.getConfigUrl());
Repository subModRepo = generator.getRepository();
assertNotNull(subModRepo);
subModRepo.close();
assertEquals(commit, repo.resolve(Constants.HEAD));
RevCommit submoduleCommit = git.commit().setMessage("submodule add")
.setOnly(path).call();
assertNotNull(submoduleCommit);
try (TreeWalk walk = new TreeWalk(db)) {
walk.addTree(commit.getTree());
walk.addTree(submoduleCommit.getTree());
walk.setFilter(TreeFilter.ANY_DIFF);
List<DiffEntry> diffs = DiffEntry.scan(walk);
assertEquals(1, diffs.size());
DiffEntry subDiff = diffs.get(0);
assertEquals(FileMode.MISSING, subDiff.getOldMode());
assertEquals(FileMode.GITLINK, subDiff.getNewMode());
assertEquals(ObjectId.zeroId(), subDiff.getOldId().toObjectId());
assertEquals(commit, subDiff.getNewId().toObjectId());
assertEquals(path, subDiff.getNewPath());
}
}
}
@Test
public void commitSubmoduleUpdate() throws Exception {
Git git = new Git(db);
writeTrashFile("file.txt", "content");
git.add().addFilepattern("file.txt").call();
RevCommit commit = git.commit().setMessage("create file").call();
writeTrashFile("file.txt", "content2");
git.add().addFilepattern("file.txt").call();
RevCommit commit2 = git.commit().setMessage("edit file").call();
SubmoduleAddCommand command = new SubmoduleAddCommand(db);
String path = "sub";
command.setPath(path);
String uri = db.getDirectory().toURI().toString();
command.setURI(uri);
Repository repo = command.call();
assertNotNull(repo);
addRepoToClose(repo);
SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
assertTrue(generator.next());
assertEquals(path, generator.getPath());
assertEquals(commit2, generator.getObjectId());
assertEquals(uri, generator.getModulesUrl());
assertEquals(path, generator.getModulesPath());
assertEquals(uri, generator.getConfigUrl());
Repository subModRepo = generator.getRepository();
assertNotNull(subModRepo);
subModRepo.close();
assertEquals(commit2, repo.resolve(Constants.HEAD));
RevCommit submoduleAddCommit = git.commit().setMessage("submodule add")
.setOnly(path).call();
assertNotNull(submoduleAddCommit);
RefUpdate update = repo.updateRef(Constants.HEAD);
update.setNewObjectId(commit);
assertEquals(Result.FORCED, update.forceUpdate());
RevCommit submoduleEditCommit = git.commit()
.setMessage("submodule add").setOnly(path).call();
assertNotNull(submoduleEditCommit);
TreeWalk walk = new TreeWalk(db);
walk.addTree(submoduleAddCommit.getTree());
walk.addTree(submoduleEditCommit.getTree());
walk.setFilter(TreeFilter.ANY_DIFF);
List<DiffEntry> diffs = DiffEntry.scan(walk);
assertEquals(1, diffs.size());
DiffEntry subDiff = diffs.get(0);
assertEquals(FileMode.GITLINK, subDiff.getOldMode());
assertEquals(FileMode.GITLINK, subDiff.getNewMode());
assertEquals(commit2, subDiff.getOldId().toObjectId());
assertEquals(commit, subDiff.getNewId().toObjectId());
assertEquals(path, subDiff.getNewPath());
assertEquals(path, subDiff.getOldPath());
try (Git git = new Git(db)) {
writeTrashFile("file.txt", "content");
git.add().addFilepattern("file.txt").call();
RevCommit commit = git.commit().setMessage("create file").call();
writeTrashFile("file.txt", "content2");
git.add().addFilepattern("file.txt").call();
RevCommit commit2 = git.commit().setMessage("edit file").call();
SubmoduleAddCommand command = new SubmoduleAddCommand(db);
String path = "sub";
command.setPath(path);
String uri = db.getDirectory().toURI().toString();
command.setURI(uri);
Repository repo = command.call();
assertNotNull(repo);
addRepoToClose(repo);
SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
assertTrue(generator.next());
assertEquals(path, generator.getPath());
assertEquals(commit2, generator.getObjectId());
assertEquals(uri, generator.getModulesUrl());
assertEquals(path, generator.getModulesPath());
assertEquals(uri, generator.getConfigUrl());
Repository subModRepo = generator.getRepository();
assertNotNull(subModRepo);
subModRepo.close();
assertEquals(commit2, repo.resolve(Constants.HEAD));
RevCommit submoduleAddCommit = git.commit().setMessage("submodule add")
.setOnly(path).call();
assertNotNull(submoduleAddCommit);
RefUpdate update = repo.updateRef(Constants.HEAD);
update.setNewObjectId(commit);
assertEquals(Result.FORCED, update.forceUpdate());
RevCommit submoduleEditCommit = git.commit()
.setMessage("submodule add").setOnly(path).call();
assertNotNull(submoduleEditCommit);
try (TreeWalk walk = new TreeWalk(db)) {
walk.addTree(submoduleAddCommit.getTree());
walk.addTree(submoduleEditCommit.getTree());
walk.setFilter(TreeFilter.ANY_DIFF);
List<DiffEntry> diffs = DiffEntry.scan(walk);
assertEquals(1, diffs.size());
DiffEntry subDiff = diffs.get(0);
assertEquals(FileMode.GITLINK, subDiff.getOldMode());
assertEquals(FileMode.GITLINK, subDiff.getNewMode());
assertEquals(commit2, subDiff.getOldId().toObjectId());
assertEquals(commit, subDiff.getNewId().toObjectId());
assertEquals(path, subDiff.getNewPath());
assertEquals(path, subDiff.getOldPath());
}
}
}
@Test
public void commitUpdatesSmudgedEntries() throws Exception {
Git git = new Git(db);
File file1 = writeTrashFile("file1.txt", "content1");
assertTrue(file1.setLastModified(file1.lastModified() - 5000));
File file2 = writeTrashFile("file2.txt", "content2");
assertTrue(file2.setLastModified(file2.lastModified() - 5000));
File file3 = writeTrashFile("file3.txt", "content3");
assertTrue(file3.setLastModified(file3.lastModified() - 5000));
assertNotNull(git.add().addFilepattern("file1.txt")
.addFilepattern("file2.txt").addFilepattern("file3.txt").call());
RevCommit commit = git.commit().setMessage("add files").call();
assertNotNull(commit);
DirCache cache = DirCache.read(db.getIndexFile(), db.getFS());
int file1Size = cache.getEntry("file1.txt").getLength();
int file2Size = cache.getEntry("file2.txt").getLength();
int file3Size = cache.getEntry("file3.txt").getLength();
ObjectId file2Id = cache.getEntry("file2.txt").getObjectId();
ObjectId file3Id = cache.getEntry("file3.txt").getObjectId();
assertTrue(file1Size > 0);
assertTrue(file2Size > 0);
assertTrue(file3Size > 0);
// Smudge entries
cache = DirCache.lock(db.getIndexFile(), db.getFS());
cache.getEntry("file1.txt").setLength(0);
cache.getEntry("file2.txt").setLength(0);
cache.getEntry("file3.txt").setLength(0);
cache.write();
assertTrue(cache.commit());
// Verify entries smudged
cache = DirCache.read(db.getIndexFile(), db.getFS());
assertEquals(0, cache.getEntry("file1.txt").getLength());
assertEquals(0, cache.getEntry("file2.txt").getLength());
assertEquals(0, cache.getEntry("file3.txt").getLength());
long indexTime = db.getIndexFile().lastModified();
db.getIndexFile().setLastModified(indexTime - 5000);
write(file1, "content4");
assertTrue(file1.setLastModified(file1.lastModified() + 2500));
assertNotNull(git.commit().setMessage("edit file").setOnly("file1.txt")
.call());
cache = db.readDirCache();
assertEquals(file1Size, cache.getEntry("file1.txt").getLength());
assertEquals(file2Size, cache.getEntry("file2.txt").getLength());
assertEquals(file3Size, cache.getEntry("file3.txt").getLength());
assertEquals(file2Id, cache.getEntry("file2.txt").getObjectId());
assertEquals(file3Id, cache.getEntry("file3.txt").getObjectId());
try (Git git = new Git(db)) {
File file1 = writeTrashFile("file1.txt", "content1");
assertTrue(file1.setLastModified(file1.lastModified() - 5000));
File file2 = writeTrashFile("file2.txt", "content2");
assertTrue(file2.setLastModified(file2.lastModified() - 5000));
File file3 = writeTrashFile("file3.txt", "content3");
assertTrue(file3.setLastModified(file3.lastModified() - 5000));
assertNotNull(git.add().addFilepattern("file1.txt")
.addFilepattern("file2.txt").addFilepattern("file3.txt").call());
RevCommit commit = git.commit().setMessage("add files").call();
assertNotNull(commit);
DirCache cache = DirCache.read(db.getIndexFile(), db.getFS());
int file1Size = cache.getEntry("file1.txt").getLength();
int file2Size = cache.getEntry("file2.txt").getLength();
int file3Size = cache.getEntry("file3.txt").getLength();
ObjectId file2Id = cache.getEntry("file2.txt").getObjectId();
ObjectId file3Id = cache.getEntry("file3.txt").getObjectId();
assertTrue(file1Size > 0);
assertTrue(file2Size > 0);
assertTrue(file3Size > 0);
// Smudge entries
cache = DirCache.lock(db.getIndexFile(), db.getFS());
cache.getEntry("file1.txt").setLength(0);
cache.getEntry("file2.txt").setLength(0);
cache.getEntry("file3.txt").setLength(0);
cache.write();
assertTrue(cache.commit());
// Verify entries smudged
cache = DirCache.read(db.getIndexFile(), db.getFS());
assertEquals(0, cache.getEntry("file1.txt").getLength());
assertEquals(0, cache.getEntry("file2.txt").getLength());
assertEquals(0, cache.getEntry("file3.txt").getLength());
long indexTime = db.getIndexFile().lastModified();
db.getIndexFile().setLastModified(indexTime - 5000);
write(file1, "content4");
assertTrue(file1.setLastModified(file1.lastModified() + 2500));
assertNotNull(git.commit().setMessage("edit file").setOnly("file1.txt")
.call());
cache = db.readDirCache();
assertEquals(file1Size, cache.getEntry("file1.txt").getLength());
assertEquals(file2Size, cache.getEntry("file2.txt").getLength());
assertEquals(file3Size, cache.getEntry("file3.txt").getLength());
assertEquals(file2Id, cache.getEntry("file2.txt").getObjectId());
assertEquals(file3Id, cache.getEntry("file3.txt").getObjectId());
}
}
@Test
public void commitIgnoresSmudgedEntryWithDifferentId() throws Exception {
Git git = new Git(db);
File file1 = writeTrashFile("file1.txt", "content1");
assertTrue(file1.setLastModified(file1.lastModified() - 5000));
File file2 = writeTrashFile("file2.txt", "content2");
assertTrue(file2.setLastModified(file2.lastModified() - 5000));
assertNotNull(git.add().addFilepattern("file1.txt")
.addFilepattern("file2.txt").call());
RevCommit commit = git.commit().setMessage("add files").call();
assertNotNull(commit);
DirCache cache = DirCache.read(db.getIndexFile(), db.getFS());
int file1Size = cache.getEntry("file1.txt").getLength();
int file2Size = cache.getEntry("file2.txt").getLength();
assertTrue(file1Size > 0);
assertTrue(file2Size > 0);
writeTrashFile("file2.txt", "content3");
assertNotNull(git.add().addFilepattern("file2.txt").call());
writeTrashFile("file2.txt", "content4");
// Smudge entries
cache = DirCache.lock(db.getIndexFile(), db.getFS());
cache.getEntry("file1.txt").setLength(0);
cache.getEntry("file2.txt").setLength(0);
cache.write();
assertTrue(cache.commit());
// Verify entries smudged
cache = db.readDirCache();
assertEquals(0, cache.getEntry("file1.txt").getLength());
assertEquals(0, cache.getEntry("file2.txt").getLength());
long indexTime = db.getIndexFile().lastModified();
db.getIndexFile().setLastModified(indexTime - 5000);
write(file1, "content5");
assertTrue(file1.setLastModified(file1.lastModified() + 1000));
assertNotNull(git.commit().setMessage("edit file").setOnly("file1.txt")
.call());
cache = db.readDirCache();
assertEquals(file1Size, cache.getEntry("file1.txt").getLength());
assertEquals(0, cache.getEntry("file2.txt").getLength());
try (Git git = new Git(db)) {
File file1 = writeTrashFile("file1.txt", "content1");
assertTrue(file1.setLastModified(file1.lastModified() - 5000));
File file2 = writeTrashFile("file2.txt", "content2");
assertTrue(file2.setLastModified(file2.lastModified() - 5000));
assertNotNull(git.add().addFilepattern("file1.txt")
.addFilepattern("file2.txt").call());
RevCommit commit = git.commit().setMessage("add files").call();
assertNotNull(commit);
DirCache cache = DirCache.read(db.getIndexFile(), db.getFS());
int file1Size = cache.getEntry("file1.txt").getLength();
int file2Size = cache.getEntry("file2.txt").getLength();
assertTrue(file1Size > 0);
assertTrue(file2Size > 0);
writeTrashFile("file2.txt", "content3");
assertNotNull(git.add().addFilepattern("file2.txt").call());
writeTrashFile("file2.txt", "content4");
// Smudge entries
cache = DirCache.lock(db.getIndexFile(), db.getFS());
cache.getEntry("file1.txt").setLength(0);
cache.getEntry("file2.txt").setLength(0);
cache.write();
assertTrue(cache.commit());
// Verify entries smudged
cache = db.readDirCache();
assertEquals(0, cache.getEntry("file1.txt").getLength());
assertEquals(0, cache.getEntry("file2.txt").getLength());
long indexTime = db.getIndexFile().lastModified();
db.getIndexFile().setLastModified(indexTime - 5000);
write(file1, "content5");
assertTrue(file1.setLastModified(file1.lastModified() + 1000));
assertNotNull(git.commit().setMessage("edit file").setOnly("file1.txt")
.call());
cache = db.readDirCache();
assertEquals(file1Size, cache.getEntry("file1.txt").getLength());
assertEquals(0, cache.getEntry("file2.txt").getLength());
}
}
@Test
public void commitAfterSquashMerge() throws Exception {
Git git = new Git(db);
writeTrashFile("file1", "file1");
git.add().addFilepattern("file1").call();
RevCommit first = git.commit().setMessage("initial commit").call();
try (Git git = new Git(db)) {
writeTrashFile("file1", "file1");
git.add().addFilepattern("file1").call();
RevCommit first = git.commit().setMessage("initial commit").call();
assertTrue(new File(db.getWorkTree(), "file1").exists());
createBranch(first, "refs/heads/branch1");
checkoutBranch("refs/heads/branch1");
assertTrue(new File(db.getWorkTree(), "file1").exists());
createBranch(first, "refs/heads/branch1");
checkoutBranch("refs/heads/branch1");
writeTrashFile("file2", "file2");
git.add().addFilepattern("file2").call();
git.commit().setMessage("second commit").call();
assertTrue(new File(db.getWorkTree(), "file2").exists());
writeTrashFile("file2", "file2");
git.add().addFilepattern("file2").call();
git.commit().setMessage("second commit").call();
assertTrue(new File(db.getWorkTree(), "file2").exists());
checkoutBranch("refs/heads/master");
checkoutBranch("refs/heads/master");
MergeResult result = git.merge()
.include(db.exactRef("refs/heads/branch1"))
.setSquash(true)
.call();
MergeResult result = git.merge()
.include(db.exactRef("refs/heads/branch1"))
.setSquash(true)
.call();
assertTrue(new File(db.getWorkTree(), "file1").exists());
assertTrue(new File(db.getWorkTree(), "file2").exists());
assertEquals(MergeResult.MergeStatus.FAST_FORWARD_SQUASHED,
result.getMergeStatus());
assertTrue(new File(db.getWorkTree(), "file1").exists());
assertTrue(new File(db.getWorkTree(), "file2").exists());
assertEquals(MergeResult.MergeStatus.FAST_FORWARD_SQUASHED,
result.getMergeStatus());
// comment not set, should be inferred from SQUASH_MSG
RevCommit squashedCommit = git.commit().call();
// comment not set, should be inferred from SQUASH_MSG
RevCommit squashedCommit = git.commit().call();
assertEquals(1, squashedCommit.getParentCount());
assertNull(db.readSquashCommitMsg());
assertEquals("commit: Squashed commit of the following:", db
.getReflogReader(Constants.HEAD).getLastEntry().getComment());
assertEquals("commit: Squashed commit of the following:", db
.getReflogReader(db.getBranch()).getLastEntry().getComment());
assertEquals(1, squashedCommit.getParentCount());
assertNull(db.readSquashCommitMsg());
assertEquals("commit: Squashed commit of the following:", db
.getReflogReader(Constants.HEAD).getLastEntry().getComment());
assertEquals("commit: Squashed commit of the following:", db
.getReflogReader(db.getBranch()).getLastEntry().getComment());
}
}
@Test(expected = WrongRepositoryStateException.class)
public void commitAmendOnInitialShouldFail() throws Exception {
Git git = new Git(db);
git.commit().setAmend(true).setMessage("initial commit").call();
try (Git git = new Git(db)) {
git.commit().setAmend(true).setMessage("initial commit").call();
}
}
@Test
public void commitAmendWithoutAuthorShouldSetOriginalAuthorAndAuthorTime()
throws Exception {
Git git = new Git(db);
writeTrashFile("file1", "file1");
git.add().addFilepattern("file1").call();
try (Git git = new Git(db)) {
writeTrashFile("file1", "file1");
git.add().addFilepattern("file1").call();
final String authorName = "First Author";
final String authorEmail = "author@example.org";
final Date authorDate = new Date(1349621117000L);
PersonIdent firstAuthor = new PersonIdent(authorName, authorEmail,
authorDate, TimeZone.getTimeZone("UTC"));
git.commit().setMessage("initial commit").setAuthor(firstAuthor).call();
final String authorName = "First Author";
final String authorEmail = "author@example.org";
final Date authorDate = new Date(1349621117000L);
PersonIdent firstAuthor = new PersonIdent(authorName, authorEmail,
authorDate, TimeZone.getTimeZone("UTC"));
git.commit().setMessage("initial commit").setAuthor(firstAuthor).call();
RevCommit amended = git.commit().setAmend(true)
.setMessage("amend commit").call();
RevCommit amended = git.commit().setAmend(true)
.setMessage("amend commit").call();
PersonIdent amendedAuthor = amended.getAuthorIdent();
assertEquals(authorName, amendedAuthor.getName());
assertEquals(authorEmail, amendedAuthor.getEmailAddress());
assertEquals(authorDate.getTime(), amendedAuthor.getWhen().getTime());
PersonIdent amendedAuthor = amended.getAuthorIdent();
assertEquals(authorName, amendedAuthor.getName());
assertEquals(authorEmail, amendedAuthor.getEmailAddress());
assertEquals(authorDate.getTime(), amendedAuthor.getWhen().getTime());
}
}
@Test
public void commitAmendWithAuthorShouldUseIt() throws Exception {
Git git = new Git(db);
writeTrashFile("file1", "file1");
git.add().addFilepattern("file1").call();
git.commit().setMessage("initial commit").call();
try (Git git = new Git(db)) {
writeTrashFile("file1", "file1");
git.add().addFilepattern("file1").call();
git.commit().setMessage("initial commit").call();
RevCommit amended = git.commit().setAmend(true)
.setAuthor("New Author", "newauthor@example.org")
.setMessage("amend commit").call();
RevCommit amended = git.commit().setAmend(true)
.setAuthor("New Author", "newauthor@example.org")
.setMessage("amend commit").call();
PersonIdent amendedAuthor = amended.getAuthorIdent();
assertEquals("New Author", amendedAuthor.getName());
assertEquals("newauthor@example.org", amendedAuthor.getEmailAddress());
PersonIdent amendedAuthor = amended.getAuthorIdent();
assertEquals("New Author", amendedAuthor.getName());
assertEquals("newauthor@example.org", amendedAuthor.getEmailAddress());
}
}
@Test
@ -532,18 +537,19 @@ public class CommitCommandTest extends RepositoryTestCase {
+ "[unmerged2, mode:100644, stage:3]",
indexState(0));
Git git = new Git(db);
RevCommit commit = git.commit().setOnly("unmerged1")
.setMessage("Only one file").call();
try (Git git = new Git(db)) {
RevCommit commit = git.commit().setOnly("unmerged1")
.setMessage("Only one file").call();
assertEquals("[other, mode:100644]" + "[unmerged1, mode:100644]"
+ "[unmerged2, mode:100644, stage:1]"
+ "[unmerged2, mode:100644, stage:2]"
+ "[unmerged2, mode:100644, stage:3]",
indexState(0));
assertEquals("[other, mode:100644]" + "[unmerged1, mode:100644]"
+ "[unmerged2, mode:100644, stage:1]"
+ "[unmerged2, mode:100644, stage:2]"
+ "[unmerged2, mode:100644, stage:3]",
indexState(0));
try (TreeWalk walk = TreeWalk.forPath(db, "unmerged1", commit.getTree())) {
assertEquals(FileMode.REGULAR_FILE, walk.getFileMode(0));
try (TreeWalk walk = TreeWalk.forPath(db, "unmerged1", commit.getTree())) {
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");
folder.mkdir();
write(new File(folder, "folder.txt"), "folder");
Git git = new Git(db);
git.add().addFilepattern(".").call();
git.commit().setMessage("Initial commit").call();
write(new File(folder, "folder.txt"), "folder change");
OutputStream out = new ByteArrayOutputStream();
List<DiffEntry> entries = git.diff().setOutputStream(out).call();
assertEquals(1, entries.size());
assertEquals(ChangeType.MODIFY, entries.get(0)
.getChangeType());
assertEquals("folder/folder.txt", entries.get(0)
.getOldPath());
assertEquals("folder/folder.txt", entries.get(0)
.getNewPath());
String actual = out.toString();
String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n"
+ "index 0119635..95c4c65 100644\n"
+ "--- a/folder/folder.txt\n"
+ "+++ b/folder/folder.txt\n"
+ "@@ -1 +1 @@\n"
+ "-folder\n"
+ "\\ No newline at end of file\n"
+ "+folder change\n"
+ "\\ No newline at end of file\n";
assertEquals(expected.toString(), actual);
try (Git git = new Git(db)) {
git.add().addFilepattern(".").call();
git.commit().setMessage("Initial commit").call();
write(new File(folder, "folder.txt"), "folder change");
OutputStream out = new ByteArrayOutputStream();
List<DiffEntry> entries = git.diff().setOutputStream(out).call();
assertEquals(1, entries.size());
assertEquals(ChangeType.MODIFY, entries.get(0)
.getChangeType());
assertEquals("folder/folder.txt", entries.get(0)
.getOldPath());
assertEquals("folder/folder.txt", entries.get(0)
.getNewPath());
String actual = out.toString();
String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n"
+ "index 0119635..95c4c65 100644\n"
+ "--- a/folder/folder.txt\n"
+ "+++ b/folder/folder.txt\n"
+ "@@ -1 +1 @@\n"
+ "-folder\n"
+ "\\ No newline at end of file\n"
+ "+folder change\n"
+ "\\ No newline at end of file\n";
assertEquals(expected.toString(), actual);
}
}
@Test
@ -103,33 +104,34 @@ public class DiffCommandTest extends RepositoryTestCase {
write(new File(db.getWorkTree(), "test.txt"), "test");
File folder = new File(db.getWorkTree(), "folder");
folder.mkdir();
Git git = new Git(db);
git.add().addFilepattern(".").call();
git.commit().setMessage("Initial commit").call();
write(new File(folder, "folder.txt"), "folder");
git.add().addFilepattern(".").call();
OutputStream out = new ByteArrayOutputStream();
List<DiffEntry> entries = git.diff().setOutputStream(out)
.setCached(true).call();
assertEquals(1, entries.size());
assertEquals(ChangeType.ADD, entries.get(0)
.getChangeType());
assertEquals("/dev/null", entries.get(0)
.getOldPath());
assertEquals("folder/folder.txt", entries.get(0)
.getNewPath());
String actual = out.toString();
String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n"
+ "new file mode 100644\n"
+ "index 0000000..0119635\n"
+ "--- /dev/null\n"
+ "+++ b/folder/folder.txt\n"
+ "@@ -0,0 +1 @@\n"
+ "+folder\n"
+ "\\ No newline at end of file\n";
assertEquals(expected.toString(), actual);
try (Git git = new Git(db)) {
git.add().addFilepattern(".").call();
git.commit().setMessage("Initial commit").call();
write(new File(folder, "folder.txt"), "folder");
git.add().addFilepattern(".").call();
OutputStream out = new ByteArrayOutputStream();
List<DiffEntry> entries = git.diff().setOutputStream(out)
.setCached(true).call();
assertEquals(1, entries.size());
assertEquals(ChangeType.ADD, entries.get(0)
.getChangeType());
assertEquals("/dev/null", entries.get(0)
.getOldPath());
assertEquals("folder/folder.txt", entries.get(0)
.getNewPath());
String actual = out.toString();
String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n"
+ "new file mode 100644\n"
+ "index 0000000..0119635\n"
+ "--- /dev/null\n"
+ "+++ b/folder/folder.txt\n"
+ "@@ -0,0 +1 @@\n"
+ "+folder\n"
+ "\\ No newline at end of file\n";
assertEquals(expected.toString(), actual);
}
}
@Test
@ -138,107 +140,109 @@ public class DiffCommandTest extends RepositoryTestCase {
File folder = new File(db.getWorkTree(), "folder");
folder.mkdir();
write(new File(folder, "folder.txt"), "folder");
Git git = new Git(db);
git.add().addFilepattern(".").call();
git.commit().setMessage("Initial commit").call();
write(new File(folder, "folder.txt"), "folder change");
git.add().addFilepattern(".").call();
git.commit().setMessage("second commit").call();
write(new File(folder, "folder.txt"), "second folder change");
git.add().addFilepattern(".").call();
git.commit().setMessage("third commit").call();
// bad filter
DiffCommand diff = git.diff().setShowNameAndStatusOnly(true)
.setPathFilter(PathFilter.create("test.txt"))
.setOldTree(getTreeIterator("HEAD^^"))
.setNewTree(getTreeIterator("HEAD^"));
List<DiffEntry> entries = diff.call();
assertEquals(0, entries.size());
// no filter, two commits
OutputStream out = new ByteArrayOutputStream();
diff = git.diff().setOutputStream(out)
.setOldTree(getTreeIterator("HEAD^^"))
.setNewTree(getTreeIterator("HEAD^"));
entries = diff.call();
assertEquals(1, entries.size());
assertEquals(ChangeType.MODIFY, entries.get(0).getChangeType());
assertEquals("folder/folder.txt", entries.get(0).getOldPath());
assertEquals("folder/folder.txt", entries.get(0).getNewPath());
String actual = out.toString();
String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n"
+ "index 0119635..95c4c65 100644\n"
+ "--- a/folder/folder.txt\n"
+ "+++ b/folder/folder.txt\n"
+ "@@ -1 +1 @@\n"
+ "-folder\n"
+ "\\ No newline at end of file\n"
+ "+folder change\n"
+ "\\ No newline at end of file\n";
assertEquals(expected.toString(), actual);
try (Git git = new Git(db)) {
git.add().addFilepattern(".").call();
git.commit().setMessage("Initial commit").call();
write(new File(folder, "folder.txt"), "folder change");
git.add().addFilepattern(".").call();
git.commit().setMessage("second commit").call();
write(new File(folder, "folder.txt"), "second folder change");
git.add().addFilepattern(".").call();
git.commit().setMessage("third commit").call();
// bad filter
DiffCommand diff = git.diff().setShowNameAndStatusOnly(true)
.setPathFilter(PathFilter.create("test.txt"))
.setOldTree(getTreeIterator("HEAD^^"))
.setNewTree(getTreeIterator("HEAD^"));
List<DiffEntry> entries = diff.call();
assertEquals(0, entries.size());
// no filter, two commits
OutputStream out = new ByteArrayOutputStream();
diff = git.diff().setOutputStream(out)
.setOldTree(getTreeIterator("HEAD^^"))
.setNewTree(getTreeIterator("HEAD^"));
entries = diff.call();
assertEquals(1, entries.size());
assertEquals(ChangeType.MODIFY, entries.get(0).getChangeType());
assertEquals("folder/folder.txt", entries.get(0).getOldPath());
assertEquals("folder/folder.txt", entries.get(0).getNewPath());
String actual = out.toString();
String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n"
+ "index 0119635..95c4c65 100644\n"
+ "--- a/folder/folder.txt\n"
+ "+++ b/folder/folder.txt\n"
+ "@@ -1 +1 @@\n"
+ "-folder\n"
+ "\\ No newline at end of file\n"
+ "+folder change\n"
+ "\\ No newline at end of file\n";
assertEquals(expected.toString(), actual);
}
}
@Test
public void testDiffWithPrefixes() throws Exception {
write(new File(db.getWorkTree(), "test.txt"), "test");
Git git = new Git(db);
git.add().addFilepattern(".").call();
git.commit().setMessage("Initial commit").call();
write(new File(db.getWorkTree(), "test.txt"), "test change");
OutputStream out = new ByteArrayOutputStream();
git.diff().setOutputStream(out).setSourcePrefix("old/")
.setDestinationPrefix("new/")
.call();
String actual = out.toString();
String expected = "diff --git old/test.txt new/test.txt\n"
+ "index 30d74d2..4dba797 100644\n" + "--- old/test.txt\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";
assertEquals(expected.toString(), actual);
try (Git git = new Git(db)) {
git.add().addFilepattern(".").call();
git.commit().setMessage("Initial commit").call();
write(new File(db.getWorkTree(), "test.txt"), "test change");
OutputStream out = new ByteArrayOutputStream();
git.diff().setOutputStream(out).setSourcePrefix("old/")
.setDestinationPrefix("new/").call();
String actual = out.toString();
String expected = "diff --git old/test.txt new/test.txt\n"
+ "index 30d74d2..4dba797 100644\n" + "--- old/test.txt\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";
assertEquals(expected.toString(), actual);
}
}
@Test
public void testDiffWithNegativeLineCount() throws Exception {
write(new File(db.getWorkTree(), "test.txt"),
"0\n1\n2\n3\n4\n5\n6\n7\n8\n9");
Git git = new Git(db);
git.add().addFilepattern(".").call();
git.commit().setMessage("Initial commit").call();
write(new File(db.getWorkTree(), "test.txt"),
"0\n1\n2\n3\n4a\n5\n6\n7\n8\n9");
OutputStream out = new ByteArrayOutputStream();
git.diff().setOutputStream(out).setContextLines(1)
.call();
String actual = out.toString();
String expected = "diff --git a/test.txt b/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"
+ "+4a\n" + " 5\n";
assertEquals(expected.toString(), actual);
try (Git git = new Git(db)) {
git.add().addFilepattern(".").call();
git.commit().setMessage("Initial commit").call();
write(new File(db.getWorkTree(), "test.txt"),
"0\n1\n2\n3\n4a\n5\n6\n7\n8\n9");
OutputStream out = new ByteArrayOutputStream();
git.diff().setOutputStream(out).setContextLines(1).call();
String actual = out.toString();
String expected = "diff --git a/test.txt b/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"
+ "+4a\n" + " 5\n";
assertEquals(expected.toString(), actual);
}
}
@Test
public void testNoOutputStreamSet() throws Exception {
File file = writeTrashFile("test.txt", "a");
assertTrue(file.setLastModified(file.lastModified() - 5000));
Git git = new Git(db);
git.add().addFilepattern(".").call();
write(file, "b");
List<DiffEntry> diffs = git.diff().call();
assertNotNull(diffs);
assertEquals(1, diffs.size());
DiffEntry diff = diffs.get(0);
assertEquals(ChangeType.MODIFY, diff.getChangeType());
assertEquals("test.txt", diff.getOldPath());
assertEquals("test.txt", diff.getNewPath());
try (Git git = new Git(db)) {
git.add().addFilepattern(".").call();
write(file, "b");
List<DiffEntry> diffs = git.diff().call();
assertNotNull(diffs);
assertEquals(1, diffs.size());
DiffEntry diff = diffs.get(0);
assertEquals(ChangeType.MODIFY, diff.getChangeType());
assertEquals("test.txt", diff.getOldPath());
assertEquals("test.txt", diff.getNewPath());
}
}
private AbstractTreeIterator getTreeIterator(String name)
@ -247,8 +251,9 @@ public class DiffCommandTest extends RepositoryTestCase {
if (id == null)
throw new IllegalArgumentException(name);
final CanonicalTreeParser p = new CanonicalTreeParser();
try (ObjectReader or = db.newObjectReader()) {
p.reset(or, new RevWalk(db).parseTree(id));
try (ObjectReader or = db.newObjectReader();
RevWalk rw = new RevWalk(db)) {
p.reset(or, rw.parseTree(id));
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();
assertEquals(targetCommit.getId(), mergedCommits[0]);
assertEquals(sourceCommit.getId(), mergedCommits[1]);
RevCommit mergeCommit = new RevWalk(dbTarget).parseCommit(mergeResult
.getNewHead());
String message = "Merge branch 'master' of "
+ db.getWorkTree().getAbsolutePath();
assertEquals(message, mergeCommit.getShortMessage());
try (RevWalk rw = new RevWalk(dbTarget)) {
RevCommit mergeCommit = rw.parseCommit(mergeResult.getNewHead());
String message = "Merge branch 'master' of "
+ db.getWorkTree().getAbsolutePath();
assertEquals(message, mergeCommit.getShortMessage());
}
}
@Test
@ -259,11 +260,12 @@ public class PullCommandTest extends RepositoryTestCase {
ObjectId[] mergedCommits = mergeResult.getMergedCommits();
assertEquals(targetCommit.getId(), mergedCommits[0]);
assertEquals(sourceCommit.getId(), mergedCommits[1]);
RevCommit mergeCommit = new RevWalk(dbTarget).parseCommit(mergeResult
.getNewHead());
String message = "Merge branch 'other' of "
+ db.getWorkTree().getAbsolutePath();
assertEquals(message, mergeCommit.getShortMessage());
try (RevWalk rw = new RevWalk(dbTarget)) {
RevCommit mergeCommit = rw.parseCommit(mergeResult.getNewHead());
String message = "Merge branch 'other' of "
+ db.getWorkTree().getAbsolutePath();
assertEquals(message, mergeCommit.getShortMessage());
}
}
@Test
@ -293,11 +295,12 @@ public class PullCommandTest extends RepositoryTestCase {
ObjectId[] mergedCommits = mergeResult.getMergedCommits();
assertEquals(targetCommit.getId(), mergedCommits[0]);
assertEquals(sourceCommit.getId(), mergedCommits[1]);
RevCommit mergeCommit = new RevWalk(dbTarget).parseCommit(mergeResult
.getNewHead());
String message = "Merge branch 'other' of "
+ db.getWorkTree().getAbsolutePath() + " into other";
assertEquals(message, mergeCommit.getShortMessage());
try (RevWalk rw = new RevWalk(dbTarget)) {
RevCommit mergeCommit = rw.parseCommit(mergeResult.getNewHead());
String message = "Merge branch 'other' of "
+ db.getWorkTree().getAbsolutePath() + " into other";
assertEquals(message, mergeCommit.getShortMessage());
}
}
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();
assertEquals(Status.OK, res.getStatus());
RevWalk rw = new RevWalk(db);
rw.markStart(rw.parseCommit(db.resolve("refs/heads/topic")));
assertDerivedFrom(rw.next(), e);
assertDerivedFrom(rw.next(), d);
assertDerivedFrom(rw.next(), c);
assertEquals(b, rw.next());
assertEquals(a, rw.next());
try (RevWalk rw = new RevWalk(db)) {
rw.markStart(rw.parseCommit(db.resolve("refs/heads/topic")));
assertDerivedFrom(rw.next(), e);
assertDerivedFrom(rw.next(), d);
assertDerivedFrom(rw.next(), c);
assertEquals(b, rw.next());
assertEquals(a, rw.next());
}
List<ReflogEntry> headLog = db.getReflogReader(Constants.HEAD)
.getReverseEntries();
@ -354,8 +355,6 @@ public class RebaseCommandTest extends RepositoryTestCase {
*/
private void doTestRebasePreservingMerges(boolean testConflict)
throws Exception {
RevWalk rw = new RevWalk(db);
// create file1 on master
writeTrashFile(FILE1, FILE1);
git.add().addFilepattern(FILE1).call();
@ -409,7 +408,9 @@ public class RebaseCommandTest extends RepositoryTestCase {
f = git.commit().setMessage("commit f").call();
} else {
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")
@ -453,23 +454,25 @@ public class RebaseCommandTest extends RepositoryTestCase {
assertEquals("file2", read("file2"));
assertEquals("more change", read("file3"));
rw.markStart(rw.parseCommit(db.resolve("refs/heads/topic")));
RevCommit newF = rw.next();
assertDerivedFrom(newF, f);
assertEquals(2, newF.getParentCount());
RevCommit newD = rw.next();
assertDerivedFrom(newD, d);
if (testConflict)
assertEquals("d new", readFile("conflict", newD));
RevCommit newE = rw.next();
assertDerivedFrom(newE, e);
if (testConflict)
assertEquals("e new", readFile("conflict", newE));
assertEquals(newD, newF.getParent(0));
assertEquals(newE, newF.getParent(1));
assertDerivedFrom(rw.next(), c);
assertEquals(b, rw.next());
assertEquals(a, rw.next());
try (RevWalk rw = new RevWalk(db)) {
rw.markStart(rw.parseCommit(db.resolve("refs/heads/topic")));
RevCommit newF = rw.next();
assertDerivedFrom(newF, f);
assertEquals(2, newF.getParentCount());
RevCommit newD = rw.next();
assertDerivedFrom(newD, d);
if (testConflict)
assertEquals("d new", readFile("conflict", newD));
RevCommit newE = rw.next();
assertDerivedFrom(newE, e);
if (testConflict)
assertEquals("e new", readFile("conflict", newE));
assertEquals(newD, newF.getParent(0));
assertEquals(newE, newF.getParent(1));
assertDerivedFrom(rw.next(), c);
assertEquals(b, rw.next());
assertEquals(a, rw.next());
}
}
private String readFile(String path, RevCommit commit) throws IOException {
@ -517,88 +520,89 @@ public class RebaseCommandTest extends RepositoryTestCase {
*/
private void doTestRebasePreservingMergesWithUnrelatedSide(
boolean testConflict) throws Exception {
RevWalk rw = new RevWalk(db);
rw.sort(RevSort.TOPO);
try (RevWalk rw = new RevWalk(db)) {
rw.sort(RevSort.TOPO);
writeTrashFile(FILE1, FILE1);
git.add().addFilepattern(FILE1).call();
RevCommit a = git.commit().setMessage("commit a").call();
writeTrashFile(FILE1, FILE1);
git.add().addFilepattern(FILE1).call();
RevCommit a = git.commit().setMessage("commit a").call();
writeTrashFile("file2", "blah");
git.add().addFilepattern("file2").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();
writeTrashFile("file2", "blah");
git.add().addFilepattern("file2").call();
RevCommit b = git.commit().setMessage("commit b").call();
// switch back to topic and merge in side, creating d
checkoutBranch("refs/heads/topic");
MergeResult result = git.merge().include(e)
.setStrategy(MergeStrategy.RESOLVE).call();
// create a topic branch
createBranch(b, "refs/heads/topic");
checkoutBranch("refs/heads/topic");
assertEquals(MergeStatus.CONFLICTING, result.getMergeStatus());
assertEquals(result.getConflicts().keySet(),
Collections.singleton(FILE1));
writeTrashFile(FILE1, "merge resolution");
git.add().addFilepattern(FILE1).call();
RevCommit d = git.commit().setMessage("commit d").call();
writeTrashFile("file3", "more changess");
writeTrashFile(FILE1, "preparing conflict");
git.add().addFilepattern("file3").addFilepattern(FILE1).call();
RevCommit c = git.commit().setMessage("commit c").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");
writeTrashFile("fileg", "fileg");
if (testConflict)
writeTrashFile("conflict", "g");
git.add().addFilepattern(".").call();
RevCommit g = git.commit().setMessage("commit g").call();
// switch back to topic and merge in side, creating d
checkoutBranch("refs/heads/topic");
MergeResult result = git.merge().include(e)
.setStrategy(MergeStrategy.RESOLVE).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(MergeStatus.CONFLICTING, result.getMergeStatus());
assertEquals(result.getConflicts().keySet(),
Collections.singleton(FILE1));
writeTrashFile(FILE1, "merge resolution");
git.add().addFilepattern(FILE1).call();
RevCommit d = git.commit().setMessage("commit d").call();
RevCommit f = commitFile("file2", "new content two", "topic");
checkoutBranch("refs/heads/master");
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
@ -687,8 +691,10 @@ public class RebaseCommandTest extends RepositoryTestCase {
checkFile(theFile, "1master\n2\n3\ntopic\n");
// our old branch should be checked out again
assertEquals("refs/heads/topic", db.getFullBranch());
assertEquals(lastMasterChange, new RevWalk(db).parseCommit(
db.resolve(Constants.HEAD)).getParent(0));
try (RevWalk rw = new RevWalk(db)) {
assertEquals(lastMasterChange, rw.parseCommit(
db.resolve(Constants.HEAD)).getParent(0));
}
assertEquals(origHead, db.readOrigHead());
List<ReflogEntry> headLog = db.getReflogReader(Constants.HEAD)
.getReverseEntries();
@ -737,8 +743,10 @@ public class RebaseCommandTest extends RepositoryTestCase {
RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
assertEquals(Status.OK, res.getStatus());
checkFile(theFile, "1master\n2\n3\ntopic\n");
assertEquals(lastMasterChange, new RevWalk(db).parseCommit(
db.resolve(Constants.HEAD)).getParent(0));
try (RevWalk rw = new RevWalk(db)) {
assertEquals(lastMasterChange, rw.parseCommit(
db.resolve(Constants.HEAD)).getParent(0));
}
List<ReflogEntry> headLog = db.getReflogReader(Constants.HEAD)
.getReverseEntries();
@ -785,8 +793,10 @@ public class RebaseCommandTest extends RepositoryTestCase {
// our old branch should be checked out again
assertEquals("refs/heads/file3", db.getFullBranch());
assertEquals(addFile2, new RevWalk(db).parseCommit(
db.resolve(Constants.HEAD)).getParent(0));
try (RevWalk rw = new RevWalk(db)) {
assertEquals(addFile2, rw.parseCommit(
db.resolve(Constants.HEAD)).getParent(0));
}
checkoutBranch("refs/heads/file2");
assertTrue(new File(db.getWorkTree(), FILE1).exists());
@ -846,9 +856,10 @@ public class RebaseCommandTest extends RepositoryTestCase {
assertEquals(res.getStatus(), Status.ABORTED);
assertEquals("refs/heads/topic", db.getFullBranch());
checkFile(FILE1, "1topic", "2", "3", "topic4");
RevWalk rw = new RevWalk(db);
assertEquals(lastTopicCommit, rw
.parseCommit(db.resolve(Constants.HEAD)));
try (RevWalk rw = new RevWalk(db)) {
assertEquals(lastTopicCommit,
rw.parseCommit(db.resolve(Constants.HEAD)));
}
assertEquals(RepositoryState.SAFE, db.getRepositoryState());
// rebase- dir in .git must be deleted
@ -909,9 +920,10 @@ public class RebaseCommandTest extends RepositoryTestCase {
assertEquals(res.getStatus(), Status.ABORTED);
assertEquals(lastTopicCommit.getName(), db.getFullBranch());
checkFile(FILE1, "1topic", "2", "3", "topic4");
RevWalk rw = new RevWalk(db);
assertEquals(lastTopicCommit,
rw.parseCommit(db.resolve(Constants.HEAD)));
try (RevWalk rw = new RevWalk(db)) {
assertEquals(lastTopicCommit,
rw.parseCommit(db.resolve(Constants.HEAD)));
}
assertEquals(RepositoryState.SAFE, db.getRepositoryState());
// rebase- dir in .git must be deleted
@ -966,11 +978,12 @@ public class RebaseCommandTest extends RepositoryTestCase {
assertEquals(RepositoryState.SAFE, db.getRepositoryState());
ObjectId headId = db.resolve(Constants.HEAD);
RevWalk rw = new RevWalk(db);
RevCommit rc = rw.parseCommit(headId);
RevCommit parent = rw.parseCommit(rc.getParent(0));
assertEquals("change file1 in topic\n\nThis is conflicting", parent
.getFullMessage());
try (RevWalk rw = new RevWalk(db)) {
RevCommit rc = rw.parseCommit(headId);
RevCommit parent = rw.parseCommit(rc.getParent(0));
assertEquals("change file1 in topic\n\nThis is conflicting", parent
.getFullMessage());
}
}
@Test
@ -1017,9 +1030,10 @@ public class RebaseCommandTest extends RepositoryTestCase {
git.rebase().setOperation(Operation.SKIP).call();
ObjectId headId = db.resolve(Constants.HEAD);
RevWalk rw = new RevWalk(db);
RevCommit rc = rw.parseCommit(headId);
assertEquals("change file1 in master", rc.getFullMessage());
try (RevWalk rw = new RevWalk(db)) {
RevCommit rc = rw.parseCommit(headId);
assertEquals("change file1 in master", rc.getFullMessage());
}
}
@Test
@ -1308,10 +1322,11 @@ public class RebaseCommandTest extends RepositoryTestCase {
git.rebase().setOperation(Operation.SKIP).call();
ObjectId headId = db.resolve(Constants.HEAD);
RevWalk rw = new RevWalk(db);
RevCommit rc = rw.parseCommit(headId);
RevCommit parent = rw.parseCommit(rc.getParent(0));
assertEquals("A different commit message", parent.getFullMessage());
try (RevWalk rw = new RevWalk(db)) {
RevCommit rc = rw.parseCommit(headId);
RevCommit parent = rw.parseCommit(rc.getParent(0));
assertEquals("A different commit message", parent.getFullMessage());
}
}
private RevCommit writeFileAndCommit(String fileName, String commitMessage,
@ -1420,9 +1435,10 @@ public class RebaseCommandTest extends RepositoryTestCase {
res = git.rebase().setOperation(Operation.ABORT).call();
assertEquals(res.getStatus(), Status.ABORTED);
assertEquals("refs/heads/topic", db.getFullBranch());
RevWalk rw = new RevWalk(db);
assertEquals(conflicting, rw.parseCommit(db.resolve(Constants.HEAD)));
assertEquals(RepositoryState.SAFE, db.getRepositoryState());
try (RevWalk rw = new RevWalk(db)) {
assertEquals(conflicting, rw.parseCommit(db.resolve(Constants.HEAD)));
assertEquals(RepositoryState.SAFE, db.getRepositoryState());
}
// rebase- dir in .git must be deleted
assertFalse(new File(db.getDirectory(), "rebase-merge").exists());
@ -2286,14 +2302,15 @@ public class RebaseCommandTest extends RepositoryTestCase {
assertEquals(RebaseResult.Status.OK, res2.getStatus());
ObjectId headId = db.resolve(Constants.HEAD);
RevWalk rw = new RevWalk(db);
RevCommit rc = rw.parseCommit(headId);
try (RevWalk rw = new RevWalk(db)) {
RevCommit rc = rw.parseCommit(headId);
ObjectId head1Id = db.resolve(Constants.HEAD + "~1");
RevCommit rc1 = rw.parseCommit(head1Id);
ObjectId head1Id = db.resolve(Constants.HEAD + "~1");
RevCommit rc1 = rw.parseCommit(head1Id);
assertEquals(rc.getFullMessage(), c4.getFullMessage());
assertEquals(rc1.getFullMessage(), c2.getFullMessage());
assertEquals(rc.getFullMessage(), c4.getFullMessage());
assertEquals(rc1.getFullMessage(), c2.getFullMessage());
}
}
@Test
@ -2643,15 +2660,16 @@ public class RebaseCommandTest extends RepositoryTestCase {
}
}).call();
RevWalk walk = new RevWalk(db);
ObjectId headId = db.resolve(Constants.HEAD);
RevCommit headCommit = walk.parseCommit(headId);
assertEquals(headCommit.getFullMessage(),
"update file2 on master\nnew line");
ObjectId head2Id = db.resolve(Constants.HEAD + "^1");
RevCommit head1Commit = walk.parseCommit(head2Id);
assertEquals("changed", head1Commit.getFullMessage());
try (RevWalk walk = new RevWalk(db)) {
ObjectId headId = db.resolve(Constants.HEAD);
RevCommit headCommit = walk.parseCommit(headId);
assertEquals(headCommit.getFullMessage(),
"update file2 on master\nnew line");
ObjectId head2Id = db.resolve(Constants.HEAD + "^1");
RevCommit head1Commit = walk.parseCommit(head2Id);
assertEquals("changed", head1Commit.getFullMessage());
}
}
@Test
@ -2722,17 +2740,18 @@ public class RebaseCommandTest extends RepositoryTestCase {
}
}).call();
RevWalk walk = new RevWalk(db);
ObjectId headId = db.resolve(Constants.HEAD);
RevCommit headCommit = walk.parseCommit(headId);
assertEquals(headCommit.getFullMessage(),
"update file2 on master\nnew line");
ObjectId head2Id = db.resolve(Constants.HEAD + "^1");
RevCommit head1Commit = walk.parseCommit(head2Id);
assertEquals(
"Add file1\nnew line\nAdd file2\nnew line\nupdated file1 on master\nnew line",
head1Commit.getFullMessage());
try (RevWalk walk = new RevWalk(db)) {
ObjectId headId = db.resolve(Constants.HEAD);
RevCommit headCommit = walk.parseCommit(headId);
assertEquals(headCommit.getFullMessage(),
"update file2 on master\nnew line");
ObjectId head2Id = db.resolve(Constants.HEAD + "^1");
RevCommit head1Commit = walk.parseCommit(head2Id);
assertEquals(
"Add file1\nnew line\nAdd file2\nnew line\nupdated file1 on master\nnew line",
head1Commit.getFullMessage());
}
}
@Test
@ -2804,15 +2823,16 @@ public class RebaseCommandTest extends RepositoryTestCase {
}
}).call();
RevWalk walk = new RevWalk(db);
ObjectId headId = db.resolve(Constants.HEAD);
RevCommit headCommit = walk.parseCommit(headId);
assertEquals(headCommit.getFullMessage(),
"update file2 on master\nnew line");
ObjectId head2Id = db.resolve(Constants.HEAD + "^1");
RevCommit head1Commit = walk.parseCommit(head2Id);
assertEquals("changed", head1Commit.getFullMessage());
try (RevWalk walk = new RevWalk(db)) {
ObjectId headId = db.resolve(Constants.HEAD);
RevCommit headCommit = walk.parseCommit(headId);
assertEquals(headCommit.getFullMessage(),
"update file2 on master\nnew line");
ObjectId head2Id = db.resolve(Constants.HEAD + "^1");
RevCommit head1Commit = walk.parseCommit(head2Id);
assertEquals("changed", head1Commit.getFullMessage());
}
}
@Test
@ -2855,16 +2875,17 @@ public class RebaseCommandTest extends RepositoryTestCase {
}
}).call();
RevWalk walk = new RevWalk(db);
ObjectId headId = db.resolve(Constants.HEAD);
RevCommit headCommit = walk.parseCommit(headId);
assertEquals("update file2 on master\nnew line",
headCommit.getFullMessage());
ObjectId head1Id = db.resolve(Constants.HEAD + "^1");
RevCommit head1Commit = walk.parseCommit(head1Id);
assertEquals("Add file2\nnew line",
head1Commit.getFullMessage());
try (RevWalk walk = new RevWalk(db)) {
ObjectId headId = db.resolve(Constants.HEAD);
RevCommit headCommit = walk.parseCommit(headId);
assertEquals("update file2 on master\nnew line",
headCommit.getFullMessage());
ObjectId head1Id = db.resolve(Constants.HEAD + "^1");
RevCommit head1Commit = walk.parseCommit(head1Id);
assertEquals("Add file2\nnew line",
head1Commit.getFullMessage());
}
}
@Test
@ -2903,11 +2924,12 @@ public class RebaseCommandTest extends RepositoryTestCase {
}
}).call();
RevWalk walk = new RevWalk(db);
ObjectId headId = db.resolve(Constants.HEAD);
RevCommit headCommit = walk.parseCommit(headId);
assertEquals("Add file2",
headCommit.getFullMessage());
try (RevWalk walk = new RevWalk(db)) {
ObjectId headId = db.resolve(Constants.HEAD);
RevCommit headCommit = walk.parseCommit(headId);
assertEquals("Add file2",
headCommit.getFullMessage());
}
}
@Test(expected = InvalidRebaseStepException.class)

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 {
@Test
public void testBoundLineDelete() throws Exception {
Git git = new Git(db);
String[] content1 = new String[] { "first", "second" };
writeTrashFile("file.txt", join(content1));
git.add().addFilepattern("file.txt").call();
RevCommit c1 = git.commit().setMessage("create file").call();
String[] content2 = new String[] { "third", "first", "second" };
writeTrashFile("file.txt", join(content2));
git.add().addFilepattern("file.txt").call();
RevCommit c2 = git.commit().setMessage("create file").call();
try (BlameGenerator generator = new BlameGenerator(db, "file.txt")) {
generator.push(null, db.resolve(Constants.HEAD));
assertEquals(3, generator.getResultContents().size());
assertTrue(generator.next());
assertEquals(c2, generator.getSourceCommit());
assertEquals(1, generator.getRegionLength());
assertEquals(0, generator.getResultStart());
assertEquals(1, generator.getResultEnd());
assertEquals(0, generator.getSourceStart());
assertEquals(1, generator.getSourceEnd());
assertEquals("file.txt", generator.getSourcePath());
assertTrue(generator.next());
assertEquals(c1, generator.getSourceCommit());
assertEquals(2, generator.getRegionLength());
assertEquals(1, generator.getResultStart());
assertEquals(3, generator.getResultEnd());
assertEquals(0, generator.getSourceStart());
assertEquals(2, generator.getSourceEnd());
assertEquals("file.txt", generator.getSourcePath());
assertFalse(generator.next());
try (Git git = new Git(db)) {
String[] content1 = new String[] { "first", "second" };
writeTrashFile("file.txt", join(content1));
git.add().addFilepattern("file.txt").call();
RevCommit c1 = git.commit().setMessage("create file").call();
String[] content2 = new String[] { "third", "first", "second" };
writeTrashFile("file.txt", join(content2));
git.add().addFilepattern("file.txt").call();
RevCommit c2 = git.commit().setMessage("create file").call();
try (BlameGenerator generator = new BlameGenerator(db, "file.txt")) {
generator.push(null, db.resolve(Constants.HEAD));
assertEquals(3, generator.getResultContents().size());
assertTrue(generator.next());
assertEquals(c2, generator.getSourceCommit());
assertEquals(1, generator.getRegionLength());
assertEquals(0, generator.getResultStart());
assertEquals(1, generator.getResultEnd());
assertEquals(0, generator.getSourceStart());
assertEquals(1, generator.getSourceEnd());
assertEquals("file.txt", generator.getSourcePath());
assertTrue(generator.next());
assertEquals(c1, generator.getSourceCommit());
assertEquals(2, generator.getRegionLength());
assertEquals(1, generator.getResultStart());
assertEquals(3, generator.getResultEnd());
assertEquals(0, generator.getSourceStart());
assertEquals(2, generator.getSourceEnd());
assertEquals("file.txt", generator.getSourcePath());
assertFalse(generator.next());
}
}
}
@Test
public void testRenamedBoundLineDelete() throws Exception {
Git git = new Git(db);
final String FILENAME_1 = "subdir/file1.txt";
final String FILENAME_2 = "subdir/file2.txt";
String[] content1 = new String[] { "first", "second" };
writeTrashFile(FILENAME_1, join(content1));
git.add().addFilepattern(FILENAME_1).call();
RevCommit c1 = git.commit().setMessage("create file1").call();
// rename it
writeTrashFile(FILENAME_2, join(content1));
git.add().addFilepattern(FILENAME_2).call();
deleteTrashFile(FILENAME_1);
git.rm().addFilepattern(FILENAME_1).call();
git.commit().setMessage("rename file1.txt to file2.txt").call();
// and change the new file
String[] content2 = new String[] { "third", "first", "second" };
writeTrashFile(FILENAME_2, join(content2));
git.add().addFilepattern(FILENAME_2).call();
RevCommit c2 = git.commit().setMessage("change file2").call();
try (BlameGenerator generator = new BlameGenerator(db, FILENAME_2)) {
generator.push(null, db.resolve(Constants.HEAD));
assertEquals(3, generator.getResultContents().size());
assertTrue(generator.next());
assertEquals(c2, generator.getSourceCommit());
assertEquals(1, generator.getRegionLength());
assertEquals(0, generator.getResultStart());
assertEquals(1, generator.getResultEnd());
assertEquals(0, generator.getSourceStart());
assertEquals(1, generator.getSourceEnd());
assertEquals(FILENAME_2, generator.getSourcePath());
assertTrue(generator.next());
assertEquals(c1, generator.getSourceCommit());
assertEquals(2, generator.getRegionLength());
assertEquals(1, generator.getResultStart());
assertEquals(3, generator.getResultEnd());
assertEquals(0, generator.getSourceStart());
assertEquals(2, generator.getSourceEnd());
assertEquals(FILENAME_1, generator.getSourcePath());
assertFalse(generator.next());
}
// and test again with other BlameGenerator API:
try (BlameGenerator generator = new BlameGenerator(db, FILENAME_2)) {
generator.push(null, db.resolve(Constants.HEAD));
BlameResult result = generator.computeBlameResult();
assertEquals(3, result.getResultContents().size());
assertEquals(c2, result.getSourceCommit(0));
assertEquals(FILENAME_2, result.getSourcePath(0));
assertEquals(c1, result.getSourceCommit(1));
assertEquals(FILENAME_1, result.getSourcePath(1));
assertEquals(c1, result.getSourceCommit(2));
assertEquals(FILENAME_1, result.getSourcePath(2));
try (Git git = new Git(db)) {
final String FILENAME_1 = "subdir/file1.txt";
final String FILENAME_2 = "subdir/file2.txt";
String[] content1 = new String[] { "first", "second" };
writeTrashFile(FILENAME_1, join(content1));
git.add().addFilepattern(FILENAME_1).call();
RevCommit c1 = git.commit().setMessage("create file1").call();
// rename it
writeTrashFile(FILENAME_2, join(content1));
git.add().addFilepattern(FILENAME_2).call();
deleteTrashFile(FILENAME_1);
git.rm().addFilepattern(FILENAME_1).call();
git.commit().setMessage("rename file1.txt to file2.txt").call();
// and change the new file
String[] content2 = new String[] { "third", "first", "second" };
writeTrashFile(FILENAME_2, join(content2));
git.add().addFilepattern(FILENAME_2).call();
RevCommit c2 = git.commit().setMessage("change file2").call();
try (BlameGenerator generator = new BlameGenerator(db, FILENAME_2)) {
generator.push(null, db.resolve(Constants.HEAD));
assertEquals(3, generator.getResultContents().size());
assertTrue(generator.next());
assertEquals(c2, generator.getSourceCommit());
assertEquals(1, generator.getRegionLength());
assertEquals(0, generator.getResultStart());
assertEquals(1, generator.getResultEnd());
assertEquals(0, generator.getSourceStart());
assertEquals(1, generator.getSourceEnd());
assertEquals(FILENAME_2, generator.getSourcePath());
assertTrue(generator.next());
assertEquals(c1, generator.getSourceCommit());
assertEquals(2, generator.getRegionLength());
assertEquals(1, generator.getResultStart());
assertEquals(3, generator.getResultEnd());
assertEquals(0, generator.getSourceStart());
assertEquals(2, generator.getSourceEnd());
assertEquals(FILENAME_1, generator.getSourcePath());
assertFalse(generator.next());
}
// and test again with other BlameGenerator API:
try (BlameGenerator generator = new BlameGenerator(db, FILENAME_2)) {
generator.push(null, db.resolve(Constants.HEAD));
BlameResult result = generator.computeBlameResult();
assertEquals(3, result.getResultContents().size());
assertEquals(c2, result.getSourceCommit(0));
assertEquals(FILENAME_2, result.getSourcePath(0));
assertEquals(c1, result.getSourceCommit(1));
assertEquals(FILENAME_1, result.getSourcePath(1));
assertEquals(c1, result.getSourceCommit(2));
assertEquals(FILENAME_1, result.getSourcePath(2));
}
}
}
@Test
public void testLinesAllDeletedShortenedWalk() throws Exception {
Git git = new Git(db);
String[] content1 = new String[] { "first", "second", "third" };
try (Git git = new Git(db)) {
String[] content1 = new String[] { "first", "second", "third" };
writeTrashFile("file.txt", join(content1));
git.add().addFilepattern("file.txt").call();
git.commit().setMessage("create file").call();
writeTrashFile("file.txt", join(content1));
git.add().addFilepattern("file.txt").call();
git.commit().setMessage("create file").call();
String[] content2 = new String[] { "" };
String[] content2 = new String[] { "" };
writeTrashFile("file.txt", join(content2));
git.add().addFilepattern("file.txt").call();
git.commit().setMessage("create file").call();
writeTrashFile("file.txt", join(content2));
git.add().addFilepattern("file.txt").call();
git.commit().setMessage("create file").call();
writeTrashFile("file.txt", join(content1));
git.add().addFilepattern("file.txt").call();
RevCommit c3 = git.commit().setMessage("create file").call();
writeTrashFile("file.txt", join(content1));
git.add().addFilepattern("file.txt").call();
RevCommit c3 = git.commit().setMessage("create file").call();
try (BlameGenerator generator = new BlameGenerator(db, "file.txt")) {
generator.push(null, db.resolve(Constants.HEAD));
assertEquals(3, generator.getResultContents().size());
try (BlameGenerator generator = new BlameGenerator(db, "file.txt")) {
generator.push(null, db.resolve(Constants.HEAD));
assertEquals(3, generator.getResultContents().size());
assertTrue(generator.next());
assertEquals(c3, generator.getSourceCommit());
assertEquals(0, generator.getResultStart());
assertEquals(3, generator.getResultEnd());
assertTrue(generator.next());
assertEquals(c3, generator.getSourceCommit());
assertEquals(0, generator.getResultStart());
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 {
// given
writeTrashFile("a.txt", "content");
Git git = new Git(db);
git.add().addFilepattern("a.txt").call();
RevCommit c = git.commit().setMessage("initial commit").call();
// when
TreeWalk walk = new TreeWalk(db);
walk.addTree(new EmptyTreeIterator());
walk.addTree(c.getTree());
List<DiffEntry> result = DiffEntry.scan(walk);
// then
assertThat(result, notNullValue());
assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1)));
DiffEntry entry = result.get(0);
assertThat(entry.getChangeType(), is(ChangeType.ADD));
assertThat(entry.getNewPath(), is("a.txt"));
assertThat(entry.getOldPath(), is(DEV_NULL));
try (Git git = new Git(db);
TreeWalk walk = new TreeWalk(db)) {
git.add().addFilepattern("a.txt").call();
RevCommit c = git.commit().setMessage("initial commit").call();
// when
walk.addTree(new EmptyTreeIterator());
walk.addTree(c.getTree());
List<DiffEntry> result = DiffEntry.scan(walk);
// then
assertThat(result, notNullValue());
assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1)));
DiffEntry entry = result.get(0);
assertThat(entry.getChangeType(), is(ChangeType.ADD));
assertThat(entry.getNewPath(), is("a.txt"));
assertThat(entry.getOldPath(), is(DEV_NULL));
}
}
@Test
public void shouldListAddedFileBetweenTwoCommits() throws Exception {
// given
Git git = new Git(db);
RevCommit c1 = git.commit().setMessage("initial commit").call();
writeTrashFile("a.txt", "content");
git.add().addFilepattern("a.txt").call();
RevCommit c2 = git.commit().setMessage("second commit").call();
// when
TreeWalk walk = new TreeWalk(db);
walk.addTree(c1.getTree());
walk.addTree(c2.getTree());
List<DiffEntry> result = DiffEntry.scan(walk);
// then
assertThat(result, notNullValue());
assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1)));
DiffEntry entry = result.get(0);
assertThat(entry.getChangeType(), is(ChangeType.ADD));
assertThat(entry.getNewPath(), is("a.txt"));
assertThat(entry.getOldPath(), is(DEV_NULL));
try (Git git = new Git(db);
TreeWalk walk = new TreeWalk(db)) {
RevCommit c1 = git.commit().setMessage("initial commit").call();
writeTrashFile("a.txt", "content");
git.add().addFilepattern("a.txt").call();
RevCommit c2 = git.commit().setMessage("second commit").call();
// when
walk.addTree(c1.getTree());
walk.addTree(c2.getTree());
List<DiffEntry> result = DiffEntry.scan(walk);
// then
assertThat(result, notNullValue());
assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1)));
DiffEntry entry = result.get(0);
assertThat(entry.getChangeType(), is(ChangeType.ADD));
assertThat(entry.getNewPath(), is("a.txt"));
assertThat(entry.getOldPath(), is(DEV_NULL));
}
}
@Test
public void shouldListModificationBetweenTwoCommits() throws Exception {
// given
Git git = new Git(db);
File file = writeTrashFile("a.txt", "content");
git.add().addFilepattern("a.txt").call();
RevCommit c1 = git.commit().setMessage("initial commit").call();
write(file, "new content");
RevCommit c2 = git.commit().setAll(true).setMessage("second commit")
.call();
// when
TreeWalk walk = new TreeWalk(db);
walk.addTree(c1.getTree());
walk.addTree(c2.getTree());
List<DiffEntry> result = DiffEntry.scan(walk);
// then
assertThat(result, notNullValue());
assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1)));
DiffEntry entry = result.get(0);
assertThat(entry.getChangeType(), is(ChangeType.MODIFY));
assertThat(entry.getNewPath(), is("a.txt"));
try (Git git = new Git(db);
TreeWalk walk = new TreeWalk(db)) {
File file = writeTrashFile("a.txt", "content");
git.add().addFilepattern("a.txt").call();
RevCommit c1 = git.commit().setMessage("initial commit").call();
write(file, "new content");
RevCommit c2 = git.commit().setAll(true).setMessage("second commit")
.call();
// when
walk.addTree(c1.getTree());
walk.addTree(c2.getTree());
List<DiffEntry> result = DiffEntry.scan(walk);
// then
assertThat(result, notNullValue());
assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1)));
DiffEntry entry = result.get(0);
assertThat(entry.getChangeType(), is(ChangeType.MODIFY));
assertThat(entry.getNewPath(), is("a.txt"));
}
}
@Test
public void shouldListDeletionBetweenTwoCommits() throws Exception {
// given
Git git = new Git(db);
File file = writeTrashFile("a.txt", "content");
git.add().addFilepattern("a.txt").call();
RevCommit c1 = git.commit().setMessage("initial commit").call();
delete(file);
RevCommit c2 = git.commit().setAll(true).setMessage("delete a.txt")
.call();
// when
TreeWalk walk = new TreeWalk(db);
walk.addTree(c1.getTree());
walk.addTree(c2.getTree());
List<DiffEntry> result = DiffEntry.scan(walk);
// then
assertThat(result, notNullValue());
assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1)));
DiffEntry entry = result.get(0);
assertThat(entry.getOldPath(), is("a.txt"));
assertThat(entry.getNewPath(), is(DEV_NULL));
assertThat(entry.getChangeType(), is(ChangeType.DELETE));
try (Git git = new Git(db);
TreeWalk walk = new TreeWalk(db)) {
File file = writeTrashFile("a.txt", "content");
git.add().addFilepattern("a.txt").call();
RevCommit c1 = git.commit().setMessage("initial commit").call();
delete(file);
RevCommit c2 = git.commit().setAll(true).setMessage("delete a.txt")
.call();
// when
walk.addTree(c1.getTree());
walk.addTree(c2.getTree());
List<DiffEntry> result = DiffEntry.scan(walk);
// then
assertThat(result, notNullValue());
assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1)));
DiffEntry entry = result.get(0);
assertThat(entry.getOldPath(), is("a.txt"));
assertThat(entry.getNewPath(), is(DEV_NULL));
assertThat(entry.getChangeType(), is(ChangeType.DELETE));
}
}
@Test
public void shouldListModificationInDirWithoutModifiedTrees()
throws Exception {
// given
Git git = new Git(db);
File tree = new File(new File(db.getWorkTree(), "a"), "b");
FileUtils.mkdirs(tree);
File file = new File(tree, "c.txt");
FileUtils.createNewFile(file);
write(file, "content");
git.add().addFilepattern("a").call();
RevCommit c1 = git.commit().setMessage("initial commit").call();
write(file, "new line");
RevCommit c2 = git.commit().setAll(true).setMessage("second commit")
.call();
// when
TreeWalk walk = new TreeWalk(db);
walk.addTree(c1.getTree());
walk.addTree(c2.getTree());
walk.setRecursive(true);
List<DiffEntry> result = DiffEntry.scan(walk);
// then
assertThat(result, notNullValue());
assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1)));
DiffEntry entry = result.get(0);
assertThat(entry.getChangeType(), is(ChangeType.MODIFY));
assertThat(entry.getNewPath(), is("a/b/c.txt"));
try (Git git = new Git(db);
TreeWalk walk = new TreeWalk(db)) {
File tree = new File(new File(db.getWorkTree(), "a"), "b");
FileUtils.mkdirs(tree);
File file = new File(tree, "c.txt");
FileUtils.createNewFile(file);
write(file, "content");
git.add().addFilepattern("a").call();
RevCommit c1 = git.commit().setMessage("initial commit").call();
write(file, "new line");
RevCommit c2 = git.commit().setAll(true).setMessage("second commit")
.call();
// when
walk.addTree(c1.getTree());
walk.addTree(c2.getTree());
walk.setRecursive(true);
List<DiffEntry> result = DiffEntry.scan(walk);
// then
assertThat(result, notNullValue());
assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1)));
DiffEntry entry = result.get(0);
assertThat(entry.getChangeType(), is(ChangeType.MODIFY));
assertThat(entry.getNewPath(), is("a/b/c.txt"));
}
}
@Test
public void shouldListModificationInDirWithModifiedTrees() throws Exception {
// given
Git git = new Git(db);
File tree = new File(new File(db.getWorkTree(), "a"), "b");
FileUtils.mkdirs(tree);
File file = new File(tree, "c.txt");
FileUtils.createNewFile(file);
write(file, "content");
git.add().addFilepattern("a").call();
RevCommit c1 = git.commit().setMessage("initial commit").call();
write(file, "new line");
RevCommit c2 = git.commit().setAll(true).setMessage("second commit")
.call();
// when
TreeWalk walk = new TreeWalk(db);
walk.addTree(c1.getTree());
walk.addTree(c2.getTree());
List<DiffEntry> result = DiffEntry.scan(walk, true);
// then
assertThat(result, notNullValue());
assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(3)));
DiffEntry entry = result.get(0);
assertThat(entry.getChangeType(), is(ChangeType.MODIFY));
assertThat(entry.getNewPath(), is("a"));
entry = result.get(1);
assertThat(entry.getChangeType(), is(ChangeType.MODIFY));
assertThat(entry.getNewPath(), is("a/b"));
entry = result.get(2);
assertThat(entry.getChangeType(), is(ChangeType.MODIFY));
assertThat(entry.getNewPath(), is("a/b/c.txt"));
try (Git git = new Git(db);
TreeWalk walk = new TreeWalk(db)) {
File tree = new File(new File(db.getWorkTree(), "a"), "b");
FileUtils.mkdirs(tree);
File file = new File(tree, "c.txt");
FileUtils.createNewFile(file);
write(file, "content");
git.add().addFilepattern("a").call();
RevCommit c1 = git.commit().setMessage("initial commit").call();
write(file, "new line");
RevCommit c2 = git.commit().setAll(true).setMessage("second commit")
.call();
// when
walk.addTree(c1.getTree());
walk.addTree(c2.getTree());
List<DiffEntry> result = DiffEntry.scan(walk, true);
// then
assertThat(result, notNullValue());
assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(3)));
DiffEntry entry = result.get(0);
assertThat(entry.getChangeType(), is(ChangeType.MODIFY));
assertThat(entry.getNewPath(), is("a"));
entry = result.get(1);
assertThat(entry.getChangeType(), is(ChangeType.MODIFY));
assertThat(entry.getNewPath(), is("a/b"));
entry = result.get(2);
assertThat(entry.getChangeType(), is(ChangeType.MODIFY));
assertThat(entry.getNewPath(), is("a/b/c.txt"));
}
}
@Test
public void shouldListChangesInWorkingTree() throws Exception {
// given
writeTrashFile("a.txt", "content");
Git git = new Git(db);
git.add().addFilepattern("a.txt").call();
RevCommit c = git.commit().setMessage("initial commit").call();
writeTrashFile("b.txt", "new line");
// when
TreeWalk walk = new TreeWalk(db);
walk.addTree(c.getTree());
walk.addTree(new FileTreeIterator(db));
List<DiffEntry> result = DiffEntry.scan(walk, true);
// then
assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1)));
DiffEntry entry = result.get(0);
assertThat(entry.getChangeType(), is(ChangeType.ADD));
assertThat(entry.getNewPath(), is("b.txt"));
try (Git git = new Git(db);
TreeWalk walk = new TreeWalk(db)) {
git.add().addFilepattern("a.txt").call();
RevCommit c = git.commit().setMessage("initial commit").call();
writeTrashFile("b.txt", "new line");
// when
walk.addTree(c.getTree());
walk.addTree(new FileTreeIterator(db));
List<DiffEntry> result = DiffEntry.scan(walk, true);
// then
assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1)));
DiffEntry entry = result.get(0);
assertThat(entry.getChangeType(), is(ChangeType.ADD));
assertThat(entry.getNewPath(), is("b.txt"));
}
}
@Test
public void shouldMarkEntriesWhenGivenMarkTreeFilter() throws Exception {
// given
Git git = new Git(db);
RevCommit c1 = git.commit().setMessage("initial commit").call();
FileUtils.mkdir(new File(db.getWorkTree(), "b"));
writeTrashFile("a.txt", "a");
writeTrashFile("b/1.txt", "b1");
writeTrashFile("b/2.txt", "b2");
writeTrashFile("c.txt", "c");
git.add().addFilepattern("a.txt").addFilepattern("b")
.addFilepattern("c.txt").call();
RevCommit c2 = git.commit().setMessage("second commit").call();
TreeFilter filterA = PathFilterGroup.createFromStrings("a.txt");
TreeFilter filterB = PathFilterGroup.createFromStrings("b");
TreeFilter filterB2 = PathFilterGroup.createFromStrings("b/2.txt");
// when
TreeWalk walk = new TreeWalk(db);
walk.addTree(c1.getTree());
walk.addTree(c2.getTree());
List<DiffEntry> result = DiffEntry.scan(walk, true, new TreeFilter[] {
filterA, filterB, filterB2 });
// then
assertThat(result, notNullValue());
assertEquals(5, result.size());
DiffEntry entryA = result.get(0);
DiffEntry entryB = result.get(1);
DiffEntry entryB1 = result.get(2);
DiffEntry entryB2 = result.get(3);
DiffEntry entryC = result.get(4);
assertThat(entryA.getNewPath(), is("a.txt"));
assertTrue(entryA.isMarked(0));
assertFalse(entryA.isMarked(1));
assertFalse(entryA.isMarked(2));
assertEquals(1, entryA.getTreeFilterMarks());
assertThat(entryB.getNewPath(), is("b"));
assertFalse(entryB.isMarked(0));
assertTrue(entryB.isMarked(1));
assertTrue(entryB.isMarked(2));
assertEquals(6, entryB.getTreeFilterMarks());
assertThat(entryB1.getNewPath(), is("b/1.txt"));
assertFalse(entryB1.isMarked(0));
assertTrue(entryB1.isMarked(1));
assertFalse(entryB1.isMarked(2));
assertEquals(2, entryB1.getTreeFilterMarks());
assertThat(entryB2.getNewPath(), is("b/2.txt"));
assertFalse(entryB2.isMarked(0));
assertTrue(entryB2.isMarked(1));
assertTrue(entryB2.isMarked(2));
assertEquals(6, entryB2.getTreeFilterMarks());
assertThat(entryC.getNewPath(), is("c.txt"));
assertFalse(entryC.isMarked(0));
assertFalse(entryC.isMarked(1));
assertFalse(entryC.isMarked(2));
assertEquals(0, entryC.getTreeFilterMarks());
try (Git git = new Git(db);
TreeWalk walk = new TreeWalk(db)) {
RevCommit c1 = git.commit().setMessage("initial commit").call();
FileUtils.mkdir(new File(db.getWorkTree(), "b"));
writeTrashFile("a.txt", "a");
writeTrashFile("b/1.txt", "b1");
writeTrashFile("b/2.txt", "b2");
writeTrashFile("c.txt", "c");
git.add().addFilepattern("a.txt").addFilepattern("b")
.addFilepattern("c.txt").call();
RevCommit c2 = git.commit().setMessage("second commit").call();
TreeFilter filterA = PathFilterGroup.createFromStrings("a.txt");
TreeFilter filterB = PathFilterGroup.createFromStrings("b");
TreeFilter filterB2 = PathFilterGroup.createFromStrings("b/2.txt");
// when
walk.addTree(c1.getTree());
walk.addTree(c2.getTree());
List<DiffEntry> result = DiffEntry.scan(walk, true, new TreeFilter[] {
filterA, filterB, filterB2 });
// then
assertThat(result, notNullValue());
assertEquals(5, result.size());
DiffEntry entryA = result.get(0);
DiffEntry entryB = result.get(1);
DiffEntry entryB1 = result.get(2);
DiffEntry entryB2 = result.get(3);
DiffEntry entryC = result.get(4);
assertThat(entryA.getNewPath(), is("a.txt"));
assertTrue(entryA.isMarked(0));
assertFalse(entryA.isMarked(1));
assertFalse(entryA.isMarked(2));
assertEquals(1, entryA.getTreeFilterMarks());
assertThat(entryB.getNewPath(), is("b"));
assertFalse(entryB.isMarked(0));
assertTrue(entryB.isMarked(1));
assertTrue(entryB.isMarked(2));
assertEquals(6, entryB.getTreeFilterMarks());
assertThat(entryB1.getNewPath(), is("b/1.txt"));
assertFalse(entryB1.isMarked(0));
assertTrue(entryB1.isMarked(1));
assertFalse(entryB1.isMarked(2));
assertEquals(2, entryB1.getTreeFilterMarks());
assertThat(entryB2.getNewPath(), is("b/2.txt"));
assertFalse(entryB2.isMarked(0));
assertTrue(entryB2.isMarked(1));
assertTrue(entryB2.isMarked(2));
assertEquals(6, entryB2.getTreeFilterMarks());
assertThat(entryC.getNewPath(), is("c.txt"));
assertFalse(entryC.isMarked(0));
assertFalse(entryC.isMarked(1));
assertFalse(entryC.isMarked(2));
assertEquals(0, entryC.getTreeFilterMarks());
}
}
@Test(expected = IllegalArgumentException.class)
@ -339,9 +347,10 @@ public class DiffEntryTest extends RepositoryTestCase {
// given - we don't need anything here
// when
TreeWalk walk = new TreeWalk(db);
walk.addTree(new EmptyTreeIterator());
DiffEntry.scan(walk);
try (TreeWalk walk = new TreeWalk(db)) {
walk.addTree(new EmptyTreeIterator());
DiffEntry.scan(walk);
}
}
@Test(expected = IllegalArgumentException.class)
@ -350,11 +359,12 @@ public class DiffEntryTest extends RepositoryTestCase {
// given - we don't need anything here
// when
TreeWalk walk = new TreeWalk(db);
walk.addTree(new EmptyTreeIterator());
walk.addTree(new EmptyTreeIterator());
walk.addTree(new EmptyTreeIterator());
DiffEntry.scan(walk);
try (TreeWalk walk = new TreeWalk(db)) {
walk.addTree(new EmptyTreeIterator());
walk.addTree(new EmptyTreeIterator());
walk.addTree(new EmptyTreeIterator());
DiffEntry.scan(walk);
}
}
@Test(expected = IllegalArgumentException.class)
@ -363,46 +373,47 @@ public class DiffEntryTest extends RepositoryTestCase {
// given - we don't need anything here
// when
TreeWalk walk = new TreeWalk(db);
walk.addTree(new EmptyTreeIterator());
walk.addTree(new EmptyTreeIterator());
walk.setRecursive(true);
DiffEntry.scan(walk, true);
try (TreeWalk walk = new TreeWalk(db)) {
walk.addTree(new EmptyTreeIterator());
walk.addTree(new EmptyTreeIterator());
walk.setRecursive(true);
DiffEntry.scan(walk, true);
}
}
@Test
public void shouldReportFileModeChange() throws Exception {
writeTrashFile("a.txt", "content");
Git git = new Git(db);
git.add().addFilepattern("a.txt").call();
RevCommit c1 = git.commit().setMessage("initial commit").call();
DirCache cache = db.lockDirCache();
DirCacheEditor editor = cache.editor();
final TreeWalk walk = new TreeWalk(db);
walk.addTree(c1.getTree());
walk.setRecursive(true);
assertTrue(walk.next());
editor.add(new PathEdit("a.txt") {
public void apply(DirCacheEntry ent) {
ent.setFileMode(FileMode.EXECUTABLE_FILE);
ent.setObjectId(walk.getObjectId(0));
}
});
assertTrue(editor.commit());
RevCommit c2 = git.commit().setMessage("second commit").call();
walk.reset();
walk.addTree(c1.getTree());
walk.addTree(c2.getTree());
List<DiffEntry> diffs = DiffEntry.scan(walk, false);
assertEquals(1, diffs.size());
DiffEntry diff = diffs.get(0);
assertEquals(ChangeType.MODIFY,diff.getChangeType());
assertEquals(diff.getOldId(), diff.getNewId());
assertEquals("a.txt", diff.getOldPath());
assertEquals(diff.getOldPath(), diff.getNewPath());
assertEquals(FileMode.EXECUTABLE_FILE, diff.getNewMode());
assertEquals(FileMode.REGULAR_FILE, diff.getOldMode());
try (Git git = new Git(db);
TreeWalk walk = new TreeWalk(db)) {
git.add().addFilepattern("a.txt").call();
RevCommit c1 = git.commit().setMessage("initial commit").call();
DirCache cache = db.lockDirCache();
DirCacheEditor editor = cache.editor();
walk.addTree(c1.getTree());
walk.setRecursive(true);
assertTrue(walk.next());
editor.add(new PathEdit("a.txt") {
public void apply(DirCacheEntry ent) {
ent.setFileMode(FileMode.EXECUTABLE_FILE);
ent.setObjectId(walk.getObjectId(0));
}
});
assertTrue(editor.commit());
RevCommit c2 = git.commit().setMessage("second commit").call();
walk.reset();
walk.addTree(c1.getTree());
walk.addTree(c2.getTree());
List<DiffEntry> diffs = DiffEntry.scan(walk, false);
assertEquals(1, diffs.size());
DiffEntry diff = diffs.get(0);
assertEquals(ChangeType.MODIFY,diff.getChangeType());
assertEquals(diff.getOldId(), diff.getNewId());
assertEquals("a.txt", diff.getOldPath());
assertEquals(diff.getOldPath(), diff.getNewPath());
assertEquals(FileMode.EXECUTABLE_FILE, diff.getNewMode());
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.junit.Test;
@SuppressWarnings("deprecation")
public class T0003_BasicTest extends SampleDataRepositoryTestCase {
@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.junit.Test;
@SuppressWarnings("deprecation")
public class IndexDiffTest extends RepositoryTestCase {
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.junit.Test;
@SuppressWarnings("deprecation")
public class ObjectWalkTest extends RevWalkTestCase {
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
public void testDirCacheMatchingId() throws Exception {
File f = writeTrashFile("file", "content");
Git git = new Git(db);
writeTrashFile("file", "content");
fsTick(f);
git.add().addFilepattern("file").call();
try (Git git = new Git(db)) {
writeTrashFile("file", "content");
fsTick(f);
git.add().addFilepattern("file").call();
}
DirCacheEntry dce = db.readDirCache().getEntry("file");
TreeWalk tw = new TreeWalk(db);
FileTreeIterator fti = new FileTreeIterator(trash, db.getFS(), db
@ -282,11 +283,12 @@ public class FileTreeIteratorTest extends RepositoryTestCase {
@Test
public void testIsModifiedSymlinkAsFile() throws Exception {
writeTrashFile("symlink", "content");
Git git = new Git(db);
db.getConfig().setString(ConfigConstants.CONFIG_CORE_SECTION, null,
ConfigConstants.CONFIG_KEY_SYMLINKS, "false");
git.add().addFilepattern("symlink").call();
git.commit().setMessage("commit").call();
try (Git git = new Git(db)) {
db.getConfig().setString(ConfigConstants.CONFIG_CORE_SECTION, null,
ConfigConstants.CONFIG_KEY_SYMLINKS, "false");
git.add().addFilepattern("symlink").call();
git.commit().setMessage("commit").call();
}
// Modify previously committed DirCacheEntry and write it back to disk
DirCacheEntry dce = db.readDirCache().getEntry("symlink");
@ -305,20 +307,21 @@ public class FileTreeIteratorTest extends RepositoryTestCase {
@Test
public void testIsModifiedFileSmudged() throws Exception {
File f = writeTrashFile("file", "content");
Git git = new Git(db);
// The idea of this test is to check the smudged handling
// Hopefully fsTick will make sure our entry gets smudged
fsTick(f);
writeTrashFile("file", "content");
long lastModified = f.lastModified();
git.add().addFilepattern("file").call();
writeTrashFile("file", "conten2");
f.setLastModified(lastModified);
// We cannot trust this to go fast enough on
// a system with less than one-second lastModified
// resolution, so we force the index to have the
// same timestamp as the file we look at.
db.getIndexFile().setLastModified(lastModified);
try (Git git = new Git(db)) {
// The idea of this test is to check the smudged handling
// Hopefully fsTick will make sure our entry gets smudged
fsTick(f);
writeTrashFile("file", "content");
long lastModified = f.lastModified();
git.add().addFilepattern("file").call();
writeTrashFile("file", "conten2");
f.setLastModified(lastModified);
// We cannot trust this to go fast enough on
// a system with less than one-second lastModified
// resolution, so we force the index to have the
// same timestamp as the file we look at.
db.getIndexFile().setLastModified(lastModified);
}
DirCacheEntry dce = db.readDirCache().getEntry("file");
FileTreeIterator fti = new FileTreeIterator(trash, db.getFS(), db
.getConfig().get(WorkingTreeOptions.KEY));
@ -334,198 +337,204 @@ public class FileTreeIteratorTest extends RepositoryTestCase {
@Test
public void submoduleHeadMatchesIndex() throws Exception {
Git git = new Git(db);
writeTrashFile("file.txt", "content");
git.add().addFilepattern("file.txt").call();
final RevCommit id = git.commit().setMessage("create file").call();
final String path = "sub";
DirCache cache = db.lockDirCache();
DirCacheEditor editor = cache.editor();
editor.add(new PathEdit(path) {
public void apply(DirCacheEntry ent) {
ent.setFileMode(FileMode.GITLINK);
ent.setObjectId(id);
}
});
editor.commit();
Git.cloneRepository().setURI(db.getDirectory().toURI().toString())
.setDirectory(new File(db.getWorkTree(), path)).call()
.getRepository().close();
TreeWalk walk = new TreeWalk(db);
DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
FileTreeIterator workTreeIter = new FileTreeIterator(db);
walk.addTree(indexIter);
walk.addTree(workTreeIter);
walk.setFilter(PathFilter.create(path));
assertTrue(walk.next());
assertTrue(indexIter.idEqual(workTreeIter));
try (Git git = new Git(db);
TreeWalk walk = new TreeWalk(db)) {
writeTrashFile("file.txt", "content");
git.add().addFilepattern("file.txt").call();
final RevCommit id = git.commit().setMessage("create file").call();
final String path = "sub";
DirCache cache = db.lockDirCache();
DirCacheEditor editor = cache.editor();
editor.add(new PathEdit(path) {
public void apply(DirCacheEntry ent) {
ent.setFileMode(FileMode.GITLINK);
ent.setObjectId(id);
}
});
editor.commit();
Git.cloneRepository().setURI(db.getDirectory().toURI().toString())
.setDirectory(new File(db.getWorkTree(), path)).call()
.getRepository().close();
DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
FileTreeIterator workTreeIter = new FileTreeIterator(db);
walk.addTree(indexIter);
walk.addTree(workTreeIter);
walk.setFilter(PathFilter.create(path));
assertTrue(walk.next());
assertTrue(indexIter.idEqual(workTreeIter));
}
}
@Test
public void submoduleWithNoGitDirectory() throws Exception {
Git git = new Git(db);
writeTrashFile("file.txt", "content");
git.add().addFilepattern("file.txt").call();
final RevCommit id = git.commit().setMessage("create file").call();
final String path = "sub";
DirCache cache = db.lockDirCache();
DirCacheEditor editor = cache.editor();
editor.add(new PathEdit(path) {
public void apply(DirCacheEntry ent) {
ent.setFileMode(FileMode.GITLINK);
ent.setObjectId(id);
}
});
editor.commit();
File submoduleRoot = new File(db.getWorkTree(), path);
assertTrue(submoduleRoot.mkdir());
assertTrue(new File(submoduleRoot, Constants.DOT_GIT).mkdir());
TreeWalk walk = new TreeWalk(db);
DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
FileTreeIterator workTreeIter = new FileTreeIterator(db);
walk.addTree(indexIter);
walk.addTree(workTreeIter);
walk.setFilter(PathFilter.create(path));
assertTrue(walk.next());
assertFalse(indexIter.idEqual(workTreeIter));
assertEquals(ObjectId.zeroId(), workTreeIter.getEntryObjectId());
try (Git git = new Git(db);
TreeWalk walk = new TreeWalk(db)) {
writeTrashFile("file.txt", "content");
git.add().addFilepattern("file.txt").call();
final RevCommit id = git.commit().setMessage("create file").call();
final String path = "sub";
DirCache cache = db.lockDirCache();
DirCacheEditor editor = cache.editor();
editor.add(new PathEdit(path) {
public void apply(DirCacheEntry ent) {
ent.setFileMode(FileMode.GITLINK);
ent.setObjectId(id);
}
});
editor.commit();
File submoduleRoot = new File(db.getWorkTree(), path);
assertTrue(submoduleRoot.mkdir());
assertTrue(new File(submoduleRoot, Constants.DOT_GIT).mkdir());
DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
FileTreeIterator workTreeIter = new FileTreeIterator(db);
walk.addTree(indexIter);
walk.addTree(workTreeIter);
walk.setFilter(PathFilter.create(path));
assertTrue(walk.next());
assertFalse(indexIter.idEqual(workTreeIter));
assertEquals(ObjectId.zeroId(), workTreeIter.getEntryObjectId());
}
}
@Test
public void submoduleWithNoHead() throws Exception {
Git git = new Git(db);
writeTrashFile("file.txt", "content");
git.add().addFilepattern("file.txt").call();
final RevCommit id = git.commit().setMessage("create file").call();
final String path = "sub";
DirCache cache = db.lockDirCache();
DirCacheEditor editor = cache.editor();
editor.add(new PathEdit(path) {
public void apply(DirCacheEntry ent) {
ent.setFileMode(FileMode.GITLINK);
ent.setObjectId(id);
}
});
editor.commit();
assertNotNull(Git.init().setDirectory(new File(db.getWorkTree(), path))
.call().getRepository());
TreeWalk walk = new TreeWalk(db);
DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
FileTreeIterator workTreeIter = new FileTreeIterator(db);
walk.addTree(indexIter);
walk.addTree(workTreeIter);
walk.setFilter(PathFilter.create(path));
assertTrue(walk.next());
assertFalse(indexIter.idEqual(workTreeIter));
assertEquals(ObjectId.zeroId(), workTreeIter.getEntryObjectId());
try (Git git = new Git(db);
TreeWalk walk = new TreeWalk(db)) {
writeTrashFile("file.txt", "content");
git.add().addFilepattern("file.txt").call();
final RevCommit id = git.commit().setMessage("create file").call();
final String path = "sub";
DirCache cache = db.lockDirCache();
DirCacheEditor editor = cache.editor();
editor.add(new PathEdit(path) {
public void apply(DirCacheEntry ent) {
ent.setFileMode(FileMode.GITLINK);
ent.setObjectId(id);
}
});
editor.commit();
assertNotNull(Git.init().setDirectory(new File(db.getWorkTree(), path))
.call().getRepository());
DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
FileTreeIterator workTreeIter = new FileTreeIterator(db);
walk.addTree(indexIter);
walk.addTree(workTreeIter);
walk.setFilter(PathFilter.create(path));
assertTrue(walk.next());
assertFalse(indexIter.idEqual(workTreeIter));
assertEquals(ObjectId.zeroId(), workTreeIter.getEntryObjectId());
}
}
@Test
public void submoduleDirectoryIterator() throws Exception {
Git git = new Git(db);
writeTrashFile("file.txt", "content");
git.add().addFilepattern("file.txt").call();
final RevCommit id = git.commit().setMessage("create file").call();
final String path = "sub";
DirCache cache = db.lockDirCache();
DirCacheEditor editor = cache.editor();
editor.add(new PathEdit(path) {
public void apply(DirCacheEntry ent) {
ent.setFileMode(FileMode.GITLINK);
ent.setObjectId(id);
}
});
editor.commit();
Git.cloneRepository().setURI(db.getDirectory().toURI().toString())
.setDirectory(new File(db.getWorkTree(), path)).call()
.getRepository().close();
TreeWalk walk = new TreeWalk(db);
DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
FileTreeIterator workTreeIter = new FileTreeIterator(db.getWorkTree(),
db.getFS(), db.getConfig().get(WorkingTreeOptions.KEY));
walk.addTree(indexIter);
walk.addTree(workTreeIter);
walk.setFilter(PathFilter.create(path));
assertTrue(walk.next());
assertTrue(indexIter.idEqual(workTreeIter));
try (Git git = new Git(db);
TreeWalk walk = new TreeWalk(db)) {
writeTrashFile("file.txt", "content");
git.add().addFilepattern("file.txt").call();
final RevCommit id = git.commit().setMessage("create file").call();
final String path = "sub";
DirCache cache = db.lockDirCache();
DirCacheEditor editor = cache.editor();
editor.add(new PathEdit(path) {
public void apply(DirCacheEntry ent) {
ent.setFileMode(FileMode.GITLINK);
ent.setObjectId(id);
}
});
editor.commit();
Git.cloneRepository().setURI(db.getDirectory().toURI().toString())
.setDirectory(new File(db.getWorkTree(), path)).call()
.getRepository().close();
DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
FileTreeIterator workTreeIter = new FileTreeIterator(db.getWorkTree(),
db.getFS(), db.getConfig().get(WorkingTreeOptions.KEY));
walk.addTree(indexIter);
walk.addTree(workTreeIter);
walk.setFilter(PathFilter.create(path));
assertTrue(walk.next());
assertTrue(indexIter.idEqual(workTreeIter));
}
}
@Test
public void submoduleNestedWithHeadMatchingIndex() throws Exception {
Git git = new Git(db);
writeTrashFile("file.txt", "content");
git.add().addFilepattern("file.txt").call();
final RevCommit id = git.commit().setMessage("create file").call();
final String path = "sub/dir1/dir2";
DirCache cache = db.lockDirCache();
DirCacheEditor editor = cache.editor();
editor.add(new PathEdit(path) {
public void apply(DirCacheEntry ent) {
ent.setFileMode(FileMode.GITLINK);
ent.setObjectId(id);
}
});
editor.commit();
Git.cloneRepository().setURI(db.getDirectory().toURI().toString())
.setDirectory(new File(db.getWorkTree(), path)).call()
.getRepository().close();
TreeWalk walk = new TreeWalk(db);
DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
FileTreeIterator workTreeIter = new FileTreeIterator(db);
walk.addTree(indexIter);
walk.addTree(workTreeIter);
walk.setFilter(PathFilter.create(path));
assertTrue(walk.next());
assertTrue(indexIter.idEqual(workTreeIter));
try (Git git = new Git(db);
TreeWalk walk = new TreeWalk(db)) {
writeTrashFile("file.txt", "content");
git.add().addFilepattern("file.txt").call();
final RevCommit id = git.commit().setMessage("create file").call();
final String path = "sub/dir1/dir2";
DirCache cache = db.lockDirCache();
DirCacheEditor editor = cache.editor();
editor.add(new PathEdit(path) {
public void apply(DirCacheEntry ent) {
ent.setFileMode(FileMode.GITLINK);
ent.setObjectId(id);
}
});
editor.commit();
Git.cloneRepository().setURI(db.getDirectory().toURI().toString())
.setDirectory(new File(db.getWorkTree(), path)).call()
.getRepository().close();
DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
FileTreeIterator workTreeIter = new FileTreeIterator(db);
walk.addTree(indexIter);
walk.addTree(workTreeIter);
walk.setFilter(PathFilter.create(path));
assertTrue(walk.next());
assertTrue(indexIter.idEqual(workTreeIter));
}
}
@Test
public void idOffset() throws Exception {
Git git = new Git(db);
writeTrashFile("fileAinfsonly", "A");
File fileBinindex = writeTrashFile("fileBinindex", "B");
fsTick(fileBinindex);
git.add().addFilepattern("fileBinindex").call();
writeTrashFile("fileCinfsonly", "C");
TreeWalk tw = new TreeWalk(db);
DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
FileTreeIterator workTreeIter = new FileTreeIterator(db);
tw.addTree(indexIter);
tw.addTree(workTreeIter);
workTreeIter.setDirCacheIterator(tw, 0);
assertEntry("d46c305e85b630558ee19cc47e73d2e5c8c64cdc", "a,", tw);
assertEntry("58ee403f98538ec02409538b3f80adf610accdec", "a,b", tw);
assertEntry("0000000000000000000000000000000000000000", "a", tw);
assertEntry("b8d30ff397626f0f1d3538d66067edf865e201d6", "a0b", tw);
// The reason for adding this test. Check that the id is correct for
// mixed
assertEntry("8c7e5a667f1b771847fe88c01c3de34413a1b220",
"fileAinfsonly", tw);
assertEntry("7371f47a6f8bd23a8fa1a8b2a9479cdd76380e54", "fileBinindex",
tw);
assertEntry("96d80cd6c4e7158dbebd0849f4fb7ce513e5828c",
"fileCinfsonly", tw);
assertFalse(tw.next());
try (Git git = new Git(db);
TreeWalk tw = new TreeWalk(db)) {
writeTrashFile("fileAinfsonly", "A");
File fileBinindex = writeTrashFile("fileBinindex", "B");
fsTick(fileBinindex);
git.add().addFilepattern("fileBinindex").call();
writeTrashFile("fileCinfsonly", "C");
DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
FileTreeIterator workTreeIter = new FileTreeIterator(db);
tw.addTree(indexIter);
tw.addTree(workTreeIter);
workTreeIter.setDirCacheIterator(tw, 0);
assertEntry("d46c305e85b630558ee19cc47e73d2e5c8c64cdc", "a,", tw);
assertEntry("58ee403f98538ec02409538b3f80adf610accdec", "a,b", tw);
assertEntry("0000000000000000000000000000000000000000", "a", tw);
assertEntry("b8d30ff397626f0f1d3538d66067edf865e201d6", "a0b", tw);
// The reason for adding this test. Check that the id is correct for
// mixed
assertEntry("8c7e5a667f1b771847fe88c01c3de34413a1b220",
"fileAinfsonly", tw);
assertEntry("7371f47a6f8bd23a8fa1a8b2a9479cdd76380e54", "fileBinindex",
tw);
assertEntry("96d80cd6c4e7158dbebd0849f4fb7ce513e5828c",
"fileCinfsonly", tw);
assertFalse(tw.next());
}
}
private static void assertEntry(String sha1string, String path, TreeWalk tw)

Loading…
Cancel
Save