Mercurial > public > mercurial-scm > hg-stable
diff mercurial/patch.py @ 48411:6a454e7053a1
errors: return more detailed errors when failing to parse or apply patch
This patch adds subclasses of `PatchError` so we can distinguish
between failure to parse a patch from failure to apply it. It updates
the callers to raise either `InputError` or `StateError` depending on
which type of error occurred.
Differential Revision: https://phab.mercurial-scm.org/D11824
author | Martin von Zweigbergk <martinvonz@google.com> |
---|---|
date | Fri, 19 Nov 2021 12:57:53 -0800 |
parents | e0d566f3ffce |
children | 290f9c150f70 |
line wrap: on
line diff
--- a/mercurial/patch.py Fri Nov 26 17:22:14 2021 +0100 +++ b/mercurial/patch.py Fri Nov 19 12:57:53 2021 -0800 @@ -55,6 +55,8 @@ ) PatchError = error.PatchError +PatchParseError = error.PatchParseError +PatchApplicationError = error.PatchApplicationError # public functions @@ -553,7 +555,9 @@ if not self.repo.dirstate.get_entry(fname).any_tracked and self.exists( fname ): - raise PatchError(_(b'cannot patch %s: file is not tracked') % fname) + raise PatchApplicationError( + _(b'cannot patch %s: file is not tracked') % fname + ) def setfile(self, fname, data, mode, copysource): self._checkknown(fname) @@ -637,7 +641,9 @@ def _checkknown(self, fname): if fname not in self.ctx: - raise PatchError(_(b'cannot patch %s: file is not tracked') % fname) + raise PatchApplicationError( + _(b'cannot patch %s: file is not tracked') % fname + ) def getfile(self, fname): try: @@ -793,7 +799,7 @@ def apply(self, h): if not h.complete(): - raise PatchError( + raise PatchParseError( _(b"bad hunk #%d %s (%d %d %d %d)") % (h.number, h.desc, len(h.a), h.lena, len(h.b), h.lenb) ) @@ -1388,7 +1394,7 @@ def read_unified_hunk(self, lr): m = unidesc.match(self.desc) if not m: - raise PatchError(_(b"bad hunk #%d") % self.number) + raise PatchParseError(_(b"bad hunk #%d") % self.number) self.starta, self.lena, self.startb, self.lenb = m.groups() if self.lena is None: self.lena = 1 @@ -1405,7 +1411,7 @@ lr, self.hunk, self.lena, self.lenb, self.a, self.b ) except error.ParseError as e: - raise PatchError(_(b"bad hunk #%d: %s") % (self.number, e)) + raise PatchParseError(_(b"bad hunk #%d: %s") % (self.number, e)) # if we hit eof before finishing out the hunk, the last line will # be zero length. Lets try to fix it up. while len(self.hunk[-1]) == 0: @@ -1420,7 +1426,7 @@ self.desc = lr.readline() m = contextdesc.match(self.desc) if not m: - raise PatchError(_(b"bad hunk #%d") % self.number) + raise PatchParseError(_(b"bad hunk #%d") % self.number) self.starta, aend = m.groups() self.starta = int(self.starta) if aend is None: @@ -1440,7 +1446,7 @@ elif l.startswith(b' '): u = b' ' + s else: - raise PatchError( + raise PatchParseError( _(b"bad hunk #%d old text line %d") % (self.number, x) ) self.a.append(u) @@ -1454,7 +1460,7 @@ l = lr.readline() m = contextdesc.match(l) if not m: - raise PatchError(_(b"bad hunk #%d") % self.number) + raise PatchParseError(_(b"bad hunk #%d") % self.number) self.startb, bend = m.groups() self.startb = int(self.startb) if bend is None: @@ -1487,7 +1493,7 @@ lr.push(l) break else: - raise PatchError( + raise PatchParseError( _(b"bad hunk #%d old text line %d") % (self.number, x) ) self.b.append(s) @@ -1601,7 +1607,7 @@ while True: line = getline(lr, self.hunk) if not line: - raise PatchError( + raise PatchParseError( _(b'could not extract "%s" binary data') % self._fname ) if line.startswith(b'literal '): @@ -1622,14 +1628,14 @@ try: dec.append(util.b85decode(line[1:])[:l]) except ValueError as e: - raise PatchError( + raise PatchParseError( _(b'could not decode "%s" binary patch: %s') % (self._fname, stringutil.forcebytestr(e)) ) line = getline(lr, self.hunk) text = zlib.decompress(b''.join(dec)) if len(text) != size: - raise PatchError( + raise PatchParseError( _(b'"%s" length is %d bytes, should be %d') % (self._fname, len(text), size) ) @@ -1847,7 +1853,7 @@ try: p.transitions[state][newstate](p, data) except KeyError: - raise PatchError( + raise PatchParseError( b'unhandled transition: %s -> %s' % (state, newstate) ) state = newstate @@ -1874,7 +1880,7 @@ ('a//b/', 'd/e/c') >>> pathtransform(b'a/b/c', 3, b'') Traceback (most recent call last): - PatchError: unable to strip away 1 of 3 dirs from a/b/c + PatchApplicationError: unable to strip away 1 of 3 dirs from a/b/c """ pathlen = len(path) i = 0 @@ -1884,7 +1890,7 @@ while count > 0: i = path.find(b'/', i) if i == -1: - raise PatchError( + raise PatchApplicationError( _(b"unable to strip away %d of %d dirs from %s") % (count, strip, path) ) @@ -1947,7 +1953,7 @@ elif not nulla: fname = afile else: - raise PatchError(_(b"undefined source and destination files")) + raise PatchParseError(_(b"undefined source and destination files")) gp = patchmeta(fname) if create: @@ -2097,7 +2103,7 @@ gp.copy(), ) if not gitpatches: - raise PatchError( + raise PatchParseError( _(b'failed to synchronize metadata for "%s"') % afile[2:] ) newfile = True @@ -2193,7 +2199,7 @@ out += binchunk[i:offset_end] i += cmd else: - raise PatchError(_(b'unexpected delta opcode 0')) + raise PatchApplicationError(_(b'unexpected delta opcode 0')) return out @@ -2270,7 +2276,7 @@ data, mode = store.getfile(gp.oldpath)[:2] if data is None: # This means that the old path does not exist - raise PatchError( + raise PatchApplicationError( _(b"source file '%s' does not exist") % gp.oldpath ) if gp.mode: @@ -2283,7 +2289,7 @@ if gp.op in (b'ADD', b'RENAME', b'COPY') and backend.exists( gp.path ): - raise PatchError( + raise PatchApplicationError( _( b"cannot create %s: destination " b"already exists" @@ -2365,7 +2371,7 @@ scmutil.marktouched(repo, files, similarity) code = fp.close() if code: - raise PatchError( + raise PatchApplicationError( _(b"patch command failed: %s") % procutil.explainexit(code) ) return fuzz @@ -2397,7 +2403,7 @@ files.update(backend.close()) store.close() if ret < 0: - raise PatchError(_(b'patch failed to apply')) + raise PatchApplicationError(_(b'patch failed to apply')) return ret > 0