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

在git日志输出中结合颜色和条件换行符

  •  7
  • stkent  · 技术社区  · 7 年前

    git log --format=format:"%C(yellow)%h %C(blue)%s %C(green)%ad %C(reset)%an%n%n%-b"
    

    我得到如下输出:

    enter image description here

    我希望提交体变暗,所以我尝试插入指令 %C(dim) 在格式字符串的末尾。然而,似乎没有实现我目标的插入位置:

    • 换行后和 (conditional newline-chomping) %-b command 正确应用调暗效果,但会中断条件换行:

      git log --format=format:"%C(yellow)%h %C(blue)%s %C(green)%ad %C(reset)%an%n%n%C(dim)%-b"
      

      enter image description here

    • 正在插入 %-b 命令正确地保留了条件换行选择,但未能应用调暗效果(即与原始输出没有变化):

      git log --format=format:"%C(yellow)%h %C(blue)%s %C(green)%ad %C(reset)%an%C(dim)%n%n%-b"
      

      enter image description here

    - 对于color命令,因为它总是“求值”为非空字符串(因此,不会选择换行符)。

    2 回复  |  直到 7 年前
        1
  •  2
  •   Leon    6 年前

    最终解决方案(没有任何已知的怪癖)位于 .

    %-b 然后用 sed 在下面的示例中,占位符是 CHOMPABLENEWLINES 字符串(当然,您可以用您选择的任何文本替换它,只要确保 它不能出现在您的提交消息中

    git log --format=format:"%C(yellow)%h %C(blue)%s %C(green)%ad %C(reset)%an%C(dim)CHOMPABLENEWLINES%b"|sed -e 's/CHOMPABLENEWLINES$//; s/CHOMPABLENEWLINES/\n\n/; $ s/$/\n/'
    

    Colored output of git log with sed postprocessing

    注意,这种方法还解决了以下问题: %C 指令仅扩展到当前行的末尾(至少在git 2.7.4中)。因此,在多线体的情况下,颜色仅应用于其第一行。比较:

    # Only the first line of a multiline message body is colored
    git log --format=format:"%C(yellow)%h %C(blue)%s %C(green)%ad %C(reset)%an%n%n%Cred%b"
    

    Colored output of git log without sed postprocessing

    # Entire multiline message body is colored (though a byproduct of this
    # is that the color setting persists beyond the current
    # command - note the red prompt following the output)
    git log --format=format:"%C(yellow)%h %C(blue)%s %C(green)%ad %C(reset)%an%CredCHOMPABLENEWLINES%b"|sed -e 's/CHOMPABLENEWLINES$//; s/CHOMPABLENEWLINES/\n\n/; $ s/$/\n/'
    

    Colored output of git log with sed postprocessing

    %C(reset) 指令。然后,我们还需要一个自定义标记,用于 %b 占位符,因为视觉行尾不再是来自的实际行尾 的观点。在字符串下面的示例中 ENDOFBODY 用作这样的标记(当然,您必须选择它,使其不会出现在预期输出中)。

    # This version works with GNU sed. For a portable version (including BSD
    # and MacOS X systems) scroll down a little more
    git log --format=tformat:"%C(yellow)%h %C(blue)%s %C(green)%ad %C(reset)%an%CredCHOMPABLENEWLINES%bENDOFBODY%C(reset)"|sed -e 's/CHOMPABLENEWLINESENDOFBODY//; s/CHOMPABLENEWLINES/\n\n/; s/ENDOFBODY//'
    

    Output of the final version

    git的某些版本或设置在不直接进入终端时禁用彩色输出。在这种情况下,您还必须提供 --color=always git log .


    仅使用sed的可移植功能的最终解决方案如下:

    git log --color=always --format=tformat:"%C(yellow)%h %C(blue)%s %C(green)%ad %C(reset)%an%C(red)CHOMPABLENEWLINES%bENDOFBODY%C(reset)"|sed -e 's/CHOMPABLENEWLINESENDOFBODY//; s/CHOMPABLENEWLINES/\'$'\n''\'$'\n/; s/ENDOFBODY//'
    
        2
  •  1
  •   Patrick    4 年前

    按照我期望的工作方式,我只需要添加一个换行控件和一个颜色格式化程序,这两个都可以工作,比如

    things things %C(yellow)%+d%C(reset)
    

    yellow auto 这样地

    things things %C(auto)%+d%C(reset)
    

    git help -c | grep color 查看颜色配置值列表。上色的那些 %d

    color.decorate.HEAD
    color.decorate.branch
    color.decorate.grafted
    color.decorate.remoteBranch
    color.decorate.stash
    color.decorate.tag
    

    可能还有另一个类似的配置值会像OP希望的那样使提交消息变暗。然后你就可以跑了 git config --global color.decorate.HEAD yellow

    [color "decorate"]
        HEAD = yellow
    

    这就是我的配置最终的结果:

    [format]
        pretty = format:%C(bold cyan)%h%Creset %C(cyan)<%an>%Creset %Cgreen(%cr)%Creset%  %s%C(auto)%+d%C(reset)
    [color "decorate"]
        HEAD = yellow
        branch = yellow
        grafted = yellow
        remoteBranch = yellow
        stash = yellow
        tag = yellow