be183ae1bf3cf1c82b6250a2c99f4fd95bac53a1
[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/launch_feisty_meow.sh"
7 source "$FEISTY_MEOW_SCRIPTS/tty/terminal_titler.sh"
8
9 ##############
10
11 # the maximum depth that the recursive functions will try to go below the starting directory.
12 export MAX_DEPTH=5
13
14 # one unpleasantry to take care of first; cygwin barfs aggressively if the TMP directory
15 # is a DOS path, but we need it to be a DOS path for our GFFS testing, so that blows.
16 # to get past this, TMP gets changed below to a hopefully generic and safe place.
17 if [[ "$TMP" =~ .:.* ]]; then
18   echo "making weirdo temporary directory for PCDOS-style path."
19   export TMP=/tmp/rev_control_$USER
20 fi
21 if [ ! -d "$TMP" ]; then
22   mkdir -p $TMP
23 fi
24 if [ ! -d "$TMP" ]; then
25   echo "could not create the temporary directory TMP in: $TMP"
26   echo "this script will not work properly without an existing TMP directory."
27 fi
28 #hmmm: re-address the above code, since it doesn't make a lot of sense to me right now...
29
30
31 ##############
32
33 # checks the directory provided into the revision control system repository it belongs to.
34 function do_checkin()
35 {
36   local directory="$1"; shift
37
38   save_terminal_title
39
40   # make a nice echoer since we want to use it inside conditions below.
41   local nicedir="$directory"
42   if [ $nicedir == "." ]; then
43     nicedir=$(\pwd)
44   fi
45   local blatt="echo checking in '$nicedir'..."
46
47   local retval=0  # normally successful.
48
49   do_update "$directory"
50   retval=$?
51   test_or_die "repository update failed; this should be fixed before check-in."
52
53   pushd "$directory" &>/dev/null
54   if [ -f ".no-checkin" ]; then
55     echo "skipping check-in due to presence of .no-checkin sentinel file."
56   elif [ -d "CVS" ]; then
57     if test_writeable "CVS"; then
58       $blatt
59       cvs ci .
60       retval=$?
61     fi
62   elif [ -d ".svn" ]; then
63     if test_writeable ".svn"; then
64       $blatt
65       svn ci .
66       retval=$?
67     fi
68   elif [ -d ".git" ]; then
69     if test_writeable ".git"; then
70       $blatt
71       # snag all new files.  not to everyone's liking.
72       git add --all .
73       retval=$?
74
75       # see if there are any changes in the local repository.
76       if ! git diff-index --quiet HEAD --; then
77         # tell git about all the files and get a check-in comment.
78         git commit .
79         retval+=$?
80       fi
81       # catch if the diff-index failed somehow.
82       retval+=$?
83
84       # upload any changes to the upstream repo so others can see them.
85       git push 2>&1 | grep -v "X11 forwarding request failed" | squash_first_few_crs
86       retval+=${PIPESTATUS[0]}
87     fi
88   else
89     # nothing there.  it's not an error though.
90     echo no repository in $directory
91     retval=0
92   fi
93   popd &>/dev/null
94
95   restore_terminal_title
96
97   return $retval
98 }
99
100 # shows the local changes in a repository.
101 function do_diff
102 {
103   local directory="$1"; shift
104
105   save_terminal_title
106
107   pushd "$directory" &>/dev/null
108   local retval=0  # normally successful.
109
110   # only update if we see a repository living there.
111   if [ -d ".svn" ]; then
112     svn diff .
113     retval+=$?
114   elif [ -d ".git" ]; then
115     git diff 
116     retval+=$?
117   elif [ -d "CVS" ]; then
118     cvs diff .
119     retval+=$?
120   fi
121
122   popd &>/dev/null
123
124   restore_terminal_title
125
126   return $retval
127 }
128
129 # reports any files that are not already known to the upstream repository.
130 function do_report_new
131 {
132   local directory="$1"; shift
133
134   save_terminal_title
135
136   pushd "$directory" &>/dev/null
137   local retval=0  # normally successful.
138
139   # only update if we see a repository living there.
140   if [ -f ".no-checkin" ]; then
141     echo "skipping reporting due to presence of .no-checkin sentinel file."
142   elif [ -d ".svn" ]; then
143     # this action so far only makes sense and is needed for svn.
144     bash $FEISTY_MEOW_SCRIPTS/rev_control/svnapply.sh \? echo
145     retval=$?
146   elif [ -d ".git" ]; then
147     git status -u
148     retval=$?
149   fi
150
151   popd &>/dev/null
152
153   restore_terminal_title
154
155   return $retval
156 }
157
158 # checks in all the folders in a specified list.
159 function checkin_list()
160 {
161   # make the list of directories unique.
162   local list="$(uniquify $*)"
163
164   save_terminal_title
165
166   # turn repo list back into an array.
167   eval "repository_list=( ${REPOSITORY_LIST[*]} )"
168
169   local outer inner
170
171   for outer in "${repository_list[@]}"; do
172     # check the repository first, since it might be an absolute path.
173     if [[ $outer =~ /.* ]]; then
174       # yep, this path is absolute.  just handle it directly.
175       if [ ! -d "$outer" ]; then continue; fi
176       do_checkin $outer
177       test_or_die "running check-in on: $outer"
178       sep 28
179     else
180       for inner in $list; do
181         # add in the directory component to see if we can find the folder.
182         local path="$inner/$outer"
183         if [ ! -d "$path" ]; then continue; fi
184         do_checkin $path
185         test_or_die "running check-in on: $path"
186         sep 28
187       done
188     fi
189   done
190
191   restore_terminal_title
192 }
193
194 # takes out the first few carriage returns that are in the input.
195 function squash_first_few_crs()
196 {
197   i=0
198   while read input_text; do
199     i=$((i+1))
200     if [ $i -le 5 ]; then
201       echo -n "$input_text  "
202     else
203       echo $input_text
204     fi
205   done
206   if [ $i -le 3 ]; then
207     # if we're still squashing eols, make sure we don't leave them hanging.
208     echo
209   fi
210 }
211
212 # a helpful method that reports the git branch for the current directory's
213 # git repository.
214 function git_branch_name()
215 {
216   echo "$(git branch | grep \* | cut -d ' ' -f2-)"
217 }
218
219 # gets the latest versions of the assets from the upstream repository.
220 function do_update()
221 {
222   directory="$1"; shift
223
224   save_terminal_title
225
226   # make a nice echoer since we want to use it inside conditions below.
227   local nicedir="$directory"
228   if [ $nicedir == "." ]; then
229     nicedir=$(\pwd)
230   fi
231   local blatt="echo retrieving '$nicedir'..."
232
233   local retval=0  # plan on success for now.
234   pushd "$directory" &>/dev/null
235   if [ -d "CVS" ]; then
236     if test_writeable "CVS"; then
237       $blatt
238       cvs update . | squash_first_few_crs
239       retval=${PIPESTATUS[0]}
240     fi
241   elif [ -d ".svn" ]; then
242     if test_writeable ".svn"; then
243       $blatt
244       svn update . | squash_first_few_crs
245       retval=${PIPESTATUS[0]}
246     fi
247   elif [ -d ".git" ]; then
248     if test_writeable ".git"; then
249       $blatt
250       retval=0
251
252       if [ "$(git_branch_name)" != "master" ]; then
253         git pull origin master 2>&1 | grep -v "X11 forwarding request failed" | squash_first_few_crs
254         retval+=${PIPESTATUS[0]}
255       fi
256
257       git pull 2>&1 | grep -v "X11 forwarding request failed" | squash_first_few_crs
258       retval+=${PIPESTATUS[0]}
259     fi
260   else
261     # this is not an error necessarily; we'll just pretend they planned this.
262     echo no repository in $directory
263   fi
264   popd &>/dev/null
265
266   restore_terminal_title
267
268   return $retval
269 }
270
271 # gets all the updates for a list of folders under revision control.
272 function checkout_list()
273 {
274   local list="$(uniquify $*)"
275
276   save_terminal_title
277
278   # turn repo list back into an array.
279   eval "repository_list=( ${REPOSITORY_LIST[*]} )"
280
281   local outer inner
282
283   for outer in "${repository_list[@]}"; do
284     # check the repository first, since it might be an absolute path.
285     if [[ $outer =~ /.* ]]; then
286       # yep, this path is absolute.  just handle it directly.
287       if [ ! -d "$outer" ]; then continue; fi
288       do_update $outer
289       test_or_die "running update on: $path"
290       sep 28
291     else
292       for inner in $list; do
293         # add in the directory component to see if we can find the folder.
294         local path="$inner/$outer"
295         if [ ! -d "$path" ]; then continue; fi
296         do_update $path
297         test_or_die "running update on: $path"
298         sep 28
299       done
300     fi
301   done
302
303   restore_terminal_title
304 }
305
306 # provides a list of absolute paths of revision control directories
307 # that are located under the directory passed as the first parameter.
308 function generate_rev_ctrl_filelist()
309 {
310   local dir="$1"; shift
311   pushd "$dir" &>/dev/null
312   local dirhere="$( \cd "$(\dirname "$dir")" && /bin/pwd )"
313   local tempfile=$(mktemp /tmp/zz_checkins.XXXXXX)
314   echo >$tempfile
315   local additional_filter
316   find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".svn" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
317   find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".git" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
318   # CVS is not well behaved like git and (now) svn, and we seldom use it anymore.
319   popd &>/dev/null
320
321   # see if they've warned us not to try checking in within vendor hierarchies.
322   if [ ! -z "NO_CHECKIN_VENDOR" ]; then
323     sed -i -e '/.*\/vendor\/.*/d' "$tempfile"
324   fi
325
326   local sortfile=$(mktemp /tmp/zz_checkin_sort.XXXXXX)
327   sort <"$tempfile" >"$sortfile"
328   \rm "$tempfile"
329   echo "$sortfile"
330 }
331
332 # iterates across a list of directories contained in a file (first parameter).
333 # on each directory name, it performs the action (second parameter) provided.
334 function perform_revctrl_action_on_file()
335 {
336   local tempfile="$1"; shift
337   local action="$1"; shift
338
339   save_terminal_title
340
341   while read -u 3 dirname; do
342     if [ -z "$dirname" ]; then continue; fi
343     pushd "$dirname" &>/dev/null
344     echo "[$(pwd)]"
345     $action .
346     test_or_die "performing action $action on: $(pwd)"
347     sep 28
348     popd &>/dev/null
349   done 3<"$tempfile"
350
351   restore_terminal_title
352
353   rm $tempfile
354 }
355
356