fold: require --from flag for folding revisions to working copy
authorMartin von Zweigbergk <martinvonz@google.com>
Thu, 12 Jan 2017 13:47:49 -0800
changeset 1782 a046e78c3290
parent 1781 39ef492603c6
child 1783 9480e13ee59a
fold: require --from flag for folding revisions to working copy It's very easy to think that "hg fold 4::6" will fold exactly those revisions. In reality, it will fold those *and* any revisions between them and the working copy. To prevent users from making that mistake, require the use of a new --from flag for folding revisions from the given set to the working copy. With this change, I'm sure some users will be surprised that the command can not be run without either --from or --exact, but at least the consequences will be smaller (the command simply aborts and the user can try again).
hgext/evolve.py
tests/test-evolve.t
tests/test-tutorial.t
tests/test-userguide.t
--- a/hgext/evolve.py	Tue Dec 13 10:28:09 2016 -0800
+++ b/hgext/evolve.py	Thu Jan 12 13:47:49 2017 -0800
@@ -3068,16 +3068,17 @@
 
 @command('^fold|squash',
     [('r', 'rev', [], _("revision to fold")),
-     ('', 'exact', None, _("only fold specified revisions"))
+     ('', 'exact', None, _("only fold specified revisions")),
+     ('', 'from', None, _("fold revisions linearly to working copy parent"))
     ] + commitopts + commitopts2,
     _('hg fold [OPTION]... [-r] REV'))
 def fold(ui, repo, *revs, **opts):
     """fold multiple revisions into a single one
 
-    By default, folds all the revisions linearly between the given revisions
+    With --from, folds all the revisions linearly between the given revisions
     and the parent of the working directory.
 
-    Use --exact for folding only the specified revisions while ignoring the
+    With --exact, folds only the specified revisions while ignoring the
     parent of the working directory. In this case, the given revisions must
     form a linear unbroken chain.
 
@@ -3087,18 +3088,18 @@
 
      - Fold the current revision with its parent::
 
-         hg fold .^
+         hg fold --from .^
 
      - Fold all draft revisions with working directory parent::
 
-         hg fold 'draft()'
+         hg fold --from 'draft()'
 
        See :hg:`help phases` for more about draft revisions and
        :hg:`help revsets` for more about the `draft()` keyword
 
      - Fold revisions between 3 and 6 with the working directory parent::
 
-         hg fold 3::6
+         hg fold --from 3::6
 
      - Fold revisions 3 and 4:
 
@@ -3115,7 +3116,9 @@
 
     revs = scmutil.revrange(repo, revs)
 
-    if not opts['exact']:
+    if opts['from'] and opts['exact']:
+        raise error.Abort(_('cannot use both --from and --exact'))
+    elif opts['from']:
         # Try to extend given revision starting from the working directory
         extrevs = repo.revs('(%ld::.) or (.::%ld)', revs, revs)
         discardedrevs = [r for r in revs if r not in extrevs]
@@ -3124,6 +3127,11 @@
                                hint=_("given revisions are unrelated to parent "
                                       "of working directory"))
         revs = extrevs
+    elif opts['exact']:
+        # Nothing to do; "revs" is already set correctly
+        pass
+    else:
+        raise error.Abort(_('must specify either --from or --exact'))
 
     if len(revs) == 1:
         ui.write_err(_('single revision specified, nothing to fold\n'))
--- a/tests/test-evolve.t	Tue Dec 13 10:28:09 2016 -0800
+++ b/tests/test-evolve.t	Thu Jan 12 13:47:49 2017 -0800
@@ -679,7 +679,16 @@
   $ hg fold
   abort: no revisions specified
   [255]
+  $ hg fold --from
+  abort: no revisions specified
+  [255]
   $ hg fold .
+  abort: must specify either --from or --exact
+  [255]
+  $ hg fold --from . --exact
+  abort: cannot use both --from and --exact
+  [255]
+  $ hg fold --from .
   single revision specified, nothing to fold
   [1]
   $ hg fold 0::10 --rev 1 --exact
@@ -688,18 +697,18 @@
   $ hg fold -r 4 -r 6 --exact
   abort: cannot fold non-linear revisions (multiple roots given)
   [255]
-  $ hg fold 10 1
+  $ hg fold --from 10 1
   abort: cannot fold non-linear revisions
   (given revisions are unrelated to parent of working directory)
   [255]
   $ hg phase --public 0
-  $ hg fold -r 0
+  $ hg fold --from -r 0
   abort: cannot fold public revisions
   [255]
-  $ hg fold -r 5
+  $ hg fold --from -r 5
   3 changesets folded
   1 files updated, 0 files merged, 0 files removed, 0 files unresolved
-  $ hg fold 6 # want to run hg fold 6
+  $ hg fold --from 6 # want to run hg fold 6
   abort: hidden revision '6'!
   (use --hidden to access hidden revisions)
   [255]
@@ -806,7 +815,7 @@
 Test fold with commit messages
 
   $ cd ../work
-  $ hg fold .^ --message "Folding with custom commit message"
+  $ hg fold --from .^ --message "Folding with custom commit message"
   2 changesets folded
   1 files updated, 0 files merged, 0 files removed, 0 files unresolved
   $ glog
@@ -825,7 +834,7 @@
   >                   commit message
   > EOF
 
-  $ hg fold .^ --logfile commit-message
+  $ hg fold --from .^ --logfile commit-message
   2 changesets folded
   1 files updated, 0 files merged, 0 files removed, 0 files unresolved
   $ hg qlog
--- a/tests/test-tutorial.t	Tue Dec 13 10:28:09 2016 -0800
+++ b/tests/test-tutorial.t	Thu Jan 12 13:47:49 2017 -0800
@@ -480,17 +480,18 @@
   
   fold multiple revisions into a single one
   
-      By default, folds all the revisions linearly between the given revisions
+      With --from, folds all the revisions linearly between the given revisions
       and the parent of the working directory.
   
-      Use --exact for folding only the specified revisions while ignoring the
-      parent of the working directory. In this case, the given revisions must
-      form a linear unbroken chain.
+      With --exact, folds only the specified revisions while ignoring the parent
+      of the working directory. In this case, the given revisions must form a
+      linear unbroken chain.
   
   options ([+] can be repeated):
   
    -r --rev VALUE [+] revision to fold
       --exact         only fold specified revisions
+      --from          fold revisions linearly to working copy parent
    -m --message TEXT  use text as commit message
    -l --logfile FILE  read commit message from file
    -d --date DATE     record the specified date as commit date
--- a/tests/test-userguide.t	Tue Dec 13 10:28:09 2016 -0800
+++ b/tests/test-userguide.t	Thu Jan 12 13:47:49 2017 -0800
@@ -109,7 +109,7 @@
   7:05e61aab8294  step 1
   8:be6d5bc8e4cc  step 2
   9:35f432d9f7c1  step 3
-  $ hg fold -d '0 0' -m 'fix bug 64' -r 7::
+  $ hg fold -d '0 0' -m 'fix bug 64' --from -r 7::
   3 changesets folded
   1 files updated, 0 files merged, 0 files removed, 0 files unresolved
   $ hg --hidden shortlog -G -r 6::