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

login
register
mail settings
Submitter Ahmed S. Darwish
Date June 28, 2013, 8:10 p.m.
Message ID <20130628201027.GA6043@Darwish.PC>
Download mbox | patch
Permalink /patch/1755/
State Superseded
Headers show

Comments

Ahmed S. Darwish - June 28, 2013, 8:10 p.m.
# HG changeset patch
# User Ahmed S. Darwish <darwish.07@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/

exporting patch:
<fdopen>
Matt Mackall - June 28, 2013, 8:17 p.m.
On Fri, 2013-06-28 at 22:10 +0200, Ahmed S. Darwish wrote:
> # HG changeset patch
> # User Ahmed S. Darwish <darwish.07@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')),

Inconsistent. You should probably just copy the docstring from the
export command which is already translated and has proper
capitalization.

> +    ('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

Sorry, if/else trinary expression doesn't exist in our world. Nor does
the pseudo-trinary and/or. Please just use an if statement.

>      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>

Patch

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', '',