2017-08-30 163 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

每個分支 –