[PATCH] commands add --inverse to diff

Yannick Gingras ygingras at ygingras.net
Fri Oct 30 10:05:54 CDT 2009


I know that most of the time, one can reverse a diff by swapping the
revisions passed with -r but it happens that if you use the global -R,
and diff against the tip of the current repo, you can't swap the
revisions.  On use-case for that is reviewing changes from a bundle
before unbundling.  One could also pipe the output of `hg diff` to a
command line filter that reverses the diff but that would remove the
benefit from color diffs.  Therefore, I think that having an option in
`hg diff` to reverse a diff is a good thing.

However, I'm not sure about the option flag.  GNU patch uses
-R,--reverse but -R is already used as a global option and --reverse
would make --rev ambiguous.  I opted for --inverse but -x,--swap would
also make sense.  In any case, here is the patch:

# HG changeset patch
# User Yannick Gingras <ygingras at ygingras.net>
# Date 1256913951 14400
# Node ID 0d5dfba042a02bc666621ab515fdf49e46215d63
# Parent  96c803e9018f93493afd90f695178b9495703970
commands: add --inverse to diff

diff -r 96c803e9018f -r 0d5dfba042a0 mercurial/commands.py
--- a/mercurial/commands.py	Wed Oct 28 13:17:03 2009 -0500
+++ b/mercurial/commands.py	Fri Oct 30 10:45:51 2009 -0400
@@ -1086,6 +1086,7 @@
     revs = opts.get('rev')
     change = opts.get('change')
     stat = opts.get('stat')
+    inv = opts.get('inverse')
 
     if revs and change:
         msg = _('cannot specify --rev and --change at the same time')
@@ -1096,6 +1097,9 @@
     else:
         node1, node2 = cmdutil.revpair(repo, revs)
 
+    if inv:
+        node1, node2 = node2, node1
+
     if stat:
         opts['unified'] = '0'
     diffopts = patch.diffopts(ui, opts)
@@ -3270,6 +3274,7 @@
 
 diffopts2 = [
     ('p', 'show-function', None, _('show which function each change is in')),
+    ('', 'inverse', None, _('produce a diff that undoes the changes')),
     ('w', 'ignore-all-space', None,
      _('ignore white space when comparing lines')),
     ('b', 'ignore-space-change', None,
diff -r 96c803e9018f -r 0d5dfba042a0 tests/test-diff-inverse
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-diff-inverse	Fri Oct 30 10:45:51 2009 -0400
@@ -0,0 +1,18 @@
+#!/bin/sh
+
+hg init
+cat > a <<EOF
+a
+b
+c
+EOF
+hg ci -Am adda
+
+cat > a <<EOF
+d
+e
+f
+EOF
+hg ci -m moda
+
+hg diff --inverse -r0 -r1
diff -r 96c803e9018f -r 0d5dfba042a0 tests/test-diff-inverse.out
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-diff-inverse.out	Fri Oct 30 10:45:51 2009 -0400
@@ -0,0 +1,11 @@
+adding a
+diff -r 2855cdcfcbb7 -r 8e1805a3cf6e a
+--- a/a	Thu Jan 01 00:00:00 1970 +0000
++++ b/a	Thu Jan 01 00:00:00 1970 +0000
+@@ -1,3 +1,3 @@
+-d
+-e
+-f
++a
++b
++c


-- 
Yannick Gingras
http://ygingras.net
http://confoo.ca -- track coordinator
http://montrealpython.org -- lead organizer
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part.
Url : http://selenic.com/pipermail/mercurial-devel/attachments/20091030/9285a325/attachment.pgp 


More information about the Mercurial-devel mailing list