Patchwork D8762: revert: fix interactive reverting of end-of-file newline changes

login
register
mail settings
Submitter phabricator
Date July 17, 2020, 10:25 a.m.
Message ID <differential-rev-PHID-DREV-qbnwtbnmhginwo2sjgyw-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/46788/
State Superseded
Headers show

Comments

phabricator - July 17, 2020, 10:25 a.m.
rdamazio created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  The chunk reversal used by `revert -i` in Curses mode was not taking this case
  into account.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D8762

AFFECTED FILES
  mercurial/crecord.py
  tests/test-revert-interactive-curses.t

CHANGE DETAILS




To: rdamazio, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/tests/test-revert-interactive-curses.t b/tests/test-revert-interactive-curses.t
new file mode 100644
--- /dev/null
+++ b/tests/test-revert-interactive-curses.t
@@ -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)
+
diff --git a/mercurial/crecord.py b/mercurial/crecord.py
--- a/mercurial/crecord.py
+++ b/mercurial/crecord.py
@@ -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