5

I just want to retrieve the commitlog from Git repository that has messages for all the commit you've done on a specific repopsitory. I had found some code snippets for achieve this and ends with an exception.

try {
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository repo = builder.setGitDir(new File("https://github.com/name/repository.git")).readEnvironment().findGitDir().build();
    RevWalk walk =new RevWalk(repo);
    ObjectId head = repo.resolve(Constants.HEAD);
    RevCommit commit =walk.parseCommit(head);
    Git git =new Git(repo);
    Iterable<RevCommit> gitLog = git.log().call();
    Iterator<RevCommit> it = gitLog.iterator();
    while(it.hasNext())
    {
        RevCommit logMessage = it.next(); 
        System.out.println(logMessage.getFullMessage());
    }
}
catch(Exception e) {
    e.printStackTrace();
}

However it gives me:

org.eclipse.jgit.api.errors.NoHeadException: No HEAD exists and no explicit starting revision was specified exception.

How do I get rid of this? I am using org.eclipse.jgit JAR version 2.0.0.201206130900-r

Scott Weldon
  • 9,673
  • 6
  • 48
  • 67
Varun Kumar
  • 1,015
  • 2
  • 9
  • 17

2 Answers2

5

This is correct of piece of code will do the above..

FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repo = builder.setGitDir(new File("localrepositary"+"\\.git")).setMustExist(true).build();
Git git = new Git(repo);
Iterable<RevCommit> log = git.log().call();
for (Iterator<RevCommit> iterator = log.iterator(); iterator.hasNext();) {
  RevCommit rev = iterator.next();
  logMessages.add(rev.getFullMessage());
}
Scott Weldon
  • 9,673
  • 6
  • 48
  • 67
Varun Kumar
  • 1,015
  • 2
  • 9
  • 17
0

If https://github.com/name/repository.git is the URL of the repository that you want to get the log from you will have to clone it first:

CloneCommand cloneCommand = Git.cloneRepository();
cloneCommand.setDirectory( new File( "/path/to/local/repo" ) );
cloneCommand.setURI( "https://github.com/name/repository.git" );
Git git = cloneCommand.call();
...
git.getRepository().close();

This creates a local clone of the remote repository in /path/to/local/repo. Note that the repo directory must be non-existing or empty prior to calling cloneCommand. This repository can then be examined with git.log().

Make sure to close the repository once you are done using it to avoid leaking file handles.

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
  • Herrmann.. This looks good but what my need is I've already local source which is the one I have added into git repo. But now i want to display my commit messages and don't want to clone the directory again which I already had one. How to do that? – Varun Kumar Jul 09 '14 at 12:09
  • You can open an existing local repository like so: `Repository repo = builder.setGitDir(new File("/path/to/local/repo/.git")).setMustExist(true).build();` – Rüdiger Herrmann Jul 09 '14 at 17:03
  • Thanks.. Works Great...I also listed my commit logs. – Varun Kumar Jul 10 '14 at 05:19