1

我正在使用以下代码从 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)内容的文件内容。

我将非常感谢您的帮助。

谢谢你。~夏姆*/

4

1 回答 1

1

您已经在代码中对提交进行了硬编码,因此您将始终获得该值。

ObjectId lastCommitId = repo.resolve("b35fd0300270e6ba4d9238a1ab328b25a627885a");//userFile.sql

如果要基于当前分支,请使用repo.resolve("HEAD")

另请注意,您的 RevCommit 在这里是不必要的;如果您想获取当前树,请repo.resolve("HEAD^{tree}")改用。

于 2013-11-07T23:36:40.683 回答