apply-patches.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. # If there is a series file, use it instead of using ls sort order
  56. # to apply patches. Skip line starting with a dash.
  57. if [ -e "${path}/series" ] ; then
  58. for i in `grep -Ev "^#" ${path}/series 2> /dev/null` ; do
  59. apply_patch "$path" "$i" || exit 1
  60. done
  61. else
  62. for i in `cd $path; ls -d $patches 2> /dev/null` ; do
  63. if [ -d "${path}/$i" ] ; then
  64. echo "${path}/$i skipped"
  65. elif echo "$i" | grep -q -E "\.tar(\..*)?$|\.tbz2?$|\.tgz$" ; then
  66. unpackedarchivedir="$builddir/.patches-$(basename $i)-unpacked"
  67. rm -rf "$unpackedarchivedir" 2> /dev/null
  68. mkdir "$unpackedarchivedir"
  69. tar -C "$unpackedarchivedir" --strip-components=1 -xaf "${path}/$i"
  70. scan_patchdir "$unpackedarchivedir"
  71. else
  72. apply_patch "$path" "$i" || exit 1
  73. fi
  74. done
  75. fi
  76. }
  77. scan_patchdir $patchdir $patchpattern
  78. # Check for rejects...
  79. if [ "`find $builddir/ '(' -name '*.rej' -o -name '.*.rej' ')' -print`" ] ; then
  80. echo "Aborting. Reject files found."
  81. exit 1
  82. fi
  83. # Remove backup files
  84. find $builddir/ '(' -name '*.orig' -o -name '.*.orig' ')' -exec rm -f {} \;