Skip to content

Instantly share code, notes, and snippets.

@DurandA
Created December 8, 2019 00:28
Show Gist options
  • Save DurandA/a0a6f4c0a6dca0daf4b97c67396c8f21 to your computer and use it in GitHub Desktop.
Save DurandA/a0a6f4c0a6dca0daf4b97c67396c8f21 to your computer and use it in GitHub Desktop.
Tag git commits according to the author
#!/usr/bin/env python3
import subprocess
tags = {
'jsmith@example.com': 'tagname'
}
fetch = subprocess.run(['git', 'fetch', 'origin', 'master'], stderr=subprocess.DEVNULL)
assert(fetch.returncode == 0)
with open('.latest-sha', 'r+') as file:
latest_sha = file.read()
log_from = (latest_sha+'...HEAD',) if latest_sha else ()
log = subprocess.run(
['git', '--no-pager', 'log', 'origin/master', '-n', '2', '--pretty=format:%H %ae', *log_from],
stdout=subprocess.PIPE, encoding='utf-8')
assert(log.returncode == 0)
if not log.stdout:
raise SystemExit()
for commit in log.stdout.split('\n'):
commit_sha, user = commit.split()
if user in tags:
tag = subprocess.run(['git', 'tag', tags[user], commit_sha])
push = subprocess.run(['git', 'push', 'origin', tags[user]])
assert(push.returncode == 0)
latest_sha = commit_sha
file.seek(0)
file.write(latest_sha)
file.truncate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment