def findforward(self): # search for the new text of the hunk already in the file. # the file should already be hashed hlines = self.hunk.newctrl() orig_start = self.hunk.startb if len(hlines) < 6: # only guess about applied hunks when there is some context. return False for fuzzlen in xrange(3): lines = self.hunk.fuzzit(hlines, fuzzlen, False) cand = self.file.findlines(lines[0][1:], orig_start) for l in cand: if diffhelpers.testhunk(lines, self.file.lines, l) == 0: self.file.ui.warn( _("hunk %d already applied at line %d (fuzz %d)\n" % (self.hunk.number, l+1, fuzzlen))) return True return False
def apply(self, h, reverse): if not h.complete(): raise PatchError(_("bad hunk #%d %s (%d %d %d %d)") % (h.number, h.desc, len(h.a), h.lena, len(h.b), h.lenb)) self.hunks += 1 if reverse: h.reverse() if self.exists and h.createfile(): self.ui.warn(_("file %s already exists\n") % self.fname) self.rej.append(h) return -1 if isinstance(h, binhunk): if h.rmfile(): os.unlink(self.fname) else: self.lines[:] = h.new() self.offset += len(h.new()) self.dirty = 1 return 0 # fast case first, no offsets, no fuzz old = h.old() # patch starts counting at 1 unless we are adding the file if h.starta == 0: start = 0 else: start = h.starta + self.offset - 1 orig_start = start if diffhelpers.testhunk(old, self.lines, start) == 0: if h.rmfile(): os.unlink(self.fname) else: self.lines[start : start + h.lena] = h.new() self.offset += h.lenb - h.lena self.dirty = 1 return 0 # ok, we couldn't match the hunk. Lets look for offsets and fuzz it self.hashlines() if h.hunk[-1][0] != ' ': # if the hunk tried to put something at the bottom of the file # override the start line and use eof here search_start = len(self.lines) else: search_start = orig_start for fuzzlen in xrange(3): for toponly in [ True, False ]: old = h.old(fuzzlen, toponly) cand = self.findlines(old[0][1:], search_start) for l in cand: if diffhelpers.testhunk(old, self.lines, l) == 0: newlines = h.new(fuzzlen, toponly) self.lines[l : l + len(old)] = newlines self.offset += len(newlines) - len(old) self.dirty = 1 offset = l - orig_start - fuzzlen if fuzzlen: msg = _("Hunk #%d succeeded at %d " "with fuzz %d " "(offset %d lines).\n") self.printfile(True) self.ui.warn(msg % (h.number, l + 1, fuzzlen, offset)) else: msg = _("Hunk #%d succeeded at %d " "(offset %d lines).\n") self.ui.note(msg % (h.number, l + 1, offset)) return fuzzlen self.printfile(True) self.ui.warn(_("Hunk #%d FAILED at %d\n") % (h.number, orig_start)) self.rej.append(h) return -1