apply-patches.sh 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #! /bin/bash
  2. # A little script I whipped up to make it easy to
  3. # patch source trees and have sane error handling
  4. # -Erik
  5. #
  6. # (c) 2002 Erik Andersen <andersen@codepoet.org>
  7. # Set directories from arguments, or use defaults.
  8. builddir=${1-.}
  9. patchdir=${2-../kernel-patches}
  10. shift 2
  11. patchpattern=${@-*}
  12. if [ ! -d "${builddir}" ] ; then
  13. echo "Aborting. '${builddir}' is not a directory."
  14. exit 1
  15. fi
  16. if [ ! -d "${patchdir}" ] ; then
  17. echo "Aborting. '${patchdir}' is not a directory."
  18. exit 1
  19. fi
  20. # Remove any rejects present BEFORE patching - Because if there are
  21. # any, even if patches are well applied, at the end it will complain
  22. # about rejects in builddir.
  23. find ${builddir}/ '(' -name '*.rej' -o -name '.*.rej' ')' -print0 | \
  24. xargs -0 -r rm -f
  25. function apply_patch {
  26. path=$1
  27. patch=$2
  28. case "$patch" in
  29. *.gz)
  30. type="gzip"; uncomp="gunzip -dc"; ;;
  31. *.bz)
  32. type="bzip"; uncomp="bunzip -dc"; ;;
  33. *.bz2)
  34. type="bzip2"; uncomp="bunzip2 -dc"; ;;
  35. *.zip)
  36. type="zip"; uncomp="unzip -d"; ;;
  37. *.Z)
  38. type="compress"; uncomp="uncompress -c"; ;;
  39. *)
  40. type="plaintext"; uncomp="cat"; ;;
  41. esac
  42. echo ""
  43. echo "Applying $patch using ${type}: "
  44. echo $patch >> ${builddir}/.applied_patches_list
  45. ${uncomp} "${path}/$patch" | patch -g0 -p1 -E -d "${builddir}"
  46. if [ $? != 0 ] ; then
  47. echo "Patch failed! Please fix ${patch}!"
  48. exit 1
  49. fi
  50. }
  51. function scan_patchdir {
  52. path=$1
  53. shift 1
  54. patches=${@-*}
  55. for i in `cd $path; ls -d $patches 2> /dev/null` ; do
  56. if [ -d "${path}/$i" ] ; then
  57. echo "${path}/$i skipped"
  58. elif echo "$i" | grep -q -E "\.tar(\..*)?$|\.tbz2?$|\.tgz$" ; then
  59. unpackedarchivedir="$builddir/.patches-$(basename $i)-unpacked"
  60. rm -rf "$unpackedarchivedir" 2> /dev/null
  61. mkdir "$unpackedarchivedir"
  62. tar -C "$unpackedarchivedir" --strip-components=1 -xaf "${path}/$i"
  63. scan_patchdir "$unpackedarchivedir"
  64. else
  65. apply_patch "$path" "$i" || exit 1
  66. fi
  67. done
  68. }
  69. scan_patchdir $patchdir $patchpattern
  70. # Check for rejects...
  71. if [ "`find $builddir/ '(' -name '*.rej' -o -name '.*.rej' ')' -print`" ] ; then
  72. echo "Aborting. Reject files found."
  73. exit 1
  74. fi
  75. # Remove backup files
  76. find $builddir/ '(' -name '*.orig' -o -name '.*.orig' ')' -exec rm -f {} \;