All of lore.kernel.org
 help / color / mirror / Atom feed
From: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
To: buildroot@busybox.net
Subject: [Buildroot] [PATCH] support/scripts/apply-patches: use "git apply" as a fallback when applying patches
Date: Thu, 10 Jan 2019 21:30:04 +0100	[thread overview]
Message-ID: <20190110203004.9812-1-thomas.petazzoni@bootlin.com> (raw)

We currently use plain old "patch" to apply patches. While this works
fine in most situations, it doesn't work well for patches generated
with git format-patch that:

 - Contain changes to binary files

 - Or contain changes to files in directories that were symlinks to
   other directories, with the symlink being removed as part of the
   same patch.

We encountered such issues with a large stack of patches to be applied
on top of arm-trusted-firmware. Such patches apply perfectly fine with
"git apply".

Switching everybody to unconditionally use "git apply" seems a bit
risky, so instead we take a different route: if applying the patch
with "patch" fails, then we try with "git apply".

A few implementation notes:

 - The script has "set -e" so for the "patch --dry-run" command, we
   have to take special precautions to not bail out of the script on
   error.

 - Also due to "set -e", the check on the return value of "patch" is
   no longer needed, and we also don't need to check the return value
   of "git apply".

 - We need to pass "--git-dir=/dev/null", otherwise "git apply"
   travels up the directory hierarchy until it finds a .git folder. If
   it finds one, but the files being patched are not under version
   control, it skips patching those files. Obviously, the files in
   output/build/foo/ are not under version control, so this behavior
   is not desirable. Simply making "git apply" believe it is not
   running from inside a Git repository disables this check.

Cc: Kostya Porotchkin <kostap@marvell.com>
Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
---
 support/scripts/apply-patches.sh | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/support/scripts/apply-patches.sh b/support/scripts/apply-patches.sh
index 66fef262ee..0fd336968e 100755
--- a/support/scripts/apply-patches.sh
+++ b/support/scripts/apply-patches.sh
@@ -119,11 +119,18 @@ function apply_patch {
         exit 1
     fi
     echo "${path}/${patch}" >> ${builddir}/.applied_patches_list
-    ${uncomp} "${path}/$patch" | patch -g0 -p1 -E -d "${builddir}" -t -N $silent
-    if [ $? != 0 ] ; then
-        echo "Patch failed!  Please fix ${patch}!"
-        exit 1
+
+    # We don't want this to abort the script on failure (script is run
+    # with set -e)
+    ${uncomp} "${path}/$patch" | patch -g0 -p1 -E -d "${builddir}" --dry-run -t -N -s && retval=0 || retval=$?
+    if [ $retval -eq 0 ] ; then
+	    ${uncomp} "${path}/$patch" | patch -g0 -p1 -E -d "${builddir}" -t -N $silent
+	    # Due to set -e, if we reach here, applying the patch was successful
+	    return
     fi
+
+    [ -z "${silent}" ] && gitopts=-v
+    (cd ${builddir}; ${uncomp} "${path}/$patch" | git --git-dir=/dev/null apply -p1 ${gitopts})
 }
 
 function scan_patchdir {
-- 
2.20.1

             reply	other threads:[~2019-01-10 20:30 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-10 20:30 Thomas Petazzoni [this message]
2019-01-11  3:35 ` [Buildroot] [PATCH] support/scripts/apply-patches: use "git apply" as a fallback when applying patches Baruch Siach
2019-01-11  8:15   ` Thomas Petazzoni
2019-01-13  8:00     ` Baruch Siach
2019-01-23 14:17       ` Arnout Vandecappelle
2019-01-23 20:34         ` Peter Korsgaard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190110203004.9812-1-thomas.petazzoni@bootlin.com \
    --to=thomas.petazzoni@bootlin.com \
    --cc=buildroot@busybox.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.