[PATCH 4 of 5] grep: build list of all columns regardless of display options

Yuya Nishihara yuya at tcha.org
Thu Aug 25 11:24:57 EDT 2016


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1471499526 -32400
#      Thu Aug 18 14:52:06 2016 +0900
# Node ID 68f772b9eee689b7d65647a96e1224e755dd26ed
# Parent  9a00e4df27db537a1f9d0c2259312fd174e8a278
grep: build list of all columns regardless of display options

These columns should always be available in JSON or template outputs. The
"change" column is excluded because it has no useful data unless --all is
specified.

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -4397,20 +4397,23 @@ def grep(ui, repo, pattern, *pats, **opt
         else:
             iter = [('', l) for l in states]
         for change, l in iter:
-            cols = [(fn, 'filename'), (str(rev), 'rev')]
-
-            if opts.get('line_number'):
-                cols.append((str(l.linenum), 'linenumber'))
+            cols = [
+                ('filename', fn, True),
+                ('rev', str(rev), True),
+                ('linenumber', str(l.linenum), opts.get('line_number')),
+            ]
             if opts.get('all'):
-                cols.append((change, 'change'))
-            if opts.get('user'):
-                cols.append((ui.shortuser(ctx.user()), 'user'))
-            if opts.get('date'):
-                cols.append((datefunc(ctx.date()), 'date'))
-            for col, field in cols[:-1]:
-                ui.write(col, label='grep.%s' % field)
-                ui.write(sep, label='grep.sep')
-            ui.write(cols[-1][0], label='grep.%s' % cols[-1][1])
+                cols.append(('change', change, True))
+            cols.extend([
+                ('user', ui.shortuser(ctx.user()), opts.get('user')),
+                ('date', datefunc(ctx.date()), opts.get('date')),
+            ])
+            lastcol = next(name for name, data, cond in reversed(cols) if cond)
+            for name, data, cond in cols:
+                if cond:
+                    ui.write(data, label='grep.%s' % name)
+                if cond and name != lastcol:
+                    ui.write(sep, label='grep.sep')
             if not opts.get('files_with_matches'):
                 ui.write(sep, label='grep.sep')
                 if not opts.get('text') and binary():


More information about the Mercurial-devel mailing list