root/trunk/freewrt/scripts/update-patches

Revision 3298, 5.1 kB (checked in by tg, 1 year ago)

also bring this into a shape we can submit it to upstream,
merge the subsequent diff, and fix update-patches with new
files in new subdirs (for those who violate the policy, to
not patch files into existence)

Line 
1 #!/usr/bin/env bash
2 # $FreeWRT$
3 # $MirOS: ports/infrastructure/scripts/update-patches,v 1.5 2006/06/15 19:18:43 tg Exp $
4 #-
5 # Copyright (c) 2006
6 #       Thorsten Glaser <tg@freewrt.org>
7 #
8 # Derived from the MirPorts Framework "update-patches" script:
9 #
10 # Copyright (c) 2003, 2004, 2005
11 #       Thorsten "mirabile" Glaser <tg@MirBSD.de>
12 # Based upon code and idea (c) 2000
13 #       Marc Espie for the OpenBSD project. All rights reserved.
14 #
15 # Provided that these terms and disclaimer and all copyright notices
16 # are retained or reproduced in an accompanying document, permission
17 # is granted to deal in this work without restriction, including un-
18 # limited rights to use, publicly perform, distribute, sell, modify,
19 # merge, give away, or sublicence.
20 #
21 # This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
22 # the utmost extent permitted by applicable law, neither express nor
23 # implied; without malicious intent or gross negligence. In no event
24 # may a licensor, author or contributor be held liable for indirect,
25 # direct, other damage, loss, or other issues arising in any way out
26 # of dealing in the work, even if advised of the possibility of such
27 # damage or existence of a defect, except proven that it results out
28 # of said person's immediate fault when using the work as intended.
29
30 [[ -n $BASH_VERSION ]] && shopt -s extglob
31
32 do_diff()
33 {
34         local f1=$2/$1
35         local f2=$3/$1
36
37         if [[ ! -e $f1 ]]; then
38                 [[ -d ${f1%/*}/. ]] || mkdir -p ${f1%/*}
39                 if [[ ! -s $f2 ]]; then
40                         cat <<EOF
41 --- $f1 (non-existant)
42 +++ $f2 (empty)
43 @@ -0,0 +0,0 @@
44 EOF
45                         return 0
46                 fi
47                 touch -t 197001010000.00 "$f1"
48         fi
49         diff -adup "$f1" "$f2"
50         return $?
51 }
52
53 TRANSFORM='sed s/[.+]/\\\\&/g'
54 PATCHDIR=$CURDIR/patches
55 EXTRADIR=$CURDIR/extra
56
57 mkdir -p $PATCHDIR
58
59 SUBDIST=${WRKDIST##${WRKDIR1}?(/)}
60 if [[ -n $SUBDIST ]]; then
61         mv ${WRKDIR1}.orig/${SUBDIST} ${WRKDIR1}/${SUBDIST}.orig
62         D_BASE=${WRKDIR1}
63         D_SUB=${SUBDIST}
64 #       D_SUBP=$D_SUB
65         D_SUBP='[^/]*'
66         D_CMP=$D_SUBP
67 else
68         # WRKSRC == WRKDIR
69         D_BASE=$(dirname ${WRKDIR1})
70         D_SUB=$(basename ${WRKDIR1})
71         D_SUBP=$D_SUB
72         D_CMP=
73 fi
74 ORGDIST=${D_BASE}/${D_SUB}.orig
75
76 [[ -d $EXTRADIR ]] && \
77     (cd $EXTRADIR; find . -print0) | \
78     (cd $WRKDIST; pax -rw -0 -d $ORGDIST/) >/dev/null 2>&1
79
80 if [[ -e $WRKDIST/.patched-newfiles ]]; then
81         touch $ORGDIST/.patched-newfiles
82         patch_newfiles=1
83 else
84         patch_newfiles=0
85 fi
86
87 DIFF_FLAGS="-adu -I \"^--- $(echo $D_SUBP.orig/ | $TRANSFORM)@@ .*\""
88 DIFF_FLAGS="$DIFF_FLAGS -I \"^\+\+\+ $(echo $D_SUBP/ | $TRANSFORM)@@    .*\""
89
90 for file in $(cd ${WRKDIST}; find . -type f | sed 's#^\./##'); do
91         [[ ! -e $ORGDIST/$file && $patch_newfiles = 0 ]] && continue
92         cmp -s "$ORGDIST/$file" "$WRKDIST/$file" && continue
93         echo "Processing ${file}..." >&2
94         # look in patchdir for an existing patchfile matching this
95         cd $PATCHDIR
96         for i in $PATCH_LIST; do
97                 # Ignore non-files, or old backup
98                 [[ ! -f $i || $i = *@(.orig|.rej|~) ]] && continue
99
100                 # Patch found. Is this the one?
101                 if grep "^[+-][+-][+-] $D_CMP[^/]*/$file        " "$i" >/dev/null; then
102                         # Multiple files in the diff?
103                         if [ $(grep -c "^--- $D_CMP" "$i") -gt 1 -o \
104                             $(grep -c "^+++ $D_CMP" "$i") -gt 1 ]; then
105                                 echo "Cannot process, $i contains patches" >&2
106                                 echo "to multiple files! Aborting." >&2
107                                 echo FAIL
108                                 [[ -n $SUBDIST ]] && mv \
109                                     ${WRKDIR1}/${SUBDIST}.orig \
110                                     ${WRKDIR1}.orig/${SUBDIST}
111                                 exit 0
112                         fi
113                         # Multiple diffs with this file?
114                         let n=0
115                         pflst=
116                         for j in $PATCH_LIST; do
117                                 [[ ! -f $j || $j = *@(.orig|.rej|~) ]] && \
118                                     continue
119                                 grep "^[+-][+-][+-] $D_CMP[^/]*/$file   " \
120                                     "$j" >/dev/null || continue
121                                 let n++
122                                 pflst="$pflst '$j'"
123                         done
124                         if (( n != 1 )); then
125                                 echo "Cannot process, file $file" >&2
126                                 echo "is contained in multiple patches:" >&2
127                                 echo "$pflst" >&2
128                                 echo FAIL
129                                 [[ -n $SUBDIST ]] && mv \
130                                     ${WRKDIR1}/${SUBDIST}.orig \
131                                     ${WRKDIR1}.orig/${SUBDIST}
132                                 exit 0
133                         fi
134                         # No, process this patch
135
136                         accounted="$accounted $i"
137                         # found it, copy preamble before comparision
138                         ( sed -e "/^--- /,\$d" <$i; \
139                           cd $D_BASE && do_diff "$file" "$D_SUB.orig" "$D_SUB" \
140                         ) >"$i.new"
141                         # did it change ? mark it as changed
142                         tfile="$(echo "$file" | $TRANSFORM)"
143                         if eval diff "$(echo "${DIFF_FLAGS}" \
144                             | sed "s#@@#${tfile}#g")" \
145                             "$i" "$i.new" 1>&2; then
146                                 rm "$i.new"
147                         else
148                                 echo "Patch $i for $file updated" >&2
149                                 mv "$i" "$i.orig"
150                                 mv "$i.new" "$i"
151                                 edit="$edit $i"
152                         fi
153                         continue 2
154                 fi
155         done
156
157         # Build a sensible name for the new patch file
158         patchname=patch-$(echo "$file" | sed -e 's#[/. ]#_#g')
159         echo "No patch-* found for $file, creating $patchname" >&2
160         ( echo '$Free''WRT$'; \
161           cd $D_BASE && do_diff "$file" "$D_SUB.orig" "$D_SUB" \
162         ) >$patchname
163         edit="$edit $patchname"
164         accounted="$accounted $patchname"
165 done
166
167 # Verify all patches accounted for
168 cd $PATCHDIR
169 for i in *; do
170         [[ ! -f $i || $i = *@(.orig|.rej|~) ]] && continue
171         grep '^\\ No newline at end of file' $i >/dev/null \
172             && echo "*** Patch $i needs manual intervention" >&2
173         [[ $accounted != *@($i)* ]] \
174             && echo "*** Patch $i not accounted for" >&2
175 done
176
177 echo $edit
178 [[ -n $SUBDIST ]] && mv ${WRKDIR1}/${SUBDIST}.orig ${WRKDIR1}.orig/${SUBDIST}
179 exit 0
Note: See TracBrowser for help on using the browser.