【问题标题】:Unable to print git commit hash (using git --pretty) when runing inside a Makefile在 Makefile 中运行时无法打印 git commit hash(使用 git --pretty)
【发布时间】:2020-07-13 11:58:56
【问题描述】:

我正在尝试使用 Makefile 中的“git --pretty”格式获取文件的版本(提交哈希的前 7 个字符)。

下面是我的 Makefile

#CUR_LOC_VERSION:= $(shell git log --pretty=format:%H -n1 -- | grep -o '^.\{7\}') # works
CUR_LOC_VERSION:= $(shell git log --pretty=format:%H -n1 -- ../inter/local.py | grep -o '^.\{7\}') # doesn't work, returns empty string
$(info $$CUR_LOC_VERSION is [${CUR_LOC_VERSION}])

我希望它显示提交哈希,但它返回空字符串。

$CUR_LOC_VERSION is []

但上述命令在直接在 shell 中运行时运行良好(而不是通过 Makefile 调用)。

高度赞赏的任何指针。

目录结构。

.
|-- inter
|   |-- local.py
|
`-- vhdl
    |-- Makefile

【问题讨论】:

  • 执行这个 Makefile 时的工作目录是什么? ../inter/local.py 是相对于该 wd 时位于您的 git 存储库中的路径吗?
  • 可能是 '\' 和嵌套替换的问题;一次通过make,一次通过shell。尝试在 grep 模式中添加额外的“\”。
  • 你为什么使用grep?直接输出format:%h,不做任何处理。
  • @LeGEC- 我已经更新了目录结构。路径 ../inter/local.py 位于相对于工作目录的 git repo 中。
  • @Andreas 问题似乎出在 git 部分而不是 grep 部分。

标签: git shell makefile


【解决方案1】:

我无法解释为什么您当前的版本不起作用。但请注意,如果您想获得特定大小的缩写哈希,您可以直接执行此操作,而无需使用 grep。运行:

git log -n1 --format=%h --abbrev=7

%h 格式选项显示缩写哈希,--abbrev 表示要使用多少个字符。

根据上面的更多调查,我怀疑你运行它时的工作目录不是你想象的那样。如果我运行git log -n1 -- nosuchfile(使用不存在的文件),我不会收到任何错误消息,就像我期望的那样;我只是没有输出。这有点令人困惑,但它让我相信当你运行这个git 命令时../inter/local.py 不存在。尝试将 pwd 添加到您的 shell 命令中,以便在运行 git 之前打印出工作目录:

CUR_LOC_VERSION:= $(shell pwd; git log --format=%h -n1 -- ../inter/local.py)

看看输出是什么。

【讨论】:

  • 你是对的@MadScientist。我的pwd 无法访问文件../inter/local.py。那就是解释这个问题。再次感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2021-11-23
  • 1970-01-01
  • 2015-01-11
  • 2021-09-17
  • 2022-01-13
  • 2023-03-15
  • 1970-01-01
  • 2017-03-19
相关资源
最近更新 更多