Jgit 作者最后一次提交

Jgit Author last commit

我找到了类似的 并以此为基础,但我遇到了错误 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

你可以这样写,找到不好的作者

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

考虑 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 分支返回了最近的提交。希望对你有帮助。