changeset 45075:04ef381000a8

hooklib: fix detection of successors for changeset_obsoleted Provide a hook for obsutil.getobsolete to be used with either a transaction or the changes item of the transaction, since hooks only have access to the latter. Use that to find the correct list of revisions with obsmarkers, even new ones, and then filter out revisions with known successors. Move the processing from pretxnclose to txnclose as the transaction access itself is no longer necessary. This is more in line with notify and ensures that sanity checks can abort the transaction first. Differential Revision: https://phab.mercurial-scm.org/D8575
author Joerg Sonnenberger <joerg@bec.de>
date Thu, 21 May 2020 18:18:50 +0200
parents 797ef6f8295e
children 23119371df5e
files hgext/hooklib/changeset_obsoleted.py mercurial/obsutil.py tests/test-hooklib-changeset_obsoleted.t
diffstat 3 files changed, 56 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/hooklib/changeset_obsoleted.py	Thu Jul 09 20:46:52 2020 -0700
+++ b/hgext/hooklib/changeset_obsoleted.py	Thu May 21 18:18:50 2020 +0200
@@ -122,10 +122,18 @@
         )
 
 
+def has_successor(repo, rev):
+    return any(
+        r for r in obsutil.allsuccessors(repo.obsstore, [rev]) if r != rev
+    )
+
+
 def hook(ui, repo, hooktype, node=None, **kwargs):
-    if hooktype != b"pretxnclose":
+    if hooktype != b"txnclose":
         raise error.Abort(
             _(b'Unsupported hook type %r') % pycompat.bytestr(hooktype)
         )
-    for rev in obsutil.getobsoleted(repo, repo.currenttransaction()):
-        _report_commit(ui, repo, repo.unfiltered()[rev])
+    for rev in obsutil.getobsoleted(repo, changes=kwargs['changes']):
+        ctx = repo.unfiltered()[rev]
+        if not has_successor(repo, ctx.node()):
+            _report_commit(ui, repo, ctx)
--- a/mercurial/obsutil.py	Thu Jul 09 20:46:52 2020 -0700
+++ b/mercurial/obsutil.py	Thu May 21 18:18:50 2020 +0200
@@ -13,6 +13,7 @@
 from . import (
     diffutil,
     encoding,
+    error,
     node as nodemod,
     phases,
     pycompat,
@@ -481,14 +482,23 @@
     return effects
 
 
-def getobsoleted(repo, tr):
-    """return the set of pre-existing revisions obsoleted by a transaction"""
+def getobsoleted(repo, tr=None, changes=None):
+    """return the set of pre-existing revisions obsoleted by a transaction
+
+    Either the transaction or changes item of the transaction (for hooks)
+    must be provided, but not both.
+    """
+    if (tr is None) == (changes is None):
+        e = b"exactly one of tr and changes must be provided"
+        raise error.ProgrammingError(e)
     torev = repo.unfiltered().changelog.index.get_rev
     phase = repo._phasecache.phase
     succsmarkers = repo.obsstore.successors.get
     public = phases.public
-    addedmarkers = tr.changes[b'obsmarkers']
-    origrepolen = tr.changes[b'origrepolen']
+    if changes is None:
+        changes = tr.changes
+    addedmarkers = changes[b'obsmarkers']
+    origrepolen = changes[b'origrepolen']
     seenrevs = set()
     obsoleted = set()
     for mark in addedmarkers:
--- a/tests/test-hooklib-changeset_obsoleted.t	Thu Jul 09 20:46:52 2020 -0700
+++ b/tests/test-hooklib-changeset_obsoleted.t	Thu May 21 18:18:50 2020 +0200
@@ -24,7 +24,7 @@
   $ cat <<EOF >> b/.hg/hgrc
   > [hooks]
   > incoming.notify = python:hgext.notify.hook
-  > pretxnclose.changeset_obsoleted = python:hgext.hooklib.changeset_obsoleted.hook
+  > txnclose.changeset_obsoleted = python:hgext.hooklib.changeset_obsoleted.hook
   > EOF
   $ hg --cwd b pull ../a | "$PYTHON" $TESTDIR/unwrap-message-id.py
   pulling from ../a
@@ -72,6 +72,8 @@
   pushing to ../b
   searching for changes
   no changes found
+  1 new obsolescence markers
+  obsoleted 1 changesets
   Subject: changeset abandoned
   In-reply-to: <hg.81c297828fd2d5afaadf2775a6a71b74143b6451dfaac09fac939e9107a50d01@example.com>
   Message-Id: <hg.d6329e9481594f0f3c8a84362b3511318bfbce50748ab1123f909eb6fbcab018@example.com>
@@ -80,5 +82,33 @@
   To: baz@example.com
   
   This changeset has been abandoned.
+
+Check that known changesets with known successors do not result in a mail.
+
+  $ hg init c
+  $ hg init d
+  $ cat <<EOF >> d/.hg/hgrc
+  > [hooks]
+  > incoming.notify = python:hgext.notify.hook
+  > txnclose.changeset_obsoleted = python:hgext.hooklib.changeset_obsoleted.hook
+  > EOF
+  $ hg --cwd c debugbuilddag '.:parent.*parent'
+  $ hg --cwd c push ../d -r 1
+  pushing to ../d
+  searching for changes
+  adding changesets
+  adding manifests
+  adding file changes
+  added 2 changesets with 0 changes to 0 files
+  $ hg --cwd c debugobsolete $(hg --cwd c log -T '{node}' -r 1) $(hg --cwd c log -T '{node}' -r 2)
   1 new obsolescence markers
   obsoleted 1 changesets
+  $ hg --cwd c push ../d | "$PYTHON" $TESTDIR/unwrap-message-id.py
+  pushing to ../d
+  searching for changes
+  adding changesets
+  adding manifests
+  adding file changes
+  added 1 changesets with 0 changes to 0 files (+1 heads)
+  1 new obsolescence markers
+  obsoleted 1 changesets