Search code examples
gitgit-format

cut next placeholder with git pretty format e.g. to cut a date string


According to the pretty format documentation you can pad a placeholder. For my example I will use the apache commons-io git repository. The pad format can also take the parameter trunc. E.g.

git log --format="%<(25)%cn %<(25,trunc)%s"

will output something like this

Kristian Rosenvold        [maven-release-plugin] ..
Kristian Rosenvold        [maven-release-plugin] ..
Kristian Rosenvold        Moved to changes
Kristian Rosenvold        Updated release notes
Kristian Rosenvold        Added Shift_JIS to list..

The trunc parameter cuts the next placeholder and replaces the cut string with ...

Is it possible to just cut the next placeholder without adding ..?

Cutting a placeholder is useful when e.g. I only want to output the day and not the complete dateformat. I want to write something like

git log --format="%h %<(10, cut)%ci"

and it should output

06bcd4c 2016-01-01
11fae28 2015-12-30

Solution

  • It's possible to remove the .. using control characters. E.g.

    git log --format="%h %<(12,trunc)%ci%x08%x08"
    

    which will output something like

    06bcd4c 2016-01-01
    11fae28 2015-12-30
    

    The log statement above use %x08 after the truncated format. x08 is the backspace control character that removes one output character. So %x08%x08 will remove ...

    PS: Since trunc only appends .. if the string gets truncated the solution above only works if it is sure that the string will be truncated. Otherwise it removes the last two characters of the output string and not the ...