How to get Git log with short stat in one line?

后端 未结 7 1560
無奈伤痛
無奈伤痛 2020-12-09 15:52

Following command outputs following lines of text on console

git log --pretty=format:\"%h;%ai;%s\" --shortstat
ed6e0ab;2014-01-07 16:32:39 +0530;Foo
 3 files         


        
7条回答
  •  误落风尘
    2020-12-09 16:09

    git doesn't support stat info with plain --format, which is shame :( but it's easy to script it away, here's my quick and dirty solution, should be quite readable:

    #!/bin/bash
    
    format_log_entry ()
    {
        read commit
        read date
        read summary
        local statnum=0
        local add=0
        local rem=0
        while true; do
            read statline
            if [ -z "$statline" ]; then break; fi
            ((statnum += 1))
            ((add += $(echo $statline | cut -d' ' -f1)))
            ((rem += $(echo $statline | cut -d' ' -f2)))
        done
        if [ -n "$commit" ]; then
            echo "$commit;$date;$summary;$statnum;$add;$rem"
        else
            exit 0
        fi
    }
    
    while true; do
        format_log_entry
    done
    

    I'm sure, that it can be scripted better, but hey - it's both quick AND dirty ;)

    usage:

    $ git log --pretty=format:"%h%n%ai%n%s" --numstat | ./script
    

    Please note, that format, that you specified is not bulletproof. Semicolon can appear in commit summary, which will break number of fields in such line - you can either move summary to end of line or escape it somehow - how do you want to do it?

提交回复
热议问题