Patchwork D12097: branchmap: skip obsolete revisions while computing heads

mail settings
Submitter phabricator
Date Jan. 28, 2022, 5:40 p.m.
Message ID <>
Download mbox | patch
Permalink /patch/50426/
State New
Headers show


phabricator - Jan. 28, 2022, 5:40 p.m.
av6 created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

  It's time to make this part of core Mercurial obsolescence-aware.
  Not considering obsolete revisions when computing heads is clearly what
  Mercurial should do. But there are a couple of small issues:
  - Let's say tip of the repo is obsolete. There are two ways of finding tiprev for branchcache (both are in use): looking at input data for update() and looking at computed heads after update(). Previously, repo tip would be tiprev of the branchcache. With this patch, an obsolete revision can no longer be tiprev. And depending on what way we use for finding tiprev (input data vs computed heads) we'll get a different result. This is relevant when recomputing cache key from cache contents, and may lead to updating cache for obsolete revisions multiple times.
  - If all commits on a branch are obsolete, the branchcache will include that branch, but the list of heads will be empty (e.g. `if not heads`). This is currently required for notify extension (and test-notify.t to pass), because notify doesn't handle revsets in its subscription config very well and will throw an error if e.g. a branch doesn't exist.
  - Static HTTP repo clones try to stat() a non-existent obsstore file. The issue is that we now care about obsolescence during clone, but statichttprepo for some reason uses wrong vfs for obsstore. It's not a statichttpvfs, but a regular local one. I couldn't find the cause of this behavior yet. During one of the operations on obsstore, we're trying to stat() the file, but in static HTTP case we provide a literal URL to the obsstore file on the remote as if it were a local file path. It's very silly. On windows it actually results in a failure in test-static-http.t.
  The first issue is going to be addressed in a series dedicated to making sure
  branchcache is properly and timely written on disk (it wasn't perfect even
  before this patch, but there aren't enough tests to demonstrate that). The
  second issue will be addressed in a future patch for notify extension. And the
  third will be partially addressed in a later patch in this series and properly
  fixed in a future patch to make sure obsstore is accessed using statichttpvfs.
  filteredhash() grows a keyword argument to make sure that branchcache is also
  invalidated when there are new obsolete revisions in its repo view. This way
  the on-disk cache format is unchanged and compatible between versions (although
  it will obviously be recomputed when switching versions before/after this patch
  and the repo has obsolete revisions).
  There's one test that uses plain `hg up` without arguments while updated to a
  pruned commit. To make this test pass, simply return current working directory
  parent. Later in this series this code will be replaced by what prune command
  does: updating to the closest non-obsolete ancestor.
  Test changes:
  test-branch-change.t: update branch head and cache update message. The head of
  default listed in hg heads is changed because revision 2 was rewritten as 7,
  and 1 is the closest ancestor on the same branch, so it's the head of default
  The cache invalidation message appears now because of the cache hash change,
  since we're now accounting for obsolete revisions. Here's some context:
  "served.hidden" repo filter means everything is visible (no filtered
  revisions), so before this series branch2-served.hidden file would not contain
  any cache hash, only revnum and node. Now it also has a hash when there are
  obsolete changesets in the repo. The command that the message appears for is
  changing branch of 5 and 6, which are now obsolete, so the cache hash changes.
  In general, when cache is simply out-of-date, it can be updated using the old
  version as a base. But if cache hash differs, then the cache for that
  particular repo filter is recomputed (at least with the current
  implementation). This is what happens here.
  test-obsmarker-template.t: the pull reports 2 heads changed, but after that the
  repo correctly sees only 1. The new message could be better, but it's still an
  improvement over the previous one where hg pull suggested merging with an
  obsolete revision.
  test-obsolete.t: we can see these revisions in hg log --hidden, but they
  shouldn't be considered heads even with --hidden.
  test-rebase-obsolete{,2}.t: there were new heads created previously after
  making new orphan changesets, but they weren't detected. Now we are properly
  detecting and reporting them.
  test-rebase-obsolete4.t: there's only one head now because the other head is
  pruned and was falsely reported before.
  test-static-http.t: add obsstore to the list of requested files. This file
  doesn't exist on the remotes, but clients want it anyway (they get 404). This
  is fine, because there are other nonexistent files that clients request, like
  .hg/bookmarks or .hg/cache/tags2-served.

  rHG Mercurial





To: av6, #hg-reviewers
Cc: mercurial-patches, mercurial-devel


diff --git a/tests/test-static-http.t b/tests/test-static-http.t
--- a/tests/test-static-http.t
+++ b/tests/test-static-http.t
@@ -255,6 +255,7 @@ 
   /remote-with-names/.hg/store/data/%7E2ehgtags.i (no-py37 !)
   /remote-with-names/.hg/store/data/~2ehgtags.i (py37 !)
+  /remote-with-names/.hg/store/obsstore
@@ -274,6 +275,7 @@ 
   /remote/.hg/store/data/~2edotfile%20with%20spaces.i (py37 !)
   /remote/.hg/store/data/~2ehgtags.i (py37 !)
+  /remote/.hg/store/obsstore
diff --git a/tests/test-rebase-obsolete4.t b/tests/test-rebase-obsolete4.t
--- a/tests/test-rebase-obsolete4.t
+++ b/tests/test-rebase-obsolete4.t
@@ -169,7 +169,7 @@ 
   branch: default
   commit: 1 modified, 1 added, 1 unknown, 1 unresolved
-  update: 1 new changesets, 2 branch heads (merge)
+  update: (current)
   phases: 3 draft
   rebase: 0 rebased, 2 remaining (rebase --continue)
diff --git a/tests/test-rebase-obsolete2.t b/tests/test-rebase-obsolete2.t
--- a/tests/test-rebase-obsolete2.t
+++ b/tests/test-rebase-obsolete2.t
@@ -47,6 +47,7 @@ 
   $ hg add C
   $ hg commit -m C
   1 new orphan changesets
+  created new head
   $ hg log -G
   @  4:212cb178bcbb C
@@ -73,6 +74,7 @@ 
   $ hg add D
   $ hg commit -m D
   1 new orphan changesets
+  created new head
   $ hg --hidden strip -r 'desc(B1)'
   saved backup bundle to $TESTTMP/obsskip/.hg/strip-backup/86f6414ccda7-b1c452ee-backup.hg
   1 new orphan changesets
@@ -194,6 +196,7 @@ 
   $ hg add foo
   $ hg commit -m "bar foo"
   1 new orphan changesets
+  created new head
   $ hg log -G
   @  14:73568ab6879d bar foo
diff --git a/tests/test-rebase-obsolete.t b/tests/test-rebase-obsolete.t
--- a/tests/test-rebase-obsolete.t
+++ b/tests/test-rebase-obsolete.t
@@ -650,6 +650,7 @@ 
   $ hg add J
   $ hg commit -m J
   1 new orphan changesets
+  created new head
   $ hg debugobsolete `hg log --rev . -T '{node}'`
   1 new obsolescence markers
   obsoleted 1 changesets
diff --git a/tests/test-obsolete.t b/tests/test-obsolete.t
--- a/tests/test-obsolete.t
+++ b/tests/test-obsolete.t
@@ -169,10 +169,6 @@ 
   5:5601fb93a350 (draft) [tip ] add new_3_c
   $ hg heads --hidden
   5:5601fb93a350 (draft) [tip ] add new_3_c
-  4:ca819180edb9 (draft *obsolete*) [ ] add new_2_c [rewritten as 5:5601fb93a350]
-  3:cdbce2fbb163 (draft *obsolete*) [ ] add new_c [rewritten as 4:ca819180edb9]
-  2:245bde4270cd (draft *obsolete*) [ ] add original_c [rewritten as 3:cdbce2fbb163]
 check that summary does not report them
@@ -193,7 +189,7 @@ 
    add new_3_c
   branch: default
   commit: (clean)
-  update: 3 new changesets, 4 branch heads (merge)
+  update: (current)
   phases: 6 draft
   remote: 3 outgoing
diff --git a/tests/test-obsmarker-template.t b/tests/test-obsmarker-template.t
--- a/tests/test-obsmarker-template.t
+++ b/tests/test-obsmarker-template.t
@@ -1762,7 +1762,7 @@ 
   2 new obsolescence markers
   obsoleted 1 changesets
   new changesets 7a230b46bf61 (1 drafts)
-  (run 'hg heads' to see heads, 'hg merge' to merge)
+  (run 'hg heads' to see heads)
   $ hg log --hidden -G
   o  changeset:   2:7a230b46bf61
   |  tag:         tip
diff --git a/tests/test-branch-change.t b/tests/test-branch-change.t
--- a/tests/test-branch-change.t
+++ b/tests/test-branch-change.t
@@ -185,6 +185,7 @@ 
   changed branch on 2 changesets
   updating the branch cache
   invalid branch cache (served): tip differs
+  invalid branch cache (served.hidden): tip differs
   $ hg glog -r '(.^)::'
   @  9:de1404b45a69 Added e
@@ -211,7 +212,7 @@ 
   secret                        11:38a9b2d53f98
   foo                            7:8a4729a5e2b8
   wat                            9:de1404b45a69 (inactive)
-  default                        2:28ad74487de9 (inactive)
+  default                        1:29becc82797a (inactive)
   $ hg branch
diff --git a/mercurial/ b/mercurial/
--- a/mercurial/
+++ b/mercurial/
@@ -349,7 +349,7 @@ 
-def filteredhash(repo, maxrev):
+def filteredhash(repo, maxrev, needobsolete=False):
     """build hash of filtered revisions in the current repoview.
     Multiple caches perform up-to-date validation by checking that the
@@ -358,22 +358,31 @@ 
     of revisions in the view may change without the repository tiprev and
     tipnode changing.
-    This function hashes all the revs filtered from the view and returns
-    that SHA-1 digest.
+    This function hashes all the revs filtered from the view (and, optionally,
+    all obsolete revs) and returns that SHA-1 digest.
     cl = repo.changelog
-    if not cl.filteredrevs:
-        return None
-    key = cl._filteredrevs_hashcache.get(maxrev)
-    if not key:
-        revs = sorted(r for r in cl.filteredrevs if r <= maxrev)
+    if needobsolete:
+        obsrevs = obsolete.getrevs(repo, b'obsolete')
+        if not cl.filteredrevs and not obsrevs:
+            return None
+        key = (maxrev, hash(cl.filteredrevs), hash(frozenset(obsrevs)))
+    else:
+        if not cl.filteredrevs:
+            return None
+        key = maxrev
+        obsrevs = frozenset()
+    result = cl._filteredrevs_hashcache.get(key)
+    if not result:
+        revs = sorted(r for r in cl.filteredrevs | obsrevs if r <= maxrev)
         if revs:
             s = hashutil.sha1()
             for rev in revs:
                 s.update(b'%d;' % rev)
-            key = s.digest()
-            cl._filteredrevs_hashcache[maxrev] = key
-    return key
+            result = s.digest()
+            cl._filteredrevs_hashcache[key] = result
+    return result
 def walkrepos(path, followsym=False, seen_dirs=None, recurse=False):
diff --git a/mercurial/ b/mercurial/
--- a/mercurial/
+++ b/mercurial/
@@ -79,6 +79,9 @@ 
             node = repo.revs(b'max(%ln)', successors).first()
             if bookmarks.isactivewdirparent(repo):
                 movemark = repo[b'.'].node()
+        else:
+            # TODO: copy hg prune logic
+            node = repo[b'.'].node()
     return node, movemark, None
diff --git a/mercurial/ b/mercurial/
--- a/mercurial/
+++ b/mercurial/
@@ -17,6 +17,7 @@ 
 from . import (
+    obsolete,
@@ -177,7 +178,7 @@ 
     The first line is used to check if the cache is still valid. If the
     branch cache is for a filtered repo view, an optional third hash is
-    included that hashes the hashes of all filtered revisions.
+    included that hashes the hashes of all filtered and obsolete revisions.
     The open/closed state is represented by a single letter 'o' or 'c'.
     This field can be used to avoid changelog reads when determining if a
@@ -349,7 +350,8 @@ 
         - True when cache is up to date or a subset of current repo."""
             return (self.tipnode == repo.changelog.node(self.tiprev)) and (
-                self.filteredhash == scmutil.filteredhash(repo, self.tiprev)
+                self.filteredhash
+                == scmutil.filteredhash(repo, self.tiprev, needobsolete=True)
         except IndexError:
             return False
@@ -462,6 +464,9 @@ 
         # use the faster unfiltered parent accessor.
         parentrevs = repo.unfiltered().changelog.parentrevs
+        # Faster than using ctx.obsolete()
+        obsrevs = obsolete.getrevs(repo, b'obsolete')
         for branch, newheadrevs in pycompat.iteritems(newbranches):
             # For every branch, compute the new branchheads.
             # A branchhead is a revision such that no descendant is on
@@ -502,6 +507,11 @@ 
             bheadset = {cl.rev(node) for node in bheads}
             uncertain = set()
             for newrev in sorted(newheadrevs):
+                if newrev in obsrevs:
+                    # We ignore obsolete changesets as they shouldn't be
+                    # considered heads.
+                    continue
                 if not bheadset:
@@ -509,13 +519,19 @@ 
                 parents = [p for p in parentrevs(newrev) if p != nullrev]
                 samebranch = set()
                 otherbranch = set()
+                obsparents = set()
                 for p in parents:
-                    if p in bheadset or getbranchinfo(p)[0] == branch:
+                    if p in obsrevs:
+                        # We ignored this obsolete changeset earlier, but now
+                        # we want to handle it, since it's a parent of newrev.
+                        obsparents.add(p)
+                    elif p in bheadset or getbranchinfo(p)[0] == branch:
-                if otherbranch and not (len(bheadset) == len(samebranch) == 1):
+                if not (len(bheadset) == len(samebranch) == 1):
+                    uncertain.update(obsparents)
@@ -524,11 +540,12 @@ 
                     topoheads = set(cl.headrevs())
                 if bheadset - topoheads:
                     floorrev = min(bheadset)
-                    ancestors = set(cl.ancestors(newheadrevs, floorrev))
-                    bheadset -= ancestors
+                    if floorrev <= max(uncertain):
+                        ancestors = set(cl.ancestors(uncertain, floorrev))
+                        bheadset -= ancestors
             bheadrevs = sorted(bheadset)
             self[branch] = [cl.node(rev) for rev in bheadrevs]
-            tiprev = bheadrevs[-1]
+            tiprev = max(newheadrevs)
             if tiprev > ntiprev:
                 ntiprev = tiprev
@@ -537,15 +554,24 @@ 
             self.tipnode = cl.node(ntiprev)
         if not self.validfor(repo):
-            # cache key are not valid anymore
+            # old cache key is now invalid for the repo, but we've just updated
+            # the cache and we assume it's valid, so let's make the cache key
+            # valid as well by recomputing it from the cached data
             self.tipnode = repo.nullid
             self.tiprev = nullrev
             for heads in self.iterheads():
+                if not heads:
+                    # all revisions on a branch are obsolete
+                    continue
+                # note: tiprev is not necessarily the tip revision of repo,
+                # because the tip could be obsolete (i.e. not a head)
                 tiprev = max(cl.rev(node) for node in heads)
                 if tiprev > self.tiprev:
                     self.tipnode = cl.node(tiprev)
                     self.tiprev = tiprev
-        self.filteredhash = scmutil.filteredhash(repo, self.tiprev)
+        self.filteredhash = scmutil.filteredhash(
+            repo, self.tiprev, needobsolete=True
+        )
         duration = util.timer() - starttime