Skip to content

Instantly share code, notes, and snippets.

@dmikurube
Created August 4, 2020 05:49
Show Gist options
  • Save dmikurube/9ca9c086d182b555521d714255db5c25 to your computer and use it in GitHub Desktop.
Save dmikurube/9ca9c086d182b555521d714255db5c25 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import datetime
import os
import re
import subprocess
import sys
def retag(tag):
cat_file = subprocess.run(["git", "cat-file", "-p", "v" + tag], capture_output=True)
author_name = None
tagger_name = None
for line_unstripped in cat_file.stdout.decode("utf-8").splitlines():
line = line_unstripped.rstrip()
if line.startswith("tree"):
match = re.search("tree ([0-9a-f]+)", line)
tree = match.group(1)
elif line.startswith("parent"):
match = re.search("parent ([0-9a-f]+)", line)
parent = match.group(1)
elif line.startswith("author"):
match = re.search("author (.+) \<(.+)\> ([0-9]+) ([-+0-9]+)", line)
author_name = match.group(1)
author_email = match.group(2)
author_timestamp = match.group(3)
author_timezone_name = match.group(4)
author_timezone = datetime.datetime.strptime(author_timezone_name, "%z").tzinfo
author_datetime = datetime.datetime.fromtimestamp(int(author_timestamp), author_timezone)
elif line.startswith("committer"):
match = re.search("committer (.+) \<(.+)\> ([0-9]+) ([-+0-9]+)", line)
committer_name = match.group(1)
committer_email = match.group(2)
committer_timestamp = match.group(3)
committer_timezone_name = match.group(4)
committer_timezone = datetime.datetime.strptime(committer_timezone_name, "%z").tzinfo
committer_datetime = datetime.datetime.fromtimestamp(int(committer_timestamp), committer_timezone)
elif line.startswith("tagger"):
match = re.search("tagger (.+) \<(.+)\> ([0-9]+) ([-+0-9]+)", line)
tagger_name = match.group(1)
tagger_email = match.group(2)
tagger_timestamp = match.group(3)
tagger_timezone_name = match.group(4)
tagger_timezone = datetime.datetime.strptime(tagger_timezone_name, "%z").tzinfo
tagger_datetime = datetime.datetime.fromtimestamp(int(tagger_timestamp), tagger_timezone)
else:
continue
if tagger_name:
name = tagger_name
email = tagger_email
date = tagger_datetime
elif author_name:
name = author_name
email = author_email
date = author_datetime
else:
raise
rev_list = subprocess.run(["git", "rev-list", "-n", "1", "v" + tag], capture_output=True)
commit_lines = rev_list.stdout.decode("utf-8").splitlines()
commit = commit_lines[0].rstrip()
delete_cmd = ["git", "tag", "-d", "v" + tag]
print(delete_cmd)
tag_delete = subprocess.run(delete_cmd)
annotate_cmd = ["git", "tag", "-F", tag, "v" + tag, commit]
print(annotate_cmd)
annotate_env = {
"GIT_AUTHOR_NAME": name,
"GIT_AUTHOR_EMAIL": email,
"GIT_AUTHOR_DATE": date.strftime("%a %b %d %H:%M:%S %Y %z"),
"GIT_COMMITTER_NAME": name,
"GIT_COMMITTER_EMAIL": email,
"GIT_COMMITTER_DATE": date.strftime("%a %b %d %H:%M:%S %Y %z"),
}
tag_annotate = subprocess.run(annotate_cmd, env=dict(os.environ, **annotate_env))
return 0
def main(args):
if len(args) <= 1:
raise
for arg in args[1:]:
print("Tag: " + arg)
retag(arg)
print("")
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