summaryrefslogblamecommitdiff
path: root/check_reverts.py
blob: f8598d3e497250f35c2376a7d90f7086446d2fa2 (plain) (tree)

























































                                                                                                        
import patchstate as ps
import io, os, sys, subprocess, tempfile

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())

    ok = True
    reverted = {}
    for pi in pcur.info:
        mode = pi.mode
        p = pi.get_patch(repo)
        if mode == 'reverted':
            pid = p.id
            if pid in reverted:
                raise RuntimeError(f'reverted twice: {pid!r}')
            reverted[pid] = p
            continue
        if not mode.startswith('reverts '):
            continue
        pid = mode[8:]
        revp = reverted.pop(pid)

        d1 = ps.fmt_diff(revp.files, munge=True)
        d2 = ps.fmt_diff(p.revert().files, munge=True)
        if d1 != d2:
            ok = False
            print(f'Bad diff for {pid} {revp.title}:')
            if 0:
                with tempfile.NamedTemporaryFile('w+b') as t1, tempfile.NamedTemporaryFile('w+b') as t2:
                    t1.write(d1)
                    t1.flush()
                    t2.write(d2)
                    t2.flush()
                    diff = subprocess.run(['diff', t1.name, t2.name], capture_output=True, check=False)
                diff = diff.stdout.decode('utf-8')
                for line in diff.splitlines():
                    print(f'\t{line}')

    for p in reverted.values():
        print(f'Not reverted: {pid} {revp.title}')
        ok = False

    return 0 if ok else 1

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