Mercurial > public > mercurial-scm > hg
comparison mercurial/localrepo.py @ 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 | 240a04cedd24 |
comparison
equal
deleted
inserted
replaced
50194:8fb391363aad | 50195:11e6eee4b063 |
---|---|
98 | 98 |
99 release = lockmod.release | 99 release = lockmod.release |
100 urlerr = util.urlerr | 100 urlerr = util.urlerr |
101 urlreq = util.urlreq | 101 urlreq = util.urlreq |
102 | 102 |
103 RE_SKIP_DIRSTATE_ROLLBACK = re.compile(b"^(dirstate|narrowspec.dirstate).*") | 103 RE_SKIP_DIRSTATE_ROLLBACK = re.compile( |
104 b"^((dirstate|narrowspec.dirstate).*|branch$)" | |
105 ) | |
104 | 106 |
105 # set of (path, vfs-location) tuples. vfs-location is: | 107 # set of (path, vfs-location) tuples. vfs-location is: |
106 # - 'plain for vfs relative paths | 108 # - 'plain for vfs relative paths |
107 # - '' for svfs relative paths | 109 # - '' for svfs relative paths |
108 _cachedfiles = set() | 110 _cachedfiles = set() |
2669 # | 2671 # |
2670 # - slightly changing the behavior an applying a logic similar to "hg | 2672 # - slightly changing the behavior an applying a logic similar to "hg |
2671 # strip" to pick a working copy destination on `hg rollback` | 2673 # strip" to pick a working copy destination on `hg rollback` |
2672 if self.currentwlock() is not None: | 2674 if self.currentwlock() is not None: |
2673 ds = self.dirstate | 2675 ds = self.dirstate |
2676 if ds.branch() == b'default': | |
2677 # force a file to be written if None exist | |
2678 ds.setbranch(b'default') | |
2679 # we cannot simply add "branch" to `all_file_names` because branch | |
2680 # is written outside of the transaction control. So we need to | |
2681 # backup early. | |
2682 tr.addbackup(b"branch", hardlink=True, location=b'plain') | |
2674 | 2683 |
2675 def backup_dirstate(tr): | 2684 def backup_dirstate(tr): |
2676 for f in ds.all_file_names(): | 2685 for f in ds.all_file_names(): |
2677 # hardlink backup is okay because `dirstate` is always | 2686 # hardlink backup is okay because `dirstate` is always |
2678 # atomically written and possible data file are append only | 2687 # atomically written and possible data file are append only |
2683 return tr | 2692 return tr |
2684 | 2693 |
2685 def _journalfiles(self): | 2694 def _journalfiles(self): |
2686 return ( | 2695 return ( |
2687 (self.svfs, b'journal'), | 2696 (self.svfs, b'journal'), |
2688 (self.vfs, b'journal.branch'), | |
2689 (self.vfs, b'journal.desc'), | 2697 (self.vfs, b'journal.desc'), |
2690 ) | 2698 ) |
2691 | 2699 |
2692 def undofiles(self): | 2700 def undofiles(self): |
2693 return [(vfs, undoname(x)) for vfs, x in self._journalfiles()] | 2701 return [(vfs, undoname(x)) for vfs, x in self._journalfiles()] |
2694 | 2702 |
2695 @unfilteredmethod | 2703 @unfilteredmethod |
2696 def _writejournal(self, desc): | 2704 def _writejournal(self, desc): |
2697 self.vfs.write( | |
2698 b"journal.branch", encoding.fromlocal(self.dirstate.branch()) | |
2699 ) | |
2700 self.vfs.write(b"journal.desc", b"%d\n%s\n" % (len(self), desc)) | 2705 self.vfs.write(b"journal.desc", b"%d\n%s\n" % (len(self), desc)) |
2701 | 2706 |
2702 def recover(self): | 2707 def recover(self): |
2703 with self.lock(): | 2708 with self.lock(): |
2704 if self.svfs.exists(b"journal"): | 2709 if self.svfs.exists(b"journal"): |
2796 # There was no dirstate to backup initially, we need to drop | 2801 # There was no dirstate to backup initially, we need to drop |
2797 # the existing one. | 2802 # the existing one. |
2798 with self.dirstate.changing_parents(self): | 2803 with self.dirstate.changing_parents(self): |
2799 self.dirstate.setparents(self.nullid) | 2804 self.dirstate.setparents(self.nullid) |
2800 self.dirstate.clear() | 2805 self.dirstate.clear() |
2801 | |
2802 try: | |
2803 branch = self.vfs.read(b'undo.branch') | |
2804 self.dirstate.setbranch(encoding.tolocal(branch)) | |
2805 except IOError: | |
2806 ui.warn( | |
2807 _( | |
2808 b'named branch could not be reset: ' | |
2809 b'current branch is still \'%s\'\n' | |
2810 ) | |
2811 % self.dirstate.branch() | |
2812 ) | |
2813 | 2806 |
2814 parents = tuple([p.rev() for p in self[None].parents()]) | 2807 parents = tuple([p.rev() for p in self[None].parents()]) |
2815 if len(parents) > 1: | 2808 if len(parents) > 1: |
2816 ui.status( | 2809 ui.status( |
2817 _( | 2810 _( |