rubygitgit-commitgit-revision

Git commit revision id to be five 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 seven characters long and %an is the author name.

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

--date=iso-strict

or whatever to do so.

How do I do it?


Solution

  • 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.