[PATCH] commands: grep:don't print control sequence for binary file matches(issue2614)

Md.O.Shayan mdoshayan at gmail.com
Sun Apr 3 22:52:02 CDT 2011


# HG changeset patch
# User Md.O.Shayan<mdoshayan at gmail.com>
# Date 1301888586 -19800
# Node ID 82160ae12a3a0755b1d7e5eaea3b28aa507e0504
# Parent  0995eee8ffe4a24478379fb16fd6c38812bc3dd5
commands: grep:don't print control sequence for binary file matches(issue2614)

diff -r 0995eee8ffe4 -r 82160ae12a3a mercurial/commands.py
--- a/mercurial/commands.py	Sat Apr 02 11:07:05 2011 +0200
+++ b/mercurial/commands.py	Mon Apr 04 09:13:06 2011 +0530
@@ -1859,7 +1859,7 @@
                 for i in xrange(blo, bhi):
                     yield ('+', b[i])
 
-    def display(fn, ctx, pstates, states):
+    def display(fn, ctx, pstates, states, binary):
         rev = ctx.rev()
         datefunc = ui.quiet and util.shortdate or util.datestr
         found = False
@@ -1871,7 +1871,7 @@
         for change, l in iter:
             cols = [fn, str(rev)]
             before, match, after = None, None, None
-            if opts.get('line_number'):
+            if opts.get('line_number') and not binary:
                 cols.append(str(l.linenum))
             if opts.get('all'):
                 cols.append(change)
@@ -1889,7 +1889,9 @@
                 match = l.line[l.colstart:l.colend]
                 after = l.line[l.colend:]
             ui.write(sep.join(cols))
-            if before is not None:
+            if binary:
+                ui.write(sep+" Binary file")
+            elif before is not None:
                 ui.write(sep + before)
                 ui.write(match, label='grep.match')
                 ui.write(after)
@@ -1899,6 +1901,7 @@
 
     skip = {}
     revfiles = {}
+    binary = {}
     matchfn = cmdutil.match(repo, pats, opts)
     found = False
     follow = opts.get('follow')
@@ -1949,8 +1952,11 @@
                     skip[copy] = True
                 continue
             pstates = matches.get(parent, {}).get(copy or fn, [])
+            if fn not in binary:
+                flog = getfile(fn)
+                binary[fn] = util.binary(flog.read(ctx.filenode(fn)))
             if pstates or states:
-                r = display(fn, ctx, pstates, states)
+                r = display(fn, ctx, pstates, states, binary[fn])
                 found = found or r
                 if r and not opts.get('all'):
                     skip[fn] = True
diff -r 0995eee8ffe4 -r 82160ae12a3a tests/test-grep-bin.t
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-grep-bin.t	Mon Apr 04 09:13:06 2011 +0530
@@ -0,0 +1,7 @@
+  $ hg init a
+  $ cd a
+  $ cp $TESTDIR/binfile.bin .
+  $ hg add binfile.bin
+  $ hg ci -m 'add binfile.bin'
+  $ hg grep "MaCam" --all
+  binfile.bin:0:+: Binary file


More information about the Mercurial-devel mailing list