我研究了从 Java 控制 Git 的可能性。 我发现的是:
- 使用
Runtime.getRuntime().exec("git command")
- 让 Java 使用 git 执行命令 - 使用 JavaGit API ( http://javagit.sourceforge.net/ ) 或者 -使用 Eclipse JGit API ( http://www.jgit.org/ )
我尝试使用 Runtime 和 ProcessBuilder 为 git 编写自己的 Java 包装器,但我遇到了进程线程的问题,我一直在等待线程完成一些时间。
然后我研究了使用 API 的其他解决方案。首先,我尝试了 JavaGit API,但我根本无法让它工作。
其次,我测试了 JGit API,它看起来很棒。但是我很快发现我无法像使用 Java-wrapper 那样设置提交日期:
ProcessBuilder pb = new ProcessBuilder("git", "commit", "--date=" + "\"" + customDateString + "\"", "-m \"" + comment + "\"");
我下载了 JGit 源代码,看看我是否可以实现它,但是阅读起来太多了,我在 Github 上找不到任何问题跟踪器供 JGit 提出建议。
这里有人可以帮我做这个吗?
或者告诉我在哪里可以写信向开发人员提出建议?
最佳答案
如您所说,很简单,首先下载 jgit :
C:\> cd C:\Users\VonC\prog\git\
C:\Users\VonC\prog\git> git clone https://github.com/eclipse/jgit
C:\Users\VonC\prog\git> cd jgit
然后搜索涉及“authordate
”的测试('tst
'):
C:\Users\VonC\prog\git\jgit>grep -nRHIi authordate *|grep tst
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitCommandTest.java:446: final Date authorDate = new Date(1349621117000L);
这意味着您可以查看 org.eclipse.jgit.test.tst.org.eclipse/jgit/api.CommitCommandTest , 函数 commitAmendWithoutAuthorShouldSetOriginalAuthorAndAuthorTime()
:
您将看到如何指定作者和作者日期:
final Date authorDate = new Date(1349621117000L);
PersonIdent firstAuthor = new PersonIdent(authorName, authorEmail,
authorDate, TimeZone.getTimeZone("UTC"));
git.commit().setMessage("initial commit").setAuthor(firstAuthor).call();
请注意,as I mention here ,测试类是 JGit 文档/插图的良好来源。
关于java - 使用 JGit Eclipse 提交特定日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19614698/