From 3f3d1c0437fd3dfe1cb11fc622d317daa004ea2a Mon Sep 17 00:00:00 2001 From: Andrew Robbins Date: Mon, 18 Mar 2019 14:11:47 -0400 Subject: Rename and refactor diff_patch_file() The previous function, diff_patch_file, was more error-prone and fragile and did not take into consideration patching of more than one file at a time(!). --- libs/common | 19 ++++--------------- libs/git | 2 +- libs/project | 2 +- 3 files changed, 6 insertions(+), 17 deletions(-) diff --git a/libs/common b/libs/common index a27ff785..06b411e1 100755 --- a/libs/common +++ b/libs/common @@ -110,22 +110,11 @@ download_wrapper() { fi } -diff_patch_file() { - local repository_path="$1" - local patch_file_path="$2" +diff_patch() { + local sources_path=$1 + local patch_path=$2 - # TODO: Improve handling of filenames to avoid gotchas w/ \n, \t, etc. - local filename_in_diff="$(sed -rne 's/^-{3}\s+([^ \r\n]*).*/\1/p' "$patch_file_path")" - - local source_file_path - - if ! ( grep -E '^-{3}.*/' "$patch_file_path" >/dev/null 2>&1 ); then - source_file_path="$repository_path/$filename_in_diff" - else - source_file_path="$repository_path/${filename_in_diff##*/}" - fi - - patch "$source_file_path" "$patch_file_path" + patch -fd "$sources_path" -r - < "$patch_path" } path_wildcard_expand() { diff --git a/libs/git b/libs/git index 16dd5411..998ceae7 100755 --- a/libs/git +++ b/libs/git @@ -249,7 +249,7 @@ git_project_patch_recursive() { if [[ ${patch##*.} == patch ]]; then git_patch "$repository_path" "$branch" "$patch" else - diff_patch_file "$repository_path" "$patch" + diff_patch "$repository_path" "$patch" fi done } diff --git a/libs/project b/libs/project index f90b368e..70a76d71 100755 --- a/libs/project +++ b/libs/project @@ -531,7 +531,7 @@ project_sources_prepare_patch() { local patches_path="$project_path/$PATCHES" for patch in "$patches_path"/[!.]*.@(patch|diff); do - diff_patch_file "$sources_path" "$patch" + diff_patch "$sources_path" "$patch" done } -- cgit v1.2.3-70-g09d2 From 88a7101827ebd8ce2adb0d3d451254614d80904f Mon Sep 17 00:00:00 2001 From: Andrew Robbins Date: Mon, 18 Mar 2019 16:34:52 -0400 Subject: libs/git: Add git_apply() and git_apply_check() Since changes from an applied diff need to be added to the index and committed (vs. mailbox files with the git_patch function) new functions were necessary to account for this difference. This also fixes a bug where diffs were applied but not committed, at minimum dirtying the work tree and potentially causing problems for any later patches. --- libs/git | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/libs/git b/libs/git index 998ceae7..358d5d1f 100755 --- a/libs/git +++ b/libs/git @@ -169,6 +169,37 @@ git_commit() { ) } +git_apply() { + local repository_path=$1 + local branch=$2 + local patch=$3 + + ( + export GIT_COMMITTER_NAME=$GIT_NAME + export GIT_COMMITTER_EMAIL=$GIT_EMAIL + + cd "$repository_path" + git checkout "$branch" >/dev/null 2>&1 + + if git_apply_check "$repository_path" "$branch" "$patch"; then + git apply --index "$patch" + fi + ) +} + +git_apply_check() { + local repository_path=$1 + local branch=$2 + local patch=$3 + + ( + cd "$repository_path" + git checkout "$branch" >/dev/null 2>&1 + + git apply --check "$patch" + ) +} + git_patch() { local repository_path=$1 local branch=$2 @@ -249,7 +280,8 @@ git_project_patch_recursive() { if [[ ${patch##*.} == patch ]]; then git_patch "$repository_path" "$branch" "$patch" else - diff_patch "$repository_path" "$patch" + git_apply "$repository_path" "$branch" "$patch" + git_commit "$repository_path" "Applied ${patch##*/}" fi done } -- cgit v1.2.3-70-g09d2 From f3426c1e431a6cdf2c14963a91a219545ca5b1c8 Mon Sep 17 00:00:00 2001 From: Andrew Robbins Date: Mon, 18 Mar 2019 19:12:49 -0400 Subject: libs/git: Move patching method logic to git_patch() This simplifies git_project_patch_recursive() and provides a unified method for patching, regardless of the patch format (mailbox or diff). Logic is included for dealing with patching failures due to git_apply_check() not being enough to catch certain edge cases, such as 'git apply --check' returning 0 but a subsequent 'git apply' failing. --- libs/git | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/libs/git b/libs/git index 358d5d1f..104239c1 100755 --- a/libs/git +++ b/libs/git @@ -210,9 +210,19 @@ git_patch() { export GIT_COMMITTER_EMAIL=$GIT_EMAIL cd "$repository_path" - git checkout "$branch" 2> /dev/null > /dev/null + git checkout "$branch" >/dev/null 2>&1 - git am "$patch" || git am --abort + case $patch in + *.patch) + git am "$patch" || git am --abort + ;; + *.diff) + git_apply "$repository_path" "$branch" "$patch" && + git_commit "$repository_path" "Applied ${patch##*/}" + ;; + *) + ;; + esac ) } @@ -277,12 +287,7 @@ git_project_patch_recursive() { fi for patch in "$patches_path"/[!.]*.@(patch|diff); do - if [[ ${patch##*.} == patch ]]; then - git_patch "$repository_path" "$branch" "$patch" - else - git_apply "$repository_path" "$branch" "$patch" - git_commit "$repository_path" "Applied ${patch##*/}" - fi + git_patch "$repository_path" "$branch" "$patch" done } -- cgit v1.2.3-70-g09d2 From 4a493108d1aefe3151096c3b22234d0c8cf893d9 Mon Sep 17 00:00:00 2001 From: Andrew Robbins Date: Mon, 18 Mar 2019 22:17:45 -0400 Subject: libs/git: Avoid some redundancies in git_patch() The subshell, changing directory, and checking out was unecessary considering all of these are done within the functions called by git_patch() git_am() was added in order to mirror the same wrapper style as used by git_apply() and git_commit() --- libs/git | 48 +++++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/libs/git b/libs/git index 104239c1..f5d764ef 100755 --- a/libs/git +++ b/libs/git @@ -169,7 +169,7 @@ git_commit() { ) } -git_apply() { +git_am() { local repository_path=$1 local branch=$2 local patch=$3 @@ -182,50 +182,60 @@ git_apply() { git checkout "$branch" >/dev/null 2>&1 if git_apply_check "$repository_path" "$branch" "$patch"; then - git apply --index "$patch" + git am "$patch" || git am --abort fi ) } -git_apply_check() { +git_apply() { local repository_path=$1 local branch=$2 local patch=$3 ( + export GIT_COMMITTER_NAME=$GIT_NAME + export GIT_COMMITTER_EMAIL=$GIT_EMAIL + cd "$repository_path" git checkout "$branch" >/dev/null 2>&1 - git apply --check "$patch" + if git_apply_check "$repository_path" "$branch" "$patch"; then + git apply --index "$patch" + fi ) } -git_patch() { +git_apply_check() { local repository_path=$1 local branch=$2 local patch=$3 ( - export GIT_COMMITTER_NAME=$GIT_NAME - export GIT_COMMITTER_EMAIL=$GIT_EMAIL - cd "$repository_path" git checkout "$branch" >/dev/null 2>&1 - case $patch in - *.patch) - git am "$patch" || git am --abort - ;; - *.diff) - git_apply "$repository_path" "$branch" "$patch" && - git_commit "$repository_path" "Applied ${patch##*/}" - ;; - *) - ;; - esac + git apply --check "$patch" ) } +git_patch() { + local repository_path=$1 + local branch=$2 + local patch=$3 + + case $patch in + *.patch) + git_am "$repository_path" "$branch" "$patch" + ;; + *.diff) + git_apply "$repository_path" "$branch" "$patch" && + git_commit "$repository_path" "Applied ${patch##*/}" + ;; + *) + ;; + esac +} + git_revision() { local repository_path=$1 -- cgit v1.2.3-70-g09d2