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