jGit用groovy进行翻新解析

时间:2017-08-22 06:36:38

标签: groovy jgit

我正在尝试从rev-parse中获取相同的值但使用jGit库和Groovy脚本。我在文档jGit中发现了Link,但它直接来自Java,如何为groovy重建代码? 感谢您的提示!

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


import org.eclipse.jgit.api.*;
import org.eclipse.jgit.lib.*;
import java.io.IOException;

class RevCommit {
  static void main(String[] args) {
      Git git = Git.open( new File( ".git" ) );
      ObjectId head = git.resolve(Constants.HEAD);
      Iterable<RevCommit> commits = git.log().add(head).setMaxCount(1).call();

   }

}

1 个答案:

答案 0 :(得分:0)

您显示的代码示例搜索上次提交。如果你想使用Groovy脚本实现相同的功能,你必须将这个Java main方法的主体直接放到Groovy脚本中 - 它不需要任何具有main方法的类来执行。您还必须修复git.resolve(Constants.HEAD) - 您正在尝试调用不存在的方法。此方法存在于Repository类中。

下面你可以找到一个与Java示例类似的Groovy脚本示例:

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

import org.eclipse.jgit.api.*
import org.eclipse.jgit.lib.Constants
import org.eclipse.jgit.lib.ObjectId
import org.eclipse.jgit.revwalk.RevCommit

Git git = Git.open(new File("."))
ObjectId head = git.repository.resolve(Constants.HEAD)
Iterable<RevCommit> commits = git.log().add(head).setMaxCount(1).call()

println "Recent commit:"
commits.each {
    println it.toString()
}

我将此脚本保存到名为jgit.groovy的文件中,并使用以下命令运行:

groovy jgit.groovy

此脚本的输出类似于:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Recent commit:
commit 8504bf656a945fe199bea60fd1296eef2b083a18 1500237139 ----sp

我希望它有所帮助。