如何git branch --show-current停止附加換行符或在換行符之前插入引號?我想通過類似的方式創建一個 C 風格的頭檔案
echo -n "#define GIT_LAST_HASH \"" >> TimestampNow.h
git log --no-show-signature -n 1 --format=%h%x22 >> TimestampNow.h # works well
echo -n "#define GIT_BRANCH \"" >> TimestampNow.h
git branch --show-current --format=%x22 >> TimestampNow.h # does not work
創造
#define GIT_LAST_HASH "1e3134d" // works fine
#define GIT_LAST_BRANCH "develop" // can neither insert quotemark, nor stop newline, at end of 'develop'
uj5u.com熱心網友回復:
你不能讓你從想要的東西git branch --show-current,但幸運的是,你并不需要這么做。您已經在使用 shell,所以只需更有效地使用它:
hash=$(git rev-parse --short HEAD)
branch=$(git branch --show-current) # empty for detached HEAD
printf '#define GIT_LAST_HASH "%s"\n#define GIT_BRANCH "%s"\n' $hash "$branch" >> TimestampNow.h
(順便說一句,大多數人發現最好使用git describe,而不是像這樣將東西拼湊在一起,以制作可用于描述構建的可列印字串。git describe --always --dirty例如考慮使用。)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/343922.html
