I am creating some scripts and programs that fetch commit information using
git log --pretty=<my format> -1 <commit>
I wonder if the output of this command is suitable to be parsed by programs (plumbing) or only meant to be presented to humans (porcelain). For example, in some projects I am fetching commit SHA + author name + commit summary with this:
git log --pretty="%H%n%an%n%s" -1 HEAD
And then I split the output string by the newline character (I'm on Linux).
Besides, in some cases I also do something like this:
git log --pretty='[%h] %an: %s' -1 HEAD
And then parse the result with the following regex, expecting that a short SHA, the author name and the commit summary are in the captured groups:
^\[(\w+)\] ([^:]+): (.*)$
Is it a good approach? If not, what is the preferred way to programmatically get information about commits?
git log
is a porcelain command.
It actually performs quite a disparate number of tasks — combining walking the revision graph, git diff
and git grep
and whatnot.
A plumbing way to do someting like
git log --pretty='[%h] %an: %s' -1 HEAD
is to combine git show-ref
with git cat-file
and parse the result—something like
git cat-file commit `git show-ref -s HEAD` |
while read line; do
# do some processing
done
Actually the root Git's manual page, git(1)
—run git help git
to read it—contains the breakdown of commands into porcelain and plumbing layers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With