Thank you for your donations!   Download the book!   Public source code repository   Join the group!

Git in-a-process

A few weeks ago, even before I started working on a Nineteen variant, I found out that messing with a local history also makes mess of your local log files in a .git folder, which I had been using to figure out stuff about repository.

Since then, I switched to proper solution, by calling git with appropriate arguments. Change wasn't that big, nor painful as I expected, and so I didn't document the transition at the time. However, it might be useful if somebody stumbles upon this (or, to my older self!), so use:

  • git rev-list --count HEAD
    for commit count in the current branch
  • git rev-parse --abbrev-ref HEAD
    for short name of the current branch
  • git log --format="%H" -n 1
    for last commit-id
  • git log -1 --format=%ci
    for date-time of the last commit (I'm using %ct to get Unix timestamp, then format it to my liking)

Call any of these from within Python via subprocess.check_output() function, and just collect string output, that's it.

No comments:

Post a Comment