Skip to main content



      Home
Home » Eclipse Projects » EGit / JGit » [help ] new to JGit
[help ] new to JGit [message #659560] Mon, 14 March 2011 11:17 Go to next message
Eclipse UserFriend
This term I have to integry JGit to our project for my lab.
when I copy the code from the forum:

RepositoryBuilder builder = new RepositoryBuilder();
File repos = new File("E:\\pmsSpace\\testGit\\test\\basedir\\.git");
Repository repository = builder.setGitDir(repos).build();
Git git = new Git(repository);

LogCommand log = git.log();

for (RevCommit commit : log.call()) {
System.out.println(commit.getAuthorIdent().getName());
System.out.println(commit.getAuthorIdent().getWhen());
System.out.println(commit.getFullMessage());
}

then i run it in Eclipse3.5, the error comes out:
org.eclipse.jgit.api.errors.NoHeadException: No HEAD exists and no explicit starting revision was specified
at org.eclipse.jgit.api.LogCommand.call(LogCommand.java:103)

Hope someone could help me out, thx a lot!

YiFei
BeiJing.China
Re: [help ] new to JGit [message #659601 is a reply to message #659560] Mon, 14 March 2011 14:51 Go to previous messageGo to next message
Eclipse UserFriend
Looks like your repository is empty.

How about doing some commits with CommitCommand?

You can add a file with AddCommand too.
Re: [help ] new to JGit [message #659650 is a reply to message #659601] Mon, 14 March 2011 21:06 Go to previous messageGo to next message
Eclipse UserFriend
thx, I do some adds and commits in one function,the code:

File basedir = new File("test/basedir");
//deleteFileTree(basedir);
basedir.mkdir();

RepositoryBuilder builder = new RepositoryBuilder();
builder.setGitDir(new File(basedir, ".git"));

Repository repo = builder.build();
repo.create();

Git git = new Git(repo);

new File(basedir, "aa").createNewFile();

git.add().addFilepattern(".").call();

git.commit()
.setAll(true)
.setMessage("besouro automatic message")
.setCommitter("somename", "someemail")
.call();

the error still occurs, but if I copy the code out of the function and put it just before theLogCommand code, it runs smoothly, I just cann't figure it out.
Re: [help ] new to JGit [message #659947 is a reply to message #659650] Wed, 16 March 2011 06:13 Go to previous message
Eclipse UserFriend
That's intentional. Native git behaves the same. Just try out:

cd <tmpFolder>
git init test
cd test
git log


and you will also see an error message about a missing HEAD.

Problem is that newly created repositories where no commit has been done don't have a branch (even master is not yet born) and therefore HEAD is also not pointing to something exisiting. My suggestion is not to call log() on repo's which you know are empty (what would you expect from this call?) or to catch the NoHeadException.


Side topic: you can further streamline your code by using our API also for the repo initialization. See here:

File f = new File(...);
Git git = Git.init().setDirectory(f).call();
try {
	for (RevCommit commit : git.log().call()) {
		System.out.println(commit.getAuthorIdent().getName());
		System.out.println(commit.getAuthorIdent().getWhen());
		System.out.println(commit.getFullMessage());
	}
} catch (NoHeadException e) {
	System.out.println("Empty repo, nothing to log");
}


Previous Topic:spearce/jgit and jgit
Next Topic:Difference between EGit and JGit
Goto Forum:
  


Current Time: Thu Jul 10 04:56:00 EDT 2025

Powered by FUDForum. Page generated in 0.30933 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top