1

I try to read the latest version of a file from the GIT server. Somehow this picks up the wrong version. Could you please help?

public void copyFileToOutputStream(Repository repository, String filePath, OutputStream os) throws Exception {
        try (TreeWalk treeWalk = new TreeWalk( repository) ) {
            RevCommit revCommit = repository.parseCommit(repository.resolve("origin/head"))
            treeWalk.addTree(revCommit.getTree());
            treeWalk.setRecursive(true);
            treeWalk.setFilter(PathFilter.create(filePath));
            if (!treeWalk.next()) {
                throw new IllegalStateException("Did not find expected file '" + filePath + "'");
            }

            final ObjectId objectId = treeWalk.getObjectId(0);
            final ObjectLoader loader = repository.open(objectId);

            loader.copyTo(os);
        }
    }

DbSchema
  • 413
  • 5
  • 16

1 Answers1

1

Since this code is supposed to retrieve the latest commit on the remote "origin/head" branch from the repository and adds its associated tree object to the TreeWalk, it should get "the right version".

If said version is not the right one, maybe it is because the origin/HEAD was not updated.
Try to add a git fetch, as in "How to use fetch command in jGit?"

Git git = new Git(repository);
git.fetch().setRemote("origin").call(); // Fetch updates from the remote repository

Check also if origin/HEAD (which references the default branch of the remote repository) is the reference you want (or if you need another branch instead).

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250