我已经研究了从Java控制Git的可能性。我发现的是:
Runtime.getRuntime().exec("git command")
-使Java用git执行命令我尝试使用Runtime和ProcessBuilder为git编写自己的Java包装程序,但是我遇到了进程线程问题,一直在等待线程完成一段时间。
然后,我研究了其他使用API的解决方案。首先,我尝试了JavaGit API,但根本无法使用它。
第二,我测试了JGit API,它看起来很棒。但是我很快发现我无法像使用Java包装程序那样设置提交日期:
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
然后搜索涉及“ tst
”的测试('authordate
'::
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的文档/插图的很好来源。
前段时间,我遇到了类似的问题,因为JavaGit项目只是分叉了一点,它已经死了,解决了一些错误,并使一些功能恢复了正常工作。
您可以在the Swiss Army Java Git page尝试一下,也很高兴为您提供帮助。