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