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