首页 > 解决方案 > Jgit:Bare Repository 既没有工作树,也没有现有存储库的索引

问题描述

我有一个现有的存储库并试图指向现有的存储库并获得异常

Jgit:Bare Repository has neither a working tree, nor an index

使用现有存储库

public static Repository GetRepository(String destinationDirectory) throws IOException {
    Repository existingRepo = new FileRepositoryBuilder().setGitDir(new File(destinationDirectory))
            .readEnvironment() // scan environment GIT_* variables
            .findGitDir() // scan up the file system tree
            .build();
    return existingRepo;
}

试图获取存储库的状态

public static void GitStatus(Repository repository) throws GitAPIException {
    
    try (Git git = new Git(repository)) {
        Status status = git.status().call();
        System.out.println("Added: " + status.getAdded());
        System.out.println("Changed: " + status.getChanged());
        System.out.println("Conflicting: " + status.getConflicting());
        System.out.println("ConflictingStageState: " + status.getConflictingStageState());
        System.out.println("IgnoredNotInIndex: " + status.getIgnoredNotInIndex());
        System.out.println("Missing: " + status.getMissing());
        System.out.println("Modified: " + status.getModified());
        System.out.println("Removed: " + status.getRemoved());
        System.out.println("Untracked: " + status.getUntracked());
        System.out.println("UntrackedFolders: " + status.getUntrackedFolders());
    }
}

例外

Exception in thread "main" org.eclipse.jgit.errors.NoWorkTreeException: Bare Repository has neither a working tree, nor an index
    at org.eclipse.jgit.lib.Repository.getIndexFile(Repository.java:1147)

文件目录 在此处输入图像描述

标签: javagitjgit

解决方案


推荐阅读