Browse Source

FileTreeIteratorTest: Open Git and RevWalk in try-with-resource

Change-Id: I6e68c4b028e0aa01964eace10307372060371df7
Signed-off-by: David Pursehouse <david.pursehouse@sonymobile.com>
stable-4.2
David Pursehouse 9 years ago
parent
commit
bebd30f364
  1. 399
      org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/FileTreeIteratorTest.java

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

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

Loading…
Cancel
Save