JGit如何设置提交时间?
How to set the commit time with JGit?
有没有办法用 JGit 设置提交时间?
我翻了翻API,发现只能通过修改本地系统时间来实现。我想通过代码来实现它。并且可以在win系统上正常运行
可以使用 CommitCommand
设置提交的时间戳。请注意,名称、电子邮件和时间戳必须与 PersonIdent
对象一起指定。
例如:
Date date = ...
PersonIdent defaultCommitter = new PersonIdent(git.getRepository());
PersonIdent committer = new PersonIdent(defaultCommitter, date);
git.commit().setMessage("Commit with time").setCommitter(committer).call();
defaultCommitter
保存在 git 配置中定义的姓名和电子邮件,时间戳是当前系统时间。使用第二个 PersonIdent
构造函数,名称和电子邮件取自 defaultCommitter
并且时间戳被 date
.
覆盖
在 windows 中,可以通过在 'Administrator' 命令提示符下执行命令 'date MM-dd-yy' 来设置系统时间。
Java Windows
的代码段
//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
此命令只能以管理员身份执行。所以你应该 运行 具有管理员权限的 java 代码。
有没有办法用 JGit 设置提交时间?
我翻了翻API,发现只能通过修改本地系统时间来实现。我想通过代码来实现它。并且可以在win系统上正常运行
可以使用 CommitCommand
设置提交的时间戳。请注意,名称、电子邮件和时间戳必须与 PersonIdent
对象一起指定。
例如:
Date date = ...
PersonIdent defaultCommitter = new PersonIdent(git.getRepository());
PersonIdent committer = new PersonIdent(defaultCommitter, date);
git.commit().setMessage("Commit with time").setCommitter(committer).call();
defaultCommitter
保存在 git 配置中定义的姓名和电子邮件,时间戳是当前系统时间。使用第二个 PersonIdent
构造函数,名称和电子邮件取自 defaultCommitter
并且时间戳被 date
.
在 windows 中,可以通过在 'Administrator' 命令提示符下执行命令 'date MM-dd-yy' 来设置系统时间。
Java Windows
的代码段 //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
此命令只能以管理员身份执行。所以你应该 运行 具有管理员权限的 java 代码。