Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EGit » [help ] new to JGit
[help ] new to JGit [message #659560] Mon, 14 March 2011 15:17 Go to next message
YiFei  is currently offline YiFei
Messages: 6
Registered: March 2011
Junior Member
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 18:51 Go to previous messageGo to next message
Chris Aniszczyk is currently offline Chris Aniszczyk
Messages: 674
Registered: July 2009
Senior Member
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] Tue, 15 March 2011 01:06 Go to previous messageGo to next message
YiFei  is currently offline YiFei
Messages: 6
Registered: March 2011
Junior Member
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 10:13 Go to previous message
Christian Halstrick is currently offline Christian Halstrick
Messages: 18
Registered: July 2009
Junior Member
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: Fri Oct 24 15:01:45 GMT 2014

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

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