gpt4 book ai didi

groovy - Jgit 作者最后一次提交

转载 作者:行者123 更新时间:2023-12-02 17:16:49 25 4
gpt4 key购买 nike

我找到了类似的 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

最佳答案

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

关于groovy - Jgit 作者最后一次提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45952945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com