Beispiel #1
0
def run_applied(args):
    patchfns.chdir_to_base_dir()
    patch = patchfns.find_applied_patch(args.patch)
    if not patch:
        return cmd_result.ERROR
    output.start_pager()
    for patch in patchfns.applied_before(patch) + [patch]:
        output.write('%s\n' % patchfns.print_patch(patch))
    output.wait_for_pager()
    return cmd_result.OK
Beispiel #2
0
def run_header(args):
    def read_input():
        text = sys.stdin.read()
        if args.opt_strip_trailing_whitespace:
            return _trim_trailing_ws(text)
        return text
    def get_text(pfile):
        text = putils.get_patch_hdr(pfile, omit_diffstat=args.opt_strip_diffstat)
        if args.opt_strip_trailing_whitespace:
            return _trim_trailing_ws(text)
        return text
    def set_text(pfile, text):
        if args.opt_backup:
            try:
                shutil.copy2(pfile, pfile + '~')
            except Exception as edata:
                output.perror(edata)
        if args.opt_strip_trailing_whitespace:
            text = _trim_trailing_ws(text)
        putils.set_patch_hdr(pfile, text, omit_diffstat=args.opt_strip_diffstat)
    patchfns.chdir_to_base_dir()
    if not args.opt_backup:
        args.opt_backup = customization.get_config('QUILT_BACKUP')
    patch = patchfns.find_patch_in_series(args.arg_patch)
    if not patch:
        return cmd_result.ERROR
    patch_file = patchfns.patch_file_name(patch)
    if args.opt_replace:
        set_text(patch_file, read_input())
        output.write('Replaced header of patch %s\n' % patchfns.print_patch(patch))
    elif args.opt_append:
        set_text(patch_file, get_text(patch_file) + read_input())
        output.write('Appended text to header of patch %s\n' % patchfns.print_patch(patch))
    elif args.opt_edit:
        savelang = os.getenv('LANG', None)
        os.environ['LANG'] = patchfns.ORIGINAL_LANG
        tempfile = patchfns.gen_tempfile()
        result = shell.run_cmd('%s %s' % (os.getenv('EDITOR'), tempfile))
        if savelang:
            os.environ['LANG'] = savelang
        output.error(result.stderr)
        output.write(result.stdout)
        text = open(tempfile).read()
        os.remove(tempfile)
        if result.eflags != 0:
            return cmd_result.ERROR
        set_text(patch_file, text)
        output.write('Replaced header of patch %s\n' % patchfns.print_patch(patch))
    else:
        if not os.path.exists(patch_file):
            return cmd_result.OK
        output.start_pager()
        output.write(get_text(patch_file))
        output.wait_for_pager()
    return cmd_result.OK
Beispiel #3
0
def run_files(args):
    patchfns.chdir_to_base_dir()
    first_patch = None
    if args.opt_combine:
        args.opt_all = True
        if args.opt_combine != '-':
            first_patch = patchfns.find_patch_in_series(args.opt_combine)
            if not first_patch:
                return cmd_result.ERROR
    last_patch = patchfns.find_applied_patch(args.patch)
    if not last_patch:
        return cmd_result.ERROR
    if args.opt_all:
        if not first_patch:
            first_patch = patchfns.applied_patches()[0]
        patches = patchfns.patches_before(last_patch) + [last_patch]
        if first_patch not in patches:
            output.error('Patch %s not applied before patch %s\n' % (patchfns.print_patch(first_patch), patchfns.print_patch(last_patch)))
            return cmd_result.ERROR
        patches = patches[patches.index(first_patch):]
    else:
        patches = [last_patch]
    use_status = args.opt_verbose and not args.opt_labels
    # Note: If opt_labels is set, then use_status is not set.
    output.start_pager()
    for patch in patches:
        if args.opt_all and args.opt_verbose and not args.opt_labels:
            output.write('%s\n' % patch)
        for filename in sorted(patchfns.files_in_patch(patch)):
            if args.opt_labels:
                if args.opt_verbose:
                    output.write('[%s] ' % patch)
                else:
                    output.write('%s ' % patch)
            if not use_status:
                output.write('%s\n' % filename)
            else:
                status = ' '
                buname = patchfns.backup_file_name(patch, filename)
                if os.path.exists(buname) and os.path.getsize(buname) > 0:
                    if not os.path.exists(filename) or os.path.getsize(filename) == 0:
                        status = '-'
                elif os.path.exists(filename) or os.path.getsize(filename) > 0:
                    status = '+'
                output.write('%s %s\n' % (status, filename))
    output.wait_for_pager()
    return cmd_result.OK
Beispiel #4
0
def run_unapplied(args):
    patchfns.chdir_to_base_dir()
    if args.arg_patch:
        start = patchfns.find_patch_in_series(args.arg_patch)
        if not start:
            return cmd_result.ERROR
        patch = patchfns.patch_after(start)
    else:
        patch = patchfns.find_unapplied_patch()
    if not patch:
        return cmd_result.OK
    output.start_pager()
    patches = [patch] + patchfns.patches_after(patch)
    for patch in patches:
        output.write('%s\n' % patchfns.print_patch(patch))
    output.wait_for_pager()
    return cmd_result.OK
Beispiel #5
0
def run_annotate(args):
    patchfns.chdir_to_base_dir()
    args.opt_patch = patchfns.find_applied_patch(args.opt_patch)
    opt_file = os.path.join(patchfns.SUBDIR, args.filename)
    if not args.opt_patch:
        return cmd_result.ERROR
    patches = []
    files = []
    next_patch = None
    for patch in patchfns.applied_patches():
        old_file = patchfns.backup_file_name(patch, opt_file)
        if os.path.isfile(old_file):
            patches.append(patch)
            files.append(old_file)
        if patch == args.opt_patch:
            next_patch = patchfns.next_patch_for_file(patch, opt_file)
            break
    if not next_patch:
        files.append(opt_file)
    else:
        files.append(patchfns.backup_file_name(next_patch, opt_file))
    if len(patches) == 0:
        for line in open(files[-1]).readlines():
            output.write('\t%s\n' % line)
        return cmd_result.OK
    difftxt = ''
    for index in range(len(patches)):
        difftxt += annotation_for(files[index], files[index + 1], index + 1)
    template = patchfns.gen_tempfile()
    open(template, 'w').write('\n' * len(open(files[0]).readlines()))
    shell.run_cmd('patch %s' % template, difftxt)
    annotations = [line.rstrip() for line in open(template).readlines()]
    os.remove(template)
    output.start_pager()
    if os.path.exists(files[-1]):
        for annotation, line in zip(annotations, open(files[-1]).readlines()):
            output.write('%s\t%s' % (annotation, line))
    output.write('\n')
    for index, patch in zip(range(len(patches)), patches):
        output.write('%s\t%s\n' % (index + 1, patchfns.print_patch(patch)))
    output.wait_for_pager()
    return cmd_result.OK
Beispiel #6
0
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
Beispiel #7
0
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
Beispiel #8
0
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