changeset 45153:8b6a446508c2

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
author Rodrigo Damazio Bovendorp <rdamazio@google.com>
date Fri, 17 Jul 2020 02:31:42 -0700
parents b3b0cd8b9366
children 10f48720ef95
files mercurial/crecord.py tests/test-revert-interactive-curses.t
diffstat 2 files changed, 62 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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)
+