【问题标题】:JGit: Status of remote repositoryJGit:远程存储库的状态
【发布时间】:2014-06-13 00:05:25
【问题描述】:

我正在尝试在 Eclipse 中使用 JGit 获取 Git 存储库的状态。
我在这里找到了一些示例:12 并将它们合并:

import java.io.File;
import java.io.IOException;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.InvalidRemoteException;
import org.eclipse.jgit.api.errors.TransportException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.api.Status;

public class Main {

     private static final String REMOTE_URL = "https://github.com/github/testrepo.git";

        public static void main(String[] args) throws IOException, InvalidRemoteException, TransportException, GitAPIException {
            // prepare a new folder for the cloned repository
            File localPath = File.createTempFile("TestGitRepository", "");
            localPath.delete();

            // then clone
            System.out.println("Cloning from " + REMOTE_URL + " to " + localPath);
            Git.cloneRepository()
                    .setURI(REMOTE_URL)
                    .setDirectory(localPath)
                    .call();

            // now open the created repository
            FileRepositoryBuilder builder = new FileRepositoryBuilder();
            Repository repository = builder.setGitDir(localPath)
                    .readEnvironment() // scan environment GIT_* variables
                    .findGitDir() // scan up the file system tree
                    .build();

            System.out.println("Having repository: " + repository.getDirectory());

            Status status = new Git(repository).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());

            repository.close();
        }
}

但我收到以下错误:

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.getWorkTree(Repository.java:1245)
    at org.eclipse.jgit.treewalk.FileTreeIterator.<init>(FileTreeIterator.java:88)
    at org.eclipse.jgit.api.StatusCommand.call(StatusCommand.java:126)
    at Main.main(Main.java:38)

【问题讨论】:

    标签: java eclipse git github jgit


    【解决方案1】:

    如果您将 sn-p 的 FileRepositoryBuilder 部分替换为以下行,则 sn-p 将起作用。

    Git git = Git.open( localPath );
    Repository repository = git.getRepository();
    

    请注意,你的 sn-p 将构建器的 GitDir 设置为 localPath,但本地路径表示刚刚克隆的存储库的工作目录。

    【讨论】:

    • 谢谢,我不再收到错误消息了!你也知道,为什么我只得到空的结果,比如:Added: [] Changed: [] Conflicting: []...
    • 您期望什么?结果是空的,因为在新克隆的存储库上,工作目录、索引和 HEAD 提交之间没有区别。请确保您理解git status
    • 那我怎样才能从实际目录而不是克隆的目录中获取结果呢?我只需要诸如“名称、大小、日期等”之类的东西。的文件。
    • 恐怕我不太明白“实​​际目录的结果”是什么意思。请注意,Git 只能在本地存储库上运行。如果您希望列出远程目录的所有文件,您首先必须创建一个克隆。有了本地存储库后,您可以使用 FileTreeIterator 遍历工作目录中的所有文件。
    猜你喜欢
    • 1970-01-01
    • 2017-12-24
    • 1970-01-01
    • 2016-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多