1

I'm using the following code to retrieve the file from JGIT repo:

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

But unfortunately it is retrieving the file contents of earlier retrieved (commit Id) contents.

I would be most thankful for your help.

Thank you. ~Shyam */

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79

1 Answers1

1

You have hard-coded the commit in your code, so you'll always get that value.

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

If you want to base it on the current branch then use repo.resolve("HEAD")

Also note that your RevCommit is unnecessary here; if you want to get the current tree instead, use repo.resolve("HEAD^{tree}") instead.

AlBlue
  • 23,254
  • 14
  • 71
  • 91
  • Thank you so much for you reply AlBlue. ...Actually i am trying to retrieve the content of the commitId "b35fd0300270e6ba4d9238a1ab328b25a627885a" Object. But id does not work properly and fetching earlier commitId contents for previous commitId of the same file name Object in the repository. – user2907154 Nov 27 '13 at 13:13