topic: setup 'topic' value for working ctx
authorBoris Feld <boris.feld@octobus.net>
Wed, 13 Sep 2017 15:13:51 +0200
changeset 2928 6275808e89ef
parent 2927 01cf426bd458
child 2929 06844693bb21
topic: setup 'topic' value for working ctx The way amend work in core change in e8a7c1a0565a. The intermediate commit disappear and we can no longer rely on it to carry the topic value to amend. To fix this, we ensure the current topic value can be retrieved through the `workingctx.extra()` value. Since there is no way to carry a "missing" key information in a way that survives and "dict.update" call. We also has to introduce hacking behavior about a special empty value for topic.
hgext3rd/topic/__init__.py
--- a/hgext3rd/topic/__init__.py	Thu Sep 14 20:33:03 2017 +0200
+++ b/hgext3rd/topic/__init__.py	Wed Sep 13 15:13:51 2017 +0200
@@ -58,6 +58,7 @@
 from mercurial.i18n import _
 from mercurial import (
     bookmarks,
+    changelog,
     cmdutil,
     commands,
     context,
@@ -307,6 +308,26 @@
         repo.names.addnamespace(namespaces.namespace(
             'topics', 'topic', namemap=_namemap, nodemap=_nodemap,
             listnames=lambda repo: repo.topics))
+    # Wrap workingctx extra to return the topic name
+    extensions.wrapfunction(context.workingctx, '__init__', wrapinit)
+    # Wrap changelog.add to drop empty topic
+    extensions.wrapfunction(changelog.changelog, 'add', wrapadd)
+
+def wrapinit(orig, self, repo, *args, **kwargs):
+    orig(self, repo, *args, **kwargs)
+    if repo.currenttopic:
+        self._extra[constants.extrakey] = repo.currenttopic
+    else:
+        # Empty key will be dropped from extra by another hack at the changegroup level
+        self._extra[constants.extrakey] = ''
+
+def wrapadd(orig, cl, manifest, files, desc, transaction, p1, p2, user,
+            date=None, extra=None):
+    if constants.extrakey in extra and not extra[constants.extrakey]:
+        extra = extra.copy()
+        del extra[constants.extrakey]
+    return orig(cl, manifest, files, desc, transaction, p1, p2, user,
+                date=date, extra=extra)
 
 # revset predicates are automatically registered at loading via this symbol
 revsetpredicate = topicrevset.revsetpredicate