1

我有一个带有一些测试提交的存储库(全部在默认分支上)。使用 JGit log 命令,我可以看到这些提交(例如):

RepositoryBuilder builder = new RepositoryBuilder();
Repository repository = builder.setWorkTree("/tmp/myworkingdir").readEnvironment().findGitDir().build();
Git git = new Git(repository);

for (RevCommit commit : git.log().call()) {
    System.out.println("commit: " + commit.getId());
}

结果:

commit: commit f34a848371ff334b2e85aa16fbf9b871d028b443 1371652639 ----sp
commit: commit 33603f8c1c47712d45eb0db4b1827bbd113c2428 1371652639 ----sp
commit: commit d52bd5c768263a957d99975f5482020ffc3434d5 1371652639 ----sp

但是,当我尝试从 String SHA1 获取 RevCommit 时,它在此代码段的最后一行失败:

ObjectId revid = repository.resolve("33603f8c1c47712d45eb0db4b1827bbd113c2428");
RevWalk revWalk = new RevWalk(repository);
RevCommit commit = revWalk.parseCommit(revid);

例外:

org.eclipse.jgit.errors.MissingObjectException: Missing unknown 33603f8c1c47712d45eb0db4b1827bbd113c2428
at org.eclipse.jgit.storage.file.WindowCursor.open(WindowCursor.java:126)
at org.eclipse.jgit.lib.ObjectReader.open(ObjectReader.java:229)
at org.eclipse.jgit.revwalk.RevWalk.parseAny(RevWalk.java:817)
at org.eclipse.jgit.revwalk.RevWalk.parseCommit(RevWalk.java:730)

我究竟做错了什么 ?

解决方案 Repository 的 Init 在访问部分是错误的..

4

1 回答 1

0

好的,我的错误.. 存储库的初始化对于以后的访问是错误的(子目录而不是根目录)。但我会预料到另一个错误..

这是一个显示问题的片段:

    String wpath = "/tmp/test-git";
    ArrayList<String> commits = new ArrayList<String>();

    // get list of commit
    {
        File gitDir = new File(wpath);
        RepositoryBuilder builder = new RepositoryBuilder();
        Repository repository = builder.setWorkTree(gitDir)
                .readEnvironment().findGitDir().build();
        Git git = new Git(repository);

        for (RevCommit commit : git.log().call()) {
            String cid = commit.getId().toString().split(" ")[1];
            commits.add(cid);
        }
        Collections.shuffle(commits);
    }

    // Random access
    {
        File gitDir = new File(wpath + "/subdir");
        RepositoryBuilder builder = new RepositoryBuilder();
        Repository repository = builder.setWorkTree(gitDir)
                .readEnvironment().findGitDir().build();

        for (String cid : commits) {
            RevCommit commit = CommitUtils.getCommit(repository, cid);
            assertEquals(commit.getId().toString().split(" ")[1], cid);
        }
    }

它不应该工作,因为存储库被错误地初始化(在第二部分),但错误应该告诉它..:/

于 2013-06-26T11:30:39.927 回答