|
| 1 | +"""Updating patches. |
| 2 | +
|
| 3 | +*Dfetch* allows you to keep local changes to external projects in the form of |
| 4 | +patch files. When those local changes evolve over time, an existing patch can |
| 5 | +be updated to reflect the new state of the project. |
| 6 | +
|
| 7 | +The ``update-patch`` command automates the otherwise manual process of |
| 8 | +refreshing a patch. It safely regenerates the last patch of a project based on |
| 9 | +the current working tree, while keeping the upstream revision unchanged. |
| 10 | +
|
| 11 | +This command operates on projects defined in the :ref:`Manifest` and requires |
| 12 | +that the manifest itself is located inside a version-controlled repository |
| 13 | +(the *superproject*). The version control system of the superproject is used to |
| 14 | +calculate and regenerate the patch. |
| 15 | +
|
| 16 | +The existing patch is backed up before being overwritten. |
| 17 | +
|
| 18 | +The below statement will update the patch for ``some-project`` from your manifest. |
| 19 | +
|
| 20 | +.. code-block:: sh |
| 21 | +
|
| 22 | + dfetch update-patch some-project |
| 23 | +
|
| 24 | +.. tabs:: |
| 25 | +
|
| 26 | + .. tab:: Git |
| 27 | +
|
| 28 | + .. scenario-include:: ../features/update-patch-in-git.feature |
| 29 | +
|
| 30 | + .. tab:: SVN |
| 31 | +
|
| 32 | + .. scenario-include:: ../features/update-patch-in-svn.feature |
| 33 | +""" |
| 34 | + |
| 35 | +import argparse |
| 36 | +import pathlib |
| 37 | +import shutil |
| 38 | + |
| 39 | +import dfetch.commands.command |
| 40 | +import dfetch.manifest.project |
| 41 | +import dfetch.project |
| 42 | +from dfetch.log import get_logger |
| 43 | +from dfetch.project.superproject import SuperProject |
| 44 | +from dfetch.util.util import catch_runtime_exceptions, in_directory |
| 45 | + |
| 46 | +logger = get_logger(__name__) |
| 47 | + |
| 48 | + |
| 49 | +class UpdatePatch(dfetch.commands.command.Command): |
| 50 | + """Update a patch to reflect the last changes. |
| 51 | +
|
| 52 | + The ``update-patch`` command regenerates the last patch of one or |
| 53 | + more projects based on the current working tree. This is useful |
| 54 | + when you have modified a project after applying a patch and want |
| 55 | + to record those changes in an updated patch file. If there is no |
| 56 | + patch yet, use ``dfetch diff`` instead. |
| 57 | + """ |
| 58 | + |
| 59 | + @staticmethod |
| 60 | + def create_menu(subparsers: dfetch.commands.command.SubparserActionType) -> None: |
| 61 | + """Add the menu for the update-patch action.""" |
| 62 | + parser = dfetch.commands.command.Command.parser(subparsers, UpdatePatch) |
| 63 | + parser.add_argument( |
| 64 | + "projects", |
| 65 | + metavar="<project>", |
| 66 | + type=str, |
| 67 | + nargs="*", |
| 68 | + help="Specific project(s) to update", |
| 69 | + ) |
| 70 | + |
| 71 | + def __call__(self, args: argparse.Namespace) -> None: |
| 72 | + """Perform the update patch.""" |
| 73 | + superproject = SuperProject() |
| 74 | + |
| 75 | + exceptions: list[str] = [] |
| 76 | + |
| 77 | + if not superproject.in_vcs(): |
| 78 | + raise RuntimeError( |
| 79 | + "The project containing the manifest is not under version control," |
| 80 | + " updating patches is not supported" |
| 81 | + ) |
| 82 | + |
| 83 | + with in_directory(superproject.root_directory): |
| 84 | + for project in superproject.manifest.selected_projects(args.projects): |
| 85 | + with catch_runtime_exceptions(exceptions) as exceptions: |
| 86 | + subproject = dfetch.project.make(project) |
| 87 | + |
| 88 | + files_to_ignore = superproject.ignored_files(project.destination) |
| 89 | + |
| 90 | + # Check if the project has a patch, maybe suggest creating one? |
| 91 | + if not subproject.patch: |
| 92 | + logger.print_warning_line( |
| 93 | + project.name, |
| 94 | + f'skipped - there is no patch file, use "dfetch diff {project.name}" instead', |
| 95 | + ) |
| 96 | + return |
| 97 | + |
| 98 | + # Check if the project was ever fetched |
| 99 | + on_disk_version = subproject.on_disk_version() |
| 100 | + if not on_disk_version: |
| 101 | + logger.print_warning_line( |
| 102 | + project.name, |
| 103 | + f'skipped - the project was never fetched before, use "dfetch update {project.name}"', |
| 104 | + ) |
| 105 | + return |
| 106 | + |
| 107 | + # Make sure no uncommitted changes (don't care about ignored files) |
| 108 | + if superproject.has_local_changes_in_dir(subproject.local_path): |
| 109 | + logger.print_warning_line( |
| 110 | + project.name, |
| 111 | + f"skipped - Uncommitted changes in {subproject.local_path}", |
| 112 | + ) |
| 113 | + return |
| 114 | + |
| 115 | + # force update to fetched version from metadata without applying patch |
| 116 | + subproject.update( |
| 117 | + force=True, |
| 118 | + files_to_ignore=files_to_ignore, |
| 119 | + patch_count=len(subproject.patch) - 1, |
| 120 | + ) |
| 121 | + |
| 122 | + # generate reverse patch |
| 123 | + patch_text = subproject.diff( |
| 124 | + old_revision=superproject.current_revision(), |
| 125 | + new_revision="", |
| 126 | + # ignore=files_to_ignore, |
| 127 | + reverse=True, |
| 128 | + ) |
| 129 | + |
| 130 | + # Select patch to overwrite & make backup |
| 131 | + patch_to_update = subproject.patch[-1] |
| 132 | + |
| 133 | + if patch_text: |
| 134 | + shutil.move(patch_to_update, patch_to_update + ".backup") |
| 135 | + patch_path = pathlib.Path(patch_to_update) |
| 136 | + logger.print_info_line( |
| 137 | + project.name, f"Updating patch {patch_to_update}" |
| 138 | + ) |
| 139 | + patch_path.write_text(patch_text, encoding="UTF-8") |
| 140 | + else: |
| 141 | + logger.print_info_line( |
| 142 | + project.name, |
| 143 | + f"No diffs found, kept patch {patch_to_update} unchanged", |
| 144 | + ) |
| 145 | + |
| 146 | + # force update again to fetched version from metadata but with applying patch |
| 147 | + subproject.update( |
| 148 | + force=True, files_to_ignore=files_to_ignore, patch_count=-1 |
| 149 | + ) |
| 150 | + |
| 151 | + if exceptions: |
| 152 | + raise RuntimeError("\n".join(exceptions)) |
0 commit comments