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 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'
24 # the maximum depth that the recursive functions will try to go below the starting directory.
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.
32 TO_SPLITTER+=" --maxcol $(($cols - 1))"
39 #hmmm: move this to core
40 # this makes the status of pipe N into the main return value.
41 function promote_pipe_return()
43 ( exit ${PIPESTATUS[$1]} )
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
55 if [ ! -d "$TMP" ]; then
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."
65 # checks the directory provided into the revision control system repository it belongs to.
68 local directory="$1"; shift
72 # make a nice echoer since we want to use it inside conditions below.
73 local nicedir="$directory"
74 if [ $nicedir == "." ]; then
77 local blatt="echo -n checking in '$nicedir'... "
79 do_update "$directory"
80 test_or_die "repository update--this should be fixed before check-in."
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
89 test_or_die "cvs checkin"
91 elif [ -d ".svn" ]; then
92 if test_writeable ".svn"; then
95 test_or_die "svn checkin"
97 elif [ -d ".git" ]; then
98 if test_writeable ".git"; then
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"
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.
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] "
117 if [[ "${line:0:1}" != "y" ]]; then
118 echo "Stopping check-in process due to missing commit and user request."
124 # a new set of steps we have to take to make sure the branch integrity is good.
125 do_careful_git_update "$(\pwd)"
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.
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"
137 # nothing there. it's not an error though.
138 echo no repository in $directory
142 restore_terminal_title
147 # shows the local changes in a repository.
150 local directory="$1"; shift
154 pushd "$directory" &>/dev/null
156 # only update if we see a repository living there.
157 if [ -d ".svn" ]; then
159 test_or_die "subversion diff"
160 elif [ -d ".git" ]; then
162 test_or_die "git diff"
163 elif [ -d "CVS" ]; then
165 test_or_die "cvs diff"
170 restore_terminal_title
175 # reports any files that are not already known to the upstream repository.
176 function do_report_new
178 local directory="$1"; shift
182 pushd "$directory" &>/dev/null
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
193 test_or_die "git status -u"
198 restore_terminal_title
203 # checks in all the folders in the specified list.
204 function checkin_list()
206 # make the list of directories unique.
207 local list="$(uniquify $*)"
211 # turn repo list back into an array.
212 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
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
222 test_or_die "running check-in (absolute) on path: $outer"
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
230 test_or_die "running check-in (relative) on path: $path"
236 restore_terminal_title
239 # does a careful git update on all the folders in the specified list.
240 function puff_out_list()
242 # make the list of directories unique.
243 local list="$(uniquify $*)"
247 # turn repo list back into an array.
248 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
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"
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"
273 restore_terminal_title
277 ### takes out the first few carriage returns that are in the input.
278 ##function squash_first_few_crs()
281 ##while read input_text; do
283 ##if [ $i -le 5 ]; then
284 ##echo -n "$input_text "
289 ##if [ $i -le 3 ]; then
290 ### if we're still squashing eols, make sure we don't leave them hanging.
295 #hmmm: the below are git specific and should be named that way.
297 function all_branch_names()
299 echo "$(git branch -vv | cut -d ' ' -f2)"
302 # a helpful method that reports the git branch for the current directory's
304 function my_branch_name()
306 echo "$(git branch -vv | grep '\*' | cut -d ' ' -f2)"
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()
313 ##echo "$(git branch -vv | grep \* | cut -d ' ' -f2)"
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()
324 local branch="$1"; shift
326 if [ -z "$branch" ]; then
327 echo "No branch was passed to check branch state."
331 local to_return=120 # unknown issue.
333 local local_branch=$(git rev-parse @)
334 local remote_branch=$(git rev-parse "$branch")
335 local merge_base=$(git merge-base @ "$branch")
338 if [ "$local_branch" == "$remote_branch" ]; then
340 elif [ "$local_branch" == "$merge_base" ]; then
342 elif [ "$remote_branch" == "$merge_base" ]; then
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()
357 local this_branch="$1"; shift
359 local state=$(check_branch_state "$this_branch")
360 if [ "$state" != "okay" ]; then
361 echo "=> branch '$this_branch' state is not clean: $state"
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
368 function do_careful_git_update()
370 local directory="$1"; shift
371 pushd "$directory" &>/dev/null
372 test_or_die "changing to directory: $directory"
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"
380 local this_branch="$(my_branch_name)"
382 show_branch_conditionally "$this_branch"
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"
389 show_branch_conditionally "$this_branch"
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)
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"
402 show_branch_conditionally "$this_branch"
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
412 test_or_die "git pull of remote branch: $bran"
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"
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?
425 promote_pipe_return 0
426 test_or_die "git pulling all upstream"
431 # gets the latest versions of the assets from the upstream repository.
434 directory="$1"; shift
438 # make a nice echoer since we want to use it inside conditions below.
439 local nicedir="$directory"
440 if [ $nicedir == "." ]; then
443 local blatt="echo retrieving '$nicedir'..."
445 pushd "$directory" &>/dev/null
446 if [ -d "CVS" ]; then
447 if test_writeable "CVS"; then
449 cvs update . | $TO_SPLITTER
450 promote_pipe_return 0
451 test_or_die "cvs update"
453 elif [ -d ".svn" ]; then
454 if test_writeable ".svn"; then
456 svn update . | $TO_SPLITTER
457 promote_pipe_return 0
458 test_or_die "svn update"
460 elif [ -d ".git" ]; then
461 if test_writeable ".git"; then
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"
469 # this is not an error necessarily; we'll just pretend they planned this.
470 echo no repository in $directory
474 restore_terminal_title
479 # gets all the updates for a list of folders under revision control.
480 function checkout_list()
482 local list="$(uniquify $*)"
486 # turn repo list back into an array.
487 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
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
497 test_or_die "running update on: $path"
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
505 test_or_die "running update on: $path"
511 restore_terminal_title
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()
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)
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.
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"
534 local sortfile=$(mktemp /tmp/zz_checkin_sort.XXXXXX)
535 sort <"$tempfile" >"$sortfile"
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()
544 local tempfile="$1"; shift
545 local action="$1"; shift
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.
557 pushd "$dirname" &>/dev/null
559 # pass the current directory plus the remaining parameters from function invocation.
561 test_or_die "performing action $action on: $(pwd)"
566 if [ -z "$did_anything" ]; then
567 echo "There was nothing to do the action '$action' on."
570 restore_terminal_title