changeset 50195:11e6eee4b063

transaction: use the standard transaction mechanism to backup branch Branch is a bit special : - It currently does not collaborate with the transaction (or any scoping) for writing (this is bad) - It can change without the lock being taken (it is protected by `wlock`) So we rely on the same mechanism as for the backup of the other dirstate file: - we only do a backup if we hold the wlock - we force a backup though the transaction Since "branch" write does not collaborate with the transaction, we cannot back it up "at the last minute" as we do for the dirstate. We have to back it up "upfront". Since we have a backup, the transaction is no longer doing its "quick_abort" and get noisy. Which is quite annoying. To work around this, and to avoid jumping in yet-another-rabbit-hole of "getting branch written properly", I am doing horrible things to the transaction in the meantime. We should be able to get this code go away during the next cycle. In the meantime, I prefer to take this small stop so that we stop abusing the "journal" and "undo" mechanism instead of the proper backup mechanism of the transaction. Also note that this change regress the warning message for the legacy fallback introduced in 2008 when issue902 got fixed in dd5a501cb97f (Mercurial 1.0). I feel like this is fine as issue 902 remains fixed, and this would only affect people deploying a mix of 15 year old Mercurial and modern mercurial, and using branch and rollback extensively.
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Thu, 23 Feb 2023 15:37:46 +0100
parents 8fb391363aad
children 2a7e8471782c
files mercurial/localrepo.py mercurial/shelve.py mercurial/transaction.py tests/test-empty.t tests/test-fncache.t tests/test-hardlinks.t tests/test-inherit-mode.t tests/test-largefiles-small-disk.t tests/test-rollback.t
diffstat 9 files changed, 36 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/localrepo.py	Thu Feb 23 04:53:34 2023 +0100
+++ b/mercurial/localrepo.py	Thu Feb 23 15:37:46 2023 +0100
@@ -100,7 +100,9 @@
 urlerr = util.urlerr
 urlreq = util.urlreq
 
-RE_SKIP_DIRSTATE_ROLLBACK = re.compile(b"^(dirstate|narrowspec.dirstate).*")
+RE_SKIP_DIRSTATE_ROLLBACK = re.compile(
+    b"^((dirstate|narrowspec.dirstate).*|branch$)"
+)
 
 # set of (path, vfs-location) tuples. vfs-location is:
 # - 'plain for vfs relative paths
@@ -2671,6 +2673,13 @@
         # strip" to pick a working copy destination on `hg rollback`
         if self.currentwlock() is not None:
             ds = self.dirstate
+            if ds.branch() == b'default':
+                # force a file to be written if None exist
+                ds.setbranch(b'default')
+            # we cannot simply add "branch" to `all_file_names` because branch
+            # is written outside of the transaction control. So we need to
+            # backup early.
+            tr.addbackup(b"branch", hardlink=True, location=b'plain')
 
             def backup_dirstate(tr):
                 for f in ds.all_file_names():
@@ -2685,7 +2694,6 @@
     def _journalfiles(self):
         return (
             (self.svfs, b'journal'),
-            (self.vfs, b'journal.branch'),
             (self.vfs, b'journal.desc'),
         )
 
@@ -2694,9 +2702,6 @@
 
     @unfilteredmethod
     def _writejournal(self, desc):
-        self.vfs.write(
-            b"journal.branch", encoding.fromlocal(self.dirstate.branch())
-        )
         self.vfs.write(b"journal.desc", b"%d\n%s\n" % (len(self), desc))
 
     def recover(self):
@@ -2799,18 +2804,6 @@
                     self.dirstate.setparents(self.nullid)
                     self.dirstate.clear()
 
-            try:
-                branch = self.vfs.read(b'undo.branch')
-                self.dirstate.setbranch(encoding.tolocal(branch))
-            except IOError:
-                ui.warn(
-                    _(
-                        b'named branch could not be reset: '
-                        b'current branch is still \'%s\'\n'
-                    )
-                    % self.dirstate.branch()
-                )
-
             parents = tuple([p.rev() for p in self[None].parents()])
             if len(parents) > 1:
                 ui.status(
--- a/mercurial/shelve.py	Thu Feb 23 04:53:34 2023 +0100
+++ b/mercurial/shelve.py	Thu Feb 23 15:37:46 2023 +0100
@@ -440,6 +440,7 @@
     # These is not such other wrapping currently, but if someone try to
     # implement one in the future, this will explicitly break here instead of
     # misbehaving in subtle ways.
+    current_branch = ds.branch()
     assert 'invalidate' not in vars(ds)
     try:
         # note : we could simply disable the transaction abort callback, but
@@ -452,6 +453,7 @@
     # transaction to do so.
     assert repo.currenttransaction() is None
     repo.dirstate.write(None)
+    ds.setbranch(current_branch)
 
 
 def getshelvename(repo, parent, opts):
--- a/mercurial/transaction.py	Thu Feb 23 04:53:34 2023 +0100
+++ b/mercurial/transaction.py	Thu Feb 23 15:37:46 2023 +0100
@@ -692,6 +692,10 @@
         if entries:
             return False
         for e in self._backupentries:
+            if e[0] == b'plain' and e[1] == b'branch':
+                # XXX integrate branch to the transaction and get rid of this
+                # aberration
+                continue
             if e[1]:
                 return False
         return True
@@ -701,7 +705,9 @@
         assert self._can_quick_abort(entries)
         tmp_files = [e for e in self._backupentries if not e[1]]
         for vfs_id, old_path, tmp_path, xxx in tmp_files:
-            assert not old_path
+            # XXX integrate branch to the transaction and get rid of this
+            # aberration
+            assert not old_path or old_path == b'branch'
             vfs = self._vfsmap[vfs_id]
             try:
                 vfs.unlink(tmp_path)
--- a/tests/test-empty.t	Thu Feb 23 04:53:34 2023 +0100
+++ b/tests/test-empty.t	Thu Feb 23 15:37:46 2023 +0100
@@ -35,10 +35,12 @@
   $ hg verify -q
   $ ls .hg
   00changelog.i
+  branch
   cache
   hgrc
   requires
   store
+  undo.backup.branch
   wcache
 
 Should be empty (except for the "basic" requires):
--- a/tests/test-fncache.t	Thu Feb 23 04:53:34 2023 +0100
+++ b/tests/test-fncache.t	Thu Feb 23 15:37:46 2023 +0100
@@ -90,6 +90,7 @@
   .hg
   .hg/00changelog.i
   .hg/00manifest.i
+  .hg/branch
   .hg/cache
   .hg/cache/branch2-served
   .hg/cache/rbc-names-v1
@@ -103,8 +104,8 @@
   .hg/phaseroots
   .hg/requires
   .hg/undo
+  .hg/undo.backup.branch
   .hg/undo.backupfiles
-  .hg/undo.branch
   .hg/undo.desc
   .hg/wcache
   .hg/wcache/checkisexec (execbit !)
@@ -124,6 +125,7 @@
   $ find .hg | sort
   .hg
   .hg/00changelog.i
+  .hg/branch
   .hg/cache
   .hg/cache/branch2-served
   .hg/cache/rbc-names-v1
@@ -142,7 +144,7 @@
   .hg/store/requires
   .hg/store/undo
   .hg/store/undo.backupfiles
-  .hg/undo.branch
+  .hg/undo.backup.branch
   .hg/undo.desc
   .hg/wcache
   .hg/wcache/checkisexec (execbit !)
--- a/tests/test-hardlinks.t	Thu Feb 23 04:53:34 2023 +0100
+++ b/tests/test-hardlinks.t	Thu Feb 23 15:37:46 2023 +0100
@@ -227,7 +227,7 @@
 
   $ nlinksdir r4
   2 r4/.hg/00changelog.i
-  2 r4/.hg/branch
+  [24] r4/.hg/branch (re)
   2 r4/.hg/cache/branch2-base
   2 r4/.hg/cache/branch2-immutable
   2 r4/.hg/cache/branch2-served
@@ -256,8 +256,8 @@
   2 r4/.hg/store/undo.backup.fncache (repofncache !)
   2 r4/.hg/store/undo.backup.phaseroots
   2 r4/.hg/store/undo.backupfiles
+  [24] r4/.hg/undo.backup.branch (re)
   2 r4/\.hg/undo\.backup\.dirstate (re)
-  2 r4/.hg/undo.branch
   2 r4/.hg/undo.desc
   2 r4/.hg/wcache/checkisexec (execbit !)
   2 r4/.hg/wcache/checklink-target (symlink !)
@@ -311,8 +311,8 @@
   2 r4/.hg/store/undo.backup.fncache (repofncache !)
   2 r4/.hg/store/undo.backup.phaseroots
   2 r4/.hg/store/undo.backupfiles
+  [23] r4/.hg/undo.backup.branch (re)
   2 r4/\.hg/undo\.backup\.dirstate (re)
-  2 r4/.hg/undo.branch
   2 r4/.hg/undo.desc
   2 r4/.hg/wcache/checkisexec (execbit !)
   2 r4/.hg/wcache/checklink-target (symlink !)
--- a/tests/test-inherit-mode.t	Thu Feb 23 04:53:34 2023 +0100
+++ b/tests/test-inherit-mode.t	Thu Feb 23 15:37:46 2023 +0100
@@ -68,6 +68,7 @@
   $ "$PYTHON" ../printmodes.py .
   00700 ./.hg/
   00600 ./.hg/00changelog.i
+  00660 ./.hg/branch
   00770 ./.hg/cache/
   00660 ./.hg/cache/branch2-served
   00660 ./.hg/cache/rbc-names-v1
@@ -94,7 +95,7 @@
   00600 ./.hg/store/requires
   00660 ./.hg/store/undo
   00660 ./.hg/store/undo.backupfiles
-  00660 ./.hg/undo.branch
+  00660 ./.hg/undo.backup.branch
   00660 ./.hg/undo.desc
   00770 ./.hg/wcache/
   00711 ./.hg/wcache/checkisexec
@@ -129,6 +130,7 @@
   $ "$PYTHON" ../printmodes.py ../push
   00770 ../push/.hg/
   00660 ../push/.hg/00changelog.i
+  00660 ../push/.hg/branch
   00770 ../push/.hg/cache/
   00660 ../push/.hg/cache/branch2-base
   00660 ../push/.hg/cache/rbc-names-v1
@@ -151,7 +153,7 @@
   00660 ../push/.hg/store/requires
   00660 ../push/.hg/store/undo
   00660 ../push/.hg/store/undo.backupfiles
-  00660 ../push/.hg/undo.branch
+  00660 ../push/.hg/undo.backup.branch
   00660 ../push/.hg/undo.desc
   00770 ../push/.hg/wcache/
 
--- a/tests/test-largefiles-small-disk.t	Thu Feb 23 04:53:34 2023 +0100
+++ b/tests/test-largefiles-small-disk.t	Thu Feb 23 15:37:46 2023 +0100
@@ -10,7 +10,7 @@
   > _origcopyfileobj = shutil.copyfileobj
   > def copyfileobj(fsrc, fdst, length=16 * 1024):
   >     # allow journal files (used by transaction) to be written
-  >     if b'journal.' in fdst.name:
+  >     if b'journal.' in fdst.name or b'backup.' in fdst.name:
   >         return _origcopyfileobj(fsrc, fdst, length)
   >     fdst.write(fsrc.read(4))
   >     raise IOError(errno.ENOSPC, os.strerror(errno.ENOSPC))
--- a/tests/test-rollback.t	Thu Feb 23 04:53:34 2023 +0100
+++ b/tests/test-rollback.t	Thu Feb 23 15:37:46 2023 +0100
@@ -42,21 +42,11 @@
   $ cat .hg/last-message.txt ; echo
   modify a
 
-Test rollback of hg before issue 902 was fixed
 
-  $ hg commit -m "test3"
-  $ hg branch test
-  marked working directory as branch test
-  (branches are permanent and global, did you want a bookmark?)
-  $ rm .hg/undo.branch
-  $ hg rollback
-  repository tip rolled back to revision 0 (undo commit)
-  named branch could not be reset: current branch is still 'test'
-  working directory now based on revision 0
+working dir unaffected by rollback: do not restore dirstate et. al.
+  $ hg branch test --quiet
   $ hg branch
   test
-
-working dir unaffected by rollback: do not restore dirstate et. al.
   $ hg log --template '{rev}  {branch}  {desc|firstline}\n'
   0  default  add a again
   $ hg status
@@ -82,7 +72,7 @@
   $ hg update bar
   1 files updated, 0 files merged, 1 files removed, 0 files unresolved
   (activating bookmark bar)
-  $ cat .hg/undo.branch ; echo
+  $ cat .hg/undo.backup.branch
   test
   $ hg log -G --template '{rev}  [{branch}] ({bookmarks}) {desc|firstline}\n'
   o  2  [test] (foo) add b