def run_patches(args): patchfns.chdir_to_base_dir() if args.opt_verbose: applied = '+ ' current = '= ' unapplied = ' ' else: applied = current = unapplied = '' do_colorize = args.opt_color == 'always' or (args.opt_color in ['auto', 'tty'] and sys.stderr.isatty()) if do_colorize: colour.set_up() file_paths = [os.path.join(patchfns.SUBDIR, file_path) if patchfns.SUBDIR else file_path for file_path in args.filelist] top = patchfns.top_patch() output.start_pager() if top: scan_applied('series_app', applied, file_paths, patchfns.patches_before(top)) scan_applied('series_top', current, file_paths, (top,)) scan_unapplied('series_una', unapplied, file_paths, patchfns.patches_after(top)) output.wait_for_pager() return cmd_result.OK
def run_series(args): patchfns.chdir_to_base_dir() output.start_pager() do_colorize = args.opt_color == 'always' or (args.opt_color == 'auto' and sys.stderr.isatty()) if do_colorize: colour.set_up() if do_colorize or args.opt_verbose: top = patchfns.top_patch() for patch in patchfns.patches_before(top): string = '+ %s\n' % patchfns.print_patch(patch) output.write(colour.wrap(string, 'series_app') if do_colorize else string) if top: string = '= %s\n' % patchfns.print_patch(top) output.write(colour.wrap(string, 'series_top') if do_colorize else string) for patch in patchfns.patches_after(top): string = ' %s\n' % patchfns.print_patch(patch) output.write(colour.wrap(string, 'series_una') if do_colorize else string) else: for patch in patchfns.cat_series(): output.write('%s\n' % patchfns.print_patch(patch)) output.wait_for_pager() return cmd_result.OK
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
def run_diff(args): patchfns.chdir_to_base_dir() snap_subdir = os.path.join(patchfns.QUILT_PC, '.snap') if args.opt_snapshot else None if args.opt_combine: first_patch = '-' if args.opt_combine == '-' else patchfns.find_applied_patch(args.opt_combine) if not first_patch: return cmd_result.ERROR else: first_patch = None if len([opt for opt in [args.opt_combine, args.opt_snapshot, args.opt_relative] if opt]) > 1: output.error('Options `--combine\', `--snapshot\', and `-z\' cannot be combined.\n') return cmd_result.ERROR last_patch = patchfns.find_applied_patch(args.last_patch) if not last_patch: return cmd_result.ERROR if args.opt_strip_level is None: args.opt_strip_level = patchfns.patch_strip_level(last_patch) if args.opt_strip_level not in ['0', '1', 'ab']: output.error('Cannot diff patches with -p%s, please specify -p0, -p1, or -pab instead\n' % args.opt_strip_level) return cmd_result.ERROR files = [] if args.opt_snapshot and len(args.opt_files) == 0: for path, _dirs, bases in os.walk(snap_subdir): rpath = '' if path == snap_subdir else os.path.relpath(path, snap_subdir) files += [os.path.join(rpath, base) for base in bases] files.sort() args.opt_combine = True first_patch = patchfns.applied_patches()[0] if args.opt_combine: patches = patchfns.patches_before(last_patch) + [last_patch] if first_patch != '-': try: patches = patches[patches.index(first_patch):] except ValueError: output.error('Patch %s not applied before patch %s\n' % (patchfns.print_patch(first_patch), patchfns.print_patch(last_patch))) return cmd_result.ERROR else: patches = [last_patch] if len(args.opt_files) > 0: # use a set as it should be more efficient ofiles = set() for ofile in args.opt_files: if ofile.startswith('.' + os.sep): ofile = ofile[2:] if patchfns.SUBDIR: ofile = os.path.join(patchfns.SUBDIR, ofile) ofiles.add(ofile) for patch in patches: for fname in patchfns.files_in_patch_ordered(patch): if fname in ofiles and fname not in files: files.append(fname) else: for patch in patches: for fname in patchfns.files_in_patch_ordered(patch): if fname not in files: files.append(fname) if args.opt_sort: files.sort() if args.opt_relative: workdir = patchfns.gen_tempfile(os.path.join(os.getcwd(), 'quilt'), asdir=True) atexit.register(clean_up, workdir) if not patchfns.apply_patch_temporarily(workdir, last_patch, files): return cmd_result.ERROR is_ok = True files_were_shadowed = False if args.opt_color: colour.set_up() output.start_pager() for filename in files: snapshot_path = os.path.join(snap_subdir, filename) if snap_subdir else None if snapshot_path and os.path.exists(snapshot_path): old_file = snapshot_path elif args.opt_relative: old_file = os.path.join(workdir, filename) else: patch = patchfns.first_modified_by(filename, patches) if not patch: if not args.opt_snapshot: output.error('File %s is not being modified\n' % filename) continue old_file = patchfns.backup_file_name(patch, filename) next_patch = patchfns.next_patch_for_file(last_patch, filename) if not next_patch: new_file = filename else: new_file = patchfns.backup_file_name(next_patch, filename) files_were_shadowed = True if not do_diff(filename, old_file, new_file, args): output.error('Diff failed, aborting\n') return cmd_result.ERROR if files_were_shadowed: output.error('Warning: more recent patches modify files in patch %s\n' % patchfns.print_patch(last_patch)) output.wait_for_pager() return cmd_result.OK