我正在使用以下代码从 JGIT 存储库中检索文件:
public class JGitPrintContent3
{
public static void main(String[] args) throws Exception
{
File gitWorkDir = new File("D:/jboss/server/repo/Repository/");
Git git = Git.open(gitWorkDir);
Repository repo = git.getRepository();
ObjectId lastCommitId = repo.resolve("b35fd0300270e6ba4d9238a1ab328b25a627885a");//userFile.sql
System.out.println("Points to : " + lastCommitId.name());
RevWalk revWalk = new RevWalk(repo);
RevCommit commit = revWalk.parseCommit(lastCommitId);
revWalk.markStart(commit);
RevTree tree = commit.getTree();
TreeWalk treeWalk = new TreeWalk(repo);
treeWalk.addTree(tree);
treeWalk.setRecursive(true);
treeWalk.setFilter(PathFilter.create("userFile.sql"));
ObjectId objectId = treeWalk.getObjectId(0);
System.out.println(" objectId : " + objectId );
ObjectLoader loader = repo.open(objectId);
File targetFile = new File("C:\\temp\\gittest\\target2\\" + "userFile.sql");
OutputStream out = new FileOutputStream(targetFile);
loader.copyTo(out);
out.flush();
out.close();
System.out.println("Done");
}
}
但不幸的是,它正在检索早期检索(提交 ID)内容的文件内容。
我将非常感谢您的帮助。
谢谢你。~夏姆*/