renaming frenzy to make the revision control tools useful. sneakily renamed 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 # 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       modifier="svn://shaggy/"
93     else 
94       # no modifier for checkin.
95       modifier=
96     fi
97   fi
98 }
99
100 # selects the method for check-in based on where we are.
101 function do_checkin()
102 {
103   local directory="$1"; shift
104   pushd "$directory" &>/dev/null
105   if [ -d "CVS" ]; then cvs ci . ;
106   elif [ -d ".svn" ]; then svn ci . ;
107   elif [ -d ".git" ]; then
108     # snag all new files.  not to everyone's liking.
109     git add .
110     # tell git about all the files and get a check-in comment.
111     git commit .
112     # upload the files to the server so others can see them.
113     git push 2>&1 | grep -v "X11 forwarding request failed"
114   else
115     echo unknown repository for $directory...
116   fi
117   popd &>/dev/null
118 }
119
120 # checks in all the folders in a specified list.
121 function checkin_list {
122   local list=$*
123   for i in $list; do
124     # turn repo list back into an array.
125     eval "repository_list=( ${REPOSITORY_LIST[*]} )"
126     for j in "${repository_list[@]}"; do
127       # add in the directory component.
128       j="$i/$j"
129       if [ ! -d "$j" ]; then continue; fi
130
131 #      pushd $j &>/dev/null
132       echo "checking in '$j'..."
133       do_checkin $j
134 #      popd &>/dev/null
135     done
136   done
137 }
138
139 # takes out the first few carriage returns that are in the input.
140 function squash_first_few_crs()
141 {
142   i=0
143   while read line; do
144     i=$((i+1))
145     if [ $i -le 3 ]; then
146       echo -n "$line  "
147     else
148       echo $line
149     fi
150   done
151   if [ $i -le 3 ]; then
152     # if we're still squashing eols, make sure we don't leave them hanging.
153     echo
154   fi
155 }
156
157 # selects the checkout method based on where we are (the host the script runs on).
158 function do_update()
159 {
160   directory="$1"; shift
161  
162   pushd "$directory" &>/dev/null
163   if [ -d "CVS" ]; then
164     cvs update . | squash_first_few_crs
165   elif [ -d ".svn" ]; then
166     svn update . | squash_first_few_crs
167   elif [ -d ".git" ]; then
168     git pull 2>&1 | grep -v "X11 forwarding request failed" | squash_first_few_crs
169   else
170     echo unknown repository for $directory...
171   fi
172   popd &>/dev/null
173 }
174
175 # gets all the updates for a list of folders under revision control.
176 function checkout_list {
177   list=$*
178   for i in $list; do
179     # turn repo list back into an array.
180     eval "repository_list=( ${REPOSITORY_LIST[*]} )"
181     for j in "${repository_list[@]}"; do
182       # add in the directory for our purposes here.
183       j="$i/$j"
184       if [ ! -d $j ]; then
185         if [ ! -z "$SHELL_DEBUG" ]; then
186           echo "No directory called $j exists."
187         fi
188         continue
189       fi
190
191       echo -n "retrieving '$j'...  "
192       do_update $j
193     done
194   done
195 }
196