Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/patch.py @ 41548:47c92f8ed128
patch: properly escape \ in string literals
Python 3.8 will emit a SyntaxWarning for str/bytes with invalid
escapes. This commit addresses 4 occurrences where we had a bare
\ in a str/bytes.
Differential Revision: https://phab.mercurial-scm.org/D5818
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Mon, 04 Feb 2019 08:59:11 -0800 |
parents | bd3f03d8cc9f |
children | e834f6f6f221 |
comparison
equal
deleted
inserted
replaced
41547:0f64091cc851 | 41548:47c92f8ed128 |
---|---|
751 lines = ["--- %s\n+++ %s\n" % (base, base)] | 751 lines = ["--- %s\n+++ %s\n" % (base, base)] |
752 for x in self.rej: | 752 for x in self.rej: |
753 for l in x.hunk: | 753 for l in x.hunk: |
754 lines.append(l) | 754 lines.append(l) |
755 if l[-1:] != '\n': | 755 if l[-1:] != '\n': |
756 lines.append("\n\ No newline at end of file\n") | 756 lines.append("\n\\ No newline at end of file\n") |
757 self.backend.writerej(self.fname, len(self.rej), self.hunks, lines) | 757 self.backend.writerej(self.fname, len(self.rej), self.hunks, lines) |
758 | 758 |
759 def apply(self, h): | 759 def apply(self, h): |
760 if not h.complete(): | 760 if not h.complete(): |
761 raise PatchError(_("bad hunk #%d %s (%d %d %d %d)") % | 761 raise PatchError(_("bad hunk #%d %s (%d %d %d %d)") % |
1303 (self.number, x)) | 1303 (self.number, x)) |
1304 self.a.append(u) | 1304 self.a.append(u) |
1305 self.hunk.append(u) | 1305 self.hunk.append(u) |
1306 | 1306 |
1307 l = lr.readline() | 1307 l = lr.readline() |
1308 if l.startswith('\ '): | 1308 if l.startswith(br'\ '): |
1309 s = self.a[-1][:-1] | 1309 s = self.a[-1][:-1] |
1310 self.a[-1] = s | 1310 self.a[-1] = s |
1311 self.hunk[-1] = s | 1311 self.hunk[-1] = s |
1312 l = lr.readline() | 1312 l = lr.readline() |
1313 m = contextdesc.match(l) | 1313 m = contextdesc.match(l) |
1321 if self.startb: | 1321 if self.startb: |
1322 self.lenb += 1 | 1322 self.lenb += 1 |
1323 hunki = 1 | 1323 hunki = 1 |
1324 for x in pycompat.xrange(self.lenb): | 1324 for x in pycompat.xrange(self.lenb): |
1325 l = lr.readline() | 1325 l = lr.readline() |
1326 if l.startswith('\ '): | 1326 if l.startswith(br'\ '): |
1327 # XXX: the only way to hit this is with an invalid line range. | 1327 # XXX: the only way to hit this is with an invalid line range. |
1328 # The no-eol marker is not counted in the line range, but I | 1328 # The no-eol marker is not counted in the line range, but I |
1329 # guess there are diff(1) out there which behave differently. | 1329 # guess there are diff(1) out there which behave differently. |
1330 s = self.b[-1][:-1] | 1330 s = self.b[-1][:-1] |
1331 self.b[-1] = s | 1331 self.b[-1] = s |
1378 self.hunk[0] = self.desc | 1378 self.hunk[0] = self.desc |
1379 self._fixnewline(lr) | 1379 self._fixnewline(lr) |
1380 | 1380 |
1381 def _fixnewline(self, lr): | 1381 def _fixnewline(self, lr): |
1382 l = lr.readline() | 1382 l = lr.readline() |
1383 if l.startswith('\ '): | 1383 if l.startswith(br'\ '): |
1384 diffhelper.fixnewline(self.hunk, self.a, self.b) | 1384 diffhelper.fixnewline(self.hunk, self.a, self.b) |
1385 else: | 1385 else: |
1386 lr.push(l) | 1386 lr.push(l) |
1387 | 1387 |
1388 def complete(self): | 1388 def complete(self): |