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