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