]> git.mxchange.org Git - flightgear.git/blob - scripts/tools/fg-submit
fg-check: speedup, minor fixes, cleanup
[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 [<basename>]
16 #
17 # Example:
18 #     $ cd $FG_ROOT/Aircraft/bo105
19 #     $ fg-submit                # -> bo105.diff.bz2 or bo105.tar.bz2
20 #
21 #     $ fg-submit update         # -> update.diff.bz2 or update.tar.bz2
22 #
23 #
24 # Spaces in the basename are replaced with "%20". People who prefer
25 # to have the date in the archive name can conveniently achieve this
26 # by defining a shell alias in ~/.bashrc:
27 #
28 #     alias submit='fg-submit "${PWD/#*\/}-$(date +%Y-%m-%d)"'
29 #
30 #
31 # If the script finds an application named "fg-upload", then it calls
32 # this at the end with three arguments:
33 #
34 #     $1 ... working directory ($PWD)
35 #     $2 ... archive or compressed diff for submission
36 #     $3 ... accessory diff, *NOT* for submission!
37 #
38 # $2 and $3 are guaranteed not to contain spaces, only $1 is guaranteed
39 # to actually exist. Such as script can be used to upload the file to an
40 # ftp-/webserver, and/or to remove one or both files. Example using
41 # KDE's kfmclient for upload (alternatives: ncftpput, gnomevfs-copy, ...):
42 #
43 #     $ cat ~/bin/fg-upload
44 #     #!/bin/bash
45 #     echo "uploading $2"
46 #     if kfmclient copy $2 ftp://user:password@server.com; then
47 #             echo "deleting $2 $3"
48 #             rm -rf $2 $3
49 #
50 #             echo "Done.  URL: ftp://server.com/$2"
51 #     else
52 #             echo "arghh ... HELP! HELP!"
53 #     fi
54
55
56 SELF=${0/#*\/}
57 DIR=${PWD/#*\/}
58 BASE=${1:-$DIR}
59 BASE=${BASE// /%20}
60
61 CVS=/usr/bin/cvs                 # avoid colorcvs wrapper from
62 [ -x $CVS ] || CVS=cvs           # http://www.hakubi.us/colorcvs/
63 UPLOAD=$(which fg-upload 2>/dev/null)
64
65 ARCHIVE=$BASE.tar.bz2
66 DIFF=$BASE.diff
67 CDIFF=$DIFF.bz2
68
69
70 function ERROR   { echo -e "\e[31;1m$*\e[m";   }
71 function LOG     { echo -e "\e[35m$*\e[m";     }
72 function NEW     { echo -e "\e[32m\t+ $*\e[m"; }
73 function CHANGED { echo -e "\e[36m\t+ $*\e[m"; }
74 function REJECT  { echo -e "\e[31m\t- $*\e[m"; }
75
76 function diffstat {
77         # output diff statistics, similar to the "diffstat" utility
78         awk '
79                 function line(a, r, c, f) {
80                         print "\t\033[32m"a"\033[m\t\033[31m"r"\033[m\t\033[34m"c"\033[m\t"f
81                 }
82                 function dofile() {
83                         if (!file) {
84                                 return
85                         }
86                         if (bin) {
87                                 print "\t. . . . binary  . . . . \033[36m"file"\033[m"
88                         } else {
89                                 line(a, r, c, file)
90                                 at += a; rt += r; ct += c
91                         }
92                         a = r = c = 0
93                 }
94                 BEGIN      {
95                         print "\tadded---removed-changed----------------------------------------"
96                         a = r = c = at = rt = ct = n = bin = 0
97                 }
98                 /^Index: / { dofile(); scan = bin = 0; file = $2; n++; next }
99                 /^@@/      { scan = 1; next }
100                 /^Binary/  { if (!scan) bin = 1; next }
101                 /^+/       { if (scan) a++; next }
102                 /^-/       { if (scan) r++; next }
103                 /^!/       { if (scan) c++; next }
104                 END        {
105                         dofile()
106                         print "\t----------------------------------------total------------------"
107                         line(at, rt, ct, "\033[min "n" files")
108                 }
109         ' <$1
110 }
111
112
113 # create temporary dir that's automatcally removed on exit
114 TMP=$(mktemp -d -t $SELF.$BASE.XXX) || (echo "$0: can't create temporary dir"; exit 1)
115 trap "rm -rf $TMP" 0 1 2 3 13 15
116
117
118 # move old files out of the way
119 for i in $DIFF $CDIFF $ARCHIVE; do
120         [ -f $i ] && mv $i $(mktemp $i.X)
121 done
122
123
124 LOG "updating and checking for new files ..."
125 $CVS -q up -dP >$TMP/up
126
127
128 if grep "^C " $TMP/up &>/dev/null; then
129         ERROR "there are conflicts with the following files:"
130         grep "^C " $TMP/up
131         exit 1
132 fi
133
134
135 LOG "making diff ..."
136 if ! $CVS -q diff -up >$DIFF; then
137         LOG "diff statistics:"
138         diffstat $DIFF
139         echo
140
141         # add diff file itself
142         echo $DIFF >>$TMP/files
143
144         # add changed binary files
145         awk '
146                 /^Index: / { scan = 1; file = $2; next }
147                 /^@@/      { scan = 0; next }
148                 /^Binary/  { if (scan) { print file } }
149         ' <$DIFF >>$TMP/files
150 else
151         rm -f $DIFF
152 fi
153
154
155 LOG "checking for files to submit ..."
156 if [ -f $TMP/files ]; then
157         cat $TMP/files|while read i; do
158                 CHANGED "$i"
159         done
160 fi
161
162 grep "^? " $TMP/up|while read i; do
163         find ${i#? } -type f >>$TMP/check
164 done
165
166
167 # classify and filter files
168 if [ -f $TMP/check ]; then
169         for i in $(cat $TMP/check); do
170                 case "$i" in
171                 $ARCHIVE*|$DIFF*) # don't add files generated by the script
172                         ;;
173                 */.*|.*)          # silently drop hidden files
174                         ;;
175                 *~|*.|*.bak|*.orig)
176                         REJECT "$i\t\t(backup file)"
177                         ;;
178                 CVS/*|*/CVS/*)
179                         REJECT "$i\t\t(CVS file)"
180                         ;;
181                 *.blend|*.blend[0-9]|*.blend[0-9][0-9])
182                         REJECT "$i\t\t(blender file)"
183                         ;;
184                 *.xcf|*.tga|*.bmp|*.BMP|*.png)
185                         REJECT "$i\t\t(graphics file)"
186                         ;;
187                 *)
188                         NEW "$i"
189                         echo "$i" >>$TMP/files
190                         ;;
191                 esac
192         done
193 fi
194
195
196 if ! [ -f $TMP/files ]; then
197         LOG "no changed or new files found"
198         exit 0
199 fi
200
201 echo
202 numfiles=$(awk '//{n++}END{print n}' <$TMP/files)
203 if [ -f $DIFF -a $numfiles == 1 ]; then
204         LOG "only changed non-binary files found"
205         LOG "creating compressed diff \e[1;37;40m$CDIFF\e[m\e[35m ..."
206         bzip2 -k $DIFF
207         RESULT=$CDIFF
208 else
209         LOG "changed and/or new files found"
210         LOG "creating archive \e[1;37;40m$ARCHIVE\e[m\e[35m ..."
211         tar -cjf $ARCHIVE --files-from $TMP/files
212         RESULT=$ARCHIVE
213 fi
214
215 [ -x "$UPLOAD" -a -f $RESULT ] && $UPLOAD "$PWD" $RESULT $DIFF
216
217 exit 0
218