tastier
[feisty_meow.git] / scripts / rev_control / version_control.sh
1 #!/bin/bash
2
3 # these are helper functions for doing localized revision control.
4 # this script should be sourced into other scripts that use it.
5
6 source "$FEISTY_MEOW_SCRIPTS/core/launch_feisty_meow.sh"
7 source "$FEISTY_MEOW_SCRIPTS/tty/terminal_titler.sh"
8
9 ##############
10
11 # the maximum depth that the recursive functions will try to go below the starting directory.
12 export MAX_DEPTH=5
13
14 #hmmm: re-address this code, since it doesn't make a lot of sense to me right now...
15 # one unpleasantry to take care of first; cygwin barfs aggressively if the TMP directory
16 # is a DOS path, but we need it to be a DOS path for our GFFS testing, so that blows.
17 # to get past this, TMP gets changed below to a hopefully generic and safe place.
18
19 if [[ "$TMP" =~ .:.* ]]; then
20   echo making weirdo temporary directory for DOS path.
21   export TMP=/tmp/rev_control_$USER
22 fi
23 if [ ! -d "$TMP" ]; then
24   mkdir -p $TMP
25 fi
26 if [ ! -d "$TMP" ]; then
27   echo "could not create the temporary directory TMP in: $TMP"
28   echo "this script will not work properly without an existing TMP directory."
29 fi
30
31 this_host=
32 # gets the machine's hostname and stores it in the variable "this_host".
33 function get_our_hostname()
34 {
35   if [ "$OS" == "Windows_NT" ]; then
36     this_host=$(hostname)
37   elif [ ! -z "$(echo $MACHTYPE | grep apple)" ]; then
38     this_host=$(hostname)
39   elif [ ! -z "$(echo $MACHTYPE | grep suse)" ]; then
40     this_host=$(hostname --long)
41   else
42     this_host=$(hostname)
43   fi
44   #echo "hostname is $this_host"
45 }
46
47 # this function sets a variable called "home_system" to "true" if the
48 # machine is considered one of fred's home machines.  if you are not
49 # fred, you may want to change the machine choices.
50 function is_home_system()
51 {
52   # load up the name of the host.
53   get_our_hostname
54   # reset the variable that we'll be setting.
55   unset home_system
56   export home_system
57   if [[ $this_host == *.gruntose.blurgh ]]; then
58     home_system=true
59   fi
60 }
61
62 #hmmm: move to core.
63 # makes sure that the "folder" is a directory and is writable.
64 # remember that bash successful returns are zeroes...
65 function test_writeable()
66 {
67   local folder="$1"; shift
68   if [ ! -d "$folder" -o ! -w "$folder" ]; then return 1; fi
69   return 0
70 }
71
72 # we only want to totally personalize this script if the user is right.
73 function check_user()
74 {
75   if [ "$USER" == "fred" ]; then
76     export SVNUSER=fred_t_hamster@
77     export EXTRA_PROTOCOL=+ssh
78   else
79     export SVNUSER=
80     export EXTRA_PROTOCOL=
81   fi
82 }
83
84 # calculates the right modifier for hostnames / repositories.
85 modifier=
86 function compute_modifier()
87 {
88   modifier=
89   directory="$1"; shift
90   in_or_out="$1"; shift
91   check_user
92   # some project specific overrides.
93   if [[ "$directory" == hoople* ]]; then
94     modifier="svn${EXTRA_PROTOCOL}://${SVNUSER}svn.code.sf.net/p/hoople2/svn/"
95   fi
96   if [[ "$directory" == yeti* ]]; then
97     modifier="svn${EXTRA_PROTOCOL}://${SVNUSER}svn.code.sf.net/p/yeti/svn/"
98   fi
99   # see if we're on one of fred's home machines.
100   is_home_system
101   # special override to pick local servers when at home.
102   if [ "$home_system" == "true" ]; then
103 #hmmm: this "home system" feature seems to be unnecessary?
104     if [ "$in_or_out" == "out" ]; then
105       # need the right home machine for modifier when checking out.
106       modifier=
107     else 
108       # no modifier for checkin.
109       modifier=
110     fi
111   fi
112 }
113
114 ##############
115
116 # selects the method for check-in based on where we are.
117 function do_checkin()
118 {
119   local directory="$1"; shift
120
121   save_terminal_title
122
123   # make a nice echoer since we want to use it inside conditions below.
124   local nicedir="$directory"
125   if [ $nicedir == "." ]; then
126     nicedir=$(\pwd)
127   fi
128   local blatt="echo checking in '$nicedir'..."
129
130   local retval=0  # normally successful.
131
132   do_update "$directory"
133   retval=$?
134   test_or_die "repository update failed; this should be fixed before check-in."
135
136   pushd "$directory" &>/dev/null
137   if [ -f ".no-checkin" ]; then
138     echo "skipping check-in due to presence of .no-checkin sentinel file."
139   elif [ -d "CVS" ]; then
140     if test_writeable "CVS"; then
141       $blatt
142       cvs ci .
143       retval=$?
144     fi
145   elif [ -d ".svn" ]; then
146     if test_writeable ".svn"; then
147       $blatt
148       svn ci .
149       retval=$?
150     fi
151   elif [ -d ".git" ]; then
152     if test_writeable ".git"; then
153       $blatt
154       # snag all new files.  not to everyone's liking.
155       git add --all .
156       retval=$?
157 echo A: retval=$retval
158
159       # see if there are any changes in the local repository.
160       if ! git diff-index --quiet HEAD --; then
161         # tell git about all the files and get a check-in comment.
162         git commit .
163         retval+=$?
164 echo B.1: retval=$retval
165       fi
166       # catch if the diff-index failed somehow.
167       retval+=$?
168 echo B.2: retval=$retval
169
170       # upload any changes to the upstream repo so others can see them.
171       git push 2>&1 
172 #| grep -v "X11 forwarding request failed"
173 #have to do pipestatus if want to keep the above.
174       retval+=$?
175 echo C: retval=$retval
176     fi
177   else
178     echo no repository in $directory
179     retval=1
180   fi
181   popd &>/dev/null
182
183   restore_terminal_title
184
185   return $retval
186 }
187
188 function do_diff
189 {
190   local directory="$1"; shift
191
192   save_terminal_title
193
194   pushd "$directory" &>/dev/null
195   local retval=0  # normally successful.
196
197   # only update if we see a repository living there.
198   if [ -d ".svn" ]; then
199     svn diff .
200     retval+=$?
201   elif [ -d ".git" ]; then
202     git diff 
203     retval+=$?
204   elif [ -d "CVS" ]; then
205     cvs diff .
206     retval+=$?
207   fi
208
209   popd &>/dev/null
210
211   restore_terminal_title
212
213   return $retval
214 }
215
216 function do_report_new
217 {
218   local directory="$1"; shift
219
220   save_terminal_title
221
222   pushd "$directory" &>/dev/null
223   local retval=0  # normally successful.
224
225   # only update if we see a repository living there.
226   if [ -f ".no-checkin" ]; then
227     echo "skipping reporting due to presence of .no-checkin sentinel file."
228   elif [ -d ".svn" ]; then
229     # this action so far only makes sense and is needed for svn.
230     bash $FEISTY_MEOW_SCRIPTS/rev_control/svnapply.sh \? echo
231     retval=$?
232   elif [ -d ".git" ]; then
233     git status -u
234     retval=$?
235   fi
236
237   popd &>/dev/null
238
239   restore_terminal_title
240
241   return $retval
242 }
243
244 # checks in all the folders in a specified list.
245 function checkin_list()
246 {
247   # make the list of directories unique.
248   local list="$(uniquify $*)"
249
250   save_terminal_title
251
252   # turn repo list back into an array.
253   eval "repository_list=( ${REPOSITORY_LIST[*]} )"
254
255   local outer inner
256
257   for outer in "${repository_list[@]}"; do
258     # check the repository first, since it might be an absolute path.
259     if [[ $outer =~ /.* ]]; then
260       # yep, this path is absolute.  just handle it directly.
261       if [ ! -d "$outer" ]; then continue; fi
262       do_checkin $outer
263       test_or_die "running check-in on: $outer"
264       sep 28
265     else
266       for inner in $list; do
267         # add in the directory component to see if we can find the folder.
268         local path="$inner/$outer"
269         if [ ! -d "$path" ]; then continue; fi
270         do_checkin $path
271         test_or_die "running check-in on: $path"
272         sep 28
273       done
274     fi
275   done
276
277   restore_terminal_title
278 }
279
280 # takes out the first few carriage returns that are in the input.
281 function squash_first_few_crs()
282 {
283   i=0
284   while read input_text; do
285     i=$((i+1))
286     if [ $i -le 5 ]; then
287       echo -n "$input_text  "
288     else
289       echo $input_text
290     fi
291   done
292   if [ $i -le 3 ]; then
293     # if we're still squashing eols, make sure we don't leave them hanging.
294     echo
295   fi
296 }
297
298 # selects the checkout method based on where we are (the host the script runs on).
299 function do_update()
300 {
301   directory="$1"; shift
302
303   save_terminal_title
304
305   # make a nice echoer since we want to use it inside conditions below.
306   local nicedir="$directory"
307   if [ $nicedir == "." ]; then
308     nicedir=$(\pwd)
309   fi
310   local blatt="echo retrieving '$nicedir'..."
311
312   local retval=0  # plan on success for now.
313   pushd "$directory" &>/dev/null
314   if [ -d "CVS" ]; then
315     if test_writeable "CVS"; then
316       $blatt
317       cvs update . | squash_first_few_crs
318       retval=${PIPESTATUS[0]}
319     fi
320   elif [ -d ".svn" ]; then
321     if test_writeable ".svn"; then
322       $blatt
323       svn update . | squash_first_few_crs
324       retval=${PIPESTATUS[0]}
325     fi
326   elif [ -d ".git" ]; then
327     if test_writeable ".git"; then
328       $blatt
329       git pull 2>&1 | grep -v "X11 forwarding request failed" | squash_first_few_crs
330       retval=${PIPESTATUS[0]}
331     fi
332   else
333     # this is not an error necessarily; we'll just pretend they planned this.
334     echo no repository in $directory
335   fi
336   popd &>/dev/null
337
338   restore_terminal_title
339
340   return $retval
341 }
342
343 # gets all the updates for a list of folders under revision control.
344 function checkout_list()
345 {
346   local list="$(uniquify $*)"
347
348   save_terminal_title
349
350   # turn repo list back into an array.
351   eval "repository_list=( ${REPOSITORY_LIST[*]} )"
352
353   local outer inner
354
355   for outer in "${repository_list[@]}"; do
356     # check the repository first, since it might be an absolute path.
357     if [[ $outer =~ /.* ]]; then
358       # yep, this path is absolute.  just handle it directly.
359       if [ ! -d "$outer" ]; then continue; fi
360       do_update $outer
361       test_or_die "running update on: $path"
362       sep 28
363     else
364       for inner in $list; do
365         # add in the directory component to see if we can find the folder.
366         local path="$inner/$outer"
367         if [ ! -d "$path" ]; then continue; fi
368         do_update $path
369         test_or_die "running update on: $path"
370         sep 28
371       done
372     fi
373   done
374
375   restore_terminal_title
376 }
377
378 # provides a list of absolute paths of revision control directories
379 # that are located under the directory passed as the first parameter.
380 function generate_rev_ctrl_filelist()
381 {
382   local dir="$1"; shift
383   pushd "$dir" &>/dev/null
384   local dirhere="$( \cd "$(\dirname "$dir")" && /bin/pwd )"
385   local tempfile=$(mktemp /tmp/zz_checkins.XXXXXX)
386   echo >$tempfile
387   local additional_filter
388   find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".svn" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
389   find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".git" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
390   # CVS is not well behaved like git and (now) svn, and we seldom use it anymore.
391   popd &>/dev/null
392
393   # see if they've warned us not to try checking in within vendor hierarchies.
394   if [ ! -z "NO_CHECKIN_VENDOR" ]; then
395     sed -i -e '/.*\/vendor\/.*/d' "$tempfile"
396   fi
397
398   local sortfile=$(mktemp /tmp/zz_checkin_sort.XXXXXX)
399   sort <"$tempfile" >"$sortfile"
400   \rm "$tempfile"
401   echo "$sortfile"
402 }
403
404 # iterates across a list of directories contained in a file (first parameter).
405 # on each directory name, it performs the action (second parameter) provided.
406 function perform_revctrl_action_on_file()
407 {
408   local tempfile="$1"; shift
409   local action="$1"; shift
410
411   save_terminal_title
412
413   while read -u 3 dirname; do
414     if [ -z "$dirname" ]; then continue; fi
415     pushd "$dirname" &>/dev/null
416     echo "[$(pwd)]"
417     $action .
418     test_or_die "performing action $action on: $(pwd)"
419     sep 28
420     popd &>/dev/null
421   done 3<"$tempfile"
422
423   restore_terminal_title
424
425   rm $tempfile
426 }
427
428