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