color: enabled color support for export command (issue1507)
authorAnkur Dahiya <ankurd@fb.com>
Mon, 27 Aug 2012 09:37:49 -0700
changeset 17460 a306837f8c87
parent 17459 f4d15f3b96c0
child 17463 e49771e2d071
child 17469 fb72eec7efd8
color: enabled color support for export command (issue1507) The export command didn't output the diffs in color, even when color support was enabled. This patch fixes that by making the export command use the default ui.write method, instead of directly manipulating the ui.fout file object. Also added a test case to verify color output to test-export.t.
mercurial/cmdutil.py
tests/test-export.t
--- a/mercurial/cmdutil.py	Thu Sep 06 22:51:32 2012 +0200
+++ b/mercurial/cmdutil.py	Mon Aug 27 09:37:49 2012 -0700
@@ -547,30 +547,37 @@
         prev = (parents and parents[0]) or nullid
 
         shouldclose = False
-        if not fp:
+        if not fp and len(template) > 0:
             desc_lines = ctx.description().rstrip().split('\n')
             desc = desc_lines[0]    #Commit always has a first line.
             fp = makefileobj(repo, template, node, desc=desc, total=total,
                              seqno=seqno, revwidth=revwidth, mode='ab')
             if fp != template:
                 shouldclose = True
-        if fp != sys.stdout and util.safehasattr(fp, 'name'):
+        if fp and fp != sys.stdout and util.safehasattr(fp, 'name'):
             repo.ui.note("%s\n" % fp.name)
 
-        fp.write("# HG changeset patch\n")
-        fp.write("# User %s\n" % ctx.user())
-        fp.write("# Date %d %d\n" % ctx.date())
+        if not fp:
+            write = repo.ui.write
+        else:
+            def write(s, **kw):
+                fp.write(s)
+
+
+        write("# HG changeset patch\n")
+        write("# User %s\n" % ctx.user())
+        write("# Date %d %d\n" % ctx.date())
         if branch and branch != 'default':
-            fp.write("# Branch %s\n" % branch)
-        fp.write("# Node ID %s\n" % hex(node))
-        fp.write("# Parent  %s\n" % hex(prev))
+            write("# Branch %s\n" % branch)
+        write("# Node ID %s\n" % hex(node))
+        write("# Parent  %s\n" % hex(prev))
         if len(parents) > 1:
-            fp.write("# Parent  %s\n" % hex(parents[1]))
-        fp.write(ctx.description().rstrip())
-        fp.write("\n\n")
+            write("# Parent  %s\n" % hex(parents[1]))
+        write(ctx.description().rstrip())
+        write("\n\n")
 
-        for chunk in patch.diff(repo, prev, node, opts=opts):
-            fp.write(chunk)
+        for chunk, label in patch.diffui(repo, prev, node, opts=opts):
+            write(chunk, label=label)
 
         if shouldclose:
             fp.close()
--- a/tests/test-export.t	Thu Sep 06 22:51:32 2012 +0200
+++ b/tests/test-export.t	Mon Aug 27 09:37:49 2012 -0700
@@ -144,4 +144,28 @@
   abort: export requires at least one changeset
   [255]
 
+Check for color output
+  $ echo "[color]" >> $HGRCPATH
+  $ echo "mode = ansi" >> $HGRCPATH
+  $ echo "[extensions]" >> $HGRCPATH
+  $ echo "color=" >> $HGRCPATH
+
+  $ hg export --color always --nodates tip
+  # HG changeset patch
+  # User test
+  # Date 0 0
+  # Node ID * (glob)
+  # Parent * (glob)
+   !"#$%&(,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
+  
+  \x1b[0;1mdiff -r f3acbafac161 -r 197ecd81a57f foo\x1b[0m (esc)
+  \x1b[0;31;1m--- a/foo\x1b[0m (esc)
+  \x1b[0;32;1m+++ b/foo\x1b[0m (esc)
+  \x1b[0;35m@@ -10,3 +10,4 @@\x1b[0m (esc)
+   foo-9
+   foo-10
+   foo-11
+  \x1b[0;32m+line\x1b[0m (esc)
+
+
   $ cd ..