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