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