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