Skip to content

Instantly share code, notes, and snippets.

@kotnik
Last active December 16, 2015 02:39
Show Gist options
  • Save kotnik/5364244 to your computer and use it in GitHub Desktop.
Save kotnik/5364244 to your computer and use it in GitHub Desktop.
Parse git commit in Python.
def parse_commit(commit):
r = {}
info, diffstat = commit.split("ENDOFOUTPUTGITMESSAGEHERE")
info = info.strip()
lines = info.split('\n')
r["sha1"] = lines[0]
r["parent"] = lines[1]
r["author"] = lines[2]
r["commiter"] = lines[3]
r["timestamp"] = lines[4]
r["message"] = '\n'.join(lines[5:])
r["diffstat"] = diffstat.strip()
return r
if __name__ == "__main__":
# git show --numstat --summary --pretty=format:"%H%n%P%n%aN \
# <%ae>%n%cN <%ce>%n%ct%n%s%n%b%nENDOFOUTPUTGITMESSAGEHERE" a69e6d0
# Output is per line:
# - commit sha1
# - parent
# - author
# - commiter
# - timestamp
# - message until ENDOFOUTPUTGITMESSAGEHERE
# - diffstat is the rest
commit = """a69e6d06e94d5486675083e17b3b40a4f849b5e7
d2b16baa2b0c10a3f94327e87017e8ea4c01ae4f
Joe Doe <[email protected]>
Joe Doe <[email protected]>
1365604783
Add documentation.
A first documentation draft for the following:
- git
- mysql
ENDOFOUTPUTGITMESSAGEHERE
2 0 docs/xxx.rst
create mode 100644 docs/types/yyy.rst
"""
import pprint
pprint.pprint(parse_commit(commit), indent=4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment