diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py --- a/mercurial/cmdutil.py +++ b/mercurial/cmdutil.py @@ -547,30 +547,37 @@ def export(repo, revs, template='hg-%h.p 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() diff --git a/tests/test-export.t b/tests/test-export.t --- a/tests/test-export.t +++ b/tests/test-export.t @@ -144,4 +144,28 @@ Catch exporting unknown revisions (espec 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 ..