def run_pop(args): number = stop_at_patch = None patchfns.chdir_to_base_dir() if args.patchnamornum: if args.patchnamornum.isdigit(): number = int(args.patchnamornum) else: stop_at_patch = patchfns.find_applied_patch(args.patchnamornum) if not stop_at_patch: return cmd_result.ERROR elif not args.opt_all: number = 1 silent = args.opt_quiet if patchfns.top_patch_needs_refresh() and not args.opt_force: output.error('The topmost patch %s needs to be refreshed first.\n' % patchfns.print_top_patch()) return cmd_result.ERROR | cmd_result.SUGGEST_FORCE_OR_REFRESH patches = list_patches(number=number, stop_at_patch=stop_at_patch) if not patches: output.error('No patch removed\n') return cmd_result.ERROR is_ok = True for patch in patches: result = remove_patch(patch, force=args.opt_force, check=args.opt_remove, silent=silent) if result is not True: return cmd_result.ERROR if result is False else result if not args.opt_quiet: output.write('\n') if not patchfns.top_patch(): output.write('No patches applied\n') else: output.write('Now at patch %s\n' % patchfns.print_top_patch()) return cmd_result.OK if is_ok is True else cmd_result.ERROR
def run_push(args): def add_patch(patch): def apply_patch(patch_file, patch_args): if not os.path.exists(patch_file) or os.path.getsize(patch_file) == 0: return cmd_result.Result(0, '', '') return putils.apply_patch(patch_file, patch_args=patch_args) tmp = None patch_file = patchfns.patch_file_name(patch) output.write('Applying patch %s\n' % patchfns.print_patch(patch)) try: pp_args = push_patch_args(patch, reverse=False) prefix = os.path.join(patchfns.QUILT_PC, patch) if not args.opt_leave_rejects: tmp = patchfns.gen_tempfile() trf = '-r %s' % tmp else: trf = '' patch_args = '%s --backup --prefix="%s/" %s -E %s' % (pp_args, prefix, trf, more_patch_args) result = apply_patch(patch_file, patch_args=patch_args) if not args.opt_quiet or result.eflags != 0: if do_colorize: output.error(colorize(cleanup_patch_output(result.stderr, args))) output.write(colorize(cleanup_patch_output(result.stdout, args))) else: output.error(cleanup_patch_output(result.stderr, args)) output.write(cleanup_patch_output(result.stdout, args)) except KeyboardInterrupt: rollback_patch(patch) output.error('Interrupted by user; patch %s was not applied.\n' % patchfns.print_patch(patch)) return False finally: if tmp: os.remove(tmp) if result.eflags == 0 or (result.eflags == 1 and args.opt_force): patchfns.add_to_db(patch) refresh_file = os.path.join(patchfns.QUILT_PC, patch + '~refresh') if result.eflags == 0: if os.path.exists(refresh_file): os.remove(refresh_file) else: fsutils.touch(refresh_file) patch_dir = os.path.join(patchfns.QUILT_PC, patch) if os.path.exists(patch_dir): fsutils.touch(os.path.join(patch_dir, '.timestamp')) else: os.mkdir(patch_dir) if not os.path.exists(patch_file): output.write('Patch %s does not exist; applied empty patch\n' % patchfns.print_patch(patch)) elif not putils.get_patch_diff(patch_file): output.write('Patch %s appears to be empty; applied\n' % patchfns.print_patch(patch)) elif result.eflags != 0: output.write('Applied patch %s (forced; needs refresh)\n' % patchfns.print_patch(patch)) return False else: rollback_patch(patch) tmp = patchfns.gen_tempfile() trf = '-r %s' % tmp pp_args = push_patch_args(patch, reverse=True) patch_args = '%s --backup --prefix="%s/" %s -E %s' % (pp_args, prefix, trf, more_patch_args) result = apply_patch(patch_file, patch_args=patch_args) if result.eflags == 0: output.write('Patch %s can be reverse-applied\n' % patchfns.print_patch(patch)) else: output.write('Patch %s does not apply (enforce with -f)\n' % patchfns.print_patch(patch)) rollback_patch(patch) os.remove(tmp) return False return True number = stop_at_patch = None patchfns.chdir_to_base_dir() if args.patchnamornum: if args.patchnamornum.isdigit(): number = int(args.patchnamornum) else: stop_at_patch = args.patchnamornum elif not args.opt_all: number = 1 stop_at_patch = patchfns.find_unapplied_patch(stop_at_patch) if not stop_at_patch: return cmd_result.ERROR silent_unless_verbose = '-s' if not args.opt_verbose else None if args.opt_force: args.opt_leave_rejects = True more_patch_args = ' -s' if args.opt_quiet else '' more_patch_args += ' -f' if not args.opt_force or args.opt_quiet else '' if args.opt_merge is 'default': more_patch_args += ' --merge' elif args.opt_merge: more_patch_args += ' --merge=%s' % args.opt_merge more_patch_args += ' -F%d' % args.opt_fuzz if args.opt_fuzz else '' if patchfns.top_patch_needs_refresh(): output.write('The topmost patch %s needs to be refreshed first.\n' % patchfns.print_top_patch()) return cmd_result.ERROR | cmd_result.SUGGEST_REFRESH patches = list_patches(number=number, stop_at_patch=stop_at_patch, push_all=args.opt_all) patchfns.create_db() do_colorize = args.opt_color == 'always' or (args.opt_color == 'auto' and sys.stderr.isatty()) if do_colorize: colour.set_up() is_ok = True for patch in patches: is_ok = add_patch(patch) if not is_ok: break if not args.opt_quiet: output.write('\n') if is_ok: output.write('Now at patch %s\n' % patchfns.print_top_patch()) return cmd_result.OK if is_ok else cmd_result.ERROR