Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/patch.py @ 15510:5414b56cfad6
patch: simplify hunk extents parsing
Do not capture unwanted groups in regexps
author | Patrick Mezard <pmezard@gmail.com> |
---|---|
date | Mon, 14 Nov 2011 18:16:01 +0100 |
parents | 2b1ec74c961f |
children | a84698badf0b |
comparison
equal
deleted
inserted
replaced
15504:7ee7b7426aad | 15510:5414b56cfad6 |
---|---|
564 | 564 |
565 def close(self): | 565 def close(self): |
566 return self.changed | self.removed | 566 return self.changed | self.removed |
567 | 567 |
568 # @@ -start,len +start,len @@ or @@ -start +start @@ if len is 1 | 568 # @@ -start,len +start,len @@ or @@ -start +start @@ if len is 1 |
569 unidesc = re.compile('@@ -(\d+)(,(\d+))? \+(\d+)(,(\d+))? @@') | 569 unidesc = re.compile('@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@') |
570 contextdesc = re.compile('(---|\*\*\*) (\d+)(,(\d+))? (---|\*\*\*)') | 570 contextdesc = re.compile('(?:---|\*\*\*) (\d+)(?:,(\d+))? (?:---|\*\*\*)') |
571 eolmodes = ['strict', 'crlf', 'lf', 'auto'] | 571 eolmodes = ['strict', 'crlf', 'lf', 'auto'] |
572 | 572 |
573 class patchfile(object): | 573 class patchfile(object): |
574 def __init__(self, ui, gp, backend, store, eolmode='strict'): | 574 def __init__(self, ui, gp, backend, store, eolmode='strict'): |
575 self.fname = gp.path | 575 self.fname = gp.path |
828 | 828 |
829 def read_unified_hunk(self, lr): | 829 def read_unified_hunk(self, lr): |
830 m = unidesc.match(self.desc) | 830 m = unidesc.match(self.desc) |
831 if not m: | 831 if not m: |
832 raise PatchError(_("bad hunk #%d") % self.number) | 832 raise PatchError(_("bad hunk #%d") % self.number) |
833 self.starta, foo, self.lena, self.startb, foo2, self.lenb = m.groups() | 833 self.starta, self.lena, self.startb, self.lenb = m.groups() |
834 if self.lena is None: | 834 if self.lena is None: |
835 self.lena = 1 | 835 self.lena = 1 |
836 else: | 836 else: |
837 self.lena = int(self.lena) | 837 self.lena = int(self.lena) |
838 if self.lenb is None: | 838 if self.lenb is None: |
855 def read_context_hunk(self, lr): | 855 def read_context_hunk(self, lr): |
856 self.desc = lr.readline() | 856 self.desc = lr.readline() |
857 m = contextdesc.match(self.desc) | 857 m = contextdesc.match(self.desc) |
858 if not m: | 858 if not m: |
859 raise PatchError(_("bad hunk #%d") % self.number) | 859 raise PatchError(_("bad hunk #%d") % self.number) |
860 foo, self.starta, foo2, aend, foo3 = m.groups() | 860 self.starta, aend = m.groups() |
861 self.starta = int(self.starta) | 861 self.starta = int(self.starta) |
862 if aend is None: | 862 if aend is None: |
863 aend = self.starta | 863 aend = self.starta |
864 self.lena = int(aend) - self.starta | 864 self.lena = int(aend) - self.starta |
865 if self.starta: | 865 if self.starta: |
888 self.hunk[-1] = s | 888 self.hunk[-1] = s |
889 l = lr.readline() | 889 l = lr.readline() |
890 m = contextdesc.match(l) | 890 m = contextdesc.match(l) |
891 if not m: | 891 if not m: |
892 raise PatchError(_("bad hunk #%d") % self.number) | 892 raise PatchError(_("bad hunk #%d") % self.number) |
893 foo, self.startb, foo2, bend, foo3 = m.groups() | 893 self.startb, bend = m.groups() |
894 self.startb = int(self.startb) | 894 self.startb = int(self.startb) |
895 if bend is None: | 895 if bend is None: |
896 bend = self.startb | 896 bend = self.startb |
897 self.lenb = int(bend) - self.startb | 897 self.lenb = int(bend) - self.startb |
898 if self.startb: | 898 if self.startb: |