2 # A little script I whipped up to make it easy to
3 # patch source trees and have sane error handling
6 # (c) 2002 Erik Andersen <andersen@codepoet.org>
9 # - "-s", optional. Silent operation, don't print anything if there
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.
18 # The script will look recursively for patches from the patch directory. If a
19 # file is named 'series' then only patches mentionned into it will be applied.
20 # If not, the script will look for file names matching pattern(s). If the name
21 # ends with '.tar.*', '.tbz2' or '.tgz', the file is considered as an archive
22 # and will be uncompressed into a directory named
23 # '.patches-name_of_the_archive-unpacked'. It's the turn of this directory to
24 # be scanned with '*' as pattern. Remember that scanning is recursive. Other
25 # files than series file and archives are considered as a patch.
27 # Once a patch is found, the script will try to apply it. If its name doesn't
28 # end with '.gz', '.bz', '.bz2', '.xz', '.zip', '.Z', '.diff*' or '.patch*',
29 # it will be skipped. If necessary, the patch will be uncompressed before being
30 # applied. The list of the patches applied is stored in '.applied_patches_list'
31 # file in the build directory.
34 if [ "$1" = "-s" ] ; then
35 # add option to be used by the patch tool
40 # Set directories from arguments, or use defaults.
42 patchdir=${2-../kernel-patches}
46 # use a well defined sorting order
49 if [ ! -d "${builddir}" ] ; then
50 echo "Aborting. '${builddir}' is not a directory."
53 if [ ! -d "${patchdir}" ] ; then
54 echo "Aborting. '${patchdir}' is not a directory."
58 # Remove any rejects present BEFORE patching - Because if there are
59 # any, even if patches are well applied, at the end it will complain
60 # about rejects in builddir.
61 find ${builddir}/ '(' -name '*.rej' -o -name '.*.rej' ')' -print0 | \
64 function apply_patch {
69 type="gzip"; uncomp="gunzip -dc"; ;;
71 type="bzip"; uncomp="bunzip -dc"; ;;
73 type="bzip2"; uncomp="bunzip2 -dc"; ;;
75 type="xz"; uncomp="unxz -dc"; ;;
77 type="zip"; uncomp="unzip -d"; ;;
79 type="compress"; uncomp="uncompress -c"; ;;
81 type="diff"; uncomp="cat"; ;;
83 type="patch"; uncomp="cat"; ;;
85 echo "Unsupported file type for ${path}/${patch}, skipping";
89 if [ -z "$silent" ] ; then
91 echo "Applying $patch using ${type}: "
93 if [ ! -e "${path}/$patch" ] ; then
94 echo "Error: missing patch file ${path}/$patch"
97 echo $patch >> ${builddir}/.applied_patches_list
98 ${uncomp} "${path}/$patch" | patch -g0 -p1 -E -d "${builddir}" -t -N $silent
100 echo "Patch failed! Please fix ${patch}!"
105 function scan_patchdir {
110 # If there is a series file, use it instead of using ls sort order
111 # to apply patches. Skip line starting with a dash.
112 if [ -e "${path}/series" ] ; then
113 for i in `grep -Ev "^#" ${path}/series 2> /dev/null` ; do
114 apply_patch "$path" "$i"
117 for i in `cd $path; ls -d $patches 2> /dev/null` ; do
118 if [ -d "${path}/$i" ] ; then
119 scan_patchdir "${path}/$i"
120 elif echo "$i" | grep -q -E "\.tar(\..*)?$|\.tbz2?$|\.tgz$" ; then
121 unpackedarchivedir="$builddir/.patches-$(basename $i)-unpacked"
122 rm -rf "$unpackedarchivedir" 2> /dev/null
123 mkdir "$unpackedarchivedir"
124 tar -C "$unpackedarchivedir" -xaf "${path}/$i"
125 scan_patchdir "$unpackedarchivedir"
127 apply_patch "$path" "$i"
133 scan_patchdir "$patchdir" "$patchpattern"
135 # Check for rejects...
136 if [ "`find $builddir/ '(' -name '*.rej' -o -name '.*.rej' ')' -print`" ] ; then
137 echo "Aborting. Reject files found."
141 # Remove backup files
142 find $builddir/ '(' -name '*.orig' -o -name '.*.orig' ')' -exec rm -f {} \;