Skip to content

Instantly share code, notes, and snippets.

@dmikurube
Created November 25, 2021 13:40
Show Gist options
  • Save dmikurube/bf82907f336dbdea3a3e17ffe1bd6bde to your computer and use it in GitHub Desktop.
Save dmikurube/bf82907f336dbdea3a3e17ffe1bd6bde to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import subprocess
import sys
EMAIL_REPLACE = {
"dmikurube@users.noreply.github.com": "dmikurube@treasure-data.com"
}
def git_commit(tag):
return subprocess.check_output(['git', 'log', '-1', '--format=%H', tag, '--']).decode("utf-8").strip()
def git_tagger_date(tag):
tagger_date = subprocess.check_output(['git', 'for-each-ref', "--format=%(taggerdate)", 'refs/tags/{}'.format(tag)]).decode("utf-8").strip()
if tagger_date:
return tagger_date
return subprocess.check_output(['git', 'log', '-1', '--format=%cD', tag, '--']).decode("utf-8").strip()
def git_tagger_email(tag):
tagger_email = subprocess.check_output(['git', 'for-each-ref', "--format=%(taggeremail:trim)", 'refs/tags/{}'.format(tag)]).decode("utf-8").strip()
if tagger_email:
return tagger_email
committer_email = subprocess.check_output(['git', 'log', '-1', '--format=%ce', tag, '--']).decode("utf-8").strip()
if committer_email.endswith('github.com'):
author_email = subprocess.check_output(['git', 'log', '-1', '--format=%ae', tag, '--']).decode("utf-8").strip()
if author_email in EMAIL_REPLACE:
return EMAIL_REPLACE[author_email]
return author_email
return committer_email
def git_tagger_name(tag):
tagger_name = subprocess.check_output(['git', 'for-each-ref', "--format=%(taggername)", 'refs/tags/{}'.format(tag)]).decode("utf-8").strip()
if tagger_name:
return tagger_name
committer_name = subprocess.check_output(['git', 'log', '-1', '--format=%cn', tag, '--']).decode("utf-8").strip()
if committer_name == 'GitHub':
return subprocess.check_output(['git', 'log', '-1', '--format=%an', tag, '--']).decode("utf-8").strip()
return committer_name
def git_retag(tag, commit, committer_date, committer_email, committer_name):
subprocess.check_call(['git', 'tag', '-d', tag])
subprocess.check_call(['git', '-c', 'core.commentChar=@', 'tag', '-a', '-F', tag, tag, commit], env={
"GIT_COMMITTER_DATE": committer_date,
"GIT_COMMITTER_EMAIL": committer_email,
"GIT_COMMITTER_NAME": committer_name,
})
def main(argv):
if len(argv) < 2:
return 1
for tag in argv[1:]:
commit = git_commit(tag)
tagger_date = git_tagger_date(tag)
tagger_email = git_tagger_email(tag)
tagger_name = git_tagger_name(tag)
print("tag {}".format(tag))
print("commit {}".format(commit))
print("Tagger: {} <{}> {}".format(tagger_name, tagger_email, tagger_date))
print("")
git_retag(tag, commit, tagger_date, tagger_email, tagger_name)
if __name__ == '__main__':
sys.exit(main(sys.argv))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment