hgext/convert/hg.py
changeset 46113 59fa3890d40a
parent 45790 5d65e04b6a80
child 46819 d4ba4d51f85f
equal deleted inserted replaced
46112:d6afa9c149c3 46113:59fa3890d40a
    22 import re
    22 import re
    23 import time
    23 import time
    24 
    24 
    25 from mercurial.i18n import _
    25 from mercurial.i18n import _
    26 from mercurial.pycompat import open
    26 from mercurial.pycompat import open
       
    27 from mercurial.node import (
       
    28     bin,
       
    29     hex,
       
    30     nullhex,
       
    31     nullid,
       
    32 )
    27 from mercurial import (
    33 from mercurial import (
    28     bookmarks,
    34     bookmarks,
    29     context,
    35     context,
    30     error,
    36     error,
    31     exchange,
    37     exchange,
    32     hg,
    38     hg,
    33     lock as lockmod,
    39     lock as lockmod,
    34     merge as mergemod,
    40     merge as mergemod,
    35     node as nodemod,
       
    36     phases,
    41     phases,
    37     pycompat,
    42     pycompat,
    38     scmutil,
    43     scmutil,
    39     util,
    44     util,
    40 )
    45 )
   153                 self.ui.warn(_(b'invalid tag entry: "%s"\n') % line)
   158                 self.ui.warn(_(b'invalid tag entry: "%s"\n') % line)
   154                 fp.write(b'%s\n' % line)  # Bogus, but keep for hash stability
   159                 fp.write(b'%s\n' % line)  # Bogus, but keep for hash stability
   155                 continue
   160                 continue
   156             revid = revmap.get(source.lookuprev(s[0]))
   161             revid = revmap.get(source.lookuprev(s[0]))
   157             if not revid:
   162             if not revid:
   158                 if s[0] == nodemod.nullhex:
   163                 if s[0] == nullhex:
   159                     revid = s[0]
   164                     revid = s[0]
   160                 else:
   165                 else:
   161                     # missing, but keep for hash stability
   166                     # missing, but keep for hash stability
   162                     self.ui.warn(_(b'missing tag entry: "%s"\n') % line)
   167                     self.ui.warn(_(b'missing tag entry: "%s"\n') % line)
   163                     fp.write(b'%s\n' % line)
   168                     fp.write(b'%s\n' % line)
   172             if len(s) != 2:
   177             if len(s) != 2:
   173                 continue
   178                 continue
   174 
   179 
   175             revid = s[0]
   180             revid = s[0]
   176             subpath = s[1]
   181             subpath = s[1]
   177             if revid != nodemod.nullhex:
   182             if revid != nullhex:
   178                 revmap = self.subrevmaps.get(subpath)
   183                 revmap = self.subrevmaps.get(subpath)
   179                 if revmap is None:
   184                 if revmap is None:
   180                     revmap = mapfile(
   185                     revmap = mapfile(
   181                         self.ui, self.repo.wjoin(subpath, b'.hg/shamap')
   186                         self.ui, self.repo.wjoin(subpath, b'.hg/shamap')
   182                     )
   187                     )
   293             if p not in pl:
   298             if p not in pl:
   294                 pl.append(p)
   299                 pl.append(p)
   295         parents = pl
   300         parents = pl
   296         nparents = len(parents)
   301         nparents = len(parents)
   297         if self.filemapmode and nparents == 1:
   302         if self.filemapmode and nparents == 1:
   298             m1node = self.repo.changelog.read(nodemod.bin(parents[0]))[0]
   303             m1node = self.repo.changelog.read(bin(parents[0]))[0]
   299             parent = parents[0]
   304             parent = parents[0]
   300 
   305 
   301         if len(parents) < 2:
   306         if len(parents) < 2:
   302             parents.append(nodemod.nullid)
   307             parents.append(nullid)
   303         if len(parents) < 2:
   308         if len(parents) < 2:
   304             parents.append(nodemod.nullid)
   309             parents.append(nullid)
   305         p2 = parents.pop(0)
   310         p2 = parents.pop(0)
   306 
   311 
   307         text = commit.desc
   312         text = commit.desc
   308 
   313 
   309         sha1s = re.findall(sha1re, text)
   314         sha1s = re.findall(sha1re, text)
   330             if node is None:
   335             if node is None:
   331                 continue
   336                 continue
   332 
   337 
   333             # Only transplant stores its reference in binary
   338             # Only transplant stores its reference in binary
   334             if label == b'transplant_source':
   339             if label == b'transplant_source':
   335                 node = nodemod.hex(node)
   340                 node = hex(node)
   336 
   341 
   337             newrev = revmap.get(node)
   342             newrev = revmap.get(node)
   338             if newrev is not None:
   343             if newrev is not None:
   339                 if label == b'transplant_source':
   344                 if label == b'transplant_source':
   340                     newrev = nodemod.bin(newrev)
   345                     newrev = bin(newrev)
   341 
   346 
   342                 extra[label] = newrev
   347                 extra[label] = newrev
   343 
   348 
   344         if self.branchnames and commit.branch:
   349         if self.branchnames and commit.branch:
   345             extra[b'branch'] = commit.branch
   350             extra[b'branch'] = commit.branch
   349         while parents:
   354         while parents:
   350             p1 = p2
   355             p1 = p2
   351             p2 = parents.pop(0)
   356             p2 = parents.pop(0)
   352             p1ctx = self.repo[p1]
   357             p1ctx = self.repo[p1]
   353             p2ctx = None
   358             p2ctx = None
   354             if p2 != nodemod.nullid:
   359             if p2 != nullid:
   355                 p2ctx = self.repo[p2]
   360                 p2ctx = self.repo[p2]
   356             fileset = set(files)
   361             fileset = set(files)
   357             if full:
   362             if full:
   358                 fileset.update(self.repo[p1])
   363                 fileset.update(self.repo[p1])
   359                 fileset.update(self.repo[p2])
   364                 fileset.update(self.repo[p2])
   387             with self.repo.transaction(b"convert") as tr:
   392             with self.repo.transaction(b"convert") as tr:
   388                 if self.repo.ui.config(b'convert', b'hg.preserve-hash'):
   393                 if self.repo.ui.config(b'convert', b'hg.preserve-hash'):
   389                     origctx = commit.ctx
   394                     origctx = commit.ctx
   390                 else:
   395                 else:
   391                     origctx = None
   396                     origctx = None
   392                 node = nodemod.hex(self.repo.commitctx(ctx, origctx=origctx))
   397                 node = hex(self.repo.commitctx(ctx, origctx=origctx))
   393 
   398 
   394                 # If the node value has changed, but the phase is lower than
   399                 # If the node value has changed, but the phase is lower than
   395                 # draft, set it back to draft since it hasn't been exposed
   400                 # draft, set it back to draft since it hasn't been exposed
   396                 # anywhere.
   401                 # anywhere.
   397                 if commit.rev != node:
   402                 if commit.rev != node:
   404             text = b"(octopus merge fixup)\n"
   409             text = b"(octopus merge fixup)\n"
   405             p2 = node
   410             p2 = node
   406 
   411 
   407         if self.filemapmode and nparents == 1:
   412         if self.filemapmode and nparents == 1:
   408             man = self.repo.manifestlog.getstorage(b'')
   413             man = self.repo.manifestlog.getstorage(b'')
   409             mnode = self.repo.changelog.read(nodemod.bin(p2))[0]
   414             mnode = self.repo.changelog.read(bin(p2))[0]
   410             closed = b'close' in commit.extra
   415             closed = b'close' in commit.extra
   411             if not closed and not man.cmp(m1node, man.revision(mnode)):
   416             if not closed and not man.cmp(m1node, man.revision(mnode)):
   412                 self.ui.status(_(b"filtering out empty revision\n"))
   417                 self.ui.status(_(b"filtering out empty revision\n"))
   413                 self.repo.rollback(force=True)
   418                 self.repo.rollback(force=True)
   414                 return parent
   419                 return parent
   415         return p2
   420         return p2
   416 
   421 
   417     def puttags(self, tags):
   422     def puttags(self, tags):
   418         tagparent = self.repo.branchtip(self.tagsbranch, ignoremissing=True)
   423         tagparent = self.repo.branchtip(self.tagsbranch, ignoremissing=True)
   419         tagparent = tagparent or nodemod.nullid
   424         tagparent = tagparent or nullid
   420 
   425 
   421         oldlines = set()
   426         oldlines = set()
   422         for branch, heads in pycompat.iteritems(self.repo.branchmap()):
   427         for branch, heads in pycompat.iteritems(self.repo.branchmap()):
   423             for h in heads:
   428             for h in heads:
   424                 if b'.hgtags' in self.repo[h]:
   429                 if b'.hgtags' in self.repo[h]:
   466             b"convert-repo",
   471             b"convert-repo",
   467             date,
   472             date,
   468             extra,
   473             extra,
   469         )
   474         )
   470         node = self.repo.commitctx(ctx)
   475         node = self.repo.commitctx(ctx)
   471         return nodemod.hex(node), nodemod.hex(tagparent)
   476         return hex(node), hex(tagparent)
   472 
   477 
   473     def setfilemapmode(self, active):
   478     def setfilemapmode(self, active):
   474         self.filemapmode = active
   479         self.filemapmode = active
   475 
   480 
   476     def putbookmarks(self, updatedbookmark):
   481     def putbookmarks(self, updatedbookmark):
   482             lock = self.repo.lock()
   487             lock = self.repo.lock()
   483             tr = self.repo.transaction(b'bookmark')
   488             tr = self.repo.transaction(b'bookmark')
   484             self.ui.status(_(b"updating bookmarks\n"))
   489             self.ui.status(_(b"updating bookmarks\n"))
   485             destmarks = self.repo._bookmarks
   490             destmarks = self.repo._bookmarks
   486             changes = [
   491             changes = [
   487                 (bookmark, nodemod.bin(updatedbookmark[bookmark]))
   492                 (bookmark, bin(updatedbookmark[bookmark]))
   488                 for bookmark in updatedbookmark
   493                 for bookmark in updatedbookmark
   489             ]
   494             ]
   490             destmarks.applychanges(self.repo, tr, changes)
   495             destmarks.applychanges(self.repo, tr, changes)
   491             tr.close()
   496             tr.close()
   492         finally:
   497         finally:
   575 
   580 
   576     def _parents(self, ctx):
   581     def _parents(self, ctx):
   577         return [p for p in ctx.parents() if p and self.keep(p.node())]
   582         return [p for p in ctx.parents() if p and self.keep(p.node())]
   578 
   583 
   579     def getheads(self):
   584     def getheads(self):
   580         return [nodemod.hex(h) for h in self._heads if self.keep(h)]
   585         return [hex(h) for h in self._heads if self.keep(h)]
   581 
   586 
   582     def getfile(self, name, rev):
   587     def getfile(self, name, rev):
   583         try:
   588         try:
   584             fctx = self._changectx(rev)[name]
   589             fctx = self._changectx(rev)[name]
   585             return fctx.data(), fctx.flags()
   590             return fctx.data(), fctx.flags()
   676         tags = [
   681         tags = [
   677             t
   682             t
   678             for t in self.repo.tagslist()
   683             for t in self.repo.tagslist()
   679             if self.repo.tagtype(t[0]) == b'global'
   684             if self.repo.tagtype(t[0]) == b'global'
   680         ]
   685         ]
   681         return {
   686         return {name: hex(node) for name, node in tags if self.keep(node)}
   682             name: nodemod.hex(node) for name, node in tags if self.keep(node)
       
   683         }
       
   684 
   687 
   685     def getchangedfiles(self, rev, i):
   688     def getchangedfiles(self, rev, i):
   686         ctx = self._changectx(rev)
   689         ctx = self._changectx(rev)
   687         parents = self._parents(ctx)
   690         parents = self._parents(ctx)
   688         if not parents and i is None:
   691         if not parents and i is None:
   716     def hasnativeclose(self):
   719     def hasnativeclose(self):
   717         return True
   720         return True
   718 
   721 
   719     def lookuprev(self, rev):
   722     def lookuprev(self, rev):
   720         try:
   723         try:
   721             return nodemod.hex(self.repo.lookup(rev))
   724             return hex(self.repo.lookup(rev))
   722         except (error.RepoError, error.LookupError):
   725         except (error.RepoError, error.LookupError):
   723             return None
   726             return None
   724 
   727 
   725     def getbookmarks(self):
   728     def getbookmarks(self):
   726         return bookmarks.listbookmarks(self.repo)
   729         return bookmarks.listbookmarks(self.repo)