简体   繁体   English

如何使用JGit设置提交时间?

[英]How to set the commit time with JGit?

Is there a way to set the commit time with JGit? 有没有办法用JGit设置提交时间?

I flipped through the API and found that it can only be done by modifying the local system time. 我浏览了API,发现它只能通过修改本地系统时间来完成。 I want to implement it through code. 我想通过代码实现它。 And can run normally on the win system. 并且可以在win系统上正常运行。

The timestamp for a commit can be set with the CommitCommand . 可以使用CommitCommand设置提交的时间戳。 Note that name, email, and timestamp must be specified together with a PersonIdent object. 请注意,必须与PersonIdent对象一起指定名称,电子邮件和时间戳。

For example: 例如:

Date date = ...
PersonIdent defaultCommitter = new PersonIdent(git.getRepository());
PersonIdent committer = new PersonIdent(defaultCommitter, date);
git.commit().setMessage("Commit with time").setCommitter(committer).call();

The defaultCommitter holds name and email as defined in the git config, the timestamp is the current system time. defaultCommitter保存git config中定义的名称和电子邮件,时间戳是当前系统时间。 With the second PersonIdent constructor, the name and email are taken from the defaultCommitter and the timestamp is overridden with date . 使用第二个PersonIdent构造函数,名称和电子邮件来自defaultCommitter ,时间戳被date覆盖。

In windows the system time can be set by executing the command 'date MM-dd-yy' from the and 'Administrator' command prompt. 在Windows中,可以通过从'Administrator'命令提示符执行命令'date MM-dd-yy'来设置系统时间。

Java Snippet for Windows 适用于Windows的Java代码段

 //Set the Date 
 SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yy");  
 String setDate = "cmd /C date "+sdf.format(dateToSet);  
 Process dateProc = Runtime.getRuntime().exec(setDate);  
 dateProc.waitFor();//Might take a couple of seconds

 //Set the Time  
 SimpleDateFormat stf = new SimpleDateFormat("HH:mm:ss");  
 String setTime = "cmd /C time "+stf.format(dateToSet);  
 Process timeProc = Runtime.getRuntime().exec(setTime);  
 timeProc.waitFor();//Might take a couple of seconds  

This command can only be executed as an administrator. 该命令只能以管理员身份执行。 So you should run the java code with Administrator Privileges. 因此,您应该使用管理员权限运行Java代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM