Mercurial > hg
annotate mercurial/cffi/mpatch.py @ 52164:e01e84e5e426
rust-revlog: add a Rust-only `InnerRevlog`
This mirrors the Python `InnerRevlog` and will be used in a future patch
to replace said Python implementation. This allows us to start doing more
things in pure Rust, in particular reading and writing operations.
A lot of changes have to be introduced all at once, it wouldn't be very
useful to separate this patch IMO since all of them are either interlocked
or only useful with the rest.
author | Raphaël Gomès <rgomes@octobus.net> |
---|---|
date | Thu, 10 Oct 2024 10:34:51 +0200 |
parents | f4733654f144 |
children |
rev | line source |
---|---|
32512
0e8b0b9a7acc
cffi: split modules from pure
Yuya Nishihara <yuya@tcha.org>
parents:
32506
diff
changeset
|
1 # mpatch.py - CFFI implementation of mpatch.c |
7699
fac054f84600
pure Python implementation of mpatch.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
2 # |
32512
0e8b0b9a7acc
cffi: split modules from pure
Yuya Nishihara <yuya@tcha.org>
parents:
32506
diff
changeset
|
3 # Copyright 2016 Maciej Fijalkowski <fijall@gmail.com> |
7699
fac054f84600
pure Python implementation of mpatch.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
4 # |
8225
46293a0c7e9f
updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents:
7775
diff
changeset
|
5 # This software may be used and distributed according to the terms of the |
10263 | 6 # GNU General Public License version 2 or any later version. |
7699
fac054f84600
pure Python implementation of mpatch.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
7 |
51863
f4733654f144
typing: add `from __future__ import annotations` to most files
Matt Harbison <matt_harbison@yahoo.com>
parents:
51792
diff
changeset
|
8 from __future__ import annotations |
27337
9a17576103a4
mpatch: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
16683
diff
changeset
|
9 |
49599
94a797032fc4
typing: add type hints to mpatch implementations
Matt Harbison <matt_harbison@yahoo.com>
parents:
48875
diff
changeset
|
10 from typing import List |
94a797032fc4
typing: add type hints to mpatch implementations
Matt Harbison <matt_harbison@yahoo.com>
parents:
48875
diff
changeset
|
11 |
32512
0e8b0b9a7acc
cffi: split modules from pure
Yuya Nishihara <yuya@tcha.org>
parents:
32506
diff
changeset
|
12 from ..pure.mpatch import * |
0e8b0b9a7acc
cffi: split modules from pure
Yuya Nishihara <yuya@tcha.org>
parents:
32506
diff
changeset
|
13 from ..pure.mpatch import mpatchError # silence pyflakes |
46785
521ac0d7047f
typing: disable import error warnings that are already handled
Matt Harbison <matt_harbison@yahoo.com>
parents:
43077
diff
changeset
|
14 from . import _mpatch # pytype: disable=import-error |
7699
fac054f84600
pure Python implementation of mpatch.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
15 |
32512
0e8b0b9a7acc
cffi: split modules from pure
Yuya Nishihara <yuya@tcha.org>
parents:
32506
diff
changeset
|
16 ffi = _mpatch.ffi |
0e8b0b9a7acc
cffi: split modules from pure
Yuya Nishihara <yuya@tcha.org>
parents:
32506
diff
changeset
|
17 lib = _mpatch.lib |
7699
fac054f84600
pure Python implementation of mpatch.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
18 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
33572
diff
changeset
|
19 |
32513
25b37900d6e0
cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents:
32512
diff
changeset
|
20 @ffi.def_extern() |
25b37900d6e0
cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents:
32512
diff
changeset
|
21 def cffi_get_next_item(arg, pos): |
25b37900d6e0
cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents:
32512
diff
changeset
|
22 all, bins = ffi.from_handle(arg) |
51790
ecc3a893979d
cffi: pass C type and attribute names as str instead of bytes
Manuel Jacob <me@manueljacob.de>
parents:
49599
diff
changeset
|
23 container = ffi.new("struct mpatch_flist*[1]") |
51792
472699b5ddb3
cffi: pass bytes instead of str to ffi.new("char[]", …)
Manuel Jacob <me@manueljacob.de>
parents:
51790
diff
changeset
|
24 to_pass = ffi.new("char[]", bytes(bins[pos])) |
32513
25b37900d6e0
cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents:
32512
diff
changeset
|
25 all.append(to_pass) |
25b37900d6e0
cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents:
32512
diff
changeset
|
26 r = lib.mpatch_decode(to_pass, len(to_pass) - 1, container) |
25b37900d6e0
cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents:
32512
diff
changeset
|
27 if r < 0: |
25b37900d6e0
cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents:
32512
diff
changeset
|
28 return ffi.NULL |
25b37900d6e0
cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents:
32512
diff
changeset
|
29 return container[0] |
29695
f2846d546645
mpatch: write a cffi version of mpatch.patches
Maciej Fijalkowski <fijall@gmail.com>
parents:
28861
diff
changeset
|
30 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
33572
diff
changeset
|
31 |
49599
94a797032fc4
typing: add type hints to mpatch implementations
Matt Harbison <matt_harbison@yahoo.com>
parents:
48875
diff
changeset
|
32 def patches(text: bytes, bins: List[bytes]) -> bytes: |
32513
25b37900d6e0
cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents:
32512
diff
changeset
|
33 lgt = len(bins) |
25b37900d6e0
cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents:
32512
diff
changeset
|
34 all = [] |
25b37900d6e0
cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents:
32512
diff
changeset
|
35 if not lgt: |
25b37900d6e0
cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents:
32512
diff
changeset
|
36 return text |
25b37900d6e0
cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents:
32512
diff
changeset
|
37 arg = (all, bins) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
33572
diff
changeset
|
38 patch = lib.mpatch_fold(ffi.new_handle(arg), lib.cffi_get_next_item, 0, lgt) |
32513
25b37900d6e0
cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents:
32512
diff
changeset
|
39 if not patch: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
40 raise mpatchError(b"cannot decode chunk") |
32513
25b37900d6e0
cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents:
32512
diff
changeset
|
41 outlen = lib.mpatch_calcsize(len(text), patch) |
25b37900d6e0
cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents:
32512
diff
changeset
|
42 if outlen < 0: |
25b37900d6e0
cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents:
32512
diff
changeset
|
43 lib.mpatch_lfree(patch) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
44 raise mpatchError(b"inconsistency detected") |
51790
ecc3a893979d
cffi: pass C type and attribute names as str instead of bytes
Manuel Jacob <me@manueljacob.de>
parents:
49599
diff
changeset
|
45 buf = ffi.new("char[]", outlen) |
32513
25b37900d6e0
cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents:
32512
diff
changeset
|
46 if lib.mpatch_apply(buf, text, len(text), patch) < 0: |
25b37900d6e0
cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents:
32512
diff
changeset
|
47 lib.mpatch_lfree(patch) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
48 raise mpatchError(b"error applying patches") |
32513
25b37900d6e0
cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents:
32512
diff
changeset
|
49 res = ffi.buffer(buf, outlen)[:] |
25b37900d6e0
cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents:
32512
diff
changeset
|
50 lib.mpatch_lfree(patch) |
25b37900d6e0
cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents:
32512
diff
changeset
|
51 return res |