Applies string with `git patch` in . If is true we clean the tree with `git checkout .` and then apply the patch. Otherwise we apply patch only if it is not already applied; this might fail if there are conflicting changes in the tree.
(directory, patch, hard=False)
| 396 | |
| 397 | |
| 398 | def git_patch(directory, patch, hard=False): |
| 399 | ''' |
| 400 | Applies string <patch> with `git patch` in <directory>. |
| 401 | |
| 402 | If <hard> is true we clean the tree with `git checkout .` and then apply |
| 403 | the patch. |
| 404 | |
| 405 | Otherwise we apply patch only if it is not already applied; this might fail |
| 406 | if there are conflicting changes in the tree. |
| 407 | ''' |
| 408 | log(f'Applying patch in {directory}:\n{textwrap.indent(patch, " ")}') |
| 409 | if not patch: |
| 410 | return |
| 411 | # Carriage returns break `git apply` so we use `newline='\n'` in open(). |
| 412 | path = os.path.abspath(f'{directory}/pymupdf_patch.txt') |
| 413 | with open(path, 'w', newline='\n') as f: |
| 414 | f.write(patch) |
| 415 | log(f'Using patch file: {path}') |
| 416 | if hard: |
| 417 | run(f'cd {directory} && git checkout .') |
| 418 | run(f'cd {directory} && git apply {path}') |
| 419 | log(f'Have applied patch in {directory}.') |
| 420 | else: |
| 421 | e = run( f'cd {directory} && git apply --check --reverse {path}', check=0) |
| 422 | if e == 0: |
| 423 | log(f'Not patching {directory} because already patched.') |
| 424 | else: |
| 425 | run(f'cd {directory} && git apply {path}') |
| 426 | log(f'Have applied patch in {directory}.') |
| 427 | run(f'cd {directory} && git diff') |
| 428 | |
| 429 | |
| 430 | mupdf_tgz = os.path.abspath( f'{__file__}/../mupdf.tgz') |