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