Upen
Upen

Reputation: 1438

JGit Clone and get the revision hash

I am using the below code to clone a git repo from Java. I need to store the cloned latest revision hash.

localRepo = new FileRepository(path);
git = new Git(localRepo);
Git.cloneRepository().setURI(url).setBranch("master")
                .setDirectory(new File(path)).call();
git.close();

Any clue on getting the revision hash here?

Upvotes: 11

Views: 6581

Answers (1)

centic
centic

Reputation: 15890

You can get a Ref which contains the ObjectId of HEAD with the following:

        Ref head = repository.getAllRefs().get("HEAD");
        System.out.println("Ref of HEAD: " + head + ": " + head.getName() + " - " + head.getObjectId().getName());

This prints out something like this

Ref of HEAD: SymbolicRef[HEAD -> refs/heads/master=f37549b02d33486714d81c753a0bf2142eddba16]: HEAD - f37549b02d33486714d81c753a0bf2142eddba16

See also the related snippet in the jgit-cookbook

Instead of HEAD, you can also use things like refs/heads/master to get the HEAD of the branch master even if a different branch is checked out currently.

Upvotes: 11

Related Questions