head-checking: ignore obsolete section when checking for single heads stable
authorPierre-Yves David <pierre-yves.david@octobus.net>
Wed, 11 Mar 2020 23:56:11 +0100
branchstable
changeset 5272 b20d04641c0f
parent 5271 1e0516ee4cb9
child 5273 f560d23639e1
head-checking: ignore obsolete section when checking for single heads This overwrite the core code that enforce single heads to ignore obsolete section. That way older obsolete version of a branch no longer prevent the new version to be pushed.
CHANGELOG
hgext3rd/evolve/headchecking.py
tests/test-single-head-obsolescence-named-branch-A1.t
tests/test-single-head-obsolescence-named-branch-A2.t
tests/test-single-head-obsolescence-named-branch-A3.t
tests/test-single-head-obsolescence-named-branch-A4.t
tests/test-single-head-obsolescence-named-branch-A5.t
tests/testlib/push-checkheads-util.sh
--- a/CHANGELOG	Mon Apr 06 05:08:23 2020 +0200
+++ b/CHANGELOG	Wed Mar 11 23:56:11 2020 +0100
@@ -11,6 +11,7 @@
   * topic: fix auto-publish=abort with server that auto-publishes bare branches
   * evolve: checking for new head on push is no longer confused by mixed
             branches(or topics)
+  * single-heads: ignore obsolete section when enforcing one head per branch
 
 9.3.0 -- 2020-03-04
 -------------------
--- a/hgext3rd/evolve/headchecking.py	Mon Apr 06 05:08:23 2020 +0200
+++ b/hgext3rd/evolve/headchecking.py	Wed Mar 11 23:56:11 2020 +0100
@@ -4,10 +4,15 @@
 
 from mercurial import (
     discovery,
+    error,
     extensions,
     phases,
+    scmutil,
+    util,
 )
 
+from mercurial.i18n import _
+
 
 from . import (
     compat,
@@ -20,6 +25,7 @@
 @eh.uisetup
 def uisetup(ui):
     extensions.wrapfunction(discovery, '_postprocessobsolete', _postprocessobsolete)
+    extensions.wrapfunction(scmutil, 'enforcesinglehead', enforcesinglehead)
 
 def branchinfo(pushop, repo, node):
     return repo[node].branch()
@@ -111,3 +117,71 @@
             discarded.add(nh)
     newhs |= unknownheads
     return newhs, discarded
+
+def _get_branch_name(ctx):
+    # make it easy for extension with the branch logic there
+    branch = ctx.branch()
+    if util.safehasattr(ctx, 'topic'):
+        topic = ctx.topic()
+        if topic:
+            branch = "%s:%s" % (branch, topic)
+    return branch
+
+def _filter_obsolete_heads(repo, heads):
+    """filter heads to return non-obsolete ones
+
+    Given a list of heads (on the same named branch) return a new list of heads
+    where the obsolete part have been skimmed out.
+    """
+    new_heads = []
+    old_heads = heads[:]
+    while old_heads:
+        rh = old_heads.pop()
+        ctx = repo[rh]
+        current_name = _get_branch_name(ctx)
+        # run this check early to skip the evaluation of the whole branch
+        if not ctx.obsolete():
+            new_heads.append(rh)
+            continue
+
+        # Get all revs/nodes on the branch exclusive to this head
+        # (already filtered heads are "ignored"))
+        sections_revs = repo.revs(
+            b'only(%d, (%ld+%ld))', rh, old_heads, new_heads,
+        )
+        keep_revs = []
+        for r in sections_revs:
+            ctx = repo[r]
+            if ctx.obsolete():
+                continue
+            if _get_branch_name(ctx) != current_name:
+                continue
+            keep_revs.append(r)
+        for h in repo.revs(b'heads(%ld and (::%ld))', sections_revs, keep_revs):
+            new_heads.append(h)
+    new_heads.sort()
+    return new_heads
+
+def enforcesinglehead(orig, repo, tr, desc, accountclosed=False):
+    """check that no named branch has multiple heads"""
+    nodesummaries = scmutil.nodesummaries
+    if desc in (b'strip', b'repair'):
+        # skip the logic during strip
+        return
+    visible = repo.filtered(b'visible')
+    # possible improvement: we could restrict the check to affected branch
+    bm = visible.branchmap()
+    cl = repo.changelog
+    to_rev = cl.rev
+    to_node = cl.node
+    for name in bm:
+        all_heads = bm.branchheads(name, closed=accountclosed)
+        all_heads = [to_rev(n) for n in all_heads]
+        heads = _filter_obsolete_heads(repo, all_heads)
+        heads = [to_node(r) for r in heads]
+        if len(heads) > 1:
+            msg = _(b'rejecting multiple heads on branch "%s"')
+            msg %= name
+            hint = _(b'%d heads: %s')
+            hint %= (len(heads), nodesummaries(repo, heads))
+            raise error.Abort(msg, hint=hint)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-single-head-obsolescence-named-branch-A1.t	Wed Mar 11 23:56:11 2020 +0100
@@ -0,0 +1,110 @@
+=========================================
+Testing single head enforcement: Case A-1
+=========================================
+
+A repository is set to only accept a single head per name (typically named
+branch). However, obsolete changesets can make this enforcement more
+complicated, because they can be kept visible by other changeset on other
+branch.
+
+This case is part of a series of tests checking this behavior.
+
+Category A: Involving obsolescence
+TestCase 1: A fully obsolete branch kept visible by another one.
+
+.. old-state:
+..
+.. * 2 changeset changeset on branch default
+.. * 2 changeset changeset on branch Z on top of them.
+..
+.. new-state:
+..
+.. * 2 changeset changeset on branch Z at the same location
+.. * 2 changeset changeset on branch default superceeding the other ones
+..
+.. expected-result:
+..
+.. * only one head detected
+..
+.. graph-summary:
+..
+..   D ●      (branch Z)
+..     |
+..   C ●      (branch Z)
+..     |
+..   B ø⇠◔ B'
+..     | |
+..   A ø⇠◔ A'
+..     |/
+..     ●
+
+  $ . $TESTDIR/testlib/topic_setup.sh
+  $ . $TESTDIR/testlib/push-checkheads-util.sh
+
+Test setup
+----------
+
+  $ mkdir A1
+  $ cd A1
+  $ setuprepos single-head
+  creating basic server and client repo
+  updating to branch default
+  2 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  $ cd client
+  $ mkcommit B0
+  $ hg branch Z
+  marked working directory as branch Z
+  (branches are permanent and global, did you want a bookmark?)
+  $ mkcommit C0
+  $ mkcommit D0
+  $ hg push --new-branch
+  pushing to $TESTTMP/A1/server
+  searching for changes
+  adding changesets
+  adding manifests
+  adding file changes
+  added 3 changesets with 3 changes to 3 files
+  $ hg up 0
+  0 files updated, 0 files merged, 4 files removed, 0 files unresolved
+  $ mkcommit A1
+  created new head
+  (consider using topic for lightweight branches. See 'hg help topic')
+  $ mkcommit B1
+  $ hg debugobsolete `getid "desc(A0)" ` `getid "desc(A1)"`
+  1 new obsolescence markers
+  obsoleted 1 changesets
+  3 new orphan changesets
+  $ hg debugobsolete `getid "desc(B0)" ` `getid "desc(B1)"`
+  1 new obsolescence markers
+  obsoleted 1 changesets
+  $ hg log -G --hidden
+  @  262c8c798096 [default] (draft): B1
+  |
+  o  f6082bc4ffef [default] (draft): A1
+  |
+  | *  cdf1dbb37a67 [Z] (draft): D0
+  | |
+  | *  3213e3e16c67 [Z] (draft): C0
+  | |
+  | x  d73caddc5533 [default] (draft): B0
+  | |
+  | x  8aaa48160adc [default] (draft): A0
+  |/
+  o  1e4be0697311 [default] (public): root
+  
+
+Actual testing
+--------------
+
+(force push to make sure we get the changeset on the remote)
+
+  $ hg push -r 'desc("B1")' --force
+  pushing to $TESTTMP/A1/server
+  searching for changes
+  adding changesets
+  adding manifests
+  adding file changes
+  added 2 changesets with 2 changes to 2 files (+1 heads)
+  2 new obsolescence markers
+  obsoleted 2 changesets
+  2 new orphan changesets
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-single-head-obsolescence-named-branch-A2.t	Wed Mar 11 23:56:11 2020 +0100
@@ -0,0 +1,107 @@
+
+=========================================
+Testing single head enforcement: Case A-2
+=========================================
+
+A repository is set to only accept a single head per name (typically named
+branch). However, obsolete changesets can make this enforcement more
+complicated, because they can be kept visible by other changeset on other
+branch.
+
+This case is part of a series of tests checking this behavior.
+
+Category A: Involving obsolescence
+TestCase 2: A branch is split in two, effectively creating two heads
+
+.. old-state:
+..
+.. * 2 changeset changeset on branch default
+.. * 2 changeset changeset on branch Z on top of them.
+..
+.. new-state:
+..
+.. * 2 changeset changeset on branch Z at the same location
+.. * 1 changeset changeset on branch default unchanged
+.. * 1 changeset changeset on branch default superceeding the other ones
+..
+.. expected-result:
+..
+.. * two heads detected
+..
+.. graph-summary:
+..
+..   D ●      (branch Z)
+..     |
+..   C ●      (branch Z)
+..     |
+..   B ø⇠◔ B'
+..     | |
+..   A ● |
+..     |/
+..     ●
+
+  $ . $TESTDIR/testlib/topic_setup.sh
+  $ . $TESTDIR/testlib/push-checkheads-util.sh
+
+Test setup
+----------
+
+  $ mkdir A2
+  $ cd A2
+  $ setuprepos single-head
+  creating basic server and client repo
+  updating to branch default
+  2 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  $ cd client
+  $ mkcommit B0
+  $ hg branch Z
+  marked working directory as branch Z
+  (branches are permanent and global, did you want a bookmark?)
+  $ mkcommit C0
+  $ mkcommit D0
+  $ hg push --new-branch
+  pushing to $TESTTMP/A2/server
+  searching for changes
+  adding changesets
+  adding manifests
+  adding file changes
+  added 3 changesets with 3 changes to 3 files
+  $ hg up 0
+  0 files updated, 0 files merged, 4 files removed, 0 files unresolved
+  $ mkcommit B1
+  created new head
+  (consider using topic for lightweight branches. See 'hg help topic')
+  $ hg debugobsolete `getid "desc(B0)" ` `getid "desc(B1)"`
+  1 new obsolescence markers
+  obsoleted 1 changesets
+  2 new orphan changesets
+  $ hg log -G --hidden
+  @  25c56d33e4c4 [default] (draft): B1
+  |
+  | *  cdf1dbb37a67 [Z] (draft): D0
+  | |
+  | *  3213e3e16c67 [Z] (draft): C0
+  | |
+  | x  d73caddc5533 [default] (draft): B0
+  | |
+  | o  8aaa48160adc [default] (draft): A0
+  |/
+  o  1e4be0697311 [default] (public): root
+  
+
+Actual testing
+--------------
+
+(force push to make sure we get the changeset on the remote)
+
+  $ hg push -r 'desc("B1")' --force
+  pushing to $TESTTMP/A2/server
+  searching for changes
+  adding changesets
+  adding manifests
+  adding file changes
+  transaction abort!
+  rollback completed
+  abort: rejecting multiple heads on branch "default"
+  (2 heads: 8aaa48160adc 25c56d33e4c4)
+  [255]
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-single-head-obsolescence-named-branch-A3.t	Wed Mar 11 23:56:11 2020 +0100
@@ -0,0 +1,118 @@
+=========================================
+Testing single head enforcement: Case A-3
+=========================================
+
+A repository is set to only accept a single head per name (typically named
+branch). However, obsolete changesets can make this enforcement more
+complicated, because they can be kept visible by other changeset on other
+branch.
+
+This case is part of a series of tests checking this behavior.
+
+Category A: Involving obsolescence
+TestCase 3: Full superceedig of a branch interleaved with another
+
+.. old-state:
+..
+.. * 2 changeset changeset on branch default
+.. * 2 changeset changeset on branch Z interleaved with the other
+..
+.. new-state:
+..
+.. * 2 changeset changeset on branch Z at the same location
+.. * 2 changeset changeset on branch default superceeding the other ones
+..
+.. expected-result:
+..
+.. * only one head detected
+..
+.. graph-summary:
+..
+..   D ●      (branch Z)
+..     |
+..   C ø⇠◔ C'
+..     | |
+..   B ● |    (branch Z)
+..     | |
+..   A ø⇠◔ A'
+..     |/
+..     ●
+
+  $ . $TESTDIR/testlib/topic_setup.sh
+  $ . $TESTDIR/testlib/push-checkheads-util.sh
+
+Test setup
+----------
+
+  $ mkdir A3
+  $ cd A3
+  $ setuprepos single-head
+  creating basic server and client repo
+  updating to branch default
+  2 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  $ cd client
+  $ hg branch Z
+  marked working directory as branch Z
+  (branches are permanent and global, did you want a bookmark?)
+  $ mkcommit B0
+  $ hg branch default --force
+  marked working directory as branch default
+  $ mkcommit C0
+  created new head
+  (consider using topic for lightweight branches. See 'hg help topic')
+  $ hg branch Z --force
+  marked working directory as branch Z
+  $ mkcommit D0
+  created new head
+  (consider using topic for lightweight branches. See 'hg help topic')
+  $ hg push --new-branch
+  pushing to $TESTTMP/A3/server
+  searching for changes
+  adding changesets
+  adding manifests
+  adding file changes
+  added 3 changesets with 3 changes to 3 files
+  $ hg up 0
+  0 files updated, 0 files merged, 4 files removed, 0 files unresolved
+  $ mkcommit A1
+  created new head
+  (consider using topic for lightweight branches. See 'hg help topic')
+  $ mkcommit C1
+  $ hg debugobsolete `getid "desc(A0)" ` `getid "desc(A1)"`
+  1 new obsolescence markers
+  obsoleted 1 changesets
+  3 new orphan changesets
+  $ hg debugobsolete `getid "desc(C0)" ` `getid "desc(C1)"`
+  1 new obsolescence markers
+  obsoleted 1 changesets
+  $ hg log -G --hidden
+  @  0c76bc104656 [default] (draft): C1
+  |
+  o  f6082bc4ffef [default] (draft): A1
+  |
+  | *  78578c4306ce [Z] (draft): D0
+  | |
+  | x  afc55ba2ce61 [default] (draft): C0
+  | |
+  | *  93e5c1321ece [Z] (draft): B0
+  | |
+  | x  8aaa48160adc [default] (draft): A0
+  |/
+  o  1e4be0697311 [default] (public): root
+  
+
+Actual testing
+--------------
+
+(force push to make sure we get the changeset on the remote)
+
+  $ hg push -r 'desc("C1")' --force
+  pushing to $TESTTMP/A3/server
+  searching for changes
+  adding changesets
+  adding manifests
+  adding file changes
+  added 2 changesets with 2 changes to 2 files (+1 heads)
+  2 new obsolescence markers
+  obsoleted 2 changesets
+  2 new orphan changesets
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-single-head-obsolescence-named-branch-A4.t	Wed Mar 11 23:56:11 2020 +0100
@@ -0,0 +1,113 @@
+=========================================
+Testing single head enforcement: Case A-4
+=========================================
+
+A repository is set to only accept a single head per name (typically named
+branch). However, obsolete changesets can make this enforcement more
+complicated, because they can be kept visible by other changeset on other
+branch.
+
+This case is part of a series of tests checking this behavior.
+
+Category A: Involving obsolescence
+TestCase 4: Partial rewrite of a branch to dis-interleave it
+
+.. old-state:
+..
+.. * 2 changeset changeset on branch default
+.. * 2 changeset changeset on branch Z interleaved with the other one
+..
+.. new-state:
+..
+.. * 2 changeset changeset on branch Z at the same location
+.. * 1 changeset on default untouched (the lower one)
+.. * 1 changeset on default moved on the other one
+..
+.. expected-result:
+..
+.. * only one head detected
+..
+.. graph-summary:
+..
+..   D ●      (branch Z)
+..     |
+..   C ø⇠◔ C'
+..     | |
+..   B ● |    (branch Z)
+..     |/
+..   A ●
+..     |
+..     ●
+
+  $ . $TESTDIR/testlib/topic_setup.sh
+  $ . $TESTDIR/testlib/push-checkheads-util.sh
+
+Test setup
+----------
+
+  $ mkdir A4
+  $ cd A4
+  $ setuprepos single-head
+  creating basic server and client repo
+  updating to branch default
+  2 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  $ cd client
+  $ hg branch Z
+  marked working directory as branch Z
+  (branches are permanent and global, did you want a bookmark?)
+  $ mkcommit B0
+  $ hg branch default --force
+  marked working directory as branch default
+  $ mkcommit C0
+  created new head
+  (consider using topic for lightweight branches. See 'hg help topic')
+  $ hg branch Z --force
+  marked working directory as branch Z
+  $ mkcommit D0
+  created new head
+  (consider using topic for lightweight branches. See 'hg help topic')
+  $ hg push --new-branch
+  pushing to $TESTTMP/A4/server
+  searching for changes
+  adding changesets
+  adding manifests
+  adding file changes
+  added 3 changesets with 3 changes to 3 files
+  $ hg up 'desc("A0")'
+  0 files updated, 0 files merged, 3 files removed, 0 files unresolved
+  $ mkcommit C1
+  created new head
+  (consider using topic for lightweight branches. See 'hg help topic')
+  $ hg debugobsolete `getid "desc(C0)" ` `getid "desc(C1)"`
+  1 new obsolescence markers
+  obsoleted 1 changesets
+  1 new orphan changesets
+  $ hg log -G --hidden
+  @  cfe9ed94fa4a [default] (draft): C1
+  |
+  | *  78578c4306ce [Z] (draft): D0
+  | |
+  | x  afc55ba2ce61 [default] (draft): C0
+  | |
+  | o  93e5c1321ece [Z] (draft): B0
+  |/
+  o  8aaa48160adc [default] (draft): A0
+  |
+  o  1e4be0697311 [default] (public): root
+  
+
+Actual testing
+--------------
+
+(force push to make sure we get the changeset on the remote)
+
+  $ hg push -r 'desc("C1")' --force
+  pushing to $TESTTMP/A4/server
+  searching for changes
+  adding changesets
+  adding manifests
+  adding file changes
+  added 1 changesets with 1 changes to 1 files (+1 heads)
+  1 new obsolescence markers
+  obsoleted 1 changesets
+  1 new orphan changesets
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-single-head-obsolescence-named-branch-A5.t	Wed Mar 11 23:56:11 2020 +0100
@@ -0,0 +1,113 @@
+=========================================
+Testing single head enforcement: Case A-1
+=========================================
+
+A repository is set to only accept a single head per name (typically named
+branch). However, obsolete changesets can make this enforcement more
+complicated, because they can be kept visible by other changeset on other
+branch.
+
+This case is part of a series of tests checking this behavior.
+
+Category A: Involving obsolescence
+TestCase 1: obsoleting a merge reveal two heads
+
+.. old-state:
+..
+.. * 3 changeset changeset on branch default (2 on their own branch + 1 merge)
+.. * 1 changeset on branch Z (children of the merge)
+..
+.. new-state:
+..
+.. * 2 changeset changeset on branch default (merge is obsolete) each a head
+.. * 1 changeset on branch Z keeping the merge visible
+..
+.. expected-result:
+..
+.. * 2 heads detected (because we skip the merge).
+..
+.. graph-summary:
+..
+..   D ●      (branch Z)
+..     |
+..   C ●      (branch Z)
+..     |
+..   M ⊗
+..     |\
+..   A ● ● B
+..     |/
+..     ●
+
+  $ . $TESTDIR/testlib/topic_setup.sh
+  $ . $TESTDIR/testlib/push-checkheads-util.sh
+
+Test setup
+----------
+
+  $ mkdir A5
+  $ cd A5
+  $ setuprepos single-head
+  creating basic server and client repo
+  updating to branch default
+  2 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  $ cd client
+  $ hg up 0
+  0 files updated, 0 files merged, 1 files removed, 0 files unresolved
+  $ mkcommit B0
+  created new head
+  (consider using topic for lightweight branches. See 'hg help topic')
+  $ hg merge
+  1 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  (branch merge, don't forget to commit)
+  $ hg ci -m 'M0'
+  $ hg branch Z
+  marked working directory as branch Z
+  (branches are permanent and global, did you want a bookmark?)
+  $ mkcommit C0
+  $ hg push --new-branch
+  pushing to $TESTTMP/A5/server
+  searching for changes
+  adding changesets
+  adding manifests
+  adding file changes
+  added 3 changesets with 2 changes to 2 files
+  $ hg up 0
+  0 files updated, 0 files merged, 3 files removed, 0 files unresolved
+  $ mkcommit A1
+  created new head
+  (consider using topic for lightweight branches. See 'hg help topic')
+  $ mkcommit B1
+  $ hg debugobsolete `getid "desc(M0)"` --record-parents
+  1 new obsolescence markers
+  obsoleted 1 changesets
+  1 new orphan changesets
+  $ hg log -G --hidden
+  @  262c8c798096 [default] (draft): B1
+  |
+  o  f6082bc4ffef [default] (draft): A1
+  |
+  | *  61c95483cc12 [Z] (draft): C0
+  | |
+  | x    14d3d4d41d1a [default] (draft): M0
+  | |\
+  +---o  74ff5441d343 [default] (draft): B0
+  | |
+  | o  8aaa48160adc [default] (draft): A0
+  |/
+  o  1e4be0697311 [default] (public): root
+  
+
+Actual testing
+--------------
+
+(force push to make sure we get the changeset on the remote)
+
+  $ hg push -r 'desc("C0")' --force
+  pushing to $TESTTMP/A5/server
+  searching for changes
+  no changes found
+  transaction abort!
+  rollback completed
+  abort: rejecting multiple heads on branch "default"
+  (2 heads: 8aaa48160adc 74ff5441d343)
+  [255]
--- a/tests/testlib/push-checkheads-util.sh	Mon Apr 06 05:08:23 2020 +0200
+++ b/tests/testlib/push-checkheads-util.sh	Wed Mar 11 23:56:11 2020 +0100
@@ -26,4 +26,14 @@
     mkcommit A0
     cd ..
     hg clone server client
+
+    if [ "$1" = "single-head" ]; then
+        echo >> "server/.hg/hgrc" "[experimental]"
+        echo >> "server/.hg/hgrc" "# enforce a single name per branch"
+        echo >> "server/.hg/hgrc" "single-head-per-branch = yes"
+
+        echo >> "client/.hg/hgrc" "[ui]"
+        echo >> "client/.hg/hgrc" "# simpler log output"
+        echo >> "client/.hg/hgrc" 'logtemplate ="{node|short} [{branch}] ({phase}): {desc}\\n"'
+    fi
 }