|
40 | 40 | from dfetch.project import create_super_project |
41 | 41 | from dfetch.project.gitsuperproject import GitSuperProject |
42 | 42 | from dfetch.project.metadata import Metadata |
43 | | -from dfetch.project.superproject import NoVcsSuperProject, RevisionRange |
| 43 | +from dfetch.project.superproject import NoVcsSuperProject, RevisionRange, SuperProject |
44 | 44 | from dfetch.util.util import ( |
45 | 45 | check_no_path_traversal, |
46 | 46 | in_directory, |
@@ -88,73 +88,79 @@ def __call__(self, args: argparse.Namespace) -> None: |
88 | 88 | with in_directory(superproject.root_directory): |
89 | 89 | for project in superproject.manifest.selected_projects(args.projects): |
90 | 90 | try: |
91 | | - subproject = dfetch.project.create_sub_project(project) |
92 | | - destination = project.destination |
93 | | - |
94 | | - def _ignored(dst: str = destination) -> list[str]: |
95 | | - return list(superproject.ignored_files(dst)) |
96 | | - |
97 | | - # Check if the project has a patch, maybe suggest creating one? |
98 | | - if not subproject.patch: |
99 | | - logger.print_warning_line( |
100 | | - project.name, |
101 | | - f'skipped - there is no patch file, use "dfetch diff {project.name}"' |
102 | | - " to generate one instead", |
103 | | - ) |
104 | | - continue |
105 | | - |
106 | | - # Check if the project was ever fetched |
107 | | - on_disk_version = subproject.on_disk_version() |
108 | | - if not on_disk_version: |
109 | | - logger.print_warning_line( |
110 | | - project.name, |
111 | | - f'skipped - the project was never fetched before, use "dfetch update {project.name}"', |
112 | | - ) |
113 | | - continue |
114 | | - |
115 | | - # Make sure no uncommitted changes (don't care about ignored files) |
116 | | - if superproject.has_local_changes_in_dir(subproject.local_path): |
117 | | - logger.print_warning_line( |
118 | | - project.name, |
119 | | - f"skipped - Uncommitted changes in {subproject.local_path}", |
120 | | - ) |
121 | | - continue |
122 | | - |
123 | | - # force update to fetched version from metadata without applying patch |
124 | | - subproject.update( |
125 | | - force=True, |
126 | | - ignored_files_callback=_ignored, |
127 | | - patch_count=len(subproject.patch) - 1, |
128 | | - ) |
129 | | - |
130 | | - # generate reverse patch |
131 | | - patch_text = superproject.diff( |
132 | | - subproject.local_path, |
133 | | - revisions=RevisionRange("", ""), |
134 | | - ignore=(Metadata.FILENAME,), |
135 | | - reverse=True, |
136 | | - ) |
137 | | - |
138 | | - # Select patch to overwrite & make backup |
139 | | - if not self._update_patch( |
140 | | - subproject.patch[-1], |
141 | | - superproject.root_directory, |
142 | | - project.name, |
143 | | - patch_text, |
144 | | - ): |
145 | | - continue |
146 | | - |
147 | | - # force update again to fetched version from metadata but with applying patch |
148 | | - subproject.update( |
149 | | - force=True, ignored_files_callback=_ignored, patch_count=-1 |
150 | | - ) |
| 91 | + self._process_project(superproject, project) |
151 | 92 | except RuntimeError as exc: |
152 | 93 | logger.print_warning_line(project.name, str(exc)) |
153 | 94 | had_errors = True |
154 | 95 |
|
155 | 96 | if had_errors: |
156 | 97 | raise RuntimeError() |
157 | 98 |
|
| 99 | + def _process_project( |
| 100 | + self, |
| 101 | + superproject: SuperProject, |
| 102 | + project: dfetch.manifest.project.ProjectEntry, |
| 103 | + ) -> None: |
| 104 | + """Perform the patch update for a single project.""" |
| 105 | + subproject = dfetch.project.create_sub_project(project) |
| 106 | + destination = project.destination |
| 107 | + |
| 108 | + def _ignored(dst: str = destination) -> list[str]: |
| 109 | + return list(superproject.ignored_files(dst)) |
| 110 | + |
| 111 | + # Check if the project has a patch, maybe suggest creating one? |
| 112 | + if not subproject.patch: |
| 113 | + logger.print_warning_line( |
| 114 | + project.name, |
| 115 | + f'skipped - there is no patch file, use "dfetch diff {project.name}"' |
| 116 | + " to generate one instead", |
| 117 | + ) |
| 118 | + return |
| 119 | + |
| 120 | + # Check if the project was ever fetched |
| 121 | + on_disk_version = subproject.on_disk_version() |
| 122 | + if not on_disk_version: |
| 123 | + logger.print_warning_line( |
| 124 | + project.name, |
| 125 | + f'skipped - the project was never fetched before, use "dfetch update {project.name}"', |
| 126 | + ) |
| 127 | + return |
| 128 | + |
| 129 | + # Make sure no uncommitted changes (don't care about ignored files) |
| 130 | + if superproject.has_local_changes_in_dir(subproject.local_path): |
| 131 | + logger.print_warning_line( |
| 132 | + project.name, |
| 133 | + f"skipped - Uncommitted changes in {subproject.local_path}", |
| 134 | + ) |
| 135 | + return |
| 136 | + |
| 137 | + # force update to fetched version from metadata without applying patch |
| 138 | + subproject.update( |
| 139 | + force=True, |
| 140 | + ignored_files_callback=_ignored, |
| 141 | + patch_count=len(subproject.patch) - 1, |
| 142 | + ) |
| 143 | + |
| 144 | + # generate reverse patch |
| 145 | + patch_text = superproject.diff( |
| 146 | + subproject.local_path, |
| 147 | + revisions=RevisionRange("", ""), |
| 148 | + ignore=(Metadata.FILENAME,), |
| 149 | + reverse=True, |
| 150 | + ) |
| 151 | + |
| 152 | + # Select patch to overwrite & make backup |
| 153 | + if not self._update_patch( |
| 154 | + subproject.patch[-1], |
| 155 | + superproject.root_directory, |
| 156 | + project.name, |
| 157 | + patch_text, |
| 158 | + ): |
| 159 | + return |
| 160 | + |
| 161 | + # force update again to fetched version from metadata but with applying patch |
| 162 | + subproject.update(force=True, ignored_files_callback=_ignored, patch_count=-1) |
| 163 | + |
158 | 164 | def _update_patch( |
159 | 165 | self, |
160 | 166 | patch_to_update: str, |
|
0 commit comments