diff mercurial/crecord.py @ 27156:55fa7c3900ae

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.
author Laurent Charignon <lcharignon@fb.com>
date Mon, 30 Nov 2015 16:37:42 -0800
parents 8d3c5797a175
children dcdf0a52ad36
line wrap: on
line diff
--- 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