changeset 46087:64292addbe67

diff: add --from and --to flags as clearer alternative to -r -r I think it was mistake to let the `-r` flag accept two revisions in `hg diff` in 98633e60067c (Support for 0, 1, or 2 diff revs, 2005-05-07). The command clearly acts on two revisions and having a single flag to indicate which those are is unclear. It got worse when it started accepting revsets as input. This patch introduces `--from` and `--to` flags, each taking a single revision and each defaulting to the working copy. That means that `hg
author Martin von Zweigbergk <martinvonz@google.com>
date Wed, 09 Dec 2020 18:31:19 -0800
parents ac9de799d390
children 8837498ae6e0
files mercurial/commands.py relnotes/next tests/test-completion.t tests/test-diff-change.t tests/test-help.t
diffstat 5 files changed, 72 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/commands.py	Thu Dec 03 17:18:49 2020 +0530
+++ b/mercurial/commands.py	Wed Dec 09 18:31:19 2020 -0800
@@ -2456,6 +2456,8 @@
     b'diff',
     [
         (b'r', b'rev', [], _(b'revision'), _(b'REV')),
+        (b'', b'from', b'', _(b'revision to diff from'), _(b'REV')),
+        (b'', b'to', b'', _(b'revision to diff to'), _(b'REV')),
         (b'c', b'change', b'', _(b'change made by revision'), _(b'REV')),
     ]
     + diffopts
@@ -2530,13 +2532,23 @@
     opts = pycompat.byteskwargs(opts)
     revs = opts.get(b'rev')
     change = opts.get(b'change')
+    from_rev = opts.get(b'from')
+    to_rev = opts.get(b'to')
     stat = opts.get(b'stat')
     reverse = opts.get(b'reverse')
 
+    cmdutil.check_incompatible_arguments(opts, b'from', [b'rev', b'change'])
+    cmdutil.check_incompatible_arguments(opts, b'to', [b'rev', b'change'])
     if change:
         repo = scmutil.unhidehashlikerevs(repo, [change], b'nowarn')
         ctx2 = scmutil.revsingle(repo, change, None)
         ctx1 = ctx2.p1()
+    elif from_rev or to_rev:
+        repo = scmutil.unhidehashlikerevs(
+            repo, [from_rev] + [to_rev], b'nowarn'
+        )
+        ctx1 = scmutil.revsingle(repo, from_rev, None)
+        ctx2 = scmutil.revsingle(repo, to_rev, None)
     else:
         repo = scmutil.unhidehashlikerevs(repo, revs, b'nowarn')
         ctx1, ctx2 = scmutil.revpair(repo, revs)
--- a/relnotes/next	Thu Dec 03 17:18:49 2020 +0530
+++ b/relnotes/next	Wed Dec 09 18:31:19 2020 -0800
@@ -20,6 +20,9 @@
  * `hg strip`, from the strip extension, is now a core command, `hg
    debugstrip`. The extension remains for compatibility.
 
+ * `hg diff` now supports `--from <rev>` and `--to <rev>` arguments as
+   clearer alternatives to `-r <revs>`.
+
  * The memory footprint per changeset during pull/unbundle
    operations has been further reduced.
 
--- a/tests/test-completion.t	Thu Dec 03 17:18:49 2020 +0530
+++ b/tests/test-completion.t	Wed Dec 09 18:31:19 2020 -0800
@@ -333,7 +333,7 @@
   debugwhyunstable: 
   debugwireargs: three, four, five, ssh, remotecmd, insecure
   debugwireproto: localssh, peer, noreadstderr, nologhandshake, ssh, remotecmd, insecure
-  diff: rev, change, text, git, binary, nodates, noprefix, show-function, reverse, ignore-all-space, ignore-space-change, ignore-blank-lines, ignore-space-at-eol, unified, stat, root, include, exclude, subrepos
+  diff: rev, from, to, change, text, git, binary, nodates, noprefix, show-function, reverse, ignore-all-space, ignore-space-change, ignore-blank-lines, ignore-space-at-eol, unified, stat, root, include, exclude, subrepos
   export: bookmark, output, switch-parent, rev, text, git, binary, nodates, template
   files: rev, print0, include, exclude, template, subrepos
   forget: interactive, include, exclude, dry-run
--- a/tests/test-diff-change.t	Thu Dec 03 17:18:49 2020 +0530
+++ b/tests/test-diff-change.t	Wed Dec 09 18:31:19 2020 -0800
@@ -1,4 +1,4 @@
-Testing diff --change
+Testing diff --change, --from, --to
 
   $ hg init a
   $ cd a
@@ -29,6 +29,59 @@
   -first
   +second
 
+Test --from and --to
+
+  $ hg diff --from . --rev .
+  abort: cannot specify both --from and --rev
+  [10]
+  $ hg diff --to . --rev .
+  abort: cannot specify both --to and --rev
+  [10]
+  $ hg diff --from . --change .
+  abort: cannot specify both --from and --change
+  [10]
+  $ hg diff --to . --change .
+  abort: cannot specify both --to and --change
+  [10]
+  $ echo dirty > file.txt
+  $ hg diff --from .
+  diff -r bf5ff72eb7e0 file.txt
+  --- a/file.txt	Thu Jan 01 00:00:00 1970 +0000
+  +++ b/file.txt	Thu Jan 01 00:00:00 1970 +0000
+  @@ -1,1 +1,1 @@
+  -third
+  +dirty
+  $ hg diff --from . --reverse
+  diff -r bf5ff72eb7e0 file.txt
+  --- a/file.txt	Thu Jan 01 00:00:00 1970 +0000
+  +++ b/file.txt	Thu Jan 01 00:00:00 1970 +0000
+  @@ -1,1 +1,1 @@
+  -dirty
+  +third
+  $ hg diff --to .
+  diff -r bf5ff72eb7e0 file.txt
+  --- a/file.txt	Thu Jan 01 00:00:00 1970 +0000
+  +++ b/file.txt	Thu Jan 01 00:00:00 1970 +0000
+  @@ -1,1 +1,1 @@
+  -dirty
+  +third
+  $ hg diff --from 0 --to 2
+  diff -r 4bb65dda5db4 -r bf5ff72eb7e0 file.txt
+  --- a/file.txt	Thu Jan 01 00:00:00 1970 +0000
+  +++ b/file.txt	Thu Jan 01 00:00:00 1970 +0000
+  @@ -1,1 +1,1 @@
+  -first
+  +third
+  $ hg diff --from 2 --to 0
+  diff -r bf5ff72eb7e0 -r 4bb65dda5db4 file.txt
+  --- a/file.txt	Thu Jan 01 00:00:00 1970 +0000
+  +++ b/file.txt	Thu Jan 01 00:00:00 1970 +0000
+  @@ -1,1 +1,1 @@
+  -third
+  +first
+  $ hg co -C .
+  1 files updated, 0 files merged, 0 files removed, 0 files unresolved
+
   $ cd ..
 
 Test dumb revspecs: top-level "x:y", "x:", ":y" and ":" ranges should be handled
--- a/tests/test-help.t	Thu Dec 03 17:18:49 2020 +0530
+++ b/tests/test-help.t	Wed Dec 09 18:31:19 2020 -0800
@@ -662,6 +662,8 @@
   options ([+] can be repeated):
   
    -r --rev REV [+]         revision
+      --from REV            revision to diff from
+      --to REV              revision to diff to
    -c --change REV          change made by revision
    -a --text                treat all files as text
    -g --git                 use git extended diff format