diff --git a/mercurial/crecord.py b/mercurial/crecord.py --- a/mercurial/crecord.py +++ b/mercurial/crecord.py @@ -20,6 +20,7 @@ open, ) from . import ( + diffhelper, encoding, error, patch as patchmod, @@ -416,7 +417,7 @@ contextlen = ( len(self.before) + len(self.after) + removedconvertedtocontext ) - if self.after and self.after[-1] == b'\\ No newline at end of file\n': + if self.after and self.after[-1] == diffhelper.MISSING_NEWLINE_MARKER: contextlen -= 1 fromlen = contextlen + self.removed tolen = contextlen + self.added @@ -503,7 +504,7 @@ noeol = False for line in self.changedlines: text = line.linetext - if line.linetext == b'\\ No newline at end of file\n': + if line.linetext == diffhelper.MISSING_NEWLINE_MARKER: noeol = True break if line.applied: diff --git a/mercurial/diffhelper.py b/mercurial/diffhelper.py --- a/mercurial/diffhelper.py +++ b/mercurial/diffhelper.py @@ -14,6 +14,8 @@ pycompat, ) +MISSING_NEWLINE_MARKER = b'\\ No newline at end of file\n' + def addlines(fp, hunk, lena, lenb, a, b): """Read lines from fp into the hunk @@ -32,7 +34,7 @@ s = fp.readline() if not s: raise error.ParseError(_(b'incomplete hunk')) - if s == b"\\ No newline at end of file\n": + if s == MISSING_NEWLINE_MARKER: fixnewline(hunk, a, b) continue if s == b'\n' or s == b'\r\n': diff --git a/mercurial/mdiff.py b/mercurial/mdiff.py --- a/mercurial/mdiff.py +++ b/mercurial/mdiff.py @@ -17,6 +17,7 @@ setattr, ) from . import ( + diffhelper, encoding, error, policy, @@ -25,8 +26,6 @@ ) from .utils import dateutil -_missing_newline_marker = b"\\ No newline at end of file\n" - bdiff = policy.importmod('bdiff') mpatch = policy.importmod('mpatch') @@ -309,7 +308,7 @@ hunklines = [b"@@ -0,0 +1,%d @@\n" % size] + [b"+" + e for e in b] if without_newline: hunklines[-1] += b'\n' - hunklines.append(_missing_newline_marker) + hunklines.append(diffhelper.MISSING_NEWLINE_MARKER) hunks = ((hunkrange, hunklines),) elif not b: without_newline = not a.endswith(b'\n') @@ -325,7 +324,7 @@ hunklines = [b"@@ -1,%d +0,0 @@\n" % size] + [b"-" + e for e in a] if without_newline: hunklines[-1] += b'\n' - hunklines.append(_missing_newline_marker) + hunklines.append(diffhelper.MISSING_NEWLINE_MARKER) hunks = ((hunkrange, hunklines),) else: hunks = _unidiff(a, b, opts=opts) @@ -418,13 +417,13 @@ if hunklines[i].startswith(b' '): skip = True hunklines[i] += b'\n' - hunklines.insert(i + 1, _missing_newline_marker) + hunklines.insert(i + 1, diffhelper.MISSING_NEWLINE_MARKER) break if not skip and not t2.endswith(b'\n') and bstart + blen == len(l2) + 1: for i in pycompat.xrange(len(hunklines) - 1, -1, -1): if hunklines[i].startswith(b'+'): hunklines[i] += b'\n' - hunklines.insert(i + 1, _missing_newline_marker) + hunklines.insert(i + 1, diffhelper.MISSING_NEWLINE_MARKER) break yield hunkrange, hunklines diff --git a/mercurial/patch.py b/mercurial/patch.py --- a/mercurial/patch.py +++ b/mercurial/patch.py @@ -785,7 +785,7 @@ for l in x.hunk: lines.append(l) if l[-1:] != b'\n': - lines.append(b"\n\\ No newline at end of file\n") + lines.append(b'\n' + diffhelper.MISSING_NEWLINE_MARKER) self.backend.writerej(self.fname, len(self.rej), self.hunks, lines) def apply(self, h): @@ -1069,7 +1069,7 @@ def write(self, fp): delta = len(self.before) + len(self.after) - if self.after and self.after[-1] == b'\\ No newline at end of file\n': + if self.after and self.after[-1] == diffhelper.MISSING_NEWLINE_MARKER: delta -= 1 fromlen = delta + self.removed tolen = delta + self.added