Retrieving Commit Message Log from Git Using JGit

邮差的信 提交于 2019-12-05 14:27:21
Varun Kumar

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());
}

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!