hack-is-art hack-is-art - 1 month ago 6
Git Question

Git commit revision id to be 5 chars long when using pretty format in Ruby script

My code is the following

system('git log --pretty=format:[%h]: %an')


where %h gives the revision id of the commit and it is 7 chars long and %an the author name.

My problem is that I want to have 5 digits revision id and not 7, but I can't find any flag of the form.

--date=iso-strict


or whatever to do so.

Can you please tell me how to do it?

Answer

7 digits is the default and is the generally accepted minimum to ensure uniqueness on moderate sized projects. Anything less runs the risk of collisions. If you want to trim it you can ask:

--abbrev=5

This may be overruled by the git command if the 5 digit values are not unique. Consider this value a minimum and not a maximum.

You can read more with git log --help.

As a note, you generally want to break out arguments to system to avoid confusion between them:

system("git", "--log", "--pretty-format=...")

That's especially necessary when passing in arbitrary filenames as those can be interpreted by the shell in ways that are hazardous.

Comments