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