def run_revert(args): patchfns.chdir_to_base_dir() patch = patchfns.find_applied_patch(args.opt_patch) if not patch: return cmd_result.ERROR prpatch = patchfns.print_patch(patch) if patchfns.SUBDIR: args.files = [os.path.join(patchfns.SUBDIR, item) for item in args.files] is_ok = True for filename in args.files: if not patchfns.file_in_patch(filename, patch): is_ok = False output.error('File %s is not in patch %s\n' % (filename, prpatch)) continue next_patch = patchfns.next_patch_for_file(patch, filename) if next_patch: is_ok = False output.error('' % (filename, patchfns.print_patch(next_patch))) continue if not is_ok: return cmd_result.ERROR workdir = patchfns.gen_tempfile(os.getcwd(), asdir=True) atexit.register(clean_up, workdir) if not patchfns.apply_patch_temporarily(workdir, patch, args.files): return cmd_result.ERROR for filename in args.files: revert_ok = True wdfilename = os.path.join(workdir, filename) if os.path.exists(wdfilename) and os.path.getsize(wdfilename) > 0: if os.path.exists(filename) and diff.same_contents(filename, wdfilename): output.write('File %s is unchanged\n' % filename) continue try: fdir = os.path.dirname(filename) if fdir and not os.path.exists(fdir): os.makedirs(fdir) shutil.copy2(wdfilename, filename) except OSError as edata: revert_ok = False else: if not os.path.exists(filename): output.write('File %s is unchanged\n' % filename) continue try: os.remove(filename) fdir = os.path.dirname(filename) if os.path.exists(fdir) and len(os.listdir(fdir)) == 0: os.removedirs(fdir) except OSError as edata: revert_ok = False if revert_ok: output.write('Changes to %s in patch %s reverted\n' % (filename, prpatch)) else: output.error('Failed to revert changes to %s in patch %s\n' % (filename, prpatch)) is_ok = False return cmd_result.OK if is_ok else cmd_result.ERROR
def do_diff(filename, old_file, new_file, args): """Output the diff for the nominated files""" if args.opt_reverse: old_file, new_file = new_file, old_file if args.opt_diff: if not os.path.exists(old_file): old_file = '/dev/null' if not os.path.exists(new_file): new_file = '/dev/null' if not diff.same_contents(old_file, new_file): os.environ['LANG'] = patchfns.ORIGINAL_LANG shell.run_cmd('%s %s %s' % (args.opt_diff, old_file, new_file)) os.environ['LANG'] = 'POSIX' return True else: result = diff.diff_file(filename, old_file, new_file, args) output.error(result.stderr) if args.opt_color: output.write(colorize(result.stdout)) else: output.write(result.stdout) return result.eflags < 2
def check_for_pending_changes(patch): patch_file = patchfns.patch_file_name(patch) workdir = patchfns.gen_tempfile(template='quilt', asdir=True) patchdir = os.path.join(patchfns.QUILT_PC, patch) if os.path.isdir(patchdir): prefix = os.path.abspath(patchdir) if not backup.restore(prefix, to_dir=workdir, keep=True): output.error('Failed to copy files to temporary directory\n') shutil.rmtree(workdir) return False if os.path.exists(patch_file) and os.path.getsize(patch_file) > 0: patch_args = '%s --no-backup-if-mismatch -E' % ' '.join(patchfns.patch_args(patch)) result = putils.apply_patch(patch_file, indir=workdir, patch_args=patch_args) if result.eflags != 0 and not os.path.exists(patchdir): output.write(result.stdout) output.error('Failed to patch temporary files\n') shutil.rmtree(workdir) return False failed = False for file_nm in patchfns.files_in_patch(patch): wfile_nm = os.path.join(workdir, file_nm) if not os.path.exists(file_nm): if os.path.exists(wfile_nm): failed = True break else: continue elif not os.path.exists(wfile_nm): failed = True break if not diff.same_contents(file_nm, wfile_nm): failed = True break shutil.rmtree(workdir) if failed: output.error('Patch %s does not remove cleanly (refresh it or enforce with -f)\n' % patchfns.print_patch(patch)) return cmd_result.ERROR_SUGGEST_FORCE return True