[PATCH 1 of 3 topic-ext] topic: wrap the underlying update function rather than the command

Matt Mackall mpm at selenic.com
Mon Jun 15 15:21:55 CDT 2015


# HG changeset patch
# User Matt Mackall <mpm at selenic.com>
# Date 1434145179 18000
#      Fri Jun 12 16:39:39 2015 -0500
# Node ID eae26502e44f3bce3da89647c03f6db62f0e69b7
# Parent  3084687f799455df13c6c344ab93dd23f6b28891
topic: wrap the underlying update function rather than the command

This more properly manages the updates done by things like rebase.

diff -r 3084687f7994 -r eae26502e44f src/topic/__init__.py
--- a/src/topic/__init__.py	Wed Jun 10 17:52:07 2015 -0500
+++ b/src/topic/__init__.py	Fri Jun 12 16:39:39 2015 -0500
@@ -21,6 +21,7 @@
 from mercurial import obsolete
 from mercurial import phases
 from mercurial import util
+from mercurial import merge
 
 from . import constants
 from . import revset as topicrevset
@@ -142,27 +143,31 @@
                           "\nHG: topic '%s'\nHG: branch" % t)
     return ret
 
-def updatewrap(orig, ui, repo, *args, **kwargs):
-    ret = orig(ui, repo, *args, **kwargs)
-    pctx = repo['.']
-    ot = repo.currenttopic
-    if pctx.phase() == phases.public and repo.vfs.exists('topic'):
-        repo.vfs.unlink('topic')
-    else:
-        # inherit the topic of the parent revision
-        t = pctx.extra().get(constants.extrakey, '')
-        if t and pctx.phase() > phases.public:
+def mergeupdatewrap(orig, repo, node, branchmerge, force, partial,
+                    ancestor=None, mergeancestor=False, labels=None):
+    wlock = repo.wlock()
+    try:
+        ret = orig(repo, node, branchmerge, force, partial, ancestor=ancestor,
+                   mergeancestor=mergeancestor, labels=labels)
+        if not partial and not branchmerge:
+            ot = repo.currenttopic
+            t = ''
+            pctx = repo[node]
+            if pctx.phase() > phases.public:
+                t = pctx.extra().get(constants.extrakey, '')
             with repo.vfs.open('topic', 'w') as f:
                 f.write(t)
-            if t != ot:
-                ui.status(_("switching to topic %s\n") % t)
-    return ret
+            if t and t != ot:
+                repo.ui.status(_("switching to topic %s\n") % t)
+        return ret
+    finally:
+        wlock.release()
 
 entry = extensions.wrapcommand(commands.table, 'commit', commitwrap)
 entry[1].append(('t', 'topic', '',
                  _("use specified topic"), _('TOPIC')))
 
-extensions.wrapcommand(commands.table, 'update', updatewrap)
 extensions.wrapfunction(cmdutil, 'buildcommittext', committextwrap)
+extensions.wrapfunction(merge, 'update', mergeupdatewrap)
 topicrevset.modsetup()
 cmdutil.summaryhooks.add('topic', summaryhook)
diff -r 3084687f7994 -r eae26502e44f tests/test-topic.t
--- a/tests/test-topic.t	Wed Jun 10 17:52:07 2015 -0500
+++ b/tests/test-topic.t	Fri Jun 12 16:39:39 2015 -0500
@@ -77,8 +77,8 @@
   $ hg ci -m 'start on fran'
   created new head
   $ hg co narf
+  switching to topic narf
   2 files updated, 0 files merged, 0 files removed, 0 files unresolved
-  switching to topic narf
   $ hg topic
      fran
    * narf
@@ -243,8 +243,8 @@
    * query
   $ cd ../pinky
   $ hg co query
+  switching to topic query
   1 files updated, 0 files merged, 0 files removed, 0 files unresolved
-  switching to topic query
   $ echo answer >> alpha
   $ hg ci -m 'Narf is like `zort` or `poit`!'
   $ hg merge narf
@@ -447,8 +447,8 @@
 Move to fran, note that the topic activates, then deactivate the topic.
 
   $ hg co fran
+  switching to topic fran
   2 files updated, 0 files merged, 0 files removed, 0 files unresolved
-  switching to topic fran
   $ hg topics
    * fran
   $ hg topics --clear


More information about the Mercurial-devel mailing list