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