summaryrefslogtreecommitdiff
path: root/patchstate.py
blob: 79600e070911b37ab213cb54ff1a17e74b82a396 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
import io, sys, os, operator, re, base64, hashlib
import patchtheory

def strip_subject(s):
    if not s.startswith(b'[PATCH'):
        raise ValueError(f'Invalid subject: {s!r}')
    i = s.find(b'] ')
    if i == -1:
        raise ValueError(f'Invalid subject: {s!r}')
    return s[i+2:]

rem_patch = re.compile('patch ([0-9a-z]{16})').fullmatch
rem_author = re.compile('[^\0-\x1f]+ <[^\0-\x1f]+@[^\0-\x1f]+>').fullmatch
rem_date = re.compile('[^\0-\x1f]+').fullmatch
rem_title = re.compile('[^\0-\x1f]+').fullmatch

class Patch:
    __slots__ = ('author', 'date', 'title', 'body', 'diff', 'data', 'hash', 'id')

    def __init__(self, /, *, author, date, title, body, diff):
        assert type(author) is str and rem_author(author) is not None
        assert type(date) is str and rem_date(date) is not None
        assert type(title) is str and rem_title(title) is not None
        assert type(body) is str
        assert type(diff) is patchtheory.Diff

        self.author = author
        self.date = date
        self.title = title
        self.body = body
        self.diff = diff

        self._set_data()
        self._set_id()

    @classmethod
    def parse(tp, body):
        lines = iter(body.split(b'\n'))
        line = lines.__next__

        l = line()
        if l.startswith(b'From '):
            l = line()

        headers = {}
        hdr = None
        while True:
            if not l:
                break
            if hdr is not None and l.startswith(b' '):
                headers[hdr] += l
            else:
                hdr,sep,val = l.partition(b':')
                if not sep:
                    raise ValueError(f'Expected header line: {l!r}')
                headers[hdr] = val.lstrip()
            l = line()

        author = headers.pop(b'From').decode('utf-8')
        date = headers.pop(b'Date').decode('utf-8')
        title = strip_subject(headers.pop(b'Subject')).decode('utf-8')

        headers.pop(b'MIME-Version', None)
        headers.pop(b'Content-Type', None)
        headers.pop(b'Content-Transfer-Encoding', None)

        body = []
        for l in lines:
            if l.startswith(b'diff --git'):
                break
            body.append(l)
        else:
            raise RuntimeError('unexpected EOF')
        while not body[-1]:
            del body[-1]
        while body[-1].startswith(b' '):
            del body[-1]
        if body[-1] != b'---':
            raise RuntimeError
        del body[-1]
        while body and not body[-1]:
            del body[-1]

        body = b'\n'.join(body).decode('utf-8')

        return Patch(
            author = author,
            date = date,
            title = title,
            body = body,
            diff = patchtheory.Diff.parse_l(l, line),
        )

    def _set_id(self):
        out = io.BytesIO()
        w = out.write
        w(self.author.encode('utf-8'))
        w(b'\n')
        w(self.date.encode('utf-8'))
        w(b'\n')
        w(self.title.encode('utf-8'))
        w(b'\n')
        self.diff.write_munged(w)

        out = out.getvalue()
        dgst = hashlib.blake2b(out).digest()
        self.id = base64.b32encode(dgst[:10]).lower().decode('ascii')

    def as_filename(self):
        return '-'.join(re.findall('[0-9A-Za-z]+', self.title))[:72]

    def _set_data(self):
        out = io.BytesIO()
        w = out.write

        w(b'From: ')
        w(self.author.encode('utf-8'))
        w(b'\nDate: ')
        w(self.date.encode('utf-8'))
        w(b'\nSubject: [PATCH] ')
        w(self.title.encode('utf-8'))
        w(b'\n\n')
        w(self.body.encode('utf-8'))
        w(b'\n---\n\n')
        self.diff.write(w)
        w(b'-- \n0.0.0 patchstate\n\n')

        self.data = data = out.getvalue()

        self.hash = base64.b32encode(hashlib.blake2b(data).digest()[:20]).lower().decode('ascii')

    def revert(self):
        r = Patch(
            author = self.author,
            date = self.date,
            title = f'Revert "{self.title}"',
            body = self.body,
            diff = -self.diff,
        )

        r._set_data()
        r._set_id()
        return r

class PatchInfo:
    __slots__ = ('patch_id', '_body')

    def __init__(self, patch_id, info):
        self.patch_id = patch_id
        self._body = info

    def get_patch(self, repo):
        p = repo.patches[self.patch_hash]
        if p.id != self.patch_id:
            raise KeyError(f'patch id mismatch: {self.patch_id!r} -> {p.id!r}')
        if p.title != self.patch_title:
            raise KeyError(f'patch title mismatch: {self.patch_title!r} -> {p.title!r}')
        return p

    @property
    def patch_hash(self):
        return self._body[0]

    @property
    def patch_title(self):
        return self._body[1]

    @property
    def mode(self):
        return self._body[2]

    @property
    def info(self):
        return self._body[3:]

    def update(self, *, patch=None, patch_id=None, patch_hash=None, patch_title=None, mode=None, new_mode=None, info=None):
        if patch_title is None:
            patch_title = self.patch_title

        if patch is not None:
            assert patch_id is None
            assert patch_hash is None
            if patch_title != patch.title:
                raise ValueError('patch title change: {patch_title!r} -> {patch.title!r}')
            patch_id = patch.id
            patch_hash = patch.hash

        else:
            if patch_id is None:
                patch_id = self.patch_id

            if patch_hash is None:
                patch_hash = self.patch_hash

        if info is None:
            info = self.info

        if mode is None:
            mode = self.mode

        if new_mode is not None and new_mode != mode:
            info.insert(0, f'(was {mode})')
            mode = new_mode

        return PatchInfo(patch_id, [patch_hash, patch_title, mode, *info])

class Series:
    __slots__ = ('info',)

    def __init__(self, /, info=None):
        if info is None:
            info = []
        self.info = info

    @classmethod
    def parse(tp, data):
        inf = []

        lines = iter(data.split('\n'))
        l = next(lines)
        while True:
            if not l:
                try:
                    l = next(lines)
                except StopIteration:
                    break
                continue
            m = rem_patch(l)
            if m is None:
                raise ValueError(f'Invalid header: {l!r}')
            pid = m.group(1)
            ldata = []
            while True:
                l = next(lines)
                if not l.startswith('\t'):
                    break
                ldata.append(l[1:])
            if len(ldata) < 3:
                raise ValueError(f'Expected data after {f"patch {pid}"!r}')

            while ldata and not ldata[-1]:
                del ldata[-1]

            inf.append(PatchInfo(pid, ldata))

        return tp(info=inf)

    def fmt(self):
        out = io.StringIO()
        w = out.write

        for pi in self.info:
            w(f'patch {pi.patch_id}')
            for line in pi._body:
                w('\n\t')
                w(line)
            w('\n')

        return out.getvalue()

class Repository:
    __slots__ = ('path', 'patches', 'aliases', 'by_id')

    def __init__(self, path):
        self.path = path
        self.patches = {}
        self.by_id = {}

        aliases = {}
        for fn in os.listdir(path):
            if not fn.endswith('.patch'):
                continue
            fp = os.path.join(path, fn)
            with io.open(fp, 'rb') as f:
                data = f.read()

            try:
                p = Patch.parse(data)
            except ValueError as exc:
                print(f'Failed to parse {fn!r}: {exc}', file=sys.stderr)
                os.unlink(fp)
                continue

            h = p.hash
            if fn != f'{h}.patch':
                aliases[fn[:-6]] = (h,p)
            else:
                self.patches[h] = p
            self.by_id.setdefault(p.id, []).append(p)

        for h,(h2,p) in aliases.items():
            assert h != h2
            assert h not in self.patches
            if h2 in self.patches:
                continue
            print(f'Migrating patch: {h!r} -> {h2!r}', file=sys.stderr)
            self.patches[h2] = p
            with io.open(os.path.join(self.path, f'{h2}.patch'), 'xb') as f:
                f.write(p.data)

        self.aliases = {h: h2 for h,(h2,p) in aliases.items()}

    def hash_data(self, data):
        data = hashlib.blake2b(data).digest()
        return base64.b32encode(data[:20]).lower().decode()

    def insert_patch(self, patch):
        p = self.patches.setdefault(patch.hash, patch)
        if p is patch:
            pid = p.id
            try:
                pd = self.by_id[pid]
            except KeyError:
                self.by_id[pid] = [p]
            else:
                print(f'Duplicate patch ID: {pid!r} {p.title}', file=sys.stderr)
                pd.append(p)

            with io.open(os.path.join(self.path, f'{p.hash}.patch'), 'xb') as f:
                f.write(p.data)

        return p

    def import_dir(self, path):
        r = Series()
        r.info = inf = []

        for fn in sorted(os.listdir(path)):
            if not fn.endswith('.patch'):
                print(f'Skipping {fn!r}', file=sys.stderr)
                continue
            with io.open(os.path.join(path, fn), 'rb') as f:
                patch = Patch.parse(f.read())

            patch = self.insert_patch(patch)
            inf.append(PatchInfo(patch.id, [patch.hash, patch.title, 'new']))

        return r

    def gc(self, keep):
        for h in self.patches.keys() - keep:
            del self.patches[h]
            print(f'Removing patch: {h!r}', file=sys.stderr)
            os.unlink(os.path.join(self.path, f'{h}.patch'))

def argparse_next(args, func=...):
    def do_argparse(func):
        while True:
            try:
                arg = next(args)
            except StopIteration:
                return None
            if arg == '--':
                return next(args, None)
            if not arg.startswith('-'):
                return arg
            arg = func(arg)
            if arg is not None:
                return arg
    if func is not ...:
        return do_argparse(func)
    return do_argparse

def argparse_all(args, func=...):
    def do_argparse(func):
        r = []
        while True:
            try:
                arg = next(args)
            except StopIteration:
                return r
            if arg == '--':
                r.extend(args)
                return r
            if not arg.startswith('-'):
                r.append(arg)
                continue
            arg = func(arg)
            if arg is not None:
                r.extend(arg)
    if func is not ...:
        return do_argparse(func)
    return do_argparse