3 # these are helper functions for doing localized revision control.
4 # this script should be sourced into other scripts that use it.
6 # Author: Chris Koeritz
7 # Author: Kevin Wentworth
9 source "$FEISTY_MEOW_SCRIPTS/core/launch_feisty_meow.sh"
10 source "$FEISTY_MEOW_SCRIPTS/tty/terminal_titler.sh"
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
31 # the maximum depth that the recursive functions will try to go below the starting directory.
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.
39 TO_SPLITTER+=" --maxcol $(($cols - 1))"
46 #hmmm: move this to core
47 # this makes the status of pipe N into the main return value.
48 function promote_pipe_return()
50 ( exit ${PIPESTATUS[$1]} )
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
62 if [ ! -d "$TMP" ]; then
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."
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..."
78 # checks the directory provided into the revision control system repository it belongs to.
81 local directory="$1"; shift
85 # make a nice echoer since we want to use it inside conditions below.
86 local nicedir="$directory"
87 if [ $nicedir == "." ]; then
90 local blatt="echo -ne \nchecking in '$nicedir'... "
92 #hmmm: we don't do a puff out on this directory to carefully update the git repo. is there a good reason why?
93 # we know we at least have to get the remote version synched up to avoid severe tire damage, so it's sensible at
94 # least to call do_update, but is there a reason not to call the careful git update instead?
95 #AHA, below we do call a careful git update, which is at least partially redundant with calling do_update here.
96 # how about pushing the do_update down into the two cases that use it and just not calling it for the git case?
97 do_update "$directory"
98 exit_on_error "updating repository; this issue should be fixed before check-in."
100 pushd "$directory" &>/dev/null
101 if [ -f ".no-checkin" ]; then
102 echo "skipping check-in due to presence of .no-checkin sentinel file."
103 elif [ -d "CVS" ]; then
104 if test_writeable "CVS"; then
107 exit_on_error "cvs checkin"
109 elif [ -d ".svn" ]; then
110 if test_writeable ".svn"; then
113 exit_on_error "svn checkin"
115 elif [ -d ".git" ]; then
116 if test_writeable ".git"; then
119 # put all changed and new files in the commit. not to everyone's liking.
120 git add --all . | $TO_SPLITTER
121 promote_pipe_return 0
122 exit_on_error "git add all new files"
124 #hmmm: was there a reason to commit before doing the puffing out careful git update? i seem to think there was.
125 # would be nice to remember that or recapture the reason, so we can be sure we're not committing too early.
126 # see if there are any changes in the local repository.
127 if ! git diff-index --quiet HEAD --; then
128 # tell git about all the files and get a check-in comment.
129 #hmmm: begins to look like, you guessed it, a reusable bit that all commit actions could enjoy.
132 continue_on_error "git commit"
133 if [ $retval -ne 0 ]; then
134 echo -e -n "Commit failed or was aborted:\nShould we continue with other check-ins? [y/N] "
137 if [[ "${line:0:1}" != "y" ]]; then
138 echo "Stopping check-in process due to missing commit and user request."
144 # a new set of steps we have to take to make sure the branch integrity is good.
145 do_careful_git_update "$(\pwd)"
147 # we continue on to the push, even if there were no changes this time, because
148 # there could already be committed changes that haven't been pushed yet.
150 # upload any changes to the upstream repo so others can see them.
151 git push --tags origin "$(my_branch_name)" 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
152 promote_pipe_return 0
153 exit_on_error "git push"
157 # nothing there. it's not an error though.
158 log_feisty_meow_event "no repository in $directory"
162 restore_terminal_title
167 # shows the local changes in a repository.
170 local directory="$1"; shift
174 pushd "$directory" &>/dev/null
176 # only update if we see a repository living there.
177 if [ -d ".svn" ]; then
179 exit_on_error "subversion diff"
180 elif [ -d ".git" ]; then
182 exit_on_error "git diff"
183 elif [ -d "CVS" ]; then
185 exit_on_error "cvs diff"
190 restore_terminal_title
195 # reports any files that are not already known to the upstream repository.
196 function do_report_new
198 local directory="$1"; shift
202 pushd "$directory" &>/dev/null
204 # only update if we see a repository living there.
205 if [ -f ".no-checkin" ]; then
206 echo "skipping reporting due to presence of .no-checkin sentinel file."
207 elif [ -d ".svn" ]; then
208 # this action so far only makes sense and is needed for svn.
209 bash $FEISTY_MEOW_SCRIPTS/rev_control/svnapply.sh \? echo
210 exit_on_error "svn diff"
211 elif [ -d ".git" ]; then
213 exit_on_error "git status -u"
218 restore_terminal_title
223 # checks in all the folders in the specified list.
224 function checkin_list()
226 # make the list of directories unique.
227 local list="$(uniquify $*)"
231 # turn repo list back into an array.
232 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
236 for outer in "${repository_list[@]}"; do
237 # check the repository first, since it might be an absolute path.
238 if [[ $outer =~ /.* ]]; then
239 # yep, this path is absolute. just handle it directly.
240 if [ ! -d "$outer" ]; then continue; fi
242 exit_on_error "running check-in (absolute) on path: $outer"
245 for inner in $list; do
246 # add in the directory component to see if we can find the folder.
247 local path="$inner/$outer"
248 if [ ! -d "$path" ]; then continue; fi
250 exit_on_error "running check-in (relative) on path: $path"
256 restore_terminal_title
259 #hmmm: below functions are git specific and should be named that way.
261 function all_branch_names()
263 echo "$(git branch -vv | cut -d ' ' -f2)"
266 # a helpful method that reports the git branch for the current directory's
268 function my_branch_name()
270 echo "$(git branch -vv | grep '\*' | cut -d ' ' -f2)"
273 #this had a -> in it at one point for not matching, didn't it?
274 # this reports the upstream branch for the current repo.
275 ##function parent_branch_name()
277 ##echo "$(git branch -vv | grep \* | cut -d ' ' -f2)"
280 # reports the status of the branch by echoing one of these values:
281 # okay: up to date and everything is good.
282 # needs_pull: this branch needs to be pulled from origins.
283 # needs_push: there are unsaved changes on this branch to push to remote store.
284 # diverged: the branches diverged and are going to need a merge.
285 # reference: https://stackoverflow.com/questions/3258243/check-if-pull-needed-in-git
286 function check_branch_state()
288 local branch="$1"; shift
290 if [ -z "$branch" ]; then
291 echo "No branch was passed to check branch state."
295 local to_return=120 # unknown issue.
297 local local_branch=$(git rev-parse @)
298 local remote_branch=$(git rev-parse "$branch")
299 local merge_base=$(git merge-base @ "$branch")
302 if [ "$local_branch" == "$remote_branch" ]; then
304 elif [ "$local_branch" == "$merge_base" ]; then
306 elif [ "$remote_branch" == "$merge_base" ]; then
317 # only shows the branch state if it's not okay.
318 # note that this is not the same as a conditional branch (ha ha).
319 function show_branch_conditionally()
321 local this_branch="$1"; shift
323 local state=$(check_branch_state "$this_branch")
324 if [ "$state" != "okay" ]; then
325 echo "=> branch '$this_branch' state is not clean: $state"
329 # the git update process just gets more and more complex when you bring in
330 # branches, so we've moved this here to avoid having a ton of code in the
332 function do_careful_git_update()
334 local directory="$1"; shift
335 pushd "$directory" &>/dev/null
336 exit_on_error "changing to directory: $directory"
338 if [ ! -d ".git" ]; then
339 # not a git project, so just boil this down to a getem action.
341 echo "special case; not doing careful git update on non git repository: $directory"
342 do_update "$directory"
346 #hmmm: another piece of reusable code, to process the directory for printing.
347 # make a nice echoer since we want to use it inside conditions below.
348 local nicedir="$directory"
349 if [ $nicedir == "." ]; then
352 local blatt="echo -e \nretrieving '$nicedir'..."
355 local this_branch="$(my_branch_name)"
357 show_branch_conditionally "$this_branch"
359 # first update all our remote branches to their current state from the repos.
360 git remote update | $TO_SPLITTER
361 promote_pipe_return 0
362 exit_on_error "git remote update"
364 show_branch_conditionally "$this_branch"
366 # this code is now doing what i have to do when i repair the repo. and it seems to be good so far.
367 # note that we allow the local branch to be merged with its remote counterpart; otherwise we would
368 # miss changes that happened elsewhere which should be seen in our local copy.
369 local branch_list=$(all_branch_names)
371 for bran in $branch_list; do
372 # echo "synchronizing remote branch: $bran"
373 git checkout "$bran" | $TO_SPLITTER
374 promote_pipe_return 0
375 exit_on_error "git switching checkout to remote branch: $bran"
377 show_branch_conditionally "$this_branch"
379 remote_branch_info=$(git ls-remote --heads origin $bran 2>/dev/null)
380 if [ ! -z "$remote_branch_info" ]; then
381 # we are pretty sure the remote branch does exist.
382 git pull --tags $PULL_ADDITION origin "$bran" | $TO_SPLITTER
383 # we may want to choose to do fast forward, to avoid crazy multiple merge histories
384 # without any changes in them. --no-ff
385 promote_pipe_return 0
387 exit_on_error "git pull of remote branch: $bran"
389 # now switch back to our branch.
390 git checkout "$this_branch" | $TO_SPLITTER
391 promote_pipe_return 0
392 exit_on_error "git checking out our current branch: $this_branch"
394 # now pull down any changes in our own origin in the repo, to stay in synch
395 # with any changes from others.
396 git pull --tags $PULL_ADDITION --all | $TO_SPLITTER
397 #is the above really important when we did this branch already in the loop?
398 #it does an --all, but is that effective or different? should we be doing that in above loop?
400 promote_pipe_return 0
401 exit_on_error "git pulling all upstream"
406 # gets the latest versions of the assets from the upstream repository.
409 directory="$1"; shift
413 # make a nice echoer since we want to use it inside conditions below.
414 local nicedir="$directory"
415 if [ $nicedir == "." ]; then
418 local blatt="echo -e \nretrieving '$nicedir'..."
420 pushd "$directory" &>/dev/null
421 if [ -d "CVS" ]; then
422 if test_writeable "CVS"; then
424 cvs update . | $TO_SPLITTER
425 promote_pipe_return 0
426 exit_on_error "cvs update"
428 elif [ -d ".svn" ]; then
429 if test_writeable ".svn"; then
431 svn update . | $TO_SPLITTER
432 promote_pipe_return 0
433 exit_on_error "svn update"
435 elif [ -d ".git" ]; then
436 if test_writeable ".git"; then
438 git pull --tags $PULL_ADDITION 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
439 #ordinary pulls should be allowed to do fast forward: --no-ff
440 promote_pipe_return 0
441 exit_on_error "git pull of origin"
444 # this is not an error necessarily; we'll just pretend they planned this.
445 log_feisty_meow_event "no repository in $directory"
449 restore_terminal_title
454 # gets all the updates for a list of folders under revision control.
455 function checkout_list()
457 local list="$(uniquify $*)"
461 # turn repo list back into an array.
462 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
466 for outer in "${repository_list[@]}"; do
467 # check the repository first, since it might be an absolute path.
468 if [[ $outer =~ /.* ]]; then
469 # yep, this path is absolute. just handle it directly.
470 if [ ! -d "$outer" ]; then continue; fi
472 exit_on_error "running update on: $path"
475 for inner in $list; do
476 # add in the directory component to see if we can find the folder.
477 local path="$inner/$outer"
478 if [ ! -d "$path" ]; then continue; fi
480 exit_on_error "running update on: $path"
486 restore_terminal_title
489 # does a careful update on all the folders in the specified list;
490 # it looks in the REPOSITORY_LIST for those names and updates them.
491 # this is just like checkout_list, but it's for the puffing up action
492 # we need to do on git.
493 function puff_out_list()
495 # make the list of directories unique.
496 local list="$(uniquify $*)"
500 # turn repo list back into an array.
501 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
505 #hmmm: once again, seeing some reusable code in this loop...
506 for outer in "${repository_list[@]}"; do
507 # check the repository first, since it might be an absolute path.
508 if [[ $outer =~ /.* ]]; then
509 # yep, this path is absolute. just handle it directly.
510 if [ ! -d "$outer" ]; then continue; fi
511 do_careful_git_update "$outer"
512 exit_on_error "running puff-out (absolute) on path: $outer"
515 for inner in $list; do
516 # add in the directory component to see if we can find the folder.
517 local path="$inner/$outer"
518 if [ ! -d "$path" ]; then continue; fi
519 do_careful_git_update "$path"
520 exit_on_error "running puff-out (relative) on path: $path"
526 restore_terminal_title
529 # provides a list of absolute paths of revision control directories
530 # that are located under the directory passed as the first parameter.
531 function generate_rev_ctrl_filelist()
533 local dir="$1"; shift
534 pushd "$dir" &>/dev/null
535 local dirhere="$( \cd "$(\dirname "$dir")" && /bin/pwd )"
536 local tempfile=$(mktemp /tmp/zz_checkins.XXXXXX)
538 local additional_filter
539 find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".svn" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
540 find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".git" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
541 # CVS is not well behaved like git and (now) svn, and we seldom use it anymore.
544 # see if they've warned us not to try checking in within vendor hierarchies.
545 if [ ! -z "NO_CHECKIN_VENDOR" ]; then
546 sed -i -e '/.*\/vendor\/.*/d' "$tempfile"
549 local sortfile=$(mktemp /tmp/zz_checkin_sort.XXXXXX)
550 sort <"$tempfile" >"$sortfile"
555 # iterates across a list of directories contained in a file (first parameter).
556 # on each directory name, it performs the action (second parameter) provided.
557 function perform_revctrl_action_on_file()
559 local tempfile="$1"; shift
560 local action="$1"; shift
566 while read -u 3 dirname; do
567 if [ -z "$dirname" ]; then
568 # we often have blank lines in the input file for some reason.
572 pushd "$dirname" &>/dev/null
574 # pass the current directory plus the remaining parameters from function invocation.
576 exit_on_error "performing action $action on: $(pwd)"
581 if [ -z "$did_anything" ]; then
582 echo "There was nothing to do the action '$action' on."
585 restore_terminal_title