apply-patches.sh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. *.diff*)
  40. type="diff"; uncomp="cat"; ;;
  41. *.patch*)
  42. type="patch"; uncomp="cat"; ;;
  43. *)
  44. echo "Unsupported format file for ${patch}, skip it";
  45. return 0;
  46. ;;
  47. esac
  48. echo ""
  49. echo "Applying $patch using ${type}: "
  50. echo $patch >> ${builddir}/.applied_patches_list
  51. ${uncomp} "${path}/$patch" | patch -g0 -p1 -E -d "${builddir}"
  52. if [ $? != 0 ] ; then
  53. echo "Patch failed! Please fix ${patch}!"
  54. exit 1
  55. fi
  56. }
  57. function scan_patchdir {
  58. path=$1
  59. shift 1
  60. patches=${@-*}
  61. # If there is a series file, use it instead of using ls sort order
  62. # to apply patches. Skip line starting with a dash.
  63. if [ -e "${path}/series" ] ; then
  64. for i in `grep -Ev "^#" ${path}/series 2> /dev/null` ; do
  65. apply_patch "$path" "$i" || exit 1
  66. done
  67. else
  68. for i in `cd $path; ls -d $patches 2> /dev/null` ; do
  69. if [ -d "${path}/$i" ] ; then
  70. scan_patchdir "${path}/$i"
  71. elif echo "$i" | grep -q -E "\.tar(\..*)?$|\.tbz2?$|\.tgz$" ; then
  72. unpackedarchivedir="$builddir/.patches-$(basename $i)-unpacked"
  73. rm -rf "$unpackedarchivedir" 2> /dev/null
  74. mkdir "$unpackedarchivedir"
  75. tar -C "$unpackedarchivedir" -xaf "${path}/$i"
  76. scan_patchdir "$unpackedarchivedir"
  77. else
  78. apply_patch "$path" "$i" || exit 1
  79. fi
  80. done
  81. fi
  82. }
  83. scan_patchdir $patchdir $patchpattern
  84. # Check for rejects...
  85. if [ "`find $builddir/ '(' -name '*.rej' -o -name '.*.rej' ')' -print`" ] ; then
  86. echo "Aborting. Reject files found."
  87. exit 1
  88. fi
  89. # Remove backup files
  90. find $builddir/ '(' -name '*.orig' -o -name '.*.orig' ')' -exec rm -f {} \;