[英]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 config中定義的名稱和電子郵件,時間戳是當前系統時間。 使用第二個PersonIdent
構造函數,名稱和電子郵件來自defaultCommitter
,時間戳被date
覆蓋。
在Windows中,可以通過從'Administrator'命令提示符執行命令'date MM-dd-yy'來設置系統時間。
適用於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
該命令只能以管理員身份執行。 因此,您應該使用管理員權限運行Java代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.