Skip to content

Commit

Permalink
Refactor patch application in fetch_meta: add apply_patch function
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
dbt1 committed Feb 14, 2025
1 parent 7961947 commit 6d4ae33
Showing 1 changed file with 45 additions and 8 deletions.
53 changes: 45 additions & 8 deletions init.functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,41 @@ function get_metaname() {
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
function fetch_meta() {
local layer_name="$1"
Expand Down Expand Up @@ -115,14 +150,8 @@ function fetch_meta() {
## Patching
if [[ -n "$patch_list" ]]; then
for patch_file in $patch_list; do
my_echo -e "Applying patch: $patch_file"
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
fi
else
my_echo -e "\033[33;1mSkipping patch $patch_file already applied or cannot be applied cleanly.\033[0m"
if ! apply_patch "$target_git_path" "$patch_file" "$layer_name"; then
return 1
fi
done
fi
Expand All @@ -136,6 +165,14 @@ function fetch_meta() {
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\" 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 do_exec "git -C \"$target_git_path\" stash pop"; then
my_echo -e "Stash applied successfully."
Expand Down

0 comments on commit 6d4ae33

Please sign in to comment.