]> git.mxchange.org Git - flightgear.git/blob - scripts/tools/fg-submit
yeah, yet another patch ... :-]
[flightgear.git] / scripts / tools / fg-submit
1 #!/bin/bash
2 #
3 # This script called in a CVS directory compares local files with
4 # the repository, and prepares an update package containing all
5 # changes and new files for submission to a CVS maintainer. If there
6 # are only changes in text files, then a compressed unified diff is
7 # made (foo.diff.bz2). If there are also changed binary or new files,
8 # then an archive is made instead (foo.tar.bz2). The base name ("foo")
9 # can be given as command line argument. Otherwise the directory name
10 # is used. The script also leaves a diff in uncompressed/unpackaged
11 # form. This is only for developer convenience -- for a quick check
12 # of the diff correctness. It is not to be submitted. The script will
13 # not overwrite any file, but rather rename conflicting files.
14 #
15 # Usage:  fg-submit [-v] [<basename>]
16 #
17 # Options:
18 #         -v ... verbose output
19 #
20 # Example:
21 #     $ cd $FG_ROOT/Aircraft/bo105
22 #     $ fg-submit                # -> bo105.diff.bz2 or bo105.tar.bz2
23 #
24 #     $ fg-submit update         # -> update.diff.bz2 or update.tar.bz2
25 #
26 #
27 # Spaces in the basename are replaced with "%20". People who prefer
28 # to have the date in the archive name can conveniently achieve this
29 # by defining a shell alias in ~/.bashrc:
30 #
31 #     alias submit='fg-submit "${PWD/#*\/}-$(date +%Y-%m-%d)"'
32 #
33 #
34 #
35 # If the script finds an application named "fg-upload", then it calls
36 # this at the end with two arguments:
37 #
38 #     $1 ... archive or compressed diff for submission
39 #     $2 ... accessory diff, *NOT* for submission!
40 #
41 # $1 and $2 are guaranteed not to contain spaces, only $1 is guaranteed
42 # to actually exist. Such as script can be used to upload the file to an
43 # ftp-/webserver, and/or to remove one or both files. Example using
44 # KDE's kfmclient for upload (alternatives: ncftpput, gnomevfs-copy, wput):
45 #
46 #     $ cat ~/bin/fg-upload
47 #     #!/bin/bash
48 #     echo "uploading $1"
49 #     if kfmclient copy $1 ftp://user:password@server.com; then
50 #             echo "deleting $1 $2"
51 #             rm -f $1 $2
52 #
53 #             echo "Done.  URL: ftp://server.com/$1"
54 #     else
55 #             echo "arghh ... HELP! HELP!"
56 #     fi
57 #
58 #
59 #
60 # Whether a file should be included in the archive or not, is decided
61 # by pattern rules. There is a set of reasonable default rules predefined,
62 # but alternative settings can be defined in a hidden configuration file
63 # named ".fg-submit". Such a file is searched in the current directory,
64 # in its parent directory, in its grand-parent directory and so on,
65 # and finally in the $HOME directory. The first found file is taken.
66 #
67 # A file can use a list of four keywords with arguments, each on a
68 # separate line:
69 #
70 #     ALLOW   <pattern-list>     ... accept & report matching file
71 #     DENY    <pattern-list>     ... reject & report matching file
72 #     IGNORE  <pattern-list>     ... silently reject matching file
73 #     DEFAULT                    ... adds default rules
74 #
75 # A <pattern-list> is a space-separated list of shell pattern.
76 # It may also be empty, in which case it has no effect. Examples:
77 #
78 #     DENY   test.blend
79 #     ALLOW  *.xcf *.blend
80 #
81 # A config file that only contains the keyword DEFAULT causes the
82 # same behavior as no config file at all. A config file without
83 # DEFAULT drops the built-in default rules (with the exception of
84 # a few very basic ones, such as rejection of CVS files). Comments
85 # using the hash character '#' are allowed and ignored.
86 #
87 # The list of pattern is checked in the same in order in which it
88 # is built. The first match causes a file to be accepted or rejected.
89 # Further matches are not considered.
90 #
91 # Example:
92 #
93 #     DENY  test.xcf # throw out the test graphic, but ...
94 #     ALLOW *.xcf    # ... allow all other GIMP graphics (the following
95 #                    # DEFAULT keyword throws them out otherwise)
96 #
97 #     DEFAULT        # insert the default rules here
98 #
99 #     ALLOW  g.old   # add this silly file :-)
100 #     IGNORE *.old   # throw out the old files (and don't report
101 #                    # that to the terminal)
102 #
103 #
104 # .fg-submit configuration files are "sourced" bash scripts, the
105 # keywords are simple shell functions. That means that you can
106 # also use other bash commands in that file, such as "echo", or
107 # write several commands on one line, separated with semicolon.
108 # You can even put all special rules in your ~/.fg-submit file,
109 # with rules depending on the working directory:
110 #
111 #     case "$PWD" in
112 #     */bo105*)  DENY *.osg; ALLOW livery.xcf ;;
113 #     */ufo*)    DENY *.tiff ;;
114 #     esac
115 #     DEFAULT
116
117
118
119 SELF=${0/#*\/}
120 DIR=${PWD/#*\/}
121
122 if [ "$1" == "-v" ]; then
123         DBG=1
124         shift
125 fi
126
127 BASE=${1:-$DIR}
128 BASE=${BASE// /%20}
129
130 CVS=/usr/bin/cvs                 # avoid colorcvs wrapper from
131 [ -x $CVS ] || CVS=cvs           # http://www.hakubi.us/colorcvs/
132 UPLOAD=$(which fg-upload 2>/dev/null)
133
134 CONFIG_FILE=".fg-submit"
135 ARCHIVE=$BASE.tar.bz2
136 DIFF=$BASE.diff
137 CDIFF=$DIFF.bz2
138
139 # these rules are always prepended (the leading ! makes silent rejects)
140 PREFIX_RULES="
141         !$DIFF* !$CDIFF* !$ARCHIVE*
142         !CVS/* !*/CVS/*
143 "
144 # these rules are used when no other rules are specified,
145 # and wherever the DEFAULT keyword is used
146 DEFAULT_RULES="
147         +.cvsignore +*/.cvsignore
148         -*~ -*. -*.bak -*.orig
149         -*.RGB -*.RGBA -*.MDL
150         -*.xcf -*.XCF -*.tga -*.TGA -*.bmp -*.BMP -*.png -*.PNG
151         -*.blend -*.blend[0-9] -*blend[0-9][0-9] -*.blend[0-9][0-9][0-9]
152         -*.gz -*.tgz -*.bz2 -*.zip -*.tar.gz* -*.tar.bz2*
153 "
154 # these rules are always appended: ignore all hidden files that
155 # weren't explicitly allowed so far, and accept all the rest
156 POSTFIX_RULES="
157         !.* !*/.*
158         +*
159 "
160
161
162 function ERROR   { echo -e "\e[31;1m$*\e[m";   }
163 function LOG     { echo -e "\e[35m$*\e[m";     }
164 function NEW     { echo -e "\e[32m\t+ $*\e[m"; }
165 function CHANGED { echo -e "\e[36m\t+ $*\e[m"; }
166 function REJECT  { echo -e "\e[31m\t- $*\e[m"; }
167 function DEBUG   { [ $DBG ] && echo -e "$*";   }
168
169 function diffstat {
170         # output diff statistics, similar to the "diffstat" utility
171         awk '
172                 function line(a, r, c, f) {
173                         print "\t\033[32m"a"\033[m\t\033[31m"r"\033[m\t\033[34m"c"\033[m\t"f
174                 }
175                 function dofile() {
176                         if (!file) {
177                                 return
178                         }
179                         if (bin) {
180                                 print "\t. . . . binary  . . . . \033[36m"file"\033[m"
181                         } else {
182                                 line(a, r, c, file)
183                                 at += a; rt += r; ct += c
184                         }
185                         a = r = c = 0
186                 }
187                 BEGIN      {
188                         print "\tadded---removed-changed----------------------------------------"
189                         a = r = c = at = rt = ct = n = bin = 0
190                 }
191                 /^Index: / { dofile(); scan = bin = 0; file = $2; n++; next }
192                 /^@@/      { scan = 1; next }
193                 /^Binary/  { if (!scan) bin = 1; next }
194                 /^\+/       { if (scan) a++; next }
195                 /^-/       { if (scan) r++; next }
196                 /^!/       { if (scan) c++; next }
197                 END        {
198                         dofile()
199                         print "\t----------------------------------------total------------------"
200                         line(at, rt, ct, "\033[min "n" files")
201                 }
202         ' <$1
203 }
204
205 function backup_filename {
206         i=1
207         while true; do
208                 name=$1.$i
209                 if ! [ -a "$name" ]; then
210                         touch $name
211                         echo $name
212                         return
213                 fi
214                 i=$(($i + 1))
215         done
216 }
217
218
219 # set up accept/reject rules
220 function DEFAULT { RULES="$RULES $DEFAULT_RULES"; }
221 function ALLOW   { for i in $*; do RULES="$RULES +$i"; done }
222 function DENY    { for i in $*; do RULES="$RULES -$i"; done }
223 function IGNORE  { for i in $*; do RULES="$RULES !$i"; done }
224
225 RULES=
226 HERE=$PWD
227 while true; do
228         if [ -f $CONFIG_FILE ]; then
229                 CONFIG="$PWD/$CONFIG_FILE"
230                 break
231         fi
232         cd ..
233         [ "$PWD" == "/" ] && break
234 done
235 cd "$HERE"
236
237 if [ "$CONFIG" ]; then
238         DEBUG "reading config $CONFIG"
239         source "$CONFIG"
240 elif [ -f ~/$CONFIG_FILE ]; then
241         DEBUG "reading config ~/$CONFIG_FILE"
242         source ~/$CONFIG_FILE
243 elif [ -f ~/${CONFIG_FILE}rc ]; then
244         DEBUG "reading config ~/${CONFIG_FILE}rc"
245         source ~/$CONFIG_FILE
246 else
247         DEBUG "no config file found; using default rules"
248         RULES="$RULES $DEFAULT_RULES"
249 fi
250 RULES="$PREFIX_RULES $RULES $POSTFIX_RULES"
251 DEBUG "using these rules: $RULES"
252
253
254 # create temporary dir that's automatically removed on exit
255 TMP=$(mktemp -d /tmp/$SELF.$BASE.XXXXXX) || (echo "$0: can't create temporary dir"; exit 1)
256 trap "rm -rf $TMP" 0 1 2 3 13 15
257
258
259 # move old files out of the way giving sequential suffixes
260 for i in $DIFF $CDIFF $ARCHIVE; do
261         [ -f $i ] && mv $i $(backup_filename $i)
262 done
263
264
265 LOG "updating and checking for new files ..."
266 $CVS -q up -dP >$TMP/up
267
268
269 if grep "^C " $TMP/up &>/dev/null; then
270         ERROR "there are conflicts with the following files:"
271         grep "^C " $TMP/up
272         exit 1
273 fi
274
275
276 LOG "making diff ..."
277 if ! $CVS -q diff -up >$DIFF; then
278         LOG "diff statistics:"
279         diffstat $DIFF
280         echo
281
282         # add diff file itself
283         echo $DIFF >>$TMP/files
284
285         # add changed binary files
286         awk '
287                 /^Index: / { scan = 1; file = $2; next }
288                 /^@@/      { scan = 0; next }
289                 /^Binary/  { if (scan) { print file } }
290         ' <$DIFF >>$TMP/files
291 else
292         rm -f $DIFF
293 fi
294
295
296 LOG "checking for files to submit ..."
297 if [ -f $TMP/files ]; then
298         cat $TMP/files|while read i; do
299                 CHANGED "$i"
300         done
301 fi
302
303 grep "^? " $TMP/up|while read i; do
304         find ${i#? } -type f >>$TMP/check
305 done
306
307
308 # filter files according to the pattern rules
309 if [ -f $TMP/check ]; then
310         for i in $(cat $TMP/check); do
311                 DEBUG "checking whether file '$i' matches"
312                 for r in $RULES; do
313                         DEBUG "\t\trule $r"
314                         R=${r#?}
315                         case "!$i" in $r)
316                                 DEBUG "\t\t\t\"silently\" rejected\t\t$R"
317                                 break
318                                 ;;
319                         esac
320                         case "-$i" in $r)
321                                 REJECT "$i\t\t$R"
322                                 break
323                                 ;;
324                         esac
325                         case "+$i" in $r)
326                                 NEW "$i\t\t$R"
327                                 echo "$i" >>$TMP/files
328                                 break
329                                 ;;
330                         esac
331                 done
332         done
333 fi
334
335
336 if ! [ -f $TMP/files ]; then
337         LOG "no changed or new files found"
338         exit 0
339 fi
340
341 echo
342 numfiles=$(awk '//{n++}END{print n}' <$TMP/files)
343 if [ -f $DIFF -a $numfiles == 1 ]; then
344         LOG "only changed non-binary files found"
345         LOG "creating compressed diff \e[1;37;40m$CDIFF\e[m\e[35m ..."
346         bzip2 -k $DIFF
347         RESULT=$CDIFF
348 else
349         LOG "changed and/or new files found"
350         LOG "creating archive \e[1;37;40m$ARCHIVE\e[m\e[35m ..."
351         tar -cjf $ARCHIVE --files-from $TMP/files
352         RESULT=$ARCHIVE
353 fi
354
355 [ -x "$UPLOAD" -a -f $RESULT ] && $UPLOAD $RESULT $DIFF
356
357 exit 0
358