我使用JGit將存儲庫裸機克隆在本地磁盤上。現在,我需要在任何給定的提交ID(SHA1)中讀取文件的內容。我該怎麼做呢 ?我該如何使用JGit做「git show sha1:file」
回答
通過使用這個。 Iterable<RevCommit> gitLog = gitRepo.log().call();
您可以從該對象獲取所有提交哈希。
comment of Rüdiger Herrmann in this answer包含相關的提示;但爲了讓複製&粘貼解決方案的朋友更容易,我的junit測試的完整獨立示例代碼創建文件的修訂版,然後檢索此修訂版的內容。適用於jGit 4.2.0。
@Test
public void test() throws IOException, GitAPIException
{
//
// init the git repository in a temporary directory
//
File repoDir = Files.createTempDirectory("jgit-test").toFile();
Git git = Git.init().setDirectory(repoDir).call();
//
// add file with simple text content
//
String testFileName = "testFile.txt";
File testFile = new File(repoDir, testFileName);
writeContent(testFile, "initial content");
git.add().addFilepattern(testFileName).call();
RevCommit firstCommit = git.commit().setMessage("initial commit").call();
//
// given the "firstCommit": use its "tree" and
// localize the test file by its name with the help of a tree parser
//
Repository repository = git.getRepository();
try (ObjectReader reader = repository.newObjectReader())
{
CanonicalTreeParser treeParser = new CanonicalTreeParser(null, reader, firstCommit.getTree());
boolean haveFile = treeParser.findFile(testFileName);
assertTrue("test file in commit", haveFile);
assertEquals(testFileName, treeParser.getEntryPathString());
ObjectId objectForInitialVersionOfFile = treeParser.getEntryObjectId();
// now we have the object id of the file in the commit:
// open and read it from the reader
ObjectLoader oLoader = reader.open(objectForInitialVersionOfFile);
ByteArrayOutputStream contentToBytes = new ByteArrayOutputStream();
oLoader.copyTo(contentToBytes);
assertEquals("initial content", new String(contentToBytes.toByteArray(), "utf-8"));
}
git.close();
}
// simple helper to keep the main code shorter
private void writeContent(File testFile, String content) throws IOException
{
try (OutputStreamWriter wr = new OutputStreamWriter(new FileOutputStream(testFile), Charset.forName("utf-8")))
{
wr.append(content);
}
}
編輯補充:另一個,可能是更好的例子是在https://github.com/centic9/jgit-cookbook/blob/master/src/main/java/org/dstadler/jgit/api/ReadFileFromCommit.java
好主意。要重點演示ObjectLoader,可以簡化代碼片段:'setBare(false)'是默認值,不需要重新打開回收 - 重新使用'initialized','firstCommit'從'ci.call() ',不需要再次提交。 BTW臨時文件夾不會被刪除。 –
感謝您的建議簡化;這些應該現在整合在一起。我不知道可以通過'Repository repository = git.getRepository()'獲取版本庫 - 很好的發現了這一點。是的,臨時文件夾不會被刪除,我只是留下它,因爲這不是一個真正的單元測試,而是一直運行,只是演示代碼。 –
- 1. 如何用JGit做git push?
- 2. 我該如何做「git update」?
- 3. 如何用jgit做「git diff --name-status」的等價物?
- 4. 如何在git show(diffstat)中獲取文件blob SHA1?
- 5. 使用JGit和CLI git
- 6. git svn使用jgit獲取
- 7. 如何用jgit做「git grep -e <pattern>」?
- 8. 如何用jgit做「git remote update」的等價物?
- 9. 如何用jgit做「git repack -ad」的等價物?
- 10. 我該如何提供一個git SHA1來gnu make?
- 11. 使用GitPython,我該如何做git子模塊更新--init
- 12. git show file with rename detection(相當於git log --follow)
- 13. 的Git從SHA1
- 14. 我應該如何使用git分支?
- 15. 使用jgit的Java git客戶端
- 16. 使用JGit刪除遠程Git分支
- 17. 如何使用jgit庫將git HEAD指向特定的引用?
- 18. 使用LibGit2Sharp的git show-branch
- 19. JGit刪除git存儲庫
- 20. 爲什麼git show-ref --hash - HEAD顯示與HEAD不同的sha1?
- 21. 我應該如何使用Perl的File :: Temp?
- 22. 如何在jgit中編寫show-ref命令?
- 23. 我該如何做select select
- 24. 我該如何做Memcached HA?
- 25. 如何在jGit中使用過濾器?
- 26. 我該如何讓git做「你的意思」建議?
- 27. 我該如何在Maven中做一個git pull?
- 28. git push .git/description file
- 29. 如何創建Git標籤對象SHA1?
- 30. git歸檔遠程JGit
是。我得到了我想要的提交哈希。但是,如何讀取散列樹中某個特定文件的內容? –
你需要檢出散列並獲取文件的內容。在終端,你可以做'git checkout'。 –
這是一個裸倉庫。 –