Patchwork [1,of,2] cat: factor out a function that populates the formatter

login
register
mail settings
Submitter Matt Harbison
Date Jan. 17, 2018, 2:29 a.m.
Message ID <ac8052ec552b36d4bee5.1516156166@Envy>
Download mbox | patch
Permalink /patch/26802/
State Accepted
Headers show

Comments

Matt Harbison - Jan. 17, 2018, 2:29 a.m.
# HG changeset patch
# User Matt Harbison <matt_harbison@yahoo.com>
# Date 1516150560 18000
#      Tue Jan 16 19:56:00 2018 -0500
# Node ID ac8052ec552b36d4bee5bc24bc92fcee36d843cb
# Parent  821d8a5ab4ff890a7732c2e4cdcc7f32191e5942
cat: factor out a function that populates the formatter

This will allow extensions to add data to the templater.
Yuya Nishihara - Jan. 17, 2018, 1:40 p.m.
On Tue, 16 Jan 2018 21:29:26 -0500, Matt Harbison wrote:
> # HG changeset patch
> # User Matt Harbison <matt_harbison@yahoo.com>
> # Date 1516150560 18000
> #      Tue Jan 16 19:56:00 2018 -0500
> # Node ID ac8052ec552b36d4bee5bc24bc92fcee36d843cb
> # Parent  821d8a5ab4ff890a7732c2e4cdcc7f32191e5942
> cat: factor out a function that populates the formatter

Queued, thanks.

> +def _updatecatformatter(fm, ctx, matcher, path, decode):
> +    """Hook for adding data to the formatter used by ``hg cat``.
> +
> +    Extensions (e.g., lfs) can wrap this to inject keywords/data, but must call
> +    this method first."""
> +    data = ctx[path].data()
> +    if decode:
> +        data = ctx.repo().wwritedata(path, data)
> +    fm.startitem()
> +    fm.write('data', '%s', data)
> +    fm.data(abspath=path, path=matcher.rel(path))

FWIW, it'll be nice if we can add templatekw for fctx, so {rawdata} can be
a lazy function.
Matt Harbison - Jan. 18, 2018, 1:28 a.m.
On Wed, 17 Jan 2018 08:40:10 -0500, Yuya Nishihara <yuya@tcha.org> wrote:

> On Tue, 16 Jan 2018 21:29:26 -0500, Matt Harbison wrote:
>> # HG changeset patch
>> # User Matt Harbison <matt_harbison@yahoo.com>
>> # Date 1516150560 18000
>> #      Tue Jan 16 19:56:00 2018 -0500
>> # Node ID ac8052ec552b36d4bee5bc24bc92fcee36d843cb
>> # Parent  821d8a5ab4ff890a7732c2e4cdcc7f32191e5942
>> cat: factor out a function that populates the formatter
>
> Queued, thanks.
>
>> +def _updatecatformatter(fm, ctx, matcher, path, decode):
>> +    """Hook for adding data to the formatter used by ``hg cat``.
>> +
>> +    Extensions (e.g., lfs) can wrap this to inject keywords/data, but  
>> must call
>> +    this method first."""
>> +    data = ctx[path].data()
>> +    if decode:
>> +        data = ctx.repo().wwritedata(path, data)
>> +    fm.startitem()
>> +    fm.write('data', '%s', data)
>> +    fm.data(abspath=path, path=matcher.rel(path))
>
> FWIW, it'll be nice if we can add templatekw for fctx, so {rawdata} can  
> be
> a lazy function.

I found the TODO in formatter._renderitem(), but have no idea what's  
needed.  We can pick it up next cycle.  I'd like to add some of these lfs  
keywords to `hg files`, since log only notices when there's a change to  
the file.
Yuya Nishihara - Jan. 18, 2018, 12:12 p.m.
On Wed, 17 Jan 2018 20:28:52 -0500, Matt Harbison wrote:
> On Wed, 17 Jan 2018 08:40:10 -0500, Yuya Nishihara <yuya@tcha.org> wrote:
> 
> > On Tue, 16 Jan 2018 21:29:26 -0500, Matt Harbison wrote:
> >> # HG changeset patch
> >> # User Matt Harbison <matt_harbison@yahoo.com>
> >> # Date 1516150560 18000
> >> #      Tue Jan 16 19:56:00 2018 -0500
> >> # Node ID ac8052ec552b36d4bee5bc24bc92fcee36d843cb
> >> # Parent  821d8a5ab4ff890a7732c2e4cdcc7f32191e5942
> >> cat: factor out a function that populates the formatter
> >
> > Queued, thanks.
> >
> >> +def _updatecatformatter(fm, ctx, matcher, path, decode):
> >> +    """Hook for adding data to the formatter used by ``hg cat``.
> >> +
> >> +    Extensions (e.g., lfs) can wrap this to inject keywords/data, but  
> >> must call
> >> +    this method first."""
> >> +    data = ctx[path].data()
> >> +    if decode:
> >> +        data = ctx.repo().wwritedata(path, data)
> >> +    fm.startitem()
> >> +    fm.write('data', '%s', data)
> >> +    fm.data(abspath=path, path=matcher.rel(path))
> >
> > FWIW, it'll be nice if we can add templatekw for fctx, so {rawdata} can  
> > be
> > a lazy function.
> 
> I found the TODO in formatter._renderitem(), but have no idea what's  
> needed.

We'll need a way of sorting out templatekw by required resources, e.g. repo,
ctx, fctx, etc. It's in my TODO list.

Patch

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -2962,6 +2962,18 @@ 
 
     return ret
 
+def _updatecatformatter(fm, ctx, matcher, path, decode):
+    """Hook for adding data to the formatter used by ``hg cat``.
+
+    Extensions (e.g., lfs) can wrap this to inject keywords/data, but must call
+    this method first."""
+    data = ctx[path].data()
+    if decode:
+        data = ctx.repo().wwritedata(path, data)
+    fm.startitem()
+    fm.write('data', '%s', data)
+    fm.data(abspath=path, path=matcher.rel(path))
+
 def cat(ui, repo, ctx, matcher, basefm, fntemplate, prefix, **opts):
     err = 1
     opts = pycompat.byteskwargs(opts)
@@ -2977,12 +2989,7 @@ 
             except OSError:
                 pass
         with formatter.maybereopen(basefm, filename, opts) as fm:
-            data = ctx[path].data()
-            if opts.get('decode'):
-                data = repo.wwritedata(path, data)
-            fm.startitem()
-            fm.write('data', '%s', data)
-            fm.data(abspath=path, path=matcher.rel(path))
+            _updatecatformatter(fm, ctx, matcher, path, opts.get('decode'))
 
     # Automation often uses hg cat on single files, so special case it
     # for performance to avoid the cost of parsing the manifest.