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