patch.sh 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/usr/bin/env 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) 2006, 2007 Thorsten Glaser <tg@freewrt.org>
  7. # (c) 2002 Erik Andersen <andersen@codepoet.org>
  8. [[ -n $BASH_VERSION ]] && shopt -s extglob
  9. # Set directories from arguments, or use defaults.
  10. targetdir=${1-.}
  11. patchdir=${2-../patches}
  12. patchpattern=${3-*}
  13. if [ ! -d "${targetdir}" ] ; then
  14. echo "Aborting. '${targetdir}' is not a directory."
  15. exit 1
  16. fi
  17. if [ ! -d "${patchdir}" ] ; then
  18. echo "Aborting. '${patchdir}' is not a directory."
  19. exit 0
  20. fi
  21. wd=$(pwd)
  22. cd $patchdir
  23. rm -f $targetdir/.patch.tmp
  24. for i in $(eval echo ${patchpattern}); do
  25. test -e "$i" || continue
  26. i=$patchdir/$i
  27. cd $wd
  28. case $i 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. [ -d "${i}" ] && echo "Ignoring subdirectory ${i}" && continue
  43. echo ""
  44. echo "Applying ${i} using ${type}: "
  45. ${uncomp} ${i} | tee $targetdir/.patch.tmp | patch -p1 -E -d ${targetdir}
  46. fgrep '@@ -0,0 ' $targetdir/.patch.tmp >/dev/null 2>&1 && \
  47. touch $targetdir/.patched-newfiles
  48. rm -f $targetdir/.patch.tmp
  49. if [ $? != 0 ] ; then
  50. echo "Patch failed! Please fix $i!"
  51. exit 1
  52. fi
  53. cd $patchdir
  54. done
  55. # Check for rejects...
  56. if [ "`find $targetdir/ '(' -name '*.rej' -o -name '.*.rej' ')' -print`" ] ; then
  57. echo "Aborting. Reject files found."
  58. exit 1
  59. fi
  60. # Remove backup files
  61. find $targetdir/ '(' -name '*.orig' -o -name '.*.orig' ')' -exec rm -f {} \;