Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/patch.py @ 13100:66d7a1250c9b stable
patch: write .rej files without rewriting EOLs
Do not pass reject file content to patchfile.writelines() to:
- Avoid line endings transformations
- Avoid polluting overriding implementations with unrelated data. They should
override write_rej() to deal or ignore reject files properly.
Bug report, analysis and original patch and test by
Shun-ichi GOTO <shunichi.goto@gmail.com>
author | Patrick Mezard <pmezard@gmail.com> |
---|---|
date | Fri, 03 Dec 2010 11:40:30 +0900 |
parents | 61f48581d8ef |
children | 5dac0d04b838 |
comparison
equal
deleted
inserted
replaced
13073:acebcefa86d4 | 13100:66d7a1250c9b |
---|---|
483 def hashlines(self): | 483 def hashlines(self): |
484 self.hash = {} | 484 self.hash = {} |
485 for x, s in enumerate(self.lines): | 485 for x, s in enumerate(self.lines): |
486 self.hash.setdefault(s, []).append(x) | 486 self.hash.setdefault(s, []).append(x) |
487 | 487 |
488 def makerejlines(self, fname): | |
489 base = os.path.basename(fname) | |
490 yield "--- %s\n+++ %s\n" % (base, base) | |
491 for x in self.rej: | |
492 for l in x.hunk: | |
493 yield l | |
494 if l[-1] != '\n': | |
495 yield "\n\ No newline at end of file\n" | |
496 | |
488 def write_rej(self): | 497 def write_rej(self): |
489 # our rejects are a little different from patch(1). This always | 498 # our rejects are a little different from patch(1). This always |
490 # creates rejects in the same form as the original patch. A file | 499 # creates rejects in the same form as the original patch. A file |
491 # header is inserted so that you can run the reject through patch again | 500 # header is inserted so that you can run the reject through patch again |
492 # without having to type the filename. | 501 # without having to type the filename. |
497 fname = self.fname + ".rej" | 506 fname = self.fname + ".rej" |
498 self.ui.warn( | 507 self.ui.warn( |
499 _("%d out of %d hunks FAILED -- saving rejects to file %s\n") % | 508 _("%d out of %d hunks FAILED -- saving rejects to file %s\n") % |
500 (len(self.rej), self.hunks, fname)) | 509 (len(self.rej), self.hunks, fname)) |
501 | 510 |
502 def rejlines(): | 511 fp = self.opener(fname, 'w') |
503 base = os.path.basename(self.fname) | 512 fp.writelines(self.makerejlines(self.fname)) |
504 yield "--- %s\n+++ %s\n" % (base, base) | 513 fp.close() |
505 for x in self.rej: | |
506 for l in x.hunk: | |
507 yield l | |
508 if l[-1] != '\n': | |
509 yield "\n\ No newline at end of file\n" | |
510 | |
511 self.writelines(fname, rejlines()) | |
512 | 514 |
513 def apply(self, h): | 515 def apply(self, h): |
514 if not h.complete(): | 516 if not h.complete(): |
515 raise PatchError(_("bad hunk #%d %s (%d %d %d %d)") % | 517 raise PatchError(_("bad hunk #%d %s (%d %d %d %d)") % |
516 (h.number, h.desc, len(h.a), h.lena, len(h.b), | 518 (h.number, h.desc, len(h.a), h.lena, len(h.b), |