Jgit作者最后一次提交

时间:2017-08-30 05:58:17

标签: groovy jgit

我发现了类似的question并且基于它,但我收到了错误Cannot invoke method getAuthorIdent() on null object。我尝试进行最后一次提交,检查它是否等于badAuthor。为什么它不能null?如果声明会像我想要的那样工作?

def authorEqual() {

def badAuthor = 'John'
Git git = Git.open(new File(".git"))  
RevCommit lastCommit = null --> ERROR FROM HERE
List<Ref> branches = new Git(git.repository).branchList().setListMode(ListMode.ALL).call();   
    try {
        RevWalk walk = new RevWalk(git.repository) 
        for(Ref branch : branches){
          RevCommit commit = walk.parseCommit(branch.getObjectId());  
          PersonIdent aAuthor = commit.getAuthorIdent()
          if(commit.getAuthorIdent().getWhen().compareTo(
        -----------^ <-- HERE ERROR
              lastCommit.getAuthorIdent().getWhen().equals(badAuthor)) > 0)

              lastCommit = commit;
              println commit

2 个答案:

答案 0 :(得分:2)

考虑使用Groovy查找上次提交的方式:

RevCommit lastCommit = branches.collect { branch -> revWalk.parseCommit(branch.objectId) }
        .sort { commit -> commit.authorIdent.when }
        .reverse()
        .first()

它从所有分支收集最后提交的内容,然后按照后代顺序对它们进行排序并获取最新的提交。完成最后一次提交后,您可以轻松检查谁是其作者并执行任何其他逻辑。您可以在下面找到Groovy脚本示例:

import org.eclipse.jgit.api.Git
import org.eclipse.jgit.api.ListBranchCommand
import org.eclipse.jgit.lib.Ref
import org.eclipse.jgit.revwalk.RevCommit
import org.eclipse.jgit.revwalk.RevWalk

@Grab(group='org.eclipse.jgit', module='org.eclipse.jgit', version='4.8.0.201706111038-r')

Git git = Git.open(new File("."))
List<Ref> branches = git.branchList().setListMode(ListBranchCommand.ListMode.ALL).call()
RevWalk revWalk = new RevWalk(git.repository)
String excludeAuthorsCommit = 'Joe Doe'

RevCommit lastCommit = branches.collect { branch -> revWalk.parseCommit(branch.objectId) }
        .sort { commit -> commit.authorIdent.when }
        .reverse()
        .first()

println "${lastCommit.authorIdent.when}: ${lastCommit.shortMessage} (${lastCommit.authorIdent.name})"

if (lastCommit.authorIdent.name == excludeAuthorsCommit) {
    // Do what you want
}

我已经在5个分支机构的项目中对其进行了测试。它几分钟前我从test分支返回了最近的提交。我希望它有所帮助。

答案 1 :(得分:1)

你可以写下如下来找到坏作者。

def badAuthor = 'John'
Git git = Git.open(new File(".git"))  
List<Ref> branches = new Git(git.repository).branchList().setListMode(ListMode.ALL).call();   
    try {
        RevWalk walk = new RevWalk(git.repository) 
        for(Ref branch in branches){

          RevCommit commit = walk.parseCommit(branch.getObjectId());  

          PersonIdent aAuthor = commit.getAuthorIdent()

          if(commit.getAuthorIdent().getWhen.equals(badAuthor))    
              println commit