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