|
| 1 | +"""Formatting patches. |
| 2 | +
|
| 3 | +*Dfetch* allows you to keep local changes to external projects in the form of |
| 4 | +patch files. These patch files should be created with the `dfetch diff` command. |
| 5 | +However, these patch files are relative the :ref:`source directory <source-dir>` |
| 6 | +of the project inside the superproject. This makes it hard to apply these patches |
| 7 | +upstream, as upstream projects usually expect patches to be relative to their |
| 8 | +root directory. The ``format-patch`` command reformats all patches of a project |
| 9 | +to make them usable for the upstream project. |
| 10 | +
|
| 11 | +.. code-block:: sh |
| 12 | +
|
| 13 | + dfetch format-patch some-project |
| 14 | +
|
| 15 | +.. scenario-include:: ../features/format-patch-in-git.feature |
| 16 | +
|
| 17 | +""" |
| 18 | + |
| 19 | +import argparse |
| 20 | +import pathlib |
| 21 | +import re |
| 22 | + |
| 23 | +import dfetch.commands.command |
| 24 | +import dfetch.manifest.project |
| 25 | +import dfetch.project |
| 26 | +from dfetch.log import get_logger |
| 27 | +from dfetch.project.superproject import SuperProject |
| 28 | +from dfetch.util.util import catch_runtime_exceptions, in_directory |
| 29 | +from dfetch.vcs.patch import PatchAuthor, PatchInfo, format_patch_with_prefix |
| 30 | + |
| 31 | +logger = get_logger(__name__) |
| 32 | + |
| 33 | + |
| 34 | +class FormatPatch(dfetch.commands.command.Command): |
| 35 | + """Format a patch to reflect the last changes. |
| 36 | +
|
| 37 | + The ``format-patch`` reformats all patches of a project to make |
| 38 | + them usable for the upstream project. |
| 39 | + """ |
| 40 | + |
| 41 | + @staticmethod |
| 42 | + def create_menu(subparsers: dfetch.commands.command.SubparserActionType) -> None: |
| 43 | + """Add the menu for the format-patch action.""" |
| 44 | + parser = dfetch.commands.command.Command.parser(subparsers, FormatPatch) |
| 45 | + parser.add_argument( |
| 46 | + "projects", |
| 47 | + metavar="<project>", |
| 48 | + type=str, |
| 49 | + nargs="*", |
| 50 | + help="Specific project(s) to format patches of", |
| 51 | + ) |
| 52 | + parser.add_argument( |
| 53 | + "-o", |
| 54 | + "--output-directory", |
| 55 | + metavar="<output_directory>", |
| 56 | + type=str, |
| 57 | + default=".", |
| 58 | + help="Output directory for formatted patches", |
| 59 | + ) |
| 60 | + |
| 61 | + def __call__(self, args: argparse.Namespace) -> None: |
| 62 | + """Perform the format patch.""" |
| 63 | + superproject = SuperProject() |
| 64 | + |
| 65 | + exceptions: list[str] = [] |
| 66 | + |
| 67 | + output_dir_path = pathlib.Path(args.output_directory).resolve() |
| 68 | + |
| 69 | + if not output_dir_path.is_relative_to(superproject.root_directory): |
| 70 | + raise RuntimeError( |
| 71 | + f"Output directory '{output_dir_path}' must be inside" |
| 72 | + f" the superproject root '{superproject.root_directory}'" |
| 73 | + ) |
| 74 | + |
| 75 | + output_dir_path.mkdir(parents=True, exist_ok=True) |
| 76 | + |
| 77 | + with in_directory(superproject.root_directory): |
| 78 | + for project in superproject.manifest.selected_projects(args.projects): |
| 79 | + with catch_runtime_exceptions(exceptions) as exceptions: |
| 80 | + subproject = dfetch.project.make(project) |
| 81 | + |
| 82 | + # Check if the project has a patch, maybe suggest creating one? |
| 83 | + if not subproject.patch: |
| 84 | + logger.print_warning_line( |
| 85 | + project.name, |
| 86 | + f'skipped - there is no patch file, use "dfetch diff {project.name}"' |
| 87 | + " to generate one instead", |
| 88 | + ) |
| 89 | + continue |
| 90 | + |
| 91 | + for idx, patch in enumerate(subproject.patch, start=1): |
| 92 | + |
| 93 | + version = subproject.on_disk_version() |
| 94 | + |
| 95 | + patch_text = format_patch_with_prefix( |
| 96 | + patch_text=pathlib.Path(patch).read_bytes(), |
| 97 | + patch_info=PatchInfo( |
| 98 | + author=PatchAuthor( |
| 99 | + name=superproject.get_username(), |
| 100 | + email=superproject.get_useremail(), |
| 101 | + ), |
| 102 | + subject=f"Patch for {project.name}", |
| 103 | + total_patches=len(subproject.patch), |
| 104 | + current_patch_idx=idx, |
| 105 | + revision="" if not version else version.revision, |
| 106 | + ), |
| 107 | + path_prefix=re.split(r"\*", subproject.source, 1)[0].rstrip( |
| 108 | + "/" |
| 109 | + ), |
| 110 | + ) |
| 111 | + |
| 112 | + output_patch_file = output_dir_path / pathlib.Path(patch).name |
| 113 | + output_patch_file.write_text(patch_text) |
| 114 | + |
| 115 | + logger.print_info_line( |
| 116 | + project.name, |
| 117 | + f"formatted patch written to {output_patch_file}", |
| 118 | + ) |
| 119 | + |
| 120 | + if exceptions: |
| 121 | + raise RuntimeError("\n".join(exceptions)) |
0 commit comments