revert: fix interactive reverting of end-of-file newline changes
authorRodrigo Damazio Bovendorp <rdamazio@google.com>
Fri, 17 Jul 2020 02:31:42 -0700
changeset 45153 8b6a446508c2
parent 45152 b3b0cd8b9366
child 45154 10f48720ef95
revert: fix interactive reverting of end-of-file newline changes The chunk reversal used by `revert -i` in Curses mode was not taking this case into account. Differential Revision: https://phab.mercurial-scm.org/D8762
mercurial/crecord.py
tests/test-revert-interactive-curses.t
--- a/mercurial/crecord.py	Thu Jul 16 14:16:53 2020 -0400
+++ b/mercurial/crecord.py	Fri Jul 17 02:31:42 2020 -0700
@@ -500,8 +500,12 @@
         """
         dels = []
         adds = []
+        noeol = False
         for line in self.changedlines:
             text = line.linetext
+            if line.linetext == b'\\ No newline at end of file\n':
+                noeol = True
+                break
             if line.applied:
                 if text.startswith(b'+'):
                     dels.append(text[1:])
@@ -511,6 +515,9 @@
                 dels.append(text[1:])
                 adds.append(text[1:])
         hunk = [b'-%s' % l for l in dels] + [b'+%s' % l for l in adds]
+        if noeol and hunk:
+            # Remove the newline from the end of the hunk.
+            hunk[-1] = hunk[-1][:-1]
         h = self._hunk
         return patchmod.recordhunk(
             h.header, h.toline, h.fromline, h.proc, h.before, hunk, h.after
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-revert-interactive-curses.t	Fri Jul 17 02:31:42 2020 -0700
@@ -0,0 +1,55 @@
+#require tic
+
+Revert interactive tests with the Curses interface
+
+  $ cat <<EOF >> $HGRCPATH
+  > [ui]
+  > interactive = true
+  > interface = curses
+  > [experimental]
+  > crecordtest = testModeCommands
+  > EOF
+
+TODO: Make a curses version of the other tests from test-revert-interactive.t.
+
+When a line without EOL is selected during "revert -i"
+
+  $ hg init $TESTTMP/revert-i-curses-eol
+  $ cd $TESTTMP/revert-i-curses-eol
+  $ echo 0 > a
+  $ hg ci -qAm 0
+  $ printf 1 >> a
+  $ hg ci -qAm 1
+  $ cat a
+  0
+  1 (no-eol)
+
+  $ cat <<EOF >testModeCommands
+  > c
+  > EOF
+
+  $ hg revert -ir'.^'
+  reverting a
+  $ cat a
+  0
+
+When a selected line is reverted to have no EOL
+
+  $ hg init $TESTTMP/revert-i-curses-eol2
+  $ cd $TESTTMP/revert-i-curses-eol2
+  $ printf 0 > a
+  $ hg ci -qAm 0
+  $ echo 0 > a
+  $ hg ci -qAm 1
+  $ cat a
+  0
+
+  $ cat <<EOF >testModeCommands
+  > c
+  > EOF
+
+  $ hg revert -ir'.^'
+  reverting a
+  $ cat a
+  0 (no-eol)
+