2017-08-30 166 views
1

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

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 
+0

我想你在使用它之前没有设置lastCommit。 – dynamo

+0

'RevCommit lastCommit = null' - >链接问题示例 –

+0

您应该检查lastCommit是否为空。无论如何,如果你想找到由不良演员提交的提交,为什么你需要最后一次提交 – dynamo

回答

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分支,我在几分钟前做了几次分支。我希望它有帮助。

+0

酷,就是这样。谢谢 !在if语句中,'git.rm()'会从该作者中删除最后一个文件? –

+0

编号'git.rm()'返回['RmCommand']](http://download.eclipse.org/jgit/docs/jgit-2.3.1.201302201838-r/apidocs/org/eclipse/jgit/api/RmCommand .html)实例,您必须设置要从存储库中删除的文件模式。但是你有'RevCommit',你可以从它得到树,遍历文件并将它们添加到'RmCommand'。 –

+0

你有权利,但我thnik最简单的方法将使用'ResetCommand' –

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 
+0

它检查所有提交?我需要检查最后一个,然后如果它是平等的,删除它,它可以完成? –

+0

你是指每个分支上的最后一次提交或特定分支上的最后一次提交? – dynamo

+0

每个分支 –