summaryrefslogblamecommitdiff
path: root/ungone_series.py
blob: 065ed01d0ad189db61a73357f1abc8da953349d1 (plain) (tree)





































































                                                               
import patchstate as ps
import io, os, sys
from import_series import lcs

def parse_prev(info):
    if not info.startswith('(was ') or not info.endswith(')'):
        raise ValueError
    return info[5:-1]

def main(args):
    args = iter(args)
    arg0 = next(args)

    @ps.argparse_all(args)
    def path(arg):
        raise RuntimeError(f'Invalid argument: {arg!r}')

    [repo_path] = path

    repo = ps.Repository(repo_path)

    with io.open(os.path.join(repo.path, 'series'), 'r') as f:
        pcur = ps.Series.parse(f.read())

    gone_seq = []
    new_seq = []
    updated = {}

    def fix_seq():
        s = lcs(gone_seq, new_seq, key=lambda p: p.patch_title)
        del gone_seq[:]
        del new_seq[:]

        for p_gone, p_new in s:
            if p_gone is None or p_new is None:
                continue
            updated[p_new] = parse_prev(p_gone.info[0])
            updated[p_gone] = None

    for i,pi in enumerate(pcur.info):
        m = pi.mode
        if m == 'gone':
            gone_seq.append(pi)
        elif m == 'new':
            new_seq.append(pi)
        else:
            fix_seq()
    fix_seq()

    inf = []
    for pi in pcur.info:
        try:
            mode = updated[pi]
        except KeyError:
            pass
        else:
            if mode is None:
                continue
            assert pi.mode == 'new'
            pi = pi.update(mode=mode)
        inf.append(pi)

    pnew = ps.Series(inf).fmt()
    with io.open(os.path.join(repo.path, 'series'), 'w') as f:
        f.write(pnew)

    return 0

if __name__ == '__main__':
    sys.exit(main(sys.argv))