commit: add amend mode for commit -i
authorLaurent Charignon <lcharignon@fb.com>
Mon, 30 Nov 2015 16:37:42 -0800
changeset 27156 55fa7c3900ae
parent 27155 8d3c5797a175
child 27157 5f2e4eb08e41
commit: add amend mode for commit -i When I moved crecord into core, I didn't include the toggleAmend function (to switch from commit to amend mode). I did it because it would have made it more difficult to use record and crecord interchangably. This patch reintroduces the amend mode for commit -i as well as two tests to verify the behavior of the function.
mercurial/crecord.py
tests/test-commit-interactive-curses.t
--- a/mercurial/crecord.py	Mon Nov 30 16:35:21 2015 -0800
+++ b/mercurial/crecord.py	Mon Nov 30 16:37:42 2015 -0800
@@ -24,6 +24,7 @@
     encoding,
     error,
     patch as patchmod,
+    util,
 )
 
 # This is required for ncurses to display non-ASCII characters in default user
@@ -1010,7 +1011,7 @@
                         pairname="legend")
             printstring(self.statuswin,
                         " (f)old/unfold; (c)onfirm applied; (q)uit; (?) help "
-                        "| [X]=hunk applied **=folded",
+                        "| [X]=hunk applied **=folded, toggle [a]mend mode",
                         pairname="legend")
         except curses.error:
             pass
@@ -1366,7 +1367,7 @@
                       F : fold / unfold parent item and all of its ancestors
                       m : edit / resume editing the commit message
                       e : edit the currently selected hunk
-                      a : toggle amend mode (hg rev >= 2.2)
+                      a : toggle amend mode (hg rev >= 2.2), only with commit -i
                       c : confirm selected changes
                       r : review/edit and confirm selected changes
                       q : quit without confirming (no changes will be made)
@@ -1433,6 +1434,35 @@
         else:
             return False
 
+    def toggleamend(self, opts, test):
+        """Toggle the amend flag.
+
+        When the amend flag is set, a commit will modify the most recently
+        committed changeset, instead of creating a new changeset.  Otherwise, a
+        new changeset will be created (the normal commit behavior).
+
+        """
+        try:
+            ver = float(util.version()[:3])
+        except ValueError:
+            ver = 1
+        if ver < 2.19:
+            msg = ("The amend option is unavailable with hg versions < 2.2\n\n"
+                   "Press any key to continue.")
+        elif opts.get('amend') is None:
+            opts['amend'] = True
+            msg = ("Amend option is turned on -- commiting the currently "
+                   "selected changes will not create a new changeset, but "
+                   "instead update the most recently committed changeset.\n\n"
+                   "Press any key to continue.")
+        elif opts.get('amend') is True:
+            opts['amend'] = None
+            msg = ("Amend option is turned off -- commiting the currently "
+                   "selected changes will create a new changeset.\n\n"
+                   "Press any key to continue.")
+        if not test:
+            self.confirmationwindow(msg)
+
     def recenterdisplayedarea(self):
         """
         once we scrolled with pg up pg down we can be pointing outside of the
@@ -1570,6 +1600,8 @@
             self.leftarrowshiftevent()
         elif keypressed in ["q"]:
             raise error.Abort(_('user quit'))
+        elif keypressed in ['a']:
+            self.toggleamend(self.opts, test)
         elif keypressed in ["c"]:
             if self.confirmcommit():
                 return True
--- a/tests/test-commit-interactive-curses.t	Mon Nov 30 16:35:21 2015 -0800
+++ b/tests/test-commit-interactive-curses.t	Mon Nov 30 16:37:42 2015 -0800
@@ -71,6 +71,7 @@
 - unfold it
 - go down to second hunk (1 for the first hunk, 1 for the first hunkline, 1 for the second hunk, 1 for the second hunklike)
 - toggle the second hunk
+- toggle on and off the amend mode (to check that it toggles off)
 - edit the hunk and quit the editor immediately with non-zero status
 - commit
 
@@ -88,6 +89,8 @@
   > KEY_DOWN
   > KEY_DOWN
   > TOGGLE
+  > a
+  > a
   > e
   > X
   > EOF
@@ -166,3 +169,23 @@
   $ hg st
   ? testModeCommands
 
+Amend option works
+  $ echo "hello world" > x
+  $ hg diff -c .
+  diff -r a6735021574d -r 2b0e9be4d336 x
+  --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+  +++ b/x	Thu Jan 01 00:00:00 1970 +0000
+  @@ -0,0 +1,1 @@
+  +hello
+  $ cat <<EOF >testModeCommands
+  > a
+  > X
+  > EOF
+  $ hg commit -i  -m "newly added file" -d "0 0"
+  saved backup bundle to $TESTTMP/a/.hg/strip-backup/2b0e9be4d336-28bbe4e2-amend-backup.hg (glob)
+  $ hg diff -c .
+  diff -r a6735021574d -r c1d239d165ae x
+  --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+  +++ b/x	Thu Jan 01 00:00:00 1970 +0000
+  @@ -0,0 +1,1 @@
+  +hello world