JGit Clone and get a hash version

I am using the code below to clone a git repository with Java. I need to save the cloned last hash.

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

Any clue about getting a revision hash here?

+4
source share
1 answer

You can get Refthat contains ObjectIdof HEADwith the following:

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

It produces something like this

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

See also related snippet in jgit-cookbook

Instead, HEADyou can also use things such as refs/heads/masterto get HEADbranches master, even if another branch is currently being checked.

+2

Source: https://habr.com/ru/post/1611488/


All Articles