[PATCH 3 of 5] formatter: rename {abspath}/{file} to {path}, and drop relative {path} (BC)

Yuya Nishihara yuya at tcha.org
Thu Aug 30 09:09:48 EDT 2018


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1533455056 -32400
#      Sun Aug 05 16:44:16 2018 +0900
# Node ID 1fd4bcc235cd6c3615a1fe398d4df23fcc60d43e
# Parent  ff16764c1db7beeb262f88302516ae97f55fa858
formatter: rename {abspath}/{file} to {path}, and drop relative {path} (BC)

Note that {path} in status is either relative-to-cwd or repository-absolute
depending on the command argument and config knob, which can't be reproduced
by using the {path|relpath} filter. The default template is updated to always
use a relative path.

.. bc::

   ``{abspath}`` and ``{file}`` in generic templates are renamed to ``{path}``.
   Any ``{path}`` is a repository-absolute path. Use ``{path|relpath}`` to
   convert it to a filesystem path.

diff --git a/hgext/keyword.py b/hgext/keyword.py
--- a/hgext/keyword.py
+++ b/hgext/keyword.py
@@ -576,8 +576,8 @@ def files(ui, repo, *pats, **opts):
         label = 'kwfiles.' + kwstate
         for f in filenames:
             fm.startitem()
-            fm.write('kwstatus path', fmt, char,
-                     repo.pathto(f, cwd), label=label)
+            fm.data(kwstatus=char, path=f)
+            fm.plain(fmt % (char, repo.pathto(f, cwd)), label=label)
     fm.end()
 
 @command('kwshrink',
diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -2176,8 +2176,8 @@ def files(ui, ctx, m, fm, fmt, subrepos)
         if needsfctx:
             fc = ctx[f]
             fm.write('size flags', '% 10d % 1s ', fc.size(), fc.flags())
-        fm.data(abspath=f)
-        fm.write('path', fmt, m.rel(f))
+        fm.data(path=f)
+        fm.plain(fmt % m.rel(f))
         ret = 0
 
     for subpath in sorted(ctx.substate):
@@ -2323,7 +2323,7 @@ def _updatecatformatter(fm, ctx, matcher
     fm.startitem()
     fm.context(ctx=ctx)
     fm.write('data', '%s', data)
-    fm.data(abspath=path, path=matcher.rel(path))
+    fm.data(path=path)
 
 def cat(ui, repo, ctx, matcher, basefm, fntemplate, prefix, **opts):
     err = 1
diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -337,10 +337,10 @@ def annotate(ui, repo, *pats, **opts):
              ('rev', ' ', lambda x: x.fctx.rev(), formatrev),
              ('node', ' ', lambda x: hexfn(x.fctx.node()), formathex),
              ('date', ' ', lambda x: x.fctx.date(), util.cachefunc(datefunc)),
-             ('file', ' ', lambda x: x.fctx.path(), pycompat.bytestr),
+             ('path', ' ', lambda x: x.fctx.path(), pycompat.bytestr),
              ('line_number', ':', lambda x: x.lineno, pycompat.bytestr),
             ]
-    opnamemap = {'rev': 'number', 'node': 'changeset'}
+    opnamemap = {'rev': 'number', 'node': 'changeset', 'path': 'file'}
 
     if (not opts.get('user') and not opts.get('changeset')
         and not opts.get('date') and not opts.get('file')):
@@ -380,7 +380,7 @@ def annotate(ui, repo, *pats, **opts):
     for abs in ctx.walk(m):
         fctx = ctx[abs]
         rootfm.startitem()
-        rootfm.data(abspath=abs, path=m.rel(abs))
+        rootfm.data(path=abs)
         if not opts.get('text') and fctx.isbinary():
             rootfm.plain(_("%s: binary file\n")
                          % ((pats and m.rel(abs)) or abs))
@@ -2660,7 +2660,7 @@ def grep(ui, repo, pattern, *pats, **opt
             except error.WdirUnsupported:
                 return ctx[fn].isbinary()
 
-        fieldnamemap = {'filename': 'file', 'linenumber': 'line_number'}
+        fieldnamemap = {'filename': 'path', 'linenumber': 'line_number'}
         if diff:
             iter = difflinestates(pstates, states)
         else:
@@ -5187,10 +5187,12 @@ def status(ui, repo, *pats, **opts):
             for f in files:
                 fm.startitem()
                 fm.context(ctx=ctx2)
+                fm.data(path=f)
                 fm.condwrite(showchar, 'status', '%s ', char, label=label)
-                fm.write('path', fmt, repo.pathto(f, cwd), label=label)
+                fm.plain(fmt % repo.pathto(f, cwd), label=label)
                 if f in copy:
-                    fm.write("copy", '  %s' + end, repo.pathto(copy[f], cwd),
+                    fm.data(copy=copy[f])
+                    fm.plain(('  %s' + end) % repo.pathto(copy[f], cwd),
                              label='status.copied')
 
     if ((ui.verbose or ui.configbool('commands', 'status.verbose'))
diff --git a/mercurial/templates/map-cmdline.default b/mercurial/templates/map-cmdline.default
--- a/mercurial/templates/map-cmdline.default
+++ b/mercurial/templates/map-cmdline.default
@@ -74,7 +74,7 @@ description = '{if(desc|strip, "{label('
                                 {label('ui.note log.description',
                                        '{desc|strip}')}\n\n")}'
 
-status = '{status} {path}\n{if(copy, "  {copy}\n")}'
+status = '{status} {path|relpath}\n{if(copy, "  {copy|relpath}\n")}'
 
 # Obsfate templates, it would be removed once we introduce the obsfate
 # template fragment
diff --git a/tests/test-annotate.t b/tests/test-annotate.t
--- a/tests/test-annotate.t
+++ b/tests/test-annotate.t
@@ -56,7 +56,6 @@ annotate (JSON)
   $ hg annotate -Tjson a
   [
    {
-    "abspath": "a",
     "lines": [{"line": "a\n", "rev": 0}],
     "path": "a"
    }
@@ -65,8 +64,7 @@ annotate (JSON)
   $ hg annotate -Tjson -cdfnul a
   [
    {
-    "abspath": "a",
-    "lines": [{"date": [1.0, 0], "file": "a", "line": "a\n", "line_number": 1, "node": "8435f90966e442695d2ded29fdade2bac5ad8065", "rev": 0, "user": "nobody"}],
+    "lines": [{"date": [1.0, 0], "line": "a\n", "line_number": 1, "node": "8435f90966e442695d2ded29fdade2bac5ad8065", "path": "a", "rev": 0, "user": "nobody"}],
     "path": "a"
    }
   ]
@@ -127,12 +125,10 @@ annotate multiple files (JSON)
   $ hg annotate -Tjson a b
   [
    {
-    "abspath": "a",
     "lines": [{"line": "a\n", "rev": 0}, {"line": "a\n", "rev": 1}, {"line": "a\n", "rev": 1}],
     "path": "a"
    },
    {
-    "abspath": "b",
     "lines": [{"line": "a\n", "rev": 0}, {"line": "a\n", "rev": 1}, {"line": "a\n", "rev": 1}, {"line": "b4\n", "rev": 3}, {"line": "b5\n", "rev": 3}, {"line": "b6\n", "rev": 3}],
     "path": "b"
    }
@@ -140,7 +136,7 @@ annotate multiple files (JSON)
 
 annotate multiple files (template)
 
-  $ hg annotate -T'== {abspath} ==\n{lines % "{rev}: {line}"}' a b
+  $ hg annotate -T'== {path} ==\n{lines % "{rev}: {line}"}' a b
   == a ==
   0: a
   1: a
@@ -568,7 +564,6 @@ annotate modified file
   $ hg annotate -ncr "wdir()" -Tjson foo
   [
    {
-    "abspath": "foo",
     "lines": [{"line": "foo\n", "node": "472b18db256d1e8282064eab4bfdaf48cbfe83cd", "rev": 11}, {"line": "foofoo\n", "node": null, "rev": null}],
     "path": "foo"
    }
@@ -870,11 +865,9 @@ Test empty annotate output
   $ hg annotate -Tjson binary empty
   [
    {
-    "abspath": "binary",
     "path": "binary"
    },
    {
-    "abspath": "empty",
     "lines": [],
     "path": "empty"
    }
diff --git a/tests/test-cat.t b/tests/test-cat.t
--- a/tests/test-cat.t
+++ b/tests/test-cat.t
@@ -65,7 +65,7 @@ Test fileset
 
 Test template output
 
-  $ hg --cwd tmp cat ../b ../c -T '== {path} ({abspath}) r{rev} ==\n{data}'
+  $ hg --cwd tmp cat ../b ../c -T '== {path|relpath} ({path}) r{rev} ==\n{data}'
   == ../b (b) r2 ==
   1
   == ../c (c) r2 ==
@@ -74,12 +74,10 @@ Test template output
   $ hg cat b c -Tjson --output -
   [
    {
-    "abspath": "b",
     "data": "1\n",
     "path": "b"
    },
    {
-    "abspath": "c",
     "data": "3\n",
     "path": "c"
    }
@@ -89,7 +87,6 @@ Test template output
   $ cat tmp/b.json
   [
    {
-    "abspath": "b",
     "data": "1\n",
     "path": "b"
    }
@@ -97,7 +94,6 @@ Test template output
   $ cat tmp/c.json
   [
    {
-    "abspath": "c",
     "data": "3\n",
     "path": "c"
    }
diff --git a/tests/test-fastannotate-hg.t b/tests/test-fastannotate-hg.t
--- a/tests/test-fastannotate-hg.t
+++ b/tests/test-fastannotate-hg.t
@@ -69,7 +69,6 @@ annotate (JSON)
   $ hg annotate -Tjson a
   [
    {
-    "abspath": "a",
     "lines": [{"line": "a\n", "rev": 0}],
     "path": "a"
    }
@@ -78,8 +77,7 @@ annotate (JSON)
   $ hg annotate -Tjson -cdfnul a
   [
    {
-    "abspath": "a",
-    "lines": [{"date": [1.0, 0], "file": "a", "line": "a\n", "line_number": 1, "node": "8435f90966e442695d2ded29fdade2bac5ad8065", "rev": 0, "user": "nobody"}],
+    "lines": [{"date": [1.0, 0], "line": "a\n", "line_number": 1, "node": "8435f90966e442695d2ded29fdade2bac5ad8065", "path": "a", "rev": 0, "user": "nobody"}],
     "path": "a"
    }
   ]
@@ -571,7 +569,6 @@ annotate modified file
   $ hg annotate -ncr "wdir()" -Tjson foo
   [
    {
-    "abspath": "foo",
     "lines": [{"line": "foo\n", "node": "472b18db256d1e8282064eab4bfdaf48cbfe83cd", "rev": 11}, {"line": "foofoo\n", "node": null, "rev": null}],
     "path": "foo"
    }
diff --git a/tests/test-grep.t b/tests/test-grep.t
--- a/tests/test-grep.t
+++ b/tests/test-grep.t
@@ -43,17 +43,17 @@ simple with color
 simple templated
 
   $ hg grep port -r tip:0 \
-  > -T '{file}:{rev}:{node|short}:{texts % "{if(matched, text|upper, text)}"}\n'
+  > -T '{path}:{rev}:{node|short}:{texts % "{if(matched, text|upper, text)}"}\n'
   port:4:914fa752cdea:exPORT
   port:4:914fa752cdea:vaPORTight
   port:4:914fa752cdea:imPORT/exPORT
 
-  $ hg grep port -r tip:0 -T '{file}:{rev}:{texts}\n'
+  $ hg grep port -r tip:0 -T '{path}:{rev}:{texts}\n'
   port:4:export
   port:4:vaportight
   port:4:import/export
 
-  $ hg grep port -r tip:0 -T '{file}:{tags}:{texts}\n'
+  $ hg grep port -r tip:0 -T '{path}:{tags}:{texts}\n'
   port:tip:export
   port:tip:vaportight
   port:tip:import/export
@@ -64,27 +64,27 @@ simple JSON (no "change" field)
   [
    {
     "date": [4, 0],
-    "file": "port",
     "line_number": 1,
     "node": "914fa752cdea87777ac1a8d5c858b0c736218f6c",
+    "path": "port",
     "rev": 4,
     "texts": [{"matched": false, "text": "ex"}, {"matched": true, "text": "port"}],
     "user": "spam"
    },
    {
     "date": [4, 0],
-    "file": "port",
     "line_number": 2,
     "node": "914fa752cdea87777ac1a8d5c858b0c736218f6c",
+    "path": "port",
     "rev": 4,
     "texts": [{"matched": false, "text": "va"}, {"matched": true, "text": "port"}, {"matched": false, "text": "ight"}],
     "user": "spam"
    },
    {
     "date": [4, 0],
-    "file": "port",
     "line_number": 3,
     "node": "914fa752cdea87777ac1a8d5c858b0c736218f6c",
+    "path": "port",
     "rev": 4,
     "texts": [{"matched": false, "text": "im"}, {"matched": true, "text": "port"}, {"matched": false, "text": "/ex"}, {"matched": true, "text": "port"}],
     "user": "spam"
@@ -97,9 +97,9 @@ simple JSON without matching lines
   [
    {
     "date": [4, 0],
-    "file": "port",
     "line_number": 1,
     "node": "914fa752cdea87777ac1a8d5c858b0c736218f6c",
+    "path": "port",
     "rev": 4,
     "user": "spam"
    }
@@ -125,9 +125,9 @@ all JSON
    {
     "change": "-",
     "date": [4, 0],
-    "file": "port",
     "line_number": 4,
     "node": "914fa752cdea87777ac1a8d5c858b0c736218f6c",
+    "path": "port",
     "rev": 4,
     "texts": [{"matched": false, "text": "im"}, {"matched": true, "text": "port"}, {"matched": false, "text": "/ex"}, {"matched": true, "text": "port"}],
     "user": "spam"
@@ -135,9 +135,9 @@ all JSON
    {
     "change": "+",
     "date": [3, 0],
-    "file": "port",
     "line_number": 4,
     "node": "95040cfd017d658c536071c6290230a613c4c2a6",
+    "path": "port",
     "rev": 3,
     "texts": [{"matched": false, "text": "im"}, {"matched": true, "text": "port"}, {"matched": false, "text": "/ex"}, {"matched": true, "text": "port"}],
     "user": "eggs"
@@ -145,9 +145,9 @@ all JSON
    {
     "change": "-",
     "date": [2, 0],
-    "file": "port",
     "line_number": 1,
     "node": "3b325e3481a1f07435d81dfdbfa434d9a0245b47",
+    "path": "port",
     "rev": 2,
     "texts": [{"matched": false, "text": "im"}, {"matched": true, "text": "port"}],
     "user": "spam"
@@ -155,9 +155,9 @@ all JSON
    {
     "change": "-",
     "date": [2, 0],
-    "file": "port",
     "line_number": 2,
     "node": "3b325e3481a1f07435d81dfdbfa434d9a0245b47",
+    "path": "port",
     "rev": 2,
     "texts": [{"matched": false, "text": "ex"}, {"matched": true, "text": "port"}],
     "user": "spam"
@@ -165,9 +165,9 @@ all JSON
    {
     "change": "+",
     "date": [2, 0],
-    "file": "port",
     "line_number": 1,
     "node": "3b325e3481a1f07435d81dfdbfa434d9a0245b47",
+    "path": "port",
     "rev": 2,
     "texts": [{"matched": false, "text": "ex"}, {"matched": true, "text": "port"}],
     "user": "spam"
@@ -175,9 +175,9 @@ all JSON
    {
     "change": "+",
     "date": [2, 0],
-    "file": "port",
     "line_number": 2,
     "node": "3b325e3481a1f07435d81dfdbfa434d9a0245b47",
+    "path": "port",
     "rev": 2,
     "texts": [{"matched": false, "text": "va"}, {"matched": true, "text": "port"}, {"matched": false, "text": "ight"}],
     "user": "spam"
@@ -185,9 +185,9 @@ all JSON
    {
     "change": "+",
     "date": [2, 0],
-    "file": "port",
     "line_number": 3,
     "node": "3b325e3481a1f07435d81dfdbfa434d9a0245b47",
+    "path": "port",
     "rev": 2,
     "texts": [{"matched": false, "text": "im"}, {"matched": true, "text": "port"}, {"matched": false, "text": "/ex"}, {"matched": true, "text": "port"}],
     "user": "spam"
@@ -195,9 +195,9 @@ all JSON
    {
     "change": "+",
     "date": [1, 0],
-    "file": "port",
     "line_number": 2,
     "node": "8b20f75c158513ff5ac80bd0e5219bfb6f0eb587",
+    "path": "port",
     "rev": 1,
     "texts": [{"matched": false, "text": "ex"}, {"matched": true, "text": "port"}],
     "user": "eggs"
@@ -205,9 +205,9 @@ all JSON
    {
     "change": "+",
     "date": [0, 0],
-    "file": "port",
     "line_number": 1,
     "node": "f31323c9217050ba245ee8b537c713ec2e8ab226",
+    "path": "port",
     "rev": 0,
     "texts": [{"matched": false, "text": "im"}, {"matched": true, "text": "port"}],
     "user": "spam"
@@ -481,9 +481,9 @@ but it should be available in template d
   [
    {
     "date": [0, 0],
-    "file": "file2",
     "line_number": 1,
     "node": "ffffffffffffffffffffffffffffffffffffffff",
+    "path": "file2",
     "rev": 2147483647,
     "texts": [{"matched": true, "text": "some"}, {"matched": false, "text": " text"}],
     "user": "test"
diff --git a/tests/test-locate.t b/tests/test-locate.t
--- a/tests/test-locate.t
+++ b/tests/test-locate.t
@@ -158,7 +158,7 @@ Issue294: hg remove --after dir fails wh
 
 Convert native path separator to slash (issue5572)
 
-  $ hg files -T '{path|slashpath}\n'
+  $ hg files -T '{path|relpath|slashpath}\n'
   ../b
   ../dir.h/foo
   ../t.h
diff --git a/tests/test-subrepo.t b/tests/test-subrepo.t
--- a/tests/test-subrepo.t
+++ b/tests/test-subrepo.t
@@ -1066,19 +1066,18 @@ Prepare a repo with subrepo
   $ hg cat sub/repo/foo -Tjson | sed 's|\\\\|/|g'
   [
    {
-    "abspath": "foo",
     "data": "test\ntest\n",
-    "path": "sub/repo/foo"
+    "path": "foo"
    }
   ]
 
  non-exact match:
 
-  $ hg cat -T '{path}\n' 'glob:**'
+  $ hg cat -T '{path|relpath}\n' 'glob:**'
   .hgsub
   .hgsubstate
   sub/repo/foo
-  $ hg cat -T '{path}\n' 're:^sub'
+  $ hg cat -T '{path|relpath}\n' 're:^sub'
   sub/repo/foo
 
  missing subrepos in working directory:


More information about the Mercurial-devel mailing list