代码之家  ›  专栏  ›  技术社区  ›  brewmanz

如何显示git分支名称,然后附加quotemark或取消换行?

  •  0
  • brewmanz  · 技术社区  · 3 年前

    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'
    
    1 回复  |  直到 3 年前
        1
  •  1
  •   torek    3 年前

    你不能从我这里得到你想要的 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