Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Makefile中运行时无法打印git提交哈希(使用git--pretty)_Git_Shell_Makefile - Fatal编程技术网

在Makefile中运行时无法打印git提交哈希(使用git--pretty)

在Makefile中运行时无法打印git提交哈希(使用git--pretty),git,shell,makefile,Git,Shell,Makefile,我试图在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\}

我试图在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调用)时,上面的命令运行良好

非常感谢您的指点

目录结构。
我无法解释为什么您当前的版本不起作用。但请注意,如果您想获得特定大小的缩写哈希,您可以直接这样做,而无需使用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)

然后看看输出是什么。

我无法解释当前版本无法工作的原因。但请注意,如果您想获得特定大小的缩写哈希,您可以直接这样做,而无需使用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)

然后查看输出是什么。

执行此Makefile时,工作目录是什么?
。/inter/local.py
是属于git repo中的路径,当与该wd相关时?可能是“\”和嵌套替换的问题;一次由制造,一次由外壳。尝试在grep模式中添加额外的“\”。为什么要使用
grep
?只需输出
格式:%h
,无需任何处理。@LeGEC-我已更新了目录结构。路径../inter/local.py位于相对于工作目录的git repo中。@Andreas问题似乎在git部分而不是grep部分。执行此Makefile时,工作目录是什么?
。/inter/local.py
是属于git repo中的路径,当与该wd相关时?可能是“\”和嵌套替换的问题;一次由制造,一次由外壳。尝试在grep模式中添加额外的“\”。为什么要使用
grep
?只需输出
格式:%h
,无需任何处理。@LeGEC-我已更新了目录结构。路径../inter/local.py位于相对于工作目录的git repo中。@Andreas问题似乎在git部分而不是grep部分。您是对的@MadScientist。无法从我的
pwd
访问文件
。/inter/local.py
。这就是问题的原因。再次感谢你的帮助。你是对的@MadScientist。无法从我的
pwd
访问文件
。/inter/local.py
。这就是问题的原因。再次感谢你的帮助。