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