From 14ecc86b60caad7f9e65552128353222b3eb8334 Mon Sep 17 00:00:00 2001 From: Ricardo Pardini Date: Sun, 8 Jan 2023 20:17:19 +0100 Subject: [PATCH] armbian-next: patching: grouping tryout, very basic by-dir: `SPLIT_PATCHES=yes` --- lib/tools/common/patching_utils.py | 40 +++++++++++++++++++++++++++++- lib/tools/patching.py | 19 ++++++++------ 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/lib/tools/common/patching_utils.py b/lib/tools/common/patching_utils.py index 863500e372..12f412ed90 100644 --- a/lib/tools/common/patching_utils.py +++ b/lib/tools/common/patching_utils.py @@ -419,7 +419,7 @@ class PatchInPatchFile: raise Exception( f"Failed to apply patch {self.parent.full_file_path()}:{stderr_output}{stdout_output}") - def commit_changes_to_git(self, repo: git.Repo, add_rebase_tags: bool): + def commit_changes_to_git(self, repo: git.Repo, add_rebase_tags: bool, split_patches: bool): log.info(f"Committing changes to git: {self.parent.relative_dirs_and_base_file_name}") # add all the files that were touched by the patch # if the patch failed to parse, this will be an empty list, so we'll just add all changes. @@ -440,6 +440,10 @@ class PatchInPatchFile: add_all_changes_in_git = True else: all_files_to_add.append(file_name) + + if split_patches: + return self.commit_changes_to_git_grouped(all_files_to_add, repo) + if not add_all_changes_in_git: repo.git.add("-f", all_files_to_add) @@ -471,6 +475,40 @@ class PatchInPatchFile: f"Commit {commit.hexsha} ended up empty; source patch is {self} at {self.parent.full_file_path()}(:{self.counter})") return {"commit_hash": commit.hexsha, "patch": self} + def commit_changes_to_git_grouped(self, all_files_to_add: list[str], repo: git.Repo): + all_commits = [] + prefix = "Feiteng " + grouped_files = {} + # group files by directory + for file_name in all_files_to_add: + dir_name = os.path.dirname(file_name) + if dir_name not in grouped_files: + grouped_files[dir_name] = [] + grouped_files[dir_name].append(file_name) + + for group_name, files in grouped_files.items(): + for one_file in files: + repo.git.add(one_file) + + commit_message = f"{prefix}{group_name}\n\n{prefix}{group_name}" + author: git.Actor = git.Actor("Ricardo Pardini", "ricardo@pardini.net") + commit = repo.index.commit( + message=commit_message, + author=author, + committer=author, + author_date=self.date, + commit_date=self.date, + skip_hooks=True + ) + log.info(f"Committed changes to git: {commit.hexsha}") + # Make sure the commit is not empty + if commit.stats.total["files"] == 0: + self.problems.append("empty_commit") + raise Exception( + f"Commit {commit.hexsha} ended up empty; source patch is {self} at {self.parent.full_file_path()}(:{self.counter})") + all_commits.append({"commit_hash": commit.hexsha, "patch": self}) + return all_commits + def patch_rebase_tags_desc(self): tags = {} tags["Patch-File"] = self.parent.relative_dirs_and_base_file_name diff --git a/lib/tools/patching.py b/lib/tools/patching.py index e7d4636aaa..99d163d121 100755 --- a/lib/tools/patching.py +++ b/lib/tools/patching.py @@ -26,6 +26,7 @@ PATCH_DIRS_TO_APPLY = armbian_utils.parse_env_for_tokens("PATCH_DIRS_TO_APPLY") APPLY_PATCHES = armbian_utils.get_from_env("APPLY_PATCHES") PATCHES_TO_GIT = armbian_utils.get_from_env("PATCHES_TO_GIT") REWRITE_PATCHES = armbian_utils.get_from_env("REWRITE_PATCHES") +SPLIT_PATCHES = armbian_utils.get_from_env("SPLIT_PATCHES") ALLOW_RECREATE_EXISTING_FILES = armbian_utils.get_from_env("ALLOW_RECREATE_EXISTING_FILES") GIT_ARCHEOLOGY = armbian_utils.get_from_env("GIT_ARCHEOLOGY") FAST_ARCHEOLOGY = armbian_utils.get_from_env("FAST_ARCHEOLOGY") @@ -34,6 +35,7 @@ apply_patches_to_git = PATCHES_TO_GIT == "yes" git_archeology = GIT_ARCHEOLOGY == "yes" fast_archeology = FAST_ARCHEOLOGY == "yes" rewrite_patches_in_place = REWRITE_PATCHES == "yes" +split_patches = SPLIT_PATCHES == "yes" apply_options = { "allow_recreate_existing_files": (ALLOW_RECREATE_EXISTING_FILES == "yes"), "set_patch_date": True, @@ -225,13 +227,16 @@ if apply_patches: log.error(f"Exception while applying patch {one_patch}: {e}", exc_info=True) if one_patch.applied_ok and apply_patches_to_git: - committed = one_patch.commit_changes_to_git(git_repo, (not rewrite_patches_in_place)) - commit_hash = committed['commit_hash'] - one_patch.git_commit_hash = commit_hash - if rewrite_patches_in_place: - rewritten_patch = patching_utils.export_commit_as_patch( - git_repo, commit_hash) - one_patch.rewritten_patch = rewritten_patch + committed = one_patch.commit_changes_to_git(git_repo, (not rewrite_patches_in_place), split_patches) + + if not split_patches: + commit_hash = committed['commit_hash'] + one_patch.git_commit_hash = commit_hash + + if rewrite_patches_in_place: + rewritten_patch = patching_utils.export_commit_as_patch( + git_repo, commit_hash) + one_patch.rewritten_patch = rewritten_patch if rewrite_patches_in_place: # Now; we need to write the patches to files.