Ejemplo n.º 1
0
def run_fork(args):
    patchfns.chdir_to_base_dir()
    top_patch = patchfns.find_top_patch()
    if not top_patch:
        return cmd_result.ERROR
    new_patch = args.arg_new_name if args.arg_new_name else patchfns.next_filename(top_patch)
    new_patch_file = patchfns.patch_file_name(new_patch)
    new_patch_dir = os.path.join(patchfns.QUILT_PC, new_patch)
    if patchfns.patch_in_series(new_patch) or os.path.isdir(new_patch_dir) or os.path.exists(new_patch_file):
        output.error('Patch %s exists already, please choose a new name\n' % patchfns.print_patch(new_patch))
        return cmd_result.ERROR | cmd_result.SUGGEST_RENAME
    is_ok = patchfns.rename_in_db(top_patch, new_patch)
    is_ok = is_ok if not is_ok else patchfns.rename_in_series(top_patch, new_patch)
    if is_ok:
        top_patch_dir = os.path.join(patchfns.QUILT_PC, top_patch)
        try:
            os.rename(top_patch_dir, new_patch_dir)
        except OSError:
            is_ok = False
    if is_ok:
        top_patch_file = patchfns.patch_file_name(top_patch)
        if os.path.exists(top_patch_file):
            try:
                shutil.copy2(top_patch_file, new_patch_file)
            except Exception:
                is_ok = False
    if not is_ok:
        output.write('Fork of patch %s to patch %s failed\n' % (patchfns.print_patch(top_patch), patchfns.print_patch(new_patch)))
    else:
        output.error('Fork of patch %s created as %s\n' % (patchfns.print_patch(top_patch), patchfns.print_patch(new_patch)))
    return cmd_result.OK if is_ok else cmd_result.ERROR
Ejemplo n.º 2
0
def run_refresh(args):
    workdir = None
    def clean_up(status):
        if workdir and os.path.isdir(workdir):
            shutil.rmtree(workdir)
        return status
    patchfns.chdir_to_base_dir()
    patch = patchfns.find_applied_patch(args.patchname)
    if not patch:
        return cmd_result.ERROR
    if not args.opt_sort:
        files = patchfns.files_in_patch_ordered(patch)
    else:
        files = patchfns.files_in_patch(patch)
    if args.opt_new_name:
        if args.patchname:
            output.error('Can only refresh the topmost patch with -z currently\n')
            return cmd_result.ERROR
        old_patch = patch
        old_patch_args = patchfns.patch_args(old_patch)
        if args.opt_new_name is True:
            patch = patchfns.next_filename(patch)
        else:
            patch = args.opt_new_name
        if os.path.exists(patchfns.patch_file_name(patch)):
            output.error('Patch %s exists already\n' % patchfns.print_patch(patch))
            return cmd_result.ERROR
    if args.opt_strip_level is None:
        args.opt_strip_level = patchfns.patch_strip_level(patch)
    if args.opt_strip_level in ['0', '1']:
        num_strip_level = args.opt_strip_level
    elif args.opt_strip_level == 'ab':
        num_strip_level = '1'
    else:
        output.error('Cannot refresh patches with -p%s, please specify -p0 or -p1 instead\n' % args.opt_strip_level)
        return cmd_result.ERROR
    if args.opt_new_name:
        workdir = patchfns.gen_tempfile(asdir=True, template=os.path.join(os.getcwd(), 'quilt'))
        if not patchfns.apply_patch_temporarily(workdir, old_patch):
            return clean_up(cmd_result.ERROR)
    patch_content = ''
    files_were_shadowed = False
    for filn in files:
        if args.opt_new_name:
            old_file = os.path.join(workdir, filn)
            new_file = filn
        else:
            old_file = patchfns.backup_file_name(patch, filn)
            next_patch = patchfns.next_patch_for_file(patch, filn)
            if not next_patch:
                new_file = filn
            else:
                new_file = patchfns.backup_file_name(next_patch, filn)
                files_were_shadowed = True
        result = diff.diff_file(filn, old_file, new_file, args)
        if result.eflags > 1:
            output.error('\n'.join(result.stderr, 'Diff failed, aborting\n'))
            return clean_up(cmd_result.ERROR)
        elif result.eflags == 0 or not result.stdout:
            continue
        else:
            patch_content += result.stdout
    patch_file = patchfns.patch_file_name(patch)
    prev_patch_file = patch_file if os.path.isfile(patch_file) else '/dev/null'
    result_content = patchfns.patch_header(prev_patch_file)
    if not patch_content:
        output.error('Nothing in patch %s\n' % patchfns.print_patch(patch))
    else:
        if files_were_shadowed:
            if not args.opt_force:
                output.error('More recent patches modify files in patch %s. Enforce refresh with -f.\n' % patchfns.print_patch(patch))
                return clean_up(cmd_result.ERROR_SUGGEST_FORCE)
            if args.opt_strip_trailing_whitespace:
                output.error('Cannot use --strip-trailing-whitespace on a patch that has shadowed files.\n')
        if args.opt_strip_trailing_whitespace and not files_were_shadowed:
            result = putils.remove_trailing_ws(patch_content, num_strip_level)
            if result.eflags == cmd_result.OK:
                patch_content = result.stdout
            if result.stderr:
                output.error(result.stderr)
        else:
            result = putils.remove_trailing_ws(patch_content, num_strip_level, dry_run=True)
            if result.stderr:
                output.error(result.stderr)
        if args.opt_diffstat:
            diffstat_text = diffstat.get_diffstat(patch_content, num_strip_level)
            result_content += diffstat_text
        result_content += patch_content
    patch_file_dir = os.path.dirname(patch_file)
    if not os.path.exists(patch_file_dir):
        os.makedirs(patch_file_dir)
    is_ok = True
    QUILT_PC = customization.get_config('QUILT_PC')
    if fsutils.file_contents_equal(patch_file, result_content):
        output.write('Patch %s is unchanged\n' % patchfns.print_patch(patch))
    else:
        if args.opt_backup and os.path.isfile(patch_file):
            try:
                os.rename(patch_file, patch_file + '~')
            except OSError:
                output.error('Failed to create backup %s\n' % patch_file + '~')
                is_ok = False
        if is_ok:
            is_ok = fsutils.set_file_contents(patch_file, result_content)
        if is_ok and args.opt_new_name:
            insert_ok = patchfns.insert_in_series(patch, old_patch_args)
            if not insert_ok:
                output.error('Failed to insert patch %s into file series\n' % patchfns.print_patch(patch))
                return clean_up(cmd_result.ERROR)
            try:
                patch_dir = os.path.join(QUILT_PC, patch)
                if os.path.exists(patch_dir):
                    shutil.rmtree(patch_dir)
                os.rename(workdir, patch_dir)
                open(patchfns.DB, 'a').write(patch + '\n')
            except:
                output.error('Failed to create patch %s\n' % patchfns.print_patch(patch))
                return clean_up(cmd_result.ERROR)
            output.write('Fork of patch %s created as %s\n' % (patchfns.print_patch(old_patch), patchfns.print_patch(patch)))
        elif is_ok and patch_content:
            output.write('Refreshed patch %s\n' % patchfns.print_patch(patch))
        fsutils.touch(os.path.join(QUILT_PC, patch, '.timestamp'))
    if is_ok:
        tagf = os.path.join(QUILT_PC, patch + '~refresh')
        if os.path.exists(tagf):
            os.remove(tagf)
        is_ok = patchfns.change_db_strip_level('-p%s' % num_strip_level, patch)
    return clean_up(cmd_result.OK if is_ok and patch_content else cmd_result.ERROR)