Getting Transaction Message Logs From Git Using JGit

I just want to get commitlog from the Git repository, which has messages for all the commands you made on a specific repository. I found some code snippets to 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, this 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

+4
source share
2 answers

That's right, part of the 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());
}
+4
source

If https://github.com/name/repository.gitthis is the URL of the repository from which you want to get the log, 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();

/path/to/local/repo. , cloneCommand repo . git.log().

, , , .

0

Source: https://habr.com/ru/post/1547690/


All Articles