我正在嘗試將子模塊支持添加到project of mine,但我似乎無法使用JGit讀取特定的提交或記錄,而命令行git可以做到這一點,儘管分離首先說明子模塊在哪裏(就設計子模塊的工作而言)。JGit:從子模塊讀取提交
我有這樣的代碼,以「熔斷器人民軍」是子模塊:
FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repository = builder.setGitDir(new File("fuse-jna/.git"))
.readEnvironment() // scan environment GIT_* variables
.findGitDir() // scan up the file system tree
.build();
ObjectId lastCommitId = repository.resolve("39c1c4b78ff751b0b9e28f4fb35148a1acd6646f");
Iterable<RevCommit> commits = new Git(repository).log().add(lastCommitId).call();
for(RevCommit commit : commits) {
System.out.println("Commit: " + commit.getId());
}
這個我得到:
org.eclipse.jgit.errors.MissingObjectException: Missing commit 39c1c4b78ff751b0b9e28f4fb35148a1acd6646f
at org.eclipse.jgit.internal.storage.file.WindowCursor.open(WindowCursor.java:149)
at org.eclipse.jgit.revwalk.RevWalk.getCachedBytes(RevWalk.java:883)
at org.eclipse.jgit.revwalk.RevCommit.parseHeaders(RevCommit.java:145)
at org.eclipse.jgit.revwalk.RevWalk.markStart(RevWalk.java:279)
at org.eclipse.jgit.api.LogCommand.add(LogCommand.java:330)
at org.eclipse.jgit.api.LogCommand.add(LogCommand.java:189)
at org.dstadler.jgitfs.util.JGitHelperTest.testGitLinkRepository(JGitHelperTest.java:638)
但是在命令行中的「保險絲南斯拉夫人民軍」目錄
git diff 39c1c4b78ff751b0b9e28f4fb35148a1acd6646f~
工作得很好,所以有可能這樣做。
有沒有其他方法如何與JGit做到這一點?或者是不完全支持子模塊呢?
僅供參考,同樣使用內部的「git的/模塊/熔斷器JNA/git的」不工作。此外,「repository.getAllRefs()」也會返回一個空列表,所以在這裏根本找不到參考... – centic 2014-09-28 22:07:47
我不知道JGit是如何工作的,但是關於子模塊,它們基本上都是自己的倉庫。在命令行中,當你在子模塊的目錄中時,你也只能看到他們的提交。使用JGit,你是從子模塊的目錄還是主存儲庫的目錄中嘗試它? – rednaw 2014-09-28 22:15:40
在代碼中列出的代碼中,我試圖像普通的Git倉庫一樣打開子模塊庫,但這似乎不是JGit在這裏需要的,請參閱下面的自我回答。 – centic 2014-09-28 22:43:56