【问题标题】:How to reliably determine last commit date in remote repository?如何可靠地确定远程存储库中的最后提交日期?
【发布时间】:2020-06-02 06:28:36
【问题描述】:

我已经看到this 的回答,这几乎正是我所需要的。

不同之处在于它会为人眼打印日期,而我需要在 Makefile 中检查它。

特别是我需要重建某个文件 IFF 在它创建后在 repo 中发生了一些变化。

我可能没有 repo 的本地副本,所以我可能需要一个完整的“git clone”(可能很浅),但我不能直接使用文件日期,因为 git 不保留它们。

我需要类似的东西: if myfile is_older $(git log -1 --format=%cd origin/master); then ...Makefile

myfile: $(somefunc $(shell git log -1 --format=%cd origin/master))
        commands to rebuild myfile

但我不知道怎么写。

【问题讨论】:

    标签: git makefile


    【解决方案1】:

    如果您需要更改日期格式,请尝试添加 --date 选项。

    例如:

    • rfc

      git log -1 --format=%cd --date=rfc
      Wed, 5 Feb 2020 16:34:38 +0200
      
    • 亲戚

      git log -1 --format=%cd --date=relative
      13 days ago
      
    • 等严格

      git log -1 --format=%cd --date=iso-strict
      2020-02-05T16:34:38+02:00
      
    • log -1 --format=%cd --date=short
      2020-02-05
      
    • 原始

      git log -1 --format=%cd --date=raw
      1580913278 +0200
      

    有关更多文档,请查看:https://git-scm.com/docs/git-log

    也许我会使用“原始”。这种日期格式对您的脚本是否方便?

    【讨论】:

      【解决方案2】:

      正如@Slavomir 所指出的,一个关键因素将是--date 选项,但不是raw 值,而是unix 值(为了只获得从纪元开始的秒数UTC,没有时区)。

      $ git log -1 --format=%cd --date=unix
      1582030040
      

      (参见doc)。

      然后,您将能够使用 date +%s -r myfile shell 命令获取myfile 的时间戳。

      (参见man page)。

      最后,您可以设计一个 Makefile 函数来简化比较,从而得到一个最小的工作示例,如下所示:

      生成文件

      # Return 'true' if $(1) does not exist or is older than branch $(2)
      check_repo_change = $(shell if [ ! -e $(1) ] || [ $$(date +%s -r $(1)) -lt \
        $$(git log -1 --format=%cd --date=unix $(2)) ]; then echo true; fi)
      
      # Note that you'll probably need to run "git fetch" at some point
      BRANCH := origin/master
      
      all: myfile
      .PHONY: all
      
      ifeq '$(call check_repo_change,myfile,$(BRANCH))' 'true'
      myfile:
          set -x; echo ok >> $@
      else
      myfile:
          $(info No change detected in branch $(BRANCH))
      endif
      .PHONY: myfile
      

      注意:.PHONY 选项是强制性的,因此即使文件已经存在并且没有(更新的)依赖项,也会触发 myfile 目标。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-09
        • 2021-12-16
        • 1970-01-01
        • 2012-01-03
        • 1970-01-01
        • 1970-01-01
        • 2017-09-13
        • 1970-01-01
        相关资源
        最近更新 更多