Refactor patch application in fetch_meta: add apply_patch function

- Introduced a new function `apply_patch` to encapsulate patch application logic.
- The function first checks (using a reverse check) if the patch is
  already applied.
- If not, it uses `git apply --check` and `git apply` to apply the patch,
  then commits the change.
- Updated `fetch_meta` to call `apply_patch` for all patches instead of
  using `git am`.
- This refactoring simplifies the patching process and ensures that
  partially applied or faulty patches do not leave the repository
  in an inconsistent state.
This commit is contained in:
2025-02-14 22:07:17 +01:00
parent 7961947fa9
commit 6d4ae33a0e

View File

@@ -87,6 +87,41 @@ function get_metaname() {
echo "$META_NAME" echo "$META_NAME"
} }
## Function to apply a patch using git apply with check and commit
# Parameters:
# $1: target_git_path (Path to the Git repository)
# $2: patch_file (Name of the patch file located in $FILES_DIR)
# $3: layer_name (Name of the layer, only for log output)
function apply_patch() {
local target_git_path="$1"
local patch_file="$2"
local layer_name="$3"
my_echo -e "Applying patch: $patch_file"
# Check if the patch has already been applied by testing it in reverse
if git -C "$target_git_path" apply --reverse --check "$FILES_DIR/$patch_file" > /dev/null 2>&1; then
my_echo -e "\033[33;1mPatch $patch_file already applied to $layer_name; skipping.\033[0m"
return 0
fi
# Check if the patch can be applied cleanly
if do_exec "git -C \"$target_git_path\" apply --check \"$FILES_DIR/$patch_file\""; then
if do_exec "git -C \"$target_git_path\" apply \"$FILES_DIR/$patch_file\""; then
# After successfully applying the patch: add changes and commit
do_exec "git -C \"$target_git_path\" add -A"
do_exec "git -C \"$target_git_path\" commit -m \"Apply patch $patch_file\""
else
my_echo -e "\033[31;1mFailed to apply patch $patch_file to $layer_name using git apply\033[0m"
return 1
fi
else
my_echo -e "\033[33;1mSkipping patch $patch_file: cannot be applied cleanly.\033[0m"
fi
return 0
}
## Clone or update required branch for a meta-layer ## Clone or update required branch for a meta-layer
function fetch_meta() { function fetch_meta() {
local layer_name="$1" local layer_name="$1"
@@ -115,15 +150,9 @@ function fetch_meta() {
## Patching ## Patching
if [[ -n "$patch_list" ]]; then if [[ -n "$patch_list" ]]; then
for patch_file in $patch_list; do for patch_file in $patch_list; do
my_echo -e "Applying patch: $patch_file" if ! apply_patch "$target_git_path" "$patch_file" "$layer_name"; then
if do_exec "git -C \"$target_git_path\" apply --check \"$FILES_DIR/$patch_file\""; then
if ! do_exec "git -C \"$target_git_path\" am < \"$FILES_DIR/$patch_file\""; then
my_echo -e "\033[31;1mFailed to apply patch $patch_file to $layer_name\033[0m"
return 1 return 1
fi fi
else
my_echo -e "\033[33;1mSkipping patch $patch_file already applied or cannot be applied cleanly.\033[0m"
fi
done done
fi fi
else else
@@ -136,6 +165,14 @@ function fetch_meta() {
fi fi
do_exec "git -C \"$target_git_path\" checkout \"$branch_name\"" || do_exec "git -C \"$target_git_path\" checkout -b \"$branch_name\"" do_exec "git -C \"$target_git_path\" checkout \"$branch_name\"" || do_exec "git -C \"$target_git_path\" checkout -b \"$branch_name\""
do_exec "git -C \"$target_git_path\" pull -r origin \"$branch_name\"" do_exec "git -C \"$target_git_path\" pull -r origin \"$branch_name\""
## Patching
if [[ -n "$patch_list" ]]; then
for patch_file in $patch_list; do
if ! apply_patch "$target_git_path" "$patch_file" "$layer_name"; then
return 1
fi
done
fi
if [[ "$stash_applied" == true ]]; then if [[ "$stash_applied" == true ]]; then
if do_exec "git -C \"$target_git_path\" stash pop"; then if do_exec "git -C \"$target_git_path\" stash pop"; then
my_echo -e "Stash applied successfully." my_echo -e "Stash applied successfully."