apply-patches.sh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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) 2002 Erik Andersen <andersen@codepoet.org>
  7. #
  8. # Parameters:
  9. # - "-s", optional. Silent operation, don't print anything if there
  10. # isn't any error.
  11. # - the build directory, optional, default value is '.'. The place where are
  12. # the package sources.
  13. # - the patch directory, optional, default '../kernel-patches'. The place
  14. # where are the scripts you want to apply.
  15. # - other parameters are the patch name patterns, optional, default value is
  16. # '*'. Pattern(s) describing the patch names you want to apply.
  17. #
  18. # The script will look recursively for patches from the patch directory. If a
  19. # file named 'series' exists then the patches mentioned in it will be applied
  20. # as plain patches, regardless of their file name. If no 'series' file exists,
  21. # the script will look for file names matching pattern(s). If the name
  22. # ends with '.tar.*', '.tbz2' or '.tgz', the file is considered as an archive
  23. # and will be uncompressed into a directory named
  24. # '.patches-name_of_the_archive-unpacked'. It's the turn of this directory to
  25. # be scanned with '*' as pattern. Remember that scanning is recursive. Other
  26. # files than series file and archives are considered as a patch.
  27. #
  28. # Once a patch is found, the script will try to apply it. If its name doesn't
  29. # end with '.gz', '.bz', '.bz2', '.xz', '.zip', '.Z', '.diff*' or '.patch*',
  30. # it will be skipped. If necessary, the patch will be uncompressed before being
  31. # applied. The list of the patches applied is stored in '.applied_patches_list'
  32. # file in the build directory.
  33. set -e
  34. silent=
  35. if [ "$1" = "-s" ] ; then
  36. # add option to be used by the patch tool
  37. silent=-s
  38. shift
  39. fi
  40. # Set directories from arguments, or use defaults.
  41. builddir=${1-.}
  42. patchdir=${2-../kernel-patches}
  43. shift 2
  44. patchpattern=${@-*}
  45. export TAR=${TAR:-tar}
  46. # use a well defined sorting order
  47. export LC_COLLATE=C
  48. if [ ! -d "${builddir}" ] ; then
  49. echo "Aborting. '${builddir}' is not a directory."
  50. exit 1
  51. fi
  52. if [ ! -d "${patchdir}" ] ; then
  53. echo "Aborting. '${patchdir}' is not a directory."
  54. exit 1
  55. fi
  56. # Remove any rejects present BEFORE patching - Because if there are
  57. # any, even if patches are well applied, at the end it will complain
  58. # about rejects in builddir.
  59. find ${builddir}/ '(' -name '*.rej' -o -name '.*.rej' ')' -print0 | \
  60. xargs -0 -r rm -f
  61. function apply_patch {
  62. path="${1%%/}"
  63. patch="${2}"
  64. case "${path}" in
  65. /*) ;;
  66. *) path="$PWD/${path}";;
  67. esac
  68. if [ "$3" ]; then
  69. type="series"; uncomp="cat"
  70. else
  71. case "$patch" in
  72. *.gz)
  73. type="gzip"; uncomp="gunzip -dc"; ;;
  74. *.bz)
  75. type="bzip"; uncomp="bunzip -dc"; ;;
  76. *.bz2)
  77. type="bzip2"; uncomp="bunzip2 -dc"; ;;
  78. *.xz)
  79. type="xz"; uncomp="unxz -dc"; ;;
  80. *.zip)
  81. type="zip"; uncomp="unzip -d"; ;;
  82. *.Z)
  83. type="compress"; uncomp="uncompress -c"; ;;
  84. *.diff*)
  85. type="diff"; uncomp="cat"; ;;
  86. *.patch*)
  87. type="patch"; uncomp="cat"; ;;
  88. *)
  89. echo "Unsupported file type for ${path}/${patch}, skipping";
  90. return 0
  91. ;;
  92. esac
  93. fi
  94. if [ -z "$silent" ] ; then
  95. echo ""
  96. echo "Applying $patch using ${type}: "
  97. fi
  98. if [ ! -e "${path}/$patch" ] ; then
  99. echo "Error: missing patch file ${path}/$patch"
  100. exit 1
  101. fi
  102. existing="$(grep -E "/${patch}\$" ${builddir}/.applied_patches_list || true)"
  103. if [ -n "${existing}" ]; then
  104. echo "Error: duplicate filename '${patch}'"
  105. echo "Conflicting files are:"
  106. echo " already applied: ${existing}"
  107. echo " to be applied : ${path}/${patch}"
  108. exit 1
  109. fi
  110. echo "${path}/${patch}" >> ${builddir}/.applied_patches_list
  111. ${uncomp} "${path}/$patch" | patch -F0 -g0 -p1 --no-backup-if-mismatch -d "${builddir}" -t -N $silent
  112. if [ $? != 0 ] ; then
  113. echo "Patch failed! Please fix ${patch}!"
  114. exit 1
  115. fi
  116. }
  117. function scan_patchdir {
  118. local path=$1
  119. shift 1
  120. patches=${@-*}
  121. # If there is a series file, use it instead of using ls sort order
  122. # to apply patches. Skip line starting with a dash.
  123. if [ -e "${path}/series" ] ; then
  124. # The format of a series file accepts a second field that is
  125. # used to specify the number of directory components to strip
  126. # when applying the patch, in the form -pN (N an integer >= 0)
  127. # We assume this field to always be -p1 whether it is present
  128. # or missing.
  129. series_patches="`grep -Ev "^#" ${path}/series | cut -d ' ' -f1 2> /dev/null`"
  130. for i in $series_patches; do
  131. apply_patch "$path" "$i" series
  132. done
  133. else
  134. for i in `cd $path; ls -d $patches 2> /dev/null` ; do
  135. if [ -d "${path}/$i" ] ; then
  136. scan_patchdir "${path}/$i"
  137. elif echo "$i" | grep -q -E "\.tar(\..*)?$|\.tbz2?$|\.tgz$" ; then
  138. unpackedarchivedir="$builddir/.patches-$(basename $i)-unpacked"
  139. rm -rf "$unpackedarchivedir" 2> /dev/null
  140. mkdir "$unpackedarchivedir"
  141. ${TAR} -C "$unpackedarchivedir" -xaf "${path}/$i"
  142. scan_patchdir "$unpackedarchivedir"
  143. else
  144. apply_patch "$path" "$i"
  145. fi
  146. done
  147. fi
  148. }
  149. touch ${builddir}/.applied_patches_list
  150. scan_patchdir "$patchdir" "$patchpattern"
  151. # Check for rejects...
  152. if [ "`find $builddir/ '(' -name '*.rej' -o -name '.*.rej' ')' -print`" ] ; then
  153. echo "Aborting. Reject files found."
  154. exit 1
  155. fi