diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/filter/IndexDiffFilterTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/filter/IndexDiffFilterTest.java index 008463691..43ea78d72 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/filter/IndexDiffFilterTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/filter/IndexDiffFilterTest.java @@ -43,6 +43,10 @@ */ package org.eclipse.jgit.treewalk.filter; +import java.io.File; + +import junit.framework.AssertionFailedError; + import org.eclipse.jgit.api.Git; import org.eclipse.jgit.dircache.DirCacheIterator; import org.eclipse.jgit.lib.RepositoryTestCase; @@ -51,28 +55,45 @@ import org.eclipse.jgit.treewalk.FileTreeIterator; import org.eclipse.jgit.treewalk.TreeWalk; public class IndexDiffFilterTest extends RepositoryTestCase { - private RevCommit commit; + private static final String FILE = "file"; - public void setUp() throws Exception { - super.setUp(); + private static final String UNTRACKED_FILE = "untracked_file"; + + private static final String IGNORED_FILE = "ignored_file"; + + private static final String FILE_IN_FOLDER = "folder/file"; + + private static final String UNTRACKED_FILE_IN_FOLDER = "folder/untracked_file"; + + private static final String IGNORED_FILE_IN_FOLDER = "folder/ignored_file"; + + private static final String FILE_IN_IGNORED_FOLDER = "ignored_folder/file"; + + private static final String FOLDER = "folder"; + + private static final String UNTRACKED_FOLDER = "untracked_folder"; + + private static final String IGNORED_FOLDER = "ignored_folder"; + + private static final String GITIGNORE = ".gitignore"; - Git git = new Git(db); - writeTrashFile("folder/file", "content"); - git.add().addFilepattern("folder/file").call(); - commit = git.commit().setMessage("commit").call(); + private static final String FILE_CONTENT = "content"; - deleteTrashFile("folder/file"); - deleteTrashFile("folder"); - writeTrashFile("folder", "content"); + private static final String MODIFIED_FILE_CONTENT = "modified_content"; + + private Git git; + + public void setUp() throws Exception { + super.setUp(); + git = new Git(db); } public void testRecursiveTreeWalk() throws Exception { - TreeWalk treeWalk = new TreeWalk(db); - treeWalk.setRecursive(true); - treeWalk.addTree(commit.getTree()); - treeWalk.addTree(new DirCacheIterator(db.readDirCache())); - treeWalk.addTree(new FileTreeIterator(db)); - treeWalk.setFilter(new IndexDiffFilter(1, 2)); + RevCommit commit = writeFileInFolderAndCommit(); + deleteAll(); + writeFileWithFolderName(); + TreeWalk treeWalk = createTreeWalk(commit); + assertTrue(treeWalk.next()); assertEquals("folder", treeWalk.getPathString()); assertTrue(treeWalk.next()); @@ -81,12 +102,11 @@ public class IndexDiffFilterTest extends RepositoryTestCase { } public void testNonRecursiveTreeWalk() throws Exception { - TreeWalk treeWalk = new TreeWalk(db); - treeWalk.setRecursive(false); - treeWalk.addTree(commit.getTree()); - treeWalk.addTree(new DirCacheIterator(db.readDirCache())); - treeWalk.addTree(new FileTreeIterator(db)); - treeWalk.setFilter(new IndexDiffFilter(1, 2)); + RevCommit commit = writeFileInFolderAndCommit(); + deleteAll(); + writeFileWithFolderName(); + TreeWalk treeWalk = createNonRecursiveTreeWalk(commit); + assertTrue(treeWalk.next()); assertEquals("folder", treeWalk.getPathString()); assertTrue(treeWalk.next()); @@ -97,4 +117,481 @@ public class IndexDiffFilterTest extends RepositoryTestCase { assertEquals("folder/file", treeWalk.getPathString()); assertFalse(treeWalk.next()); } + + public void testFileCommitted() throws Exception { + RevCommit commit = writeFileAndCommit(); + TreeWalk treeWalk = createTreeWalk(commit); + assertFalse(treeWalk.next()); + } + + public void testFileInFolderCommitted() throws Exception { + RevCommit commit = writeFileInFolderAndCommit(); + TreeWalk treeWalk = createTreeWalk(commit); + assertFalse(treeWalk.next()); + } + + public void testEmptyFolderCommitted() throws Exception { + RevCommit commit = createEmptyFolderAndCommit(); + TreeWalk treeWalk = createTreeWalk(commit); + assertFalse(treeWalk.next()); + } + + public void testFileCommittedChangedNotModified() throws Exception { + RevCommit commit = writeFileAndCommit(); + writeFile(); + TreeWalk treeWalk = createTreeWalk(commit); + assertFalse(treeWalk.next()); + } + + public void testFileInFolderCommittedChangedNotModified() throws Exception { + RevCommit commit = writeFileInFolderAndCommit(); + writeFileInFolder(); + TreeWalk treeWalk = createTreeWalk(commit); + assertFalse(treeWalk.next()); + } + + public void testFileCommittedModified() throws Exception { + RevCommit commit = writeFileAndCommit(); + writeFileModified(); + TreeWalk treeWalk = createTreeWalk(commit); + assertPaths(treeWalk, FILE); + } + + public void testFileInFolderCommittedModified() throws Exception { + RevCommit commit = writeFileInFolderAndCommit(); + writeFileInFolderModified(); + TreeWalk treeWalk = createTreeWalk(commit); + assertPaths(treeWalk, FILE_IN_FOLDER); + } + + public void testFileCommittedDeleted() throws Exception { + RevCommit commit = writeFileAndCommit(); + deleteFile(); + TreeWalk treeWalk = createTreeWalk(commit); + assertPaths(treeWalk, FILE); + } + + public void testFileInFolderCommittedDeleted() throws Exception { + RevCommit commit = writeFileInFolderAndCommit(); + deleteFileInFolder(); + TreeWalk treeWalk = createTreeWalk(commit); + assertPaths(treeWalk, FILE_IN_FOLDER); + } + + public void testFileInFolderCommittedAllDeleted() throws Exception { + RevCommit commit = writeFileInFolderAndCommit(); + deleteAll(); + TreeWalk treeWalk = createTreeWalk(commit); + assertPaths(treeWalk, FILE_IN_FOLDER); + } + + public void testEmptyFolderCommittedDeleted() throws Exception { + RevCommit commit = createEmptyFolderAndCommit(); + deleteFolder(); + TreeWalk treeWalk = createTreeWalk(commit); + assertFalse(treeWalk.next()); + } + + public void testFileCommittedModifiedCommittedComparedWithInitialCommit() + throws Exception { + RevCommit commit = writeFileAndCommit(); + writeFileModifiedAndCommit(); + TreeWalk treeWalk = createTreeWalk(commit); + assertPaths(treeWalk, FILE); + } + + public void testFileInFolderCommittedModifiedCommittedComparedWithInitialCommit() + throws Exception { + RevCommit commit = writeFileInFolderAndCommit(); + writeFileInFolderModifiedAndCommit(); + TreeWalk treeWalk = createTreeWalk(commit); + assertPaths(treeWalk, FILE_IN_FOLDER); + } + + public void testFileCommittedDeletedCommittedComparedWithInitialCommit() + throws Exception { + RevCommit commit = writeFileAndCommit(); + deleteFileAndCommit(); + TreeWalk treeWalk = createTreeWalk(commit); + assertPaths(treeWalk, FILE); + } + + public void testFileInFolderCommittedDeletedCommittedComparedWithInitialCommit() + throws Exception { + RevCommit commit = writeFileInFolderAndCommit(); + deleteFileInFolderAndCommit(); + TreeWalk treeWalk = createTreeWalk(commit); + assertPaths(treeWalk, FILE_IN_FOLDER); + } + + public void testFileInFolderCommittedAllDeletedCommittedComparedWithInitialCommit() + throws Exception { + RevCommit commit = writeFileInFolderAndCommit(); + deleteAllAndCommit(); + TreeWalk treeWalk = createTreeWalk(commit); + assertPaths(treeWalk, FILE_IN_FOLDER); + } + + public void testEmptyFolderCommittedDeletedCommittedComparedWithInitialCommit() + throws Exception { + RevCommit commit = createEmptyFolderAndCommit(); + deleteFolderAndCommit(); + TreeWalk treeWalk = createTreeWalk(commit); + assertFalse(treeWalk.next()); + } + + public void testFileUntracked() throws Exception { + RevCommit commit = writeFileAndCommit(); + writeFileUntracked(); + TreeWalk treeWalk = createTreeWalk(commit); + assertPaths(treeWalk, UNTRACKED_FILE); + } + + public void testFileInFolderUntracked() throws Exception { + RevCommit commit = writeFileInFolderAndCommit(); + writeFileInFolderUntracked(); + TreeWalk treeWalk = createTreeWalk(commit); + assertPaths(treeWalk, UNTRACKED_FILE_IN_FOLDER); + } + + public void testEmptyFolderUntracked() throws Exception { + RevCommit commit = createEmptyFolderAndCommit(); + createEmptyFolderUntracked(); + TreeWalk treeWalk = createTreeWalk(commit); + assertFalse(treeWalk.next()); + } + + public void testFileIgnored() throws Exception { + RevCommit commit = writeFileAndCommit(); + writeFileIgnored(); + TreeWalk treeWalk = createTreeWalk(commit); + assertFalse(treeWalk.next()); + } + + public void testFileInFolderIgnored() throws Exception { + RevCommit commit = writeFileInFolderAndCommit(); + writeFileInFolderIgnored(); + TreeWalk treeWalk = createTreeWalk(commit); + assertFalse(treeWalk.next()); + } + + public void testFileInFolderAllIgnored() throws Exception { + RevCommit commit = writeFileInFolderAndCommit(); + writeFileInFolderAllIgnored(); + TreeWalk treeWalk = createTreeWalk(commit); + assertFalse(treeWalk.next()); + } + + public void testEmptyFolderIgnored() throws Exception { + RevCommit commit = createEmptyFolderAndCommit(); + createEmptyFolderIgnored(); + TreeWalk treeWalk = createTreeWalk(commit); + assertFalse(treeWalk.next()); + } + + public void testFileIgnoredNotHonored() throws Exception { + RevCommit commit = writeFileAndCommit(); + writeFileIgnored(); + TreeWalk treeWalk = createTreeWalkDishonorIgnores(commit); + assertPaths(treeWalk, IGNORED_FILE, GITIGNORE); + } + + public void testFileCommittedModifiedIgnored() throws Exception { + RevCommit commit = writeFileAndCommit(); + writeFileModifiedIgnored(); + TreeWalk treeWalk = createTreeWalk(commit); + assertPaths(treeWalk, FILE); + } + + public void testFileInFolderCommittedModifiedIgnored() throws Exception { + RevCommit commit = writeFileInFolderAndCommit(); + writeFileInFolderModifiedIgnored(); + TreeWalk treeWalk = createTreeWalk(commit); + assertPaths(treeWalk, FILE_IN_FOLDER); + } + + public void testFileInFolderCommittedModifiedAllIgnored() throws Exception { + RevCommit commit = writeFileInFolderAndCommit(); + writeFileInFolderModifiedAllIgnored(); + TreeWalk treeWalk = createTreeWalk(commit); + assertPaths(treeWalk, FILE_IN_FOLDER); + } + + public void testFileCommittedDeletedCommittedIgnoredComparedWithInitialCommit() + throws Exception { + RevCommit commit = writeFileAndCommit(); + deleteFileAndCommit(); + rewriteFileIgnored(); + TreeWalk treeWalk = createTreeWalk(commit); + assertPaths(treeWalk, FILE); + } + + public void testFileInFolderCommittedDeletedCommittedIgnoredComparedWithInitialCommit() + throws Exception { + RevCommit commit = writeFileInFolderAndCommit(); + deleteFileInFolderAndCommit(); + rewriteFileInFolderIgnored(); + TreeWalk treeWalk = createTreeWalk(commit); + assertPaths(treeWalk, FILE_IN_FOLDER); + } + + public void testFileInFolderCommittedAllDeletedCommittedAllIgnoredComparedWithInitialCommit() + throws Exception { + RevCommit commit = writeFileInFolderAndCommit(); + deleteAllAndCommit(); + rewriteFileInFolderAllIgnored(); + TreeWalk treeWalk = createTreeWalk(commit); + assertPaths(treeWalk, FILE_IN_FOLDER); + } + + public void testEmptyFolderCommittedDeletedCommittedIgnoredComparedWithInitialCommit() + throws Exception { + RevCommit commit = createEmptyFolderAndCommit(); + deleteFolderAndCommit(); + recreateEmptyFolderIgnored(); + TreeWalk treeWalk = createTreeWalk(commit); + assertFalse(treeWalk.next()); + } + + public void testFileInFolderCommittedNonRecursive() throws Exception { + RevCommit commit = writeFileInFolderAndCommit(); + TreeWalk treeWalk = createNonRecursiveTreeWalk(commit); + assertPaths(treeWalk, FOLDER); + } + + public void testFolderChangedToFile() throws Exception { + RevCommit commit = writeFileInFolderAndCommit(); + deleteAll(); + writeFileWithFolderName(); + TreeWalk treeWalk = createTreeWalk(commit); + assertPaths(treeWalk, FOLDER, FILE_IN_FOLDER); + } + + public void testFolderChangedToFileCommittedComparedWithInitialCommit() + throws Exception { + RevCommit commit = writeFileInFolderAndCommit(); + deleteAll(); + writeFileWithFolderNameAndCommit(); + TreeWalk treeWalk = createTreeWalk(commit); + assertPaths(treeWalk, FOLDER, FILE_IN_FOLDER); + } + + private void writeFile() throws Exception { + writeTrashFile(FILE, FILE_CONTENT); + } + + private RevCommit writeFileAndCommit() throws Exception { + writeFile(); + return commitAdd(); + } + + private void writeFileModified() throws Exception { + writeTrashFile(FILE, MODIFIED_FILE_CONTENT); + } + + private void writeFileModifiedAndCommit() throws Exception { + writeFileModified(); + commitAdd(); + } + + private void writeFileUntracked() throws Exception { + writeTrashFile(UNTRACKED_FILE, FILE_CONTENT); + } + + private void writeFileIgnored() throws Exception { + writeTrashFile(IGNORED_FILE, FILE_CONTENT); + writeTrashFile(GITIGNORE, GITIGNORE + "\n" + IGNORED_FILE); + } + + private void writeFileModifiedIgnored() throws Exception { + writeFileModified(); + writeTrashFile(GITIGNORE, GITIGNORE + "\n" + FILE); + } + + private void rewriteFileIgnored() throws Exception { + writeFile(); + writeTrashFile(GITIGNORE, GITIGNORE + "\n" + FILE); + } + + private void writeFileWithFolderName() throws Exception { + writeTrashFile(FOLDER, FILE_CONTENT); + } + + private void writeFileWithFolderNameAndCommit() throws Exception { + writeFileWithFolderName(); + commitAdd(); + } + + private void deleteFile() throws Exception { + deleteTrashFile(FILE); + } + + private void deleteFileAndCommit() throws Exception { + deleteFile(); + commitRm(FILE); + } + + private void writeFileInFolder() throws Exception { + writeTrashFile(FILE_IN_FOLDER, FILE_CONTENT); + } + + private RevCommit writeFileInFolderAndCommit() throws Exception { + writeFileInFolder(); + return commitAdd(); + } + + private void writeFileInFolderModified() throws Exception { + writeTrashFile(FILE_IN_FOLDER, MODIFIED_FILE_CONTENT); + } + + private void writeFileInFolderModifiedAndCommit() throws Exception { + writeFileInFolderModified(); + commitAdd(); + } + + private void writeFileInFolderUntracked() throws Exception { + writeTrashFile(UNTRACKED_FILE_IN_FOLDER, FILE_CONTENT); + } + + private void writeFileInFolderIgnored() throws Exception { + writeTrashFile(IGNORED_FILE_IN_FOLDER, FILE_CONTENT); + writeTrashFile(GITIGNORE, GITIGNORE + "\n" + IGNORED_FILE_IN_FOLDER); + } + + private void writeFileInFolderAllIgnored() throws Exception { + writeTrashFile(FILE_IN_IGNORED_FOLDER, FILE_CONTENT); + writeTrashFile(GITIGNORE, GITIGNORE + "\n" + IGNORED_FOLDER + "/"); + } + + private void writeFileInFolderModifiedIgnored() throws Exception { + writeFileInFolderModified(); + writeTrashFile(GITIGNORE, GITIGNORE + "\n" + FILE_IN_FOLDER); + } + + private void rewriteFileInFolderIgnored() throws Exception { + writeFileInFolder(); + writeTrashFile(GITIGNORE, GITIGNORE + "\n" + FILE_IN_FOLDER); + } + + private void writeFileInFolderModifiedAllIgnored() throws Exception { + writeFileInFolderModified(); + writeTrashFile(GITIGNORE, GITIGNORE + "\n" + FOLDER + "/"); + } + + private void rewriteFileInFolderAllIgnored() throws Exception { + writeFileInFolder(); + writeTrashFile(GITIGNORE, GITIGNORE + "\n" + FOLDER + "/"); + } + + private void deleteFileInFolder() throws Exception { + deleteTrashFile(FILE_IN_FOLDER); + } + + private void deleteFileInFolderAndCommit() throws Exception { + deleteFileInFolder(); + commitRm(FILE_IN_FOLDER); + } + + private void createEmptyFolder() throws Exception { + File path = new File(db.getWorkTree(), FOLDER); + path.mkdir(); + assertTrue(path.isDirectory()); + } + + private RevCommit createEmptyFolderAndCommit() throws Exception { + createEmptyFolder(); + return commitAdd(); + } + + private void createEmptyFolderUntracked() throws Exception { + File path = new File(db.getWorkTree(), UNTRACKED_FOLDER); + path.mkdir(); + assertTrue(path.isDirectory()); + } + + private void createEmptyFolderIgnored() throws Exception { + File path = new File(db.getWorkTree(), IGNORED_FOLDER); + path.mkdir(); + assertTrue(path.isDirectory()); + writeTrashFile(GITIGNORE, GITIGNORE + "\n" + IGNORED_FOLDER + "/"); + } + + private void recreateEmptyFolderIgnored() throws Exception { + createEmptyFolder(); + writeTrashFile(GITIGNORE, GITIGNORE + "\n" + FOLDER + "/"); + } + + private void deleteFolder() throws Exception { + deleteTrashFile(FOLDER); + } + + private void deleteFolderAndCommit() throws Exception { + deleteFolder(); + commitRm(FOLDER); + } + + private void deleteAll() throws Exception { + deleteFileInFolder(); + deleteFolder(); + } + + private void deleteAllAndCommit() throws Exception { + deleteFileInFolderAndCommit(); + deleteFolderAndCommit(); + } + + private RevCommit commitAdd() throws Exception { + git.add().addFilepattern(".").call(); + return git.commit().setMessage("commit").call(); + } + + private RevCommit commitRm(String path) throws Exception { + git.rm().addFilepattern(path).call(); + return git.commit().setMessage("commit").call(); + } + + private TreeWalk createTreeWalk(RevCommit commit) throws Exception { + return createTreeWalk(commit, true, true); + } + + private TreeWalk createTreeWalkDishonorIgnores(RevCommit commit) + throws Exception { + return createTreeWalk(commit, true, false); + } + + private TreeWalk createNonRecursiveTreeWalk(RevCommit commit) + throws Exception { + return createTreeWalk(commit, false, true); + } + + private TreeWalk createTreeWalk(RevCommit commit, boolean isRecursive, + boolean honorIgnores) throws Exception { + TreeWalk treeWalk = new TreeWalk(db); + treeWalk.setRecursive(isRecursive); + treeWalk.addTree(commit.getTree()); + treeWalk.addTree(new DirCacheIterator(db.readDirCache())); + treeWalk.addTree(new FileTreeIterator(db)); + if (!honorIgnores) + treeWalk.setFilter(new IndexDiffFilter(1, 2, honorIgnores)); + else + treeWalk.setFilter(new IndexDiffFilter(1, 2)); + return treeWalk; + } + + private void assertPaths(TreeWalk treeWalk, String... paths) + throws Exception { + for (int i = 0; i < paths.length; i++) { + assertTrue(treeWalk.next()); + assertPath(treeWalk.getPathString(), paths); + } + assertFalse(treeWalk.next()); + } + + private void assertPath(String path, String... paths) { + for (String p : paths) + if (p.equals(path)) + return; + throw new AssertionFailedError("Expected path '" + path + + "' is not returned"); + } }