JGit的状态不会列出任何文件

时间:2016-02-22 22:58:03

标签: jgit

我写了一个Spock测试来学习如何使用JGit。测试的一般想法遵循以下步骤:

  1. 创建“TestRepo”目录
  2. 在那里初始化一个新的Git存储库(“TestRepo / .git”)
  3. 在父目录(TestRepo)中创建一个新文件,并将其文本设置为占用空间的文本
  4. 致电“git status”
  5. (debug)Groovy转储返回的Status对象
  6. 断言返回的Status对象的文件列为untracked
  7. 当我运行以下测试时,它失败了。为什么?

    state.dump()打印

    Status@38989dff 
        diff=org.eclipse.jgit.lib.IndexDiff@72def3cd 
        clean=true 
        hasUncommittedChanges=false
    

    以下代码:

    class GitActionsSpec extends Specification {
        public static final ROOT_DIR_PATH = Paths.get(System.getProperty("user.home"), "TestRepo")
        public static final ROOT_DIR_STRING = ROOT_DIR_PATH.toString()
        public static final GIT_DIR_PATH = ROOT_DIR_PATH.resolve(".git")
    
        @Shared
        Git git
    
        /**
         * Creates a repository in rootDirPath
         */
        def setupSpec() {
            if (Files.exists(ROOT_DIR_PATH)) {
                deleteDirectory(ROOT_DIR_PATH)
            }
            Files.createDirectory(ROOT_DIR_PATH)
    
            /*
            GitActions.createRepoIn(File parentDirectory) {
                return Git.init().setDirectory(f).call()
            }
            */
            git = GitActions.createRepoIn(ROOT_DIR_PATH.toFile())
            assert git.repository.getDirectory().exists()
        }
    
        // The actual test
        def "A newly-created file should be listed as 'untracked'"() {
            given: "A new file"
            Path file = ROOT_DIR_PATH.relativize(ROOT_DIR_PATH.resolve("file.txt"))
            file.text = "filler text"
            assert Files.exists(file)
    
            when: "user requests the status"
            Status state = git.status().addPath(file.toString()).call()
    
            then: "Git lists that file as untracked"
            println state.dump()
            !state.getUntracked().isEmpty()
        }
    
        def cleanupSpec() {
            git.close()
            deleteDirectory(ROOT_DIR_PATH)
        }
    
        def deleteDirectory(Path directory) {
            Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
                @Override
                FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                    Files.delete(dir)
                    return FileVisitResult.CONTINUE
                }
    
                @Override
                FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    Files.delete(file)
                    return FileVisitResult.CONTINUE
                }
            })
        }
    }
    

1 个答案:

答案 0 :(得分:0)

说明设置file的代码中存在的问题。

文件toString()返回A,而不是B:

答:/home/user/Project/Module/file.txt

B:/home/user/TestRepo/file.txt