How to make git log cut long comments?

后端 未结 7 1024
执笔经年
执笔经年 2020-12-14 14:43

I have a git log alias that prints each commit as a single line. Since some people write far too long one-liners in the commit log, many commits wrap to a new line. How can

7条回答
  •  感情败类
    2020-12-14 15:23

    Truncate and pad the commit message only

    As per other answers, the format placeholders %<(50,trunc)%s will print the commit message truncated at 50 characters. But that will also pad shorter values to the same, and there's no way to tell it not to.

    If that suits you, then you're done. If not, another approach is needed.

    Truncate the entire line at terminal width

    Also per other answers, you could configure less -S as the core.pager option globally or per-repository. This will trim the entire log string at the terminal width, avoiding wrapped lines.

    But it will do that to all Git commands! (At least all the ones that produce paged output).

    Improvement - truncate only for that specific command

    You can do this with the -c option, e.g. git -c core.pager='less -S' log --graph --oneline

    Even better, set this up as an alias so you don't have to type it every time:

    git config --global alias.graph "-c core.pager='less -S' \
     log --graph --oneline`
    

    Combining both

    You can also combine this with formatting placeholders. Here's an example using the --graph flag, where the commit message is also padded/truncated to 50 characters, but since the --graph option creates a variable-width drawing of the commit graph, you need to combine both approaches. And you don't want to be typing this out every time:

    git config --global alias.graph "-c core.pager='less -S' \
    log --pretty='tformat:%C(bold cyan)%h %C(blue)%<(10,trunc)%aN \
    %<(50,trunc)%C(white)%s %C(auto)%d %C(dim green)%ar' --graph"
    

提交回复
热议问题