3 # Get our required options
9 # The remainder is for diff
12 # This function checks that the files listed in the file in "$1"
13 # do exist, at the given depth-stripping level (aka diff -p#)
14 do_check_files_at_depth() {
17 local ok=0 # 0: OK, !0: KO
23 f="$( echo "${f}" |sed -r -e "s:^([^/]+/){${depth}}::;" )"
34 dst="$( cd "${dst}"; pwd )"
36 # Iterate through patches
37 for p in "${src}/"*.patch; do
38 pname="$( basename "${p}" )"
40 printf "Handling patch '${pname}'...\n"
42 printf " creating reference..."
43 cp -a "${base}" "${base}.orig"
46 printf " retrieving patch comment..."
49 $0~/^diff --/ { nextfile; }
50 $1=="---" { mark=1; next; }
51 $1=="+++" && mark==1 { nextfile; }
56 printf " creating patched file list..."
57 diffstat -f 4 -r 2 -u -p 0 "${p}" \
59 |awk '{ for(i=NF;i>=NF-5;i--) { $(i) = ""; } print; }' \
64 pushd "${base}" >/dev/null 2>&1
66 # Check all files exist, up to depth 3
67 printf " checking depth:"
68 for((d=0;d<4;d++)); do
70 if do_check_files_at_depth "../diffstat.orig" ${d}; then
71 printf " ok, using depth '${d}'\n"
75 if [ ${d} -ge 4 ]; then
77 printf " checking depth failed\n"
78 read -p " --> enter patch depth (or Ctrl-C to abort): " d
81 # Store the original list of fiels touched by the patch,
82 # removing the $d leading components
83 sed -r -e "s:^([^/]+/){${d}}::;" "../diffstat.orig" >"${dst}/${pname}.diffstat.orig"
85 # Apply the patch proper, and check it applied cleanly.
86 # We can't check with --dry-run because of patches that
87 # contain multiple accumulated patches onto a single file.
88 printf " applying patch..."
89 if ! patch -g0 -F1 -f -p${d} <"${p}" >"../patch.out" 2>&1; then
93 printf " restoring '${base}'..."
96 mv "${base}.orig" "${base}"
98 printf "There was an error while applying:\n --> ${p} <--\n"
99 printf "'${base}' was restored to the state it was prior to applying this faulty patch.\n"
100 printf "Here's the 'patch' command, and its output:\n"
101 printf " ----8<----\n"
102 printf " patch -g0 -F1 -f -p${d} <'${p}'\n"
103 cat "patch.out" |(IFS=$(printf "\n"); while read line; do printf " ${line}\n"; done)
105 printf " ----8<----\n"
110 printf " removing '.orig' files..."
111 find . -type f -name '*.orig' -exec rm -f {} +
116 printf " re-diffing the patch..."
117 printf "%s\n\n" "${comment}" >"${dst}/${pname}"
118 diff -durN "${base}.orig" "${base}" >>"${dst}/${pname}"
121 if [ -n "${diff}" ]; then
122 printf " applying diff filter..."
123 filterdiff -x "${diff}" "${dst}/${pname}" >"tmp-diff"
124 mv "tmp-diff" "${dst}/${pname}"
128 printf " creating new patched file list..."
129 diffstat -f 4 -r 2 -u -p 1 "${dst}/${pname}" \
131 |awk '{ for(i=NF;i>=NF-5;i--) { $(i) = ""; } print; }' \
133 >"${dst}/${pname}.diffstat.new"
136 printf " removing temporary files/dirs..."
139 rm -rf "${base}.orig"
143 # Scan all new patches to see if they touch
144 # more files than the original patches
145 printf "\nChecking resulting patchset:\n"
146 for p in "${dst}/"*.patch; do
147 pname="$( basename "${p}" )"
149 if ! cmp "${p}.diffstat.orig" "${p}.diffstat.new" >/dev/null; then
150 printf " --> '${pname}' differ in touched files <--\n"