gpt4 book ai didi

java - Jgit RevWalk.parseCommit() 抛出 MissingObjectException

转载 作者:行者123 更新时间:2023-12-01 04:36:56 25 4
gpt4 key购买 nike

我有一个包含一些测试提交的存储库(全部位于默认分支上)。使用 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

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

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

异常(exception):

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)

我做错了什么?

解决方案存储库的初始化在访问部分出错..

最佳答案

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

这里是显示问题的片段:

    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);
}
}

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

关于java - Jgit RevWalk.parseCommit() 抛出 MissingObjectException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17208655/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com