def run_new(args): patchfns.chdir_to_base_dir() patch = patchfns.patch_name_base(args.patchname) if patchfns.patch_in_series(patch): output.error('Patch "%s" exists already\n' % patchfns.print_patch(patch)) return cmd_result.ERROR | cmd_result.SUGGEST_RENAME patchfns.create_db() if not patchfns.insert_in_series(patch) or not patchfns.add_to_db(patch): output.error('Failed to create patch %s\n' % patchfns.print_patch(patch)) return cmd_result.ERROR else: output.write('Patch %s is now on top\n' % patchfns.print_patch(patch)) return cmd_result.OK
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