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}}::;" )"
35 src="$( cd "${src}"; pwd )"
36 dst="$( cd "${dst}"; pwd )"
38 # Iterate through patches
39 for p in "${src}/"*.patch; do
40 pname="$( basename "${p}" )"
42 printf "Handling patch '${pname}'...\n"
44 printf " creating reference..."
45 cp -a "${base}" "${base}.orig"
48 printf " retrieving patch comment..."
51 $0~/^diff --/ { nextfile; }
52 $1=="---" { mark=1; next; }
53 $1=="+++" && mark==1 { nextfile; }
58 printf " creating patched file list..."
59 diffstat -f 4 -r 2 -u -p 0 "${p}" \
61 |awk '{ for(i=NF;i>=NF-5;i--) { $(i) = ""; } print; }' \
66 pushd "${base}" >/dev/null 2>&1
68 # Check all files exist, up to depth 3
69 printf " checking depth:"
70 for((d=0;d<4;d++)); do
72 if do_check_files_at_depth "../diffstat.orig" ${d}; then
73 printf " ok, using depth '${d}'\n"
77 if [ ${d} -ge 4 ]; then
79 printf " checking depth failed\n"
80 read -p " --> enter patch depth (or Ctrl-C to abort): " d
83 # Store the original list of fiels touched by the patch,
84 # removing the $d leading components
85 sed -r -e "s:^([^/]+/){${d}}::;" "../diffstat.orig" >"${dst}/${pname}.diffstat.orig"
87 # Apply the patch proper, and check it applied cleanly.
88 # We can't check with --dry-run because of patches that
89 # contain multiple accumulated patches onto a single file.
90 printf " applying patch..."
91 if ! patch -g0 -F1 -f -p${d} <"${p}" >"../patch.out" 2>&1; then
94 printf "There was an error while applying:\n --> ${p} <--\n"
95 printf "'${base}' was restored to the state it was prior to applying this faulty patch.\n"
96 printf "Here's the 'patch' command, and its output:\n"
97 printf " ----8<----\n"
98 printf " patch -g0 -F1 -f -p${d} <'${p}'\n"
99 sed -r -e 's/^/ /;' "patch.out"
100 printf " ----8<----\n"
105 printf " removing '.orig' files..."
106 find . -type f -name '*.orig' -exec rm -f {} +
111 printf " re-diffing the patch..."
112 printf "%s\n\n" "${comment}" >"${dst}/${pname}"
113 diff -durN "${base}.orig" "${base}" >>"${dst}/${pname}"
116 if [ -n "${diff}" ]; then
117 printf " applying diff filter..."
118 filterdiff -x "${diff}" "${dst}/${pname}" >"tmp-diff"
119 mv "tmp-diff" "${dst}/${pname}"
123 printf " creating new patched file list..."
124 diffstat -f 4 -r 2 -u -p 1 "${dst}/${pname}" \
126 |awk '{ for(i=NF;i>=NF-5;i--) { $(i) = ""; } print; }' \
128 >"${dst}/${pname}.diffstat.new"
131 printf " removing temporary files/dirs..."
134 rm -f "diffstat.orig"
135 rm -rf "${base}.orig"
139 # Scan all new patches to see if they touch
140 # more files than the original patches
141 printf "\nChecking resulting patchset:\n"
142 for p in "${dst}/"*.patch; do
143 pname="$( basename "${p}" )"
145 if ! cmp "${p}.diffstat.orig" "${p}.diffstat.new" >/dev/null; then
146 printf " --> '${pname}' differ in touched files <--\n"
148 rm -f "${p}.diffstat.orig" "${p}.diffstat.new"