hg-fast-import.py: Sanitize ref names
authorRocco Rutte <pdmef@gmx.net>
Fri, 26 Oct 2007 14:06:40 +0000 (16:06 +0200)
committerRocco Rutte <pdmef@gmx.net>
Fri, 26 Oct 2007 14:06:40 +0000 (16:06 +0200)
At least the opensolaris hg repo has tag names containing '*',
so sanitize all branch and tag names roughly according to the
specs for git-check-ref-format(1).

Signed-off-by: Rocco Rutte <pdmef@gmx.net>
hg-fast-export.py

index 6b501f8..97dba5f 100755 (executable)
@@ -133,10 +133,31 @@ def is_merge(parents):
       c+=1
   return c>1
 
+def sanitize_name(name,what="branch"):
+  """Sanitize input roughly according to git-check-ref-format(1)"""
+
+  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]+'_'
+  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
+
 def export_commit(ui,repo,revision,marks,heads,last,max,count,authors,sob):
   (revnode,_,user,(time,timezone),files,desc,branch,_)=get_changeset(ui,repo,revision,authors)
   parents=repo.changelog.parentrevs(revision)
 
+  branch=sanitize_name(branch)
+
   wr('commit refs/heads/%s' % branch)
   wr('mark :%d' % (revision+1))
   if sob:
@@ -220,6 +241,7 @@ def export_commit(ui,repo,revision,marks,heads,last,max,count,authors,sob):
 def export_tags(ui,repo,marks_cache,start,end,count,authors):
   l=repo.tagslist()
   for tag,node in l:
+    tag=sanitize_name(tag,"tag")
     # ignore latest revision
     if tag=='tip': continue
     rev=repo.changelog.rev(node)