The long SHA can be gotten like below:
repo = git.Repo(search_parent_directories=True)
sha = repo.head.object.hexsha
Or, in git 3.1.7:
sha = repo.head.commit.hexsha
How about short one?
(short SHA
is decided by the scale of the repo, so it should not be like sha[:7]
)
As far as I can tell, the gitpython Commit
object does not support the short sha directly. However, you can use still gitpython's support for calling git directly to retrieve it (as of git 3.1.7):
repo = git.Repo(search_parent_directories=True)
sha = repo.head.commit.hexsha
short_sha = repo.git.rev_parse(sha, short=4)
This is the equivalent of running
git rev-parse --short=4 ...
on the command-line, which is the usual way of getting the short hash. This will return the shortest possible unambiguous hash of length >= 4 (You could pass in a smaller number, but since git's internal min is 4 it will have the same effect).