30

I'm trying to get my hands on the HEAD commit with JGit:

val builder = new FileRepositoryBuilder()
val repo = builder.setGitDir(new File("/www/test-repo"))
  .readEnvironment()
  .findGitDir()
  .build()

val walk: RevWalk = new RevWalk(repo, 100)

val head: ObjectId = repo.resolve(Constants.HEAD)
val headCommit: RevCommit = walk.parseCommit(head)

I find that it opens the repo fine, but head value is set to null. I wonder why it can't find HEAD?

I'm reading this documentation: http://wiki.eclipse.org/JGit/User_Guide

The repository is constructed just like the doc says, and the RevWalk as well. I'm using the latest version of JGit which is 2.0.0.201206130900-r from http://download.eclipse.org/jgit/maven.

My question: what do I need to change in my code to get JGit to return actual instances of RevCommit instead of null like it now does?

Update: This code:

val git = new Git(repo)
val logs: Iterable[RevCommit] = git.log().call().asInstanceOf[Iterable[RevCommit]]

Gives me this exception: No HEAD exists and no explicit starting revision was specified

The exception is odd, because a simple git rev-parse HEAD tells me 0b0e8bf2cae9201f30833d93cc248986276a4d75, which means there is a HEAD in the repository. I've tried different repositories, mine and from other people.

Tower
  • 98,741
  • 129
  • 357
  • 507
  • 2
    I'm sorry that I don't know the answer to this question. I just have a comment that rather than saying "I'm using the latest version of JGit" you should state the exact version number. What if someone with a similar problem reads this question a year from now? How will they know which version was "the latest version" when you originally asked the question? – Code-Apprentice Sep 09 '12 at 20:59

5 Answers5

31

You need to point to the Git metadata directory (probably /www/test-repo/.git) when you call setGitDir, not to the working directory (/www/test-repo).

I have to admit I'm not sure what findGitDir is supposed to do, but I've run into this problem before and specifying the .git directory worked.

Travis Brown
  • 138,631
  • 12
  • 375
  • 680
  • I think (but I'm not sure I never got the mechanics of this) that more specifically, he should use either setGitDir or findGitDir, not both. But yeah I've been through a lot of troubles before finding the right combination. – Vince Sep 09 '12 at 22:24
  • I've just confirmed that specifying the metadata directory with `setGitDir` works, and it doesn't seem to make any difference whether you add `findGitDir` or not. – Travis Brown Sep 09 '12 at 22:44
  • And here's an example: https://github.com/centic9/jgit-cookbook/blob/master/src/main/java/org/dstadler/jgit/OpenRepository.java – MewX Apr 09 '18 at 14:12
6

For the completeness sake, here is a fully working example how to get the hash for the HEAD commit:

public String getHeadName(Repository repo) {
  String result = null;
  try {
    ObjectId id = repo.resolve(Constants.HEAD);
    result = id.getName();
  } catch (IOException e) {
    e.printStackTrace();
  }
  return result;
}
chao
  • 1,893
  • 2
  • 23
  • 27
3

For me (using 4.5.0.201609210915-r) the solution was to use just RepositoryBuilder instead of FileRepositoryBuilder. Until I made this change all methods were returning null.

rb = new org.eclipse.jgit.lib.RepositoryBuilder()
    .readEnvironment()
    .findGitDir()
    .build();

headRef = rb.getRef(rb.getFullBranch());
headHash = headRef.getObjectId().name();
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Ed Randall
  • 6,887
  • 2
  • 50
  • 45
1

You can also use val git: Git = Git.open( new File( "/www/test-repo" ) ). JGit will then scan the given folder for the git meta directory (usually .git). If it fails to find this folder, an IOException will be thrown.

Taig
  • 6,718
  • 4
  • 44
  • 65
0

There is not much info available for the usage of findGitDir(), so I thought I share my findings here. So use this code to setup JGit Repository object from anywhere of its working tree:

RepositoryBuilder repositoryBuilder = new RepositoryBuilder();

Repository repo = repositoryBuilder
  .findGitDir(new File("~/workspace/project1/src/main/java"))
  .setMustExist(true)
  .build();

In my case I was using the repo to check its status:

Git git = Git.wrap(repo);
git.status().call();

The code is based on JGit version 5.10.0.202012080955-r