Tower Tower - 3 months ago 16x
Java Question

JGit and finding the Head

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

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
value is set to
. I wonder why it can't find HEAD?

I'm reading this documentation:

The repository is constructed just like the doc says, and the
as well. I'm using the latest version of JGit which is

My question: what do I need to change in my code to get JGit to return actual instances of
instead of
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
, which means there is a HEAD in the repository. I've tried different repositories, mine and from other people.


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.