Skip to content

Instantly share code, notes, and snippets.

@jasonmc
Created August 13, 2012 23:42
Show Gist options
  • Save jasonmc/3344878 to your computer and use it in GitHub Desktop.
Save jasonmc/3344878 to your computer and use it in GitHub Desktop.
Test sanitizing names for Mercurial to Git conversion.
import subprocess
from mercurial import commands
from mercurial import ui
from mercurial import localrepo
def sha_string(hash):
import base64
return base64.b16encode(hash).lower()
def sanitize_name(name,what="branch"):
"""Sanitize input roughly according to git-check-ref-format(1)"""
import re
def dot(name):
if name[0] == '.': return '_'+name[1:]
return name
n=name
p=re.compile('([[ ~^:?*]|\.\.)')
n=p.sub('_', n)
if n[-1] == '/': n=n[:-1]+'_'
if n[0] == '/': n='_'+n[1:] #JASON FIX
n='/'.join(map(dot,n.split('/')))
p=re.compile('_+')
n=p.sub('_', n)
# if n!=name:
# sys.stderr.write('Warning: sanitized %s [%s] to [%s]\n' % (what,name,n))
return n
HG_DIR = '/home/jason/hg'
ui = ui.ui()
repo = localrepo.instance(ui, HG_DIR, create=False)
for tag,node in repo.branchtags().items():
#print repo.changelog.rev(node), sha_string(node), tag, sanitize_name(tag)
if tag != sanitize_name(tag):
print tag + "\t->\t" + sanitize_name(tag)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment