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