version 1.40.130 release
[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 # Author: Chris Koeritz
7 # Author: Kevin Wentworth
8
9 source "$FEISTY_MEOW_SCRIPTS/core/launch_feisty_meow.sh"
10 source "$FEISTY_MEOW_SCRIPTS/tty/terminal_titler.sh"
11
12 ##############
13
14 # the maximum depth that the recursive functions will try to go below the starting directory.
15 export MAX_DEPTH=5
16
17 # the name of our "don't check this stuff in" file.
18 export NO_CHECKIN=".no-checkin"
19
20 # use our splitter tool for lengthy output if it's available.
21 if [ ! -z "$(whichable splitter)" ]; then
22   TO_SPLITTER="$(whichable splitter)"
23   # calculate the number of columsn in the terminal.
24   cols=$(get_maxcols)
25   TO_SPLITTER+=" --maxcol $(($cols - 1))"
26 else
27   TO_SPLITTER=cat
28 fi
29
30 ##############
31
32 # one unpleasantry to take care of first; cygwin barfs aggressively if the TMP directory
33 # is a DOS path, but we need it to be a DOS path for our GFFS testing, so that blows.
34 # to get past this, TMP gets changed below to a hopefully generic and safe place.
35 if [[ "$TMP" =~ .:.* ]]; then
36   log_feisty_meow_event "making weirdo temporary directory for PCDOS-style path."
37   export TMP=/tmp/rev_control_$USER
38 fi
39 if [ ! -d "$TMP" ]; then
40   mkdir -p $TMP
41 fi
42 if [ ! -d "$TMP" ]; then
43   echo "could not create the temporary directory TMP in: $TMP"
44   echo "this script will not work properly without an existing TMP directory."
45   echo
46 #hmmm: odd approach to solving the "sourced scripts shouldn't exit or they take down the
47 #      original caller too" issue.
48   echo "hit ctrl-c to stay in this shell now, otherwise it may exit in 10 seconds..."
49   sleep 10
50   exit 1
51 fi
52
53 ##############
54
55 # checks the directory provided into the revision control system repository it belongs to.
56 function do_revctrl_checkin()
57 {
58   local directory="$1"; shift
59
60 #hmmm: another piece of reusable code, to process the directory for printing.
61   # make a nice echoer since we want to use it inside conditions below.
62   local nicedir="$directory"
63   if [ $nicedir == "." ]; then
64     nicedir=$(\pwd)
65   fi
66   local blatt_report="echo -ne \nchecking in '$nicedir'...  "
67   local tell_no_checkin="echo -ne \nskipping check-in due to presence of $NO_CHECKIN sentinel file: $nicedir"
68
69   pushd "$directory" &>/dev/null
70 #hmmm: overly elaborate sections below here, but we do want precise handling for git case.
71   if [ -d "CVS" ]; then
72     if test_writeable "CVS"; then
73       do_revctrl_simple_update "$directory"
74       exit_on_error "updating repository; this issue should be fixed before check-in."
75       if [ -f "$NO_CHECKIN" ]; then
76 #        echo -ne "\nskipping check-in due to presence of $NO_CHECKIN sentinel file: $directory"
77         $tell_no_checkin
78       else
79         $blatt_report
80         cvs ci .
81         exit_on_error "cvs checkin"
82       fi
83     fi
84   elif [ -d ".svn" ]; then
85     if test_writeable ".svn"; then
86       do_revctrl_simple_update "$directory"
87       exit_on_error "updating repository; this issue should be fixed before check-in."
88       if [ -f "$NO_CHECKIN" ]; then
89 #        echo -ne "\nskipping check-in due to presence of $NO_CHECKIN sentinel file: $directory"
90         $tell_no_checkin
91       else
92         $blatt_report
93         svn ci .
94         exit_on_error "svn checkin"
95       fi
96     fi
97   elif [ -d ".git" ]; then
98     if test_writeable ".git"; then
99
100       # take steps to make sure the branch integrity is good and we're up to date against remote repos.
101       do_revctrl_careful_update "$(\pwd)"
102
103       if [ -f "$NO_CHECKIN" ]; then
104 #        echo -ne "\nskipping check-in due to presence of $NO_CHECKIN sentinel file: $directory"
105         $tell_no_checkin
106       else
107         $blatt_report
108
109         # put all changed and new files in the commit.  not to everyone's liking.
110         git add --all . | $TO_SPLITTER
111         promote_pipe_return 0
112         exit_on_error "git add all new files"
113
114         # see if there are any changes in the local repository.
115         if ! git diff-index --quiet HEAD --; then
116           # tell git about all the files and get a check-in comment.
117 #hmmm: begins to look like, you guessed it, a reusable bit that all commit actions could enjoy.
118           git commit .
119           retval=$?
120           continue_on_error "git commit"
121           if [ $retval -ne 0 ]; then
122             echo -e -n "Commit failed or was aborted:\nShould we continue with other check-ins? [y/N] "
123             local line
124             read line
125             if [[ "${line:0:1}" != "y" ]]; then
126               echo "Stopping check-in process due to missing commit and user request."
127               exit 1
128             fi
129           fi
130         fi
131
132         # we continue on to the push, even if there were no obvious changes this run, because
133         # there could already be committed changes that haven't been pushed yet.
134
135         # upload any changes to the upstream repo so others can see them.
136         git push --tags origin "$(my_branch_name)" 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
137         promote_pipe_return 0
138         exit_on_error "git push"
139       fi
140     fi
141   else
142     # nothing there.  it's not an error though.
143     log_feisty_meow_event "no repository in $directory"
144   fi
145   popd &>/dev/null
146
147   return 0
148 }
149
150 # shows the local changes in a repository.
151 function do_revctrl_diff
152 {
153   local directory="$1"; shift
154
155   pushd "$directory" &>/dev/null
156
157   # only update if we see a repository living there.
158   if [ -d ".svn" ]; then
159     svn diff .
160     exit_on_error "subversion diff"
161   elif [ -d ".git" ]; then
162     git --no-pager diff 
163     exit_on_error "git diff"
164   elif [ -d "CVS" ]; then
165     cvs diff .
166     exit_on_error "cvs diff"
167   fi
168
169   popd &>/dev/null
170
171   return 0
172 }
173
174 # reports any files that are not already known to the upstream repository.
175 function do_revctrl_report_new
176 {
177   local directory="$1"; shift
178
179   pushd "$directory" &>/dev/null
180
181   # only update if we see a repository living there.
182   if [ -f "$NO_CHECKIN" ]; then
183     echo -ne "\nskipping reporting due to presence of $NO_CHECKIN sentinel file: $directory"
184   elif [ -d ".svn" ]; then
185     # this action so far only makes sense and is needed for svn.
186     bash $FEISTY_MEOW_SCRIPTS/rev_control/svnapply.sh \? echo
187     exit_on_error "svn diff"
188   elif [ -d ".git" ]; then
189     git status -u
190     exit_on_error "git status -u"
191   fi
192
193   popd &>/dev/null
194
195   return 0
196 }
197
198 # checks in all the folders in the specified list.
199 function checkin_list()
200 {
201   # make the list of directories unique.
202   local list="$(uniquify $*)"
203
204   # turn repo list back into an array.
205   eval "repository_list=( ${REPOSITORY_LIST[*]} )"
206
207   local outer inner
208
209   for outer in "${repository_list[@]}"; do
210     # check the repository first, since it might be an absolute path.
211     if [[ $outer =~ /.* ]]; then
212       # yep, this path is absolute.  just handle it directly.
213       if [ ! -d "$outer" ]; then continue; fi
214       do_revctrl_checkin "$outer"
215       exit_on_error "running check-in (absolute) on path: $outer"
216     else
217       for inner in $list; do
218         # add in the directory component to see if we can find the folder.
219         local path="$inner/$outer"
220         if [ ! -d "$path" ]; then continue; fi
221         do_revctrl_checkin "$path"
222         exit_on_error "running check-in (relative) on path: $path"
223       done
224     fi
225   done
226 }
227
228 #hmmm: below functions are git specific and should be named that way.
229
230 function all_branch_names()
231 {
232   echo "$(git branch -vv | cut -d ' ' -f2)"
233 }
234
235 #this had a -> in it at one point for not matching, didn't it?
236 # this reports the upstream branch for the current repo.
237 ##function parent_branch_name()
238 ##{
239   ##echo "$(git branch -vv | grep \* | cut -d ' ' -f2)"
240 ##}
241
242 # a helpful method that reports the git branch for the current directory's
243 # git repository.
244 function my_branch_name()
245 {
246   echo "$(git branch -vv | grep '\*' | cut -d ' ' -f2)"
247 }
248
249 # reports the status of the branch by echoing one of these values:
250 #   okay: up to date and everything is good.
251 #   needs_pull: this branch needs to be pulled from origins.
252 #   needs_push: there are unsaved changes on this branch to push to remote store.
253 #   diverged: the branches diverged and are going to need a merge.
254 # reference: https://stackoverflow.com/questions/3258243/check-if-pull-needed-in-git
255 function check_branch_state()
256 {
257   local branch="$1"; shift
258
259   if [ -z "$branch" ]; then
260     echo "No branch was passed to check branch state."
261     return 1
262   fi
263
264   local to_return=120  # unknown issue.
265
266   local local_branch=$(git rev-parse HEAD)
267   local remote_branch=$(git rev-parse "$branch")
268   local merge_base=$(git merge-base HEAD "$branch")
269
270   local to_echo=
271   if [ "$local_branch" == "$remote_branch" ]; then
272     to_echo="okay"
273   elif [ "$local_branch" == "$merge_base" ]; then
274     to_echo="needs_pull"
275   elif [ "$remote_branch" == "$merge_base" ]; then
276     to_echo="needs_push"
277   else
278     to_echo="diverged"
279   fi
280
281   echo -n "$to_echo"
282
283   return $to_return
284 }
285
286 # showes the branch currently active in the repository.
287 function show_active_branch()
288 {
289 #hmmm: if no args, assume current dir!
290
291   for directory in "$@"; do
292     echo -n "active branch for '$directory': "
293     pushd "$directory" &>/dev/null
294
295 #hmmm: if git...
296     git rev-parse --abbrev-ref HEAD
297 #hmmm: else OTHERS!!!
298
299     popd &>/dev/null
300   done
301 }
302
303 # only shows the branch state if it's not okay.
304 # note that this is not the same as a conditional branch (ha ha).
305 function show_branch_conditionally()
306 {
307   local this_branch="$1"; shift
308
309   local state=$(check_branch_state "$this_branch")
310   if [ "$state" != "okay" ]; then
311     echo "=> branch '$this_branch' state is not clean: $state"
312   fi
313 }
314
315 # the git update process just gets more and more complex when you bring in
316 # branches, so we've moved this here to avoid having a ton of code in the
317 # other methods.
318 function do_revctrl_careful_update()
319 {
320   local directory="$1"; shift
321   pushd "$directory" &>/dev/null
322   exit_on_error "changing to directory: $directory"
323
324   if [ ! -d ".git" ]; then
325     # not a git project, so just boil this down to a getem action.
326     popd &>/dev/null
327     log_feisty_meow_event "skipping careful part and doing simple update on non-git repository: $directory"
328     do_revctrl_simple_update "$directory"
329     return $?
330   fi
331
332 #hmmm: another piece of reusable code, to process the directory for printing.
333   # make a nice echoer since we want to use it inside conditions below.
334   local nicedir="$directory"
335   if [ $nicedir == "." ]; then
336     nicedir=$(\pwd)
337   fi
338   local blatt_report="echo -e \ncarefully retrieving '$nicedir'..."
339   $blatt_report
340
341   local this_branch="$(my_branch_name)"
342
343   show_branch_conditionally "$this_branch"
344
345   # first update all our remote branches to their current state from the repos.
346   git remote update | $TO_SPLITTER
347   promote_pipe_return 0
348   exit_on_error "git remote update"
349
350   show_branch_conditionally "$this_branch"
351
352   # this code is now doing what i have to do when i repair the repo.  and it seems to be good so far.
353   # note that we allow the local branch to be merged with its remote counterpart; otherwise we would
354   # miss changes that happened elsewhere which should be seen in our local copy.
355   local branch_list=$(all_branch_names)
356   local bran
357   for bran in $branch_list; do
358     log_feisty_meow_event "synchronizing remote branch: $bran"
359     git checkout "$bran" | $TO_SPLITTER
360     promote_pipe_return 0
361     exit_on_error "git switching checkout to remote branch: $bran"
362
363     show_branch_conditionally "$this_branch"
364
365     remote_branch_info=$(git ls-remote --heads origin $bran 2>/dev/null)
366     if [ ! -z "$remote_branch_info" ]; then
367       # we are pretty sure the remote branch does exist.
368       git pull --tags origin "$bran" | $TO_SPLITTER
369       promote_pipe_return 0
370     fi
371     exit_on_error "git pull of remote branch: $bran"
372   done
373   # now switch back to our branch.
374   git checkout "$this_branch" | $TO_SPLITTER
375   promote_pipe_return 0
376   exit_on_error "git checking out our current branch: $this_branch"
377
378   # now pull down any changes in our own origin in the repo, to stay in synch
379   # with any changes from others.
380   git fetch --tags --all | $TO_SPLITTER
381 #is the above really important when we did this branch already in the loop?
382 #it does an --all, but is that effective or different?  should we be doing that in above loop?
383   promote_pipe_return 0
384   exit_on_error "git pulling all upstream"
385
386   popd &>/dev/null
387 }
388
389 # gets the latest versions of the assets from the upstream repository.
390 function do_revctrl_simple_update()
391 {
392   directory="$1"; shift
393
394 #hmmm: another piece of reusable code, to process the directory for printing.
395   # make a nice echoer since we want to use it inside conditions below.
396   local nicedir="$directory"
397   if [ $nicedir == "." ]; then
398     nicedir=$(\pwd)
399   fi
400   local blatt_report="echo -e \nretrieving '$nicedir'..."
401
402   pushd "$directory" &>/dev/null
403   if [ -d "CVS" ]; then
404     if test_writeable "CVS"; then
405       $blatt_report
406       cvs update . | $TO_SPLITTER
407       promote_pipe_return 0
408       exit_on_error "cvs update"
409     fi
410   elif [ -d ".svn" ]; then
411     if test_writeable ".svn"; then
412       $blatt_report
413       svn update . | $TO_SPLITTER
414       promote_pipe_return 0
415       exit_on_error "svn update"
416     fi
417   elif [ -d ".git" ]; then
418     if test_writeable ".git"; then
419       $blatt_report
420       git pull --tags 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
421       promote_pipe_return 0
422       exit_on_error "git pull of origin"
423     fi
424   else
425     # this is not an error necessarily; we'll just pretend they planned this.
426     log_feisty_meow_event "no repository in $directory"
427   fi
428   popd &>/dev/null
429
430   return 0
431 }
432
433 # gets all the updates for a list of folders under revision control.
434 function checkout_list()
435 {
436   local list="$(uniquify $*)"
437
438   # turn repo list back into an array.
439   eval "repository_list=( ${REPOSITORY_LIST[*]} )"
440
441   local outer inner
442
443   for outer in "${repository_list[@]}"; do
444     # check the repository first, since it might be an absolute path.
445     if [[ $outer =~ /.* ]]; then
446       # yep, this path is absolute.  just handle it directly.
447       if [ ! -d "$outer" ]; then continue; fi
448       do_revctrl_simple_update $outer
449       exit_on_error "running update on: $path"
450     else
451       for inner in $list; do
452         # add in the directory component to see if we can find the folder.
453         local path="$inner/$outer"
454         if [ ! -d "$path" ]; then continue; fi
455         do_revctrl_simple_update $path
456         exit_on_error "running update on: $path"
457       done
458     fi
459   done
460 }
461
462 # does a careful update on all the folders in the specified list;
463 # it looks in the REPOSITORY_LIST for those names and updates them.
464 # this is just like checkout_list, but it's for the puffing up action
465 # we need to do on git.
466 function puff_out_list()
467 {
468   # make the list of directories unique.
469   local list="$(uniquify $*)"
470
471   # turn repo list back into an array.
472   eval "repository_list=( ${REPOSITORY_LIST[*]} )"
473
474   local outer inner
475
476 #hmmm: once again, seeing some reusable code in this loop...
477   for outer in "${repository_list[@]}"; do
478     # check the repository first, since it might be an absolute path.
479     if [[ $outer =~ /.* ]]; then
480       # yep, this path is absolute.  just handle it directly.
481       if [ ! -d "$outer" ]; then continue; fi
482       do_revctrl_careful_update "$outer"
483       exit_on_error "running puff-out (absolute) on path: $outer"
484     else
485       for inner in $list; do
486         # add in the directory component to see if we can find the folder.
487         local path="$inner/$outer"
488         if [ ! -d "$path" ]; then continue; fi
489         do_revctrl_careful_update "$path"
490         exit_on_error "running puff-out (relative) on path: $path"
491       done
492     fi
493   done
494 }
495
496 # provides a list of absolute paths of revision control directories
497 # that are located under the directory passed as the first parameter.
498 function generate_rev_ctrl_filelist()
499 {
500   local dir="$1"; shift
501   pushd "$dir" &>/dev/null
502   local dirhere="$( \cd "$(\dirname "$dir")" && /bin/pwd )"
503   local tempfile=$(mktemp /tmp/zz_checkins.XXXXXX)
504   echo >$tempfile
505   local additional_filter
506   find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".svn" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
507   find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".git" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
508   # CVS is not well behaved like git and (now) svn, and we seldom use it anymore.
509   popd &>/dev/null
510
511   # see if they've warned us not to try checking in within vendor hierarchies.
512   if [ ! -z "NO_CHECKIN_VENDOR" ]; then
513     sed -i -e '/.*\/vendor\/.*/d' "$tempfile"
514   fi
515
516   local sortfile=$(mktemp /tmp/zz_checkin_sort.XXXXXX)
517   sort <"$tempfile" >"$sortfile"
518   echo "$sortfile"
519   \rm "$tempfile"
520 }
521
522 # iterates across a list of directories contained in a file (first parameter).
523 # on each directory name, it performs the action (second parameter) provided.
524 function perform_revctrl_action_on_file()
525 {
526   local tempfile="$1"; shift
527   local action="$1"; shift
528
529   local did_anything=
530
531   while read -u 3 dirname; do
532     if [ -z "$dirname" ]; then
533       # we often have blank lines in the input file for some reason.
534       continue
535     fi
536     did_anything=yes
537     pushd "$dirname" &>/dev/null
538     echo "[$(pwd)]"
539     # pass the current directory plus the remaining parameters from function invocation.
540     $action . 
541     exit_on_error "performing action $action on: $(pwd)"
542     popd &>/dev/null
543   done 3<"$tempfile"
544
545   if [ -z "$did_anything" ]; then
546     echo "There was nothing to do the action '$action' on."
547   fi
548
549   rm "$tempfile"
550 }
551