Skip to content

Instantly share code, notes, and snippets.

@john-kurkowski
Last active April 16, 2016 18:22
Show Gist options
  • Save john-kurkowski/d3869575c83d1a719d0a to your computer and use it in GitHub Desktop.
Save john-kurkowski/d3869575c83d1a719d0a to your computer and use it in GitHub Desktop.
Copy SemVer tags without a "v" prefix to tags with the "v" prefix, e.g. 1.2.3 -> v1.2.3.
#!/usr/bin/env python
'''Copy SemVer tags without a "v" prefix to tags with the "v" prefix, e.g.
1.2.3 -> v1.2.3. Preserve tag author, date, & message.'''
import re
import subprocess
OLD_TAG_RE = re.compile(r'^\d+\.\d+')
def tag2vtag(tag):
'''Copy a single tag without a leading "v" to a tag with a leading "v".'''
dot_count = tag.count('.')
if dot_count == 1:
new_tag = 'v{}.0'.format(tag)
else:
new_tag = 'v{}'.format(tag)
props = [
('name', '%cn'),
('email', '%ce'),
('date', '%ci'),
('ref', '%H'),
]
git_show_cmd = ('git', 'show', '-s', '--pretty=format:{}'.format(
'%n'.join(placeholder for _, placeholder in props)
))
git_show = subprocess.check_output(git_show_cmd + (tag,)).splitlines()
values = dict(zip((prop for prop, _ in props), git_show[-len(props):]))
annotation = git_show[3:-len(props)]
git_tag_cmd = ('git', 'tag', '-f', new_tag, values['ref'], '-m', '\n'.join(annotation))
env = {
'GIT_COMMITTER_NAME': values['name'],
'GIT_COMMITTER_EMAIL': values['email'],
'GIT_COMMITTER_DATE': values['date'],
}
subprocess.check_call(git_tag_cmd, env=env)
def main():
all_tags = subprocess.check_output(('git', 'tag')).splitlines()
old_tags = [line for line in all_tags if OLD_TAG_RE.match(line)]
for old_tag in old_tags:
tag2vtag(old_tag)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment