[PATCH 1 of 2] diff: add the --output option

Ahmed S. Darwish darwish.07 at gmail.com
Fri Jun 28 15:10:27 CDT 2013


# HG changeset patch
# User Ahmed S. Darwish <darwish.07 at gmail.com>
# Date 1372449051 -7200
# Node ID 3466620d7f96769c469f7e42b08f1ca3d53feb86
# Parent  84dc9669bd7173124d307f9fcac0632380de9ba6
diff: add the --output option

For all shells which cannot save a command standard output correctly,
this option is now introduced.

The most common example is Microsoft PowerShell, where the piped
output gets corrupted if saved using the standard, unix-like, stdout
rediction, '>' operator. By transforming the piped output to a
different encoding, PowerShell saves 'hg diff' patch output to a
format __not understandable__ by GNU patch and 'hg patch' commands.

Windows PowerShell is installed by default on all Windows 7+
machines (Windows 7, 8, Server 2008, and Server 2012). An easily
invokable 'hg diff > temp.patch' command should thus be available
on these systems.

For a similar real-world scenario, please check:

http://www.webcitation.org/6Hiiqf425 - Archived from the original
blog post at http://nbevans.wordpress.com/2011/02/22/lightweight-shelving-of-your-work-in-progress-with-mercurial/

diff -r 84dc9669bd71 -r 3466620d7f96 mercurial/commands.py
--- a/mercurial/commands.py	Mon Jun 24 14:02:01 2013 -0400
+++ b/mercurial/commands.py	Fri Jun 28 21:50:51 2013 +0200
@@ -2676,7 +2676,9 @@
         ui.warn("%s\n" % res2)
 
 @command('^diff',
-    [('r', 'rev', [], _('revision'), _('REV')),
+    [('o', 'output', '',
+     _('Save the diff to given output file'), _('FILE')),
+    ('r', 'rev', [], _('revision'), _('REV')),
     ('c', 'change', '', _('change made by revision'), _('REV'))
     ] + diffopts + diffopts2 + walkopts + subrepoopts,
     _('[OPTION]... ([-c REV] | [-r REV1 [-r REV2]]) [FILE]...'))
@@ -2737,6 +2739,7 @@
     Returns 0 on success.
     """
 
+    fname = opts.get('output')
     revs = opts.get('rev')
     change = opts.get('change')
     stat = opts.get('stat')
@@ -2754,10 +2757,11 @@
     if reverse:
         node1, node2 = node2, node1
 
+    fp = cmdutil.makefileobj(repo, fname) if fname else None
     diffopts = patch.diffopts(ui, opts)
     m = scmutil.match(repo[node2], pats, opts)
     cmdutil.diffordiffstat(ui, repo, diffopts, node1, node2, m, stat=stat,
-                           listsubrepos=opts.get('subrepos'))
+                           fp=fp, listsubrepos=opts.get('subrepos'))
 
 @command('^export',
     [('o', 'output', '',
exporting patch:
<fdopen>


More information about the Mercurial-devel mailing list