【问题标题】:How to set the commit time with JGit?如何使用 JGit 设置提交时间?
【发布时间】:2018-12-11 12:57:04
【问题描述】:

有没有办法用 JGit 设置提交时间?

我翻了一下API,发现只能通过修改本地系统时间来完成。我想通过代码来实现它。并且可以在win系统上正常运行。

【问题讨论】:

  • @LuCio 对此我感到非常抱歉。已编辑。
  • 据我了解这个问题是关于使用 JGit API 为提交设置显式时间戳。这不需要更改系统时间。
  • @RüdigerHerrmann 谢谢

标签: java commit jgit


【解决方案1】:

可以使用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 覆盖。

【讨论】:

    【解决方案2】:

    在 Windows 中,可以通过在“管理员”命令提示符下执行“日期 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 代码。

    【讨论】:

      猜你喜欢
      • 2015-02-06
      • 2014-05-21
      • 1970-01-01
      • 2018-04-15
      • 2017-06-23
      • 2015-05-01
      • 2017-03-28
      • 2018-06-09
      • 2013-06-15
      相关资源
      最近更新 更多