def applydiff(ui, fp, changed, strip=1, sourcefile=None, reverse=False, eol=None): """ Reads a patch from fp and tries to apply it. The dict 'changed' is filled in with all of the filenames changed by the patch. Returns 0 for a clean patch, -1 if any rejects were found and 1 if there was any fuzz. If 'eol' is None, the patch content and patched file are read in binary mode. Otherwise, line endings are ignored when patching then normalized to 'eol' (usually '\n' or \r\n'). """ rejects = 0 err = 0 current_file = None gitpatches = None opener = util.opener(os.getcwd()) textmode = eol is not None def closefile(): if not current_file: return 0 current_file.close() return len(current_file.rej) for state, values in iterhunks(ui, fp, sourcefile, textmode): if state == 'hunk': if not current_file: continue current_hunk = values ret = current_file.apply(current_hunk, reverse) if ret >= 0: changed.setdefault(current_file.fname, None) if ret > 0: err = 1 elif state == 'file': rejects += closefile() afile, bfile, first_hunk = values try: if sourcefile: current_file = patchfile(ui, sourcefile, opener, eol=eol) else: current_file, missing = selectfile(afile, bfile, first_hunk, strip, reverse) current_file = patchfile(ui, current_file, opener, missing, eol) except PatchError, err: ui.warn(str(err) + '\n') current_file, current_hunk = None, None rejects += 1 continue elif state == 'git': gitpatches = values cwd = os.getcwd() for gp in gitpatches: if gp.op in ('COPY', 'RENAME'): copyfile(gp.oldpath, gp.path, cwd) changed[gp.path] = gp
def simplemerge(local, base, other, **opts): def readfile(filename): f = open(filename, "rb") text = f.read() f.close() if util.binary(text): msg = _("%s looks like a binary file.") % filename if not opts.get('text'): raise util.Abort(msg) elif not opts.get('quiet'): warn(_('warning: %s\n') % msg) return text name_a = local name_b = other labels = opts.get('label', []) if labels: name_a = labels.pop(0) if labels: name_b = labels.pop(0) if labels: raise util.Abort(_("can only specify two labels.")) localtext = readfile(local) basetext = readfile(base) othertext = readfile(other) orig = local local = os.path.realpath(local) if not opts.get('print'): opener = util.opener(os.path.dirname(local)) out = opener(os.path.basename(local), "w", atomictemp=True) else: out = sys.stdout reprocess = not opts.get('no_minimal') m3 = Merge3Text(basetext, localtext, othertext) for line in m3.merge_lines(name_a=name_a, name_b=name_b, reprocess=reprocess): out.write(line) if not opts.get('print'): out.rename() if m3.conflicts: if not opts.get('quiet'): warn(_("warning: conflicts during merge.\n")) return 1
def applydiff(ui, fp, changed, strip=1, sourcefile=None, reverse=False): """reads a patch from fp and tries to apply it. The dict 'changed' is filled in with all of the filenames changed by the patch. Returns 0 for a clean patch, -1 if any rejects were found and 1 if there was any fuzz.""" rejects = 0 err = 0 current_file = None gitpatches = None opener = util.opener(os.getcwd()) def closefile(): if not current_file: return 0 current_file.close() return len(current_file.rej) for state, values in iterhunks(ui, fp, sourcefile): if state == "hunk": if not current_file: continue current_hunk = values ret = current_file.apply(current_hunk, reverse) if ret >= 0: changed.setdefault(current_file.fname, None) if ret > 0: err = 1 elif state == "file": rejects += closefile() afile, bfile, first_hunk = values try: if sourcefile: current_file = patchfile(ui, sourcefile, opener) else: current_file, missing = selectfile(afile, bfile, first_hunk, strip, reverse) current_file = patchfile(ui, current_file, opener, missing) except PatchError, err: ui.warn(str(err) + "\n") current_file, current_hunk = None, None rejects += 1 continue elif state == "git": gitpatches = values cwd = os.getcwd() for gp in gitpatches: if gp.op in ("COPY", "RENAME"): copyfile(gp.oldpath, gp.path, cwd) changed[gp.path] = gp
def __init__(self, name, prefix, mtime): if prefix: raise util.Abort(_('cannot give prefix when archiving to files')) self.basedir = name self.opener = util.opener(self.basedir)
def __init__(self, name, mtime): self.basedir = name self.opener = util.opener(self.basedir)