Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Truncating commit messages

I know that it is possible to truncate git commit messages in pretty-print with something like this:

git log --oneline --format="%h %<(70,trunc)%s %cn"

But this seems to pad the commit messages which are shorter than 70 characters with white space (so %cn will always be pushed to the right).

Is there a way to stop the commit message being padded with space if it is shorter than 70 characters?

like image 383
zoran119 Avatar asked Jul 07 '14 06:07

zoran119


People also ask

Should commit messages be short?

General Commit Message Guidelines As a general rule, your messages should start with a single line that's no more than about 50 characters and that describes the changeset concisely, followed by a blank line, followed by a more detailed explanation.

How detailed should commit messages be?

A good commit message should be two things: meaningful and concise. It should not contain every single detail, describing each changed line—we can see all the changes in Git—but, at the same time, it should say enough to avoid ambiguity.

Can you modify a commit message?

You can change the most recent commit message using the git commit --amend command. In Git, the text of the commit message is part of the commit. Changing the commit message will change the commit ID--i.e., the SHA1 checksum that names the commit. Effectively, you are creating a new commit that replaces the old one.

How long should my commit messages be?

See the section on Conventional Commits below for additional information. Length: The first line should ideally be no longer than 50 characters, and the body should be restricted to 72 characters.


1 Answers

As per the git-log manual, ltrunc, mtrunc and trunc is only an optional argument to the %<(<N>) placeholder, which main purpose is to do the padding:

%<(<N>[,trunc|ltrunc|mtrunc]): make the next placeholder take at least N columns, padding spaces on the right if necessary. Optionally truncate at the beginning (ltrunc), the middle (mtrunc) or the end (trunc) if the output is longer than N columns. Note that truncating only works correctly with N >=2.

As of right now the git log pretty formats don't seem to have an option that just does the truncation. I think this kinda goes along with "pretty printing" being generally used to tabularize the output to be easily human-readable.

You can remove extra whitespace from git log pretty print output with some post-processing, .e.g. using sed to replace two or more adjacent spaces with one:

git log --oneline --format="%h %<(70,trunc)%s %cn" | sed -e "s/[ ]\{2,\}/ /g"
like image 199
famousgarkin Avatar answered Sep 19 '22 07:09

famousgarkin