[PATCH 5 of 8] paths: port to generic templater

Yuya Nishihara yuya at tcha.org
Tue Jan 12 16:34:03 CST 2016


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1450012197 -32400
#      Sun Dec 13 22:09:57 2015 +0900
# Node ID 294037159c6443f46f18dc7200f36c568874a2da
# Parent  1a6fd929056f3572b596091372bf251579f41bce
paths: port to generic templater

Embedded passwords are masked only in plain output because we'll want raw
values in machine-readable format such as JSON. For custom template, we can
add a filter to mask passwords (e.g. "{url|hidepassword}").

path.rawloc field is called as "url" than "path" because we have "pushurl"
sub-option. Also, "name" and "url" are not allowed as sub-options as they
conflict with the field names.

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -5365,8 +5365,8 @@
             displayer.show(repo[n])
     displayer.close()
 
- at command('paths', [], _('[NAME]'), optionalrepo=True)
-def paths(ui, repo, search=None):
+ at command('paths', formatteropts, _('[NAME]'), optionalrepo=True)
+def paths(ui, repo, search=None, **opts):
     """show aliases for remote repositories
 
     Show definition of symbolic path name NAME. If no name is given,
@@ -5403,6 +5403,11 @@
     else:
         pathitems = sorted(ui.paths.iteritems())
 
+    fm = ui.formatter('paths', opts)
+    if fm:
+        hidepassword = str
+    else:
+        hidepassword = util.hidepassword
     if ui.quiet:
         namefmt = '%s\n'
     else:
@@ -5410,13 +5415,16 @@
     showsubopts = not search and not ui.quiet
 
     for name, path in pathitems:
-        if not search:
-            ui.write(namefmt % name)
-        if not ui.quiet:
-            ui.write("%s\n" % util.hidepassword(path.rawloc))
+        fm.startitem()
+        fm.condwrite(not search, 'name', namefmt, name)
+        fm.condwrite(not ui.quiet, 'url', '%s\n', hidepassword(path.rawloc))
         for subopt, value in sorted(path.suboptions.items()):
+            assert subopt not in ('name', 'url')
             if showsubopts:
-                ui.write('%s:%s = %s\n' % (name, subopt, value))
+                fm.plain('%s:%s = ' % (name, subopt))
+            fm.condwrite(showsubopts, subopt, '%s\n', value)
+
+    fm.end()
 
     if search and not pathitems:
         if not ui.quiet:
diff --git a/tests/test-completion.t b/tests/test-completion.t
--- a/tests/test-completion.t
+++ b/tests/test-completion.t
@@ -286,7 +286,7 @@
   manifest: rev, all, template
   outgoing: force, rev, newest-first, bookmarks, branch, patch, git, limit, no-merges, stat, graph, style, template, ssh, remotecmd, insecure, subrepos
   parents: rev, style, template
-  paths: 
+  paths: template
   phase: public, draft, secret, force, rev
   recover: 
   rename: after, force, include, exclude, dry-run
diff --git a/tests/test-paths.t b/tests/test-paths.t
--- a/tests/test-paths.t
+++ b/tests/test-paths.t
@@ -10,6 +10,9 @@
   $ hg paths unknown
   not found!
   [1]
+  $ hg paths -Tjson
+  [
+  ]
 
 with paths:
 
@@ -52,6 +55,48 @@
   [1]
   $ hg paths -q unknown
   [1]
+
+formatter output with paths:
+
+  $ echo 'dupe:pushurl = https://example.com/dupe' >> .hg/hgrc
+  $ hg paths -Tjson
+  [
+   {
+    "name": "dupe",
+    "pushurl": "https://example.com/dupe",
+    "url": "$TESTTMP/b#tip"
+   },
+   {
+    "name": "expand",
+    "url": "$TESTTMP/a/$SOMETHING/bar"
+   }
+  ]
+  $ hg paths -Tjson dupe
+  [
+   {
+    "name": "dupe",
+    "pushurl": "https://example.com/dupe",
+    "url": "$TESTTMP/b#tip"
+   }
+  ]
+  $ hg paths -Tjson -q unknown
+  [
+  ]
+  [1]
+
+password should be masked in plain output, but not in machine-readable output:
+
+  $ echo 'insecure = http://foo:insecure@example.com/' >> .hg/hgrc
+  $ hg paths insecure
+  http://foo:***@example.com/
+  $ hg paths -Tjson insecure
+  [
+   {
+    "name": "insecure",
+    "url": "http://foo:insecure@example.com/"
+   }
+  ]
+
   $ cd ..
 
 sub-options for an undeclared path are ignored


More information about the Mercurial-devel mailing list