convert: add a config option to help doing identity hg->hg conversion
authorValentin Gatien-Baron <valentin.gatienbaron@gmail.com>
Sat, 13 Jul 2019 23:45:32 -0400
changeset 42620 d98ec36be808
parent 42619 20d0e59be79b
child 42621 99ebde4fec99
convert: add a config option to help doing identity hg->hg conversion I want to change the computation of the list of files modified by a commit. In principle, this would simply change a cache. But since this information is stored in commits rather than a cache, changing it means changing commit hashes (going forward). Some users rely on the convert extension from hg to hg not changing hashes when nothing changes (usually). Allow these users to preserve hashes despite changes to the changelog files computation by reusing these files lists when the manifest is unchanged (since these files list are derived from the manifest). Differential Revision: https://phab.mercurial-scm.org/D6643
hgext/convert/__init__.py
hgext/convert/common.py
hgext/convert/hg.py
hgext/eol.py
hgext/keyword.py
hgext/lfs/__init__.py
hgext/remotefilelog/shallowrepo.py
mercurial/configitems.py
mercurial/localrepo.py
mercurial/repository.py
tests/test-commandserver.t
tests/test-convert-identity.t
tests/test-convert.t
--- a/hgext/convert/__init__.py	Tue Jul 02 12:55:51 2019 -0400
+++ b/hgext/convert/__init__.py	Sat Jul 13 23:45:32 2019 -0400
@@ -439,6 +439,11 @@
     :convert.hg.sourcename: records the given string as a 'convert_source' extra
         value on each commit made in the target repository. The default is None.
 
+    :convert.hg.preserve-hash: only works with mercurial sources. Make convert
+        prevent performance improvement to the list of modified files in commits
+        when such an improvement would cause the hash of a commit to change.
+        The default is False.
+
     All Destinations
     ################
 
--- a/hgext/convert/common.py	Tue Jul 02 12:55:51 2019 -0400
+++ b/hgext/convert/common.py	Sat Jul 13 23:45:32 2019 -0400
@@ -114,7 +114,7 @@
 class commit(object):
     def __init__(self, author, date, desc, parents, branch=None, rev=None,
                  extra=None, sortkey=None, saverev=True, phase=phases.draft,
-                 optparents=None):
+                 optparents=None, ctx=None):
         self.author = author or 'unknown'
         self.date = date or '0 0'
         self.desc = desc
@@ -126,6 +126,7 @@
         self.sortkey = sortkey
         self.saverev = saverev
         self.phase = phase
+        self.ctx = ctx # for hg to hg conversions
 
 class converter_source(object):
     """Conversion source interface"""
--- a/hgext/convert/hg.py	Tue Jul 02 12:55:51 2019 -0400
+++ b/hgext/convert/hg.py	Sat Jul 13 23:45:32 2019 -0400
@@ -339,7 +339,11 @@
                                    phases.phasenames[commit.phase], 'convert')
 
             with self.repo.transaction("convert") as tr:
-                node = nodemod.hex(self.repo.commitctx(ctx))
+                if self.repo.ui.config('convert', 'hg.preserve-hash'):
+                    origctx = commit.ctx
+                else:
+                    origctx = None
+                node = nodemod.hex(self.repo.commitctx(ctx, origctx=origctx))
 
                 # If the node value has changed, but the phase is lower than
                 # draft, set it back to draft since it hasn't been exposed
@@ -591,7 +595,8 @@
                              extra=ctx.extra(),
                              sortkey=ctx.rev(),
                              saverev=self.saverev,
-                             phase=ctx.phase())
+                             phase=ctx.phase(),
+                             ctx=ctx)
 
     def numcommits(self):
         return len(self.repo)
--- a/hgext/eol.py	Tue Jul 02 12:55:51 2019 -0400
+++ b/hgext/eol.py	Sat Jul 13 23:45:32 2019 -0400
@@ -400,7 +400,7 @@
                     if wlock is not None:
                         wlock.release()
 
-        def commitctx(self, ctx, error=False):
+        def commitctx(self, ctx, error=False, origctx=None):
             for f in sorted(ctx.added() + ctx.modified()):
                 if not self._eolmatch(f):
                     continue
@@ -416,6 +416,6 @@
                 if inconsistenteol(data):
                     raise errormod.Abort(_("inconsistent newline style "
                                            "in %s\n") % f)
-            return super(eolrepo, self).commitctx(ctx, error)
+            return super(eolrepo, self).commitctx(ctx, error, origctx)
     repo.__class__ = eolrepo
     repo._hgcleardirstate()
--- a/hgext/keyword.py	Tue Jul 02 12:55:51 2019 -0400
+++ b/hgext/keyword.py	Sat Jul 13 23:45:32 2019 -0400
@@ -785,8 +785,8 @@
             finally:
                 del self.commitctx
 
-        def kwcommitctx(self, ctx, error=False):
-            n = super(kwrepo, self).commitctx(ctx, error)
+        def kwcommitctx(self, ctx, error=False, origctx=None):
+            n = super(kwrepo, self).commitctx(ctx, error, origctx)
             # no lock needed, only called from repo.commit() which already locks
             if not kwt.postcommit:
                 restrict = kwt.restrict
--- a/hgext/lfs/__init__.py	Tue Jul 02 12:55:51 2019 -0400
+++ b/hgext/lfs/__init__.py	Sat Jul 13 23:45:32 2019 -0400
@@ -227,9 +227,9 @@
 
     class lfsrepo(repo.__class__):
         @localrepo.unfilteredmethod
-        def commitctx(self, ctx, error=False):
+        def commitctx(self, ctx, error=False, origctx=None):
             repo.svfs.options['lfstrack'] = _trackedmatcher(self)
-            return super(lfsrepo, self).commitctx(ctx, error)
+            return super(lfsrepo, self).commitctx(ctx, error, origctx=origctx)
 
     repo.__class__ = lfsrepo
 
--- a/hgext/remotefilelog/shallowrepo.py	Tue Jul 02 12:55:51 2019 -0400
+++ b/hgext/remotefilelog/shallowrepo.py	Sat Jul 13 23:45:32 2019 -0400
@@ -161,7 +161,7 @@
                                                               **kwargs)
 
         @localrepo.unfilteredmethod
-        def commitctx(self, ctx, error=False):
+        def commitctx(self, ctx, error=False, origctx=None):
             """Add a new revision to current repository.
             Revision information is passed via the context argument.
             """
@@ -179,7 +179,8 @@
                         files.append((f, hex(fparent1)))
                 self.fileservice.prefetch(files)
             return super(shallowrepository, self).commitctx(ctx,
-                                                            error=error)
+                                                            error=error,
+                                                            origctx=origctx)
 
         def backgroundprefetch(self, revs, base=None, repack=False, pats=None,
                                opts=None):
--- a/mercurial/configitems.py	Tue Jul 02 12:55:51 2019 -0400
+++ b/mercurial/configitems.py	Sat Jul 13 23:45:32 2019 -0400
@@ -291,6 +291,9 @@
 coreconfigitem('convert', 'hg.ignoreerrors',
     default=False,
 )
+coreconfigitem('convert', 'hg.preserve-hash',
+    default=False,
+)
 coreconfigitem('convert', 'hg.revs',
     default=None,
 )
--- a/mercurial/localrepo.py	Tue Jul 02 12:55:51 2019 -0400
+++ b/mercurial/localrepo.py	Sat Jul 13 23:45:32 2019 -0400
@@ -2578,7 +2578,7 @@
         return ret
 
     @unfilteredmethod
-    def commitctx(self, ctx, error=False):
+    def commitctx(self, ctx, error=False, origctx=None):
         """Add a new revision to current repository.
         Revision information is passed via the context argument.
 
@@ -2586,6 +2586,12 @@
         modified/added/removed files. On merge, it may be wider than the
         ctx.files() to be committed, since any file nodes derived directly
         from p1 or p2 are excluded from the committed ctx.files().
+
+        origctx is for convert to work around the problem that bug
+        fixes to the files list in changesets change hashes. For
+        convert to be the identity, it can pass an origctx and this
+        function will use the same files list when it makes sense to
+        do so.
         """
 
         p1, p2 = ctx.p1(), ctx.p2()
@@ -2701,6 +2707,9 @@
                 filesadded = filesadded or None
                 filesremoved = filesremoved or None
 
+            if origctx and origctx.manifestnode() == mn:
+                files = origctx.files()
+
             # update changelog
             self.ui.note(_("committing changelog\n"))
             self.changelog.delayupdate(tr)
--- a/mercurial/repository.py	Tue Jul 02 12:55:51 2019 -0400
+++ b/mercurial/repository.py	Sat Jul 13 23:45:32 2019 -0400
@@ -1656,7 +1656,7 @@
                editor=False, extra=None):
         """Add a new revision to the repository."""
 
-    def commitctx(ctx, error=False):
+    def commitctx(ctx, error=False, origctx=None):
         """Commit a commitctx instance to the repository."""
 
     def destroying():
--- a/tests/test-commandserver.t	Tue Jul 02 12:55:51 2019 -0400
+++ b/tests/test-commandserver.t	Sat Jul 13 23:45:32 2019 -0400
@@ -917,13 +917,13 @@
   >     raise error.Abort(b'fail after finalization')
   > def reposetup(ui, repo):
   >     class failrepo(repo.__class__):
-  >         def commitctx(self, ctx, error=False):
+  >         def commitctx(self, ctx, error=False, origctx=None):
   >             if self.ui.configbool(b'failafterfinalize', b'fail'):
   >                 # 'sorted()' by ASCII code on category names causes
   >                 # invoking 'fail' after finalization of changelog
   >                 # using "'cl-%i' % id(self)" as category name
   >                 self.currenttransaction().addfinalize(b'zzzzzzzz', fail)
-  >             return super(failrepo, self).commitctx(ctx, error)
+  >             return super(failrepo, self).commitctx(ctx, error, origctx)
   >     repo.__class__ = failrepo
   > EOF
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-convert-identity.t	Sat Jul 13 23:45:32 2019 -0400
@@ -0,0 +1,40 @@
+Testing that convert.hg.preserve-hash=true can be used to make hg
+convert from hg repo to hg repo preserve hashes, even if the
+computation of the files list in commits change slightly between hg
+versions.
+
+  $ cat <<'EOF' >> "$HGRCPATH"
+  > [extensions]
+  > convert =
+  > EOF
+  $ cat <<'EOF' > changefileslist.py
+  > from mercurial import (changelog, extensions)
+  > def wrap(orig, clog, manifest, files, *args, **kwargs):
+  >   return orig(clog, manifest, ["a"], *args, **kwargs)
+  > def extsetup(ui):
+  >   extensions.wrapfunction(changelog.changelog, 'add', wrap)
+  > EOF
+
+  $ hg init repo
+  $ cd repo
+  $ echo a > a; hg commit -qAm a
+  $ echo b > a; hg commit -qAm b
+  $ hg up -qr 0; echo c > c; hg commit -qAm c
+  $ hg merge -qr 1
+  $ hg commit -m_ --config extensions.x=../changefileslist.py
+  $ hg log -r . -T '{node|short} {files|json}\n'
+  c085bbe93d59 ["a"]
+
+Now that we have a commit with a files list that's not what the
+current hg version would create, check that convert either fixes it or
+keeps it depending on config:
+
+  $ hg convert -q . ../convert
+  $ hg --cwd ../convert log -r tip -T '{node|short} {files|json}\n'
+  b7c4d4bbacd3 []
+  $ rm -rf ../convert
+
+  $ hg convert -q . ../convert --config convert.hg.preserve-hash=true
+  $ hg --cwd ../convert log -r tip -T '{node|short} {files|json}\n'
+  c085bbe93d59 ["a"]
+  $ rm -rf ../convert
--- a/tests/test-convert.t	Tue Jul 02 12:55:51 2019 -0400
+++ b/tests/test-convert.t	Sat Jul 13 23:45:32 2019 -0400
@@ -373,6 +373,11 @@
                     records the given string as a 'convert_source' extra value
                     on each commit made in the target repository. The default is
                     None.
+      convert.hg.preserve-hash
+                    only works with mercurial sources. Make convert prevent
+                    performance improvement to the list of modified files in
+                    commits when such an improvement would cause the hash of a
+                    commit to change. The default is False.
   
       All Destinations
       ################