Skip to content

Instantly share code, notes, and snippets.

@mback2k
Created November 19, 2013 14:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mback2k/7545942 to your computer and use it in GitHub Desktop.
Save mback2k/7545942 to your computer and use it in GitHub Desktop.
Convert hg commit hashes to git tags after using hg-git
import sys, os, os.path
def main(args):
if len(args) == 2:
return tags(os.path.abspath(args[1]))
else:
print 'Usage: <hg-repository-path>'
def tags(repo):
if not os.path.exists(repo):
raise ValueError('Path does not exist: %s' % repo)
hgrepo = os.path.join(repo, '.hg')
if not os.path.exists(repo):
raise ValueError('Path does not exist: %s' % hgrepo)
gitrepo = os.path.join(hgrepo, 'git')
if not os.path.exists(gitrepo):
raise ValueError('Path does not exist: %s' % gitrepo)
gitmapfile = os.path.join(hgrepo, 'git-mapfile')
if not os.path.exists(gitmapfile):
raise ValueError('Path does not exist: %s' % gitmapfile)
gitrefstags = os.path.join(gitrepo, 'refs', 'tags')
if not os.path.exists(gitrefstags):
raise ValueError('Path does not exist: %s' % gitrefstags)
return proc(gitmapfile, gitrefstags)
def proc(gitmapfile, gitrefstags):
with open(gitmapfile, 'rb') as mapfile:
for line in mapfile.readlines():
tags = line.strip().split()
if len(tags) == 2:
path = os.path.join(gitrefstags, tags[1])
with open(path, 'wb') as tagfile:
tagfile.write(tags[0])
tagfile.write('\n')
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