summaryrefslogtreecommitdiff
path: root/check_reverts.py
blob: f8598d3e497250f35c2376a7d90f7086446d2fa2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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))