topic: add formatter support
authorPierre-Yves David <pierre-yves.david@ens-lyon.org>
Sat, 13 Aug 2016 00:37:33 +0200
changeset 1975 acbbf7f0751e
parent 1974 20fb4195bfc4
child 1976 ebdc2a6a9a25
topic: add formatter support Using formatter to output the list of topic will add support for color and richer templater. We add some test for output control. We introduce a dedicated test to help with coming more complexe output.
hgext3rd/topic/__init__.py
tests/test-topic-list.t
--- a/hgext3rd/topic/__init__.py	Sat Aug 13 00:13:05 2016 +0200
+++ b/hgext3rd/topic/__init__.py	Sat Aug 13 00:37:33 2016 +0200
@@ -263,7 +263,7 @@
             with repo.vfs.open('topic', 'w') as f:
                 f.write(topic)
         return
-    _listtopics(ui, repo)
+    _listtopics(ui, repo, opts)
 
 @command('stack [TOPIC]', [] + commands.formatteropts)
 def cmdstack(ui, repo, topic='', **opts):
@@ -272,11 +272,24 @@
     List the current topic by default."""
     return stack.showstack(ui, repo, topic, opts)
 
-def _listtopics(ui, repo):
-    current = repo.currenttopic
-    for t in sorted(repo.topics):
-        marker = '*' if t == current else ' '
-        ui.write(' %s %s\n' % (marker, t))
+def _listtopics(ui, repo, opts):
+    fm = ui.formatter('bookmarks', opts)
+    activetopic = repo.currenttopic
+    for topic in sorted(repo.topics):
+        fm.startitem()
+        marker = ' '
+        label = 'topic'
+        active = (topic == activetopic)
+        if active:
+            marker = '*'
+            label = 'topic.active'
+        if not ui.quiet:
+            # registering the active data is made explicitly later
+            fm.plain(' %s ' % marker, label=label)
+        fm.write('topic', '%s', topic, label=label)
+        fm.data(active=active)
+        fm.plain('\n')
+    fm.end()
 
 def summaryhook(ui, repo):
     t = repo.currenttopic
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-topic-list.t	Sat Aug 13 00:37:33 2016 +0200
@@ -0,0 +1,223 @@
+Setup
+=====
+
+  $ . "$TESTDIR/testlib"
+
+  $ hg init test-list
+  $ cd test-list
+  $ cat <<EOF >> .hg/hgrc
+  > [phases]
+  > publish=false
+  > EOF
+  $ cat <<EOF >> $HGRCPATH
+  > [experimental]
+  > # disable the new graph style until we drop 3.7 support
+  > graphstyle.missing = |
+  > # turn evolution on
+  > evolution=all
+  > EOF
+
+
+  $ mkcommit() {
+  >    echo "$1" > "$1"
+  >    hg add "$1"
+  >    hg ci -m "add $1"
+  > }
+
+Build some basic graph
+----------------------
+
+  $ for x in base_a base_b base_c base_d base_e ; do
+  >   mkcommit $x
+  > done
+
+Add another branch with two heads
+
+  $ hg up 'desc(base_a)'
+  0 files updated, 0 files merged, 4 files removed, 0 files unresolved
+  $ hg branch lake
+  marked working directory as branch lake
+  (branches are permanent and global, did you want a bookmark?)
+  $ mkcommit lake_a
+  $ mkcommit lake_b
+  $ hg up 'desc(lake_a)'
+  0 files updated, 0 files merged, 1 files removed, 0 files unresolved
+  $ mkcommit lake_c
+  created new head
+
+
+Add some topics
+---------------
+
+A simple topic that need rebasing
+
+  $ hg up 'desc(base_c)'
+  2 files updated, 0 files merged, 2 files removed, 0 files unresolved
+  $ hg topic baz
+  $ mkcommit baz_a
+  $ mkcommit baz_b
+
+A simple topic with unstability
+
+  $ hg up 'desc(base_d)'
+  1 files updated, 0 files merged, 2 files removed, 0 files unresolved
+  $ hg topic fuz
+  $ mkcommit fuz_a
+  $ mkcommit fuz_b
+  $ mkcommit fuz_c
+  $ hg up 'desc(fuz_a)'
+  0 files updated, 0 files merged, 2 files removed, 0 files unresolved
+  $ hg commit --amend --message 'fuz1_a'
+
+A topic with multiple heads
+
+  $ hg up 'desc(base_e)'
+  1 files updated, 0 files merged, 1 files removed, 0 files unresolved
+  $ hg topic bar
+  $ mkcommit bar_a
+  $ mkcommit bar_b
+  $ mkcommit bar_c
+  $ hg up 'desc(bar_b)'
+  0 files updated, 0 files merged, 1 files removed, 0 files unresolved
+  $ mkcommit bar_d
+  $ mkcommit bar_e
+  $ hg up 'desc(bar_d)'
+  0 files updated, 0 files merged, 1 files removed, 0 files unresolved
+  $ hg commit --amend --message 'bar1_d'
+
+topic 'foo' on the multi headed branch
+
+  $ hg up 'desc(lake_a)'
+  1 files updated, 0 files merged, 7 files removed, 0 files unresolved
+  $ hg topic foo
+  $ mkcommit foo_a
+  $ mkcommit foo_b
+
+Summary
+-------
+
+  $ hg summary
+  parent: 21:3e54b49a3113 tip
+   add foo_b
+  branch: lake
+  commit: (clean)
+  update: 2 new changesets (update)
+  phases: 22 draft
+  unstable: 3 changesets
+  topic:  foo
+  $ hg log --graph -T '{desc} ({branch}) [{topic}]'
+  @  add foo_b (lake) []
+  |
+  o  add foo_a (lake) []
+  |
+  | o  bar1_d (default) []
+  | |
+  | | o  add bar_e (default) []
+  | | |
+  | | x  add bar_d (default) []
+  | |/
+  | | o  add bar_c (default) []
+  | |/
+  | o  add bar_b (default) []
+  | |
+  | o  add bar_a (default) []
+  | |
+  | | o  fuz1_a (default) []
+  | | |
+  | | | o  add fuz_c (default) []
+  | | | |
+  | | | o  add fuz_b (default) []
+  | | | |
+  | | | x  add fuz_a (default) []
+  | | |/
+  | | | o  add baz_b (default) []
+  | | | |
+  | | | o  add baz_a (default) []
+  | | | |
+  +-------o  add lake_c (lake) []
+  | | | |
+  +-------o  add lake_b (lake) []
+  | | | |
+  o | | |  add lake_a (lake) []
+  | | | |
+  | o | |  add base_e (default) []
+  | |/ /
+  | o /  add base_d (default) []
+  | |/
+  | o  add base_c (default) []
+  | |
+  | o  add base_b (default) []
+  |/
+  o  add base_a (default) []
+  
+
+Actual Testing
+==============
+
+basic output
+
+  $ hg topic
+     bar
+     baz
+   * foo
+     fuz
+
+quiet version
+
+  $ hg topic --quiet
+  bar
+  baz
+  foo
+  fuz
+
+verbose
+
+  $ hg topic --verbose
+     bar
+     baz
+   * foo
+     fuz
+
+json
+
+  $ hg topic -T json
+  [
+   {
+    "active": false,
+    "topic": "bar"
+   },
+   {
+    "active": false,
+    "topic": "baz"
+   },
+   {
+    "active": true,
+    "topic": "foo"
+   },
+   {
+    "active": false,
+    "topic": "fuz"
+   }
+  ]
+
+json --verbose
+
+  $ hg topic -T json --verbose
+  [
+   {
+    "active": false,
+    "topic": "bar"
+   },
+   {
+    "active": false,
+    "topic": "baz"
+   },
+   {
+    "active": true,
+    "topic": "foo"
+   },
+   {
+    "active": false,
+    "topic": "fuz"
+   }
+  ]