contrib/convert-repo
changeset 694 51eb248d3348
parent 692 695dd9a491da
child 705 574869103985
equal deleted inserted replaced
693:10c0264751da 694:51eb248d3348
    18 # convert-repo <git-dir> <hg-dir> <mapfile>
    18 # convert-repo <git-dir> <hg-dir> <mapfile>
    19 #
    19 #
    20 # This updates the mapfile on each commit copied, so it can be
    20 # This updates the mapfile on each commit copied, so it can be
    21 # interrupted and can be run repeatedly to copy new commits.
    21 # interrupted and can be run repeatedly to copy new commits.
    22 
    22 
    23 import sys, os, zlib, sha
    23 import sys, os, zlib, sha, time
    24 from mercurial import hg, ui, util
    24 from mercurial import hg, ui, util
    25 
    25 
    26 class convert_git:
    26 class convert_git:
    27     def __init__(self, path):
    27     def __init__(self, path):
    28         self.path = path
    28         self.path = path
    33 
    33 
    34     def catfile(self, rev, type):
    34     def catfile(self, rev, type):
    35         if rev == "0" * 40: raise IOError()
    35         if rev == "0" * 40: raise IOError()
    36         path = os.getcwd()
    36         path = os.getcwd()
    37         os.chdir(self.path)
    37         os.chdir(self.path)
    38         fh = os.popen("git-cat-file %s %s" % (type, rev))
    38         fh = os.popen("git-cat-file %s %s 2>/dev/null" % (type, rev))
    39         os.chdir(path)
    39         os.chdir(path)
    40         return fh.read()
    40         return fh.read()
    41 
    41 
    42     def getfile(self, name, rev):
    42     def getfile(self, name, rev):
    43         return self.catfile(rev, "blob")
    43         return self.catfile(rev, "blob")
    79                 if committer[0] == "<": committer = committer[1:-1]
    79                 if committer[0] == "<": committer = committer[1:-1]
    80                 message += "\ncommitter: %s %s\n" % (committer, date)
    80                 message += "\ncommitter: %s %s\n" % (committer, date)
    81             if n == "parent": parents.append(v)
    81             if n == "parent": parents.append(v)
    82         return (parents, author, date, message)
    82         return (parents, author, date, message)
    83 
    83 
       
    84     def gettags(self):
       
    85         tags = {}
       
    86         for f in os.listdir(self.path + "/.git/refs/tags"):
       
    87             try:
       
    88                 h = file(self.path + "/.git/refs/tags/" + f).read().strip()
       
    89                 p, a, d, m = self.getcommit(h)
       
    90                 if not p: p = [h] # git is ugly, don't blame me
       
    91                 tags[f] = p[0]
       
    92             except:
       
    93                 pass
       
    94         return tags
       
    95 
    84 class convert_mercurial:
    96 class convert_mercurial:
    85     def __init__(self, path):
    97     def __init__(self, path):
    86         self.path = path
    98         self.path = path
    87         u = ui.ui()
    99         u = ui.ui()
    88         self.repo = hg.repository(u, path)
   100         self.repo = hg.repository(u, path)
   125             self.repo.rawcommit(files, text, author, dest,
   137             self.repo.rawcommit(files, text, author, dest,
   126                                 hg.bin(p1), hg.bin(p2))
   138                                 hg.bin(p1), hg.bin(p2))
   127             text = "(octopus merge fixup)\n"
   139             text = "(octopus merge fixup)\n"
   128 
   140 
   129         return hg.hex(self.repo.changelog.tip())
   141         return hg.hex(self.repo.changelog.tip())
       
   142 
       
   143     def puttags(self, tags):
       
   144         try:
       
   145             old = self.repo.wfile(".hgtags").read()
       
   146             oldlines = old.splitlines(1)
       
   147             oldlines.sort()
       
   148         except:
       
   149             oldlines = []
       
   150 
       
   151         k = tags.keys()
       
   152         k.sort()
       
   153         newlines = []
       
   154         for tag in k:
       
   155             newlines.append("%s %s\n" % (tags[tag], tag))
       
   156 
       
   157         newlines.sort()
       
   158 
       
   159         if newlines != oldlines:
       
   160             print "updating tags"
       
   161             f = self.repo.wfile(".hgtags", "w")
       
   162             f.write("".join(newlines))
       
   163             f.close()
       
   164             if not oldlines: self.repo.add([".hgtags"])
       
   165             date = "%s 0" % time.mktime(time.gmtime())
       
   166             self.repo.rawcommit([".hgtags"], "update tags", "convert-repo",
       
   167                                 date, self.repo.changelog.tip(), hg.nullid)
   130 
   168 
   131 class convert:
   169 class convert:
   132     def __init__(self, source, dest, mapfile):
   170     def __init__(self, source, dest, mapfile):
   133         self.source = source
   171         self.source = source
   134         self.dest = dest
   172         self.dest = dest
   227             if c in self.map: continue
   265             if c in self.map: continue
   228             desc = self.commitcache[c][3].splitlines()[0]
   266             desc = self.commitcache[c][3].splitlines()[0]
   229             print num, desc
   267             print num, desc
   230             self.copy(c)
   268             self.copy(c)
   231 
   269 
       
   270         tags = self.source.gettags()
       
   271         ctags = {}
       
   272         for k in tags:
       
   273             v = tags[k]
       
   274             if v in self.map:
       
   275                 ctags[k] = self.map[v]
       
   276 
       
   277         self.dest.puttags(ctags)
       
   278 
   232 gitpath, hgpath, mapfile = sys.argv[1:]
   279 gitpath, hgpath, mapfile = sys.argv[1:]
   233 
   280 
   234 c = convert(convert_git(gitpath), convert_mercurial(hgpath), mapfile)
   281 c = convert(convert_git(gitpath), convert_mercurial(hgpath), mapfile)
   235 c.convert()
   282 c.convert()