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$//' )"
20 echo gitvertest is $gitvertest
21 if (( $gitvertest >= 11 )); then
22 # auto-stash is not available until 2.6 for git, but ubuntu is misreporting or using a
23 # differing version number somehow. we are sure autostash was missing on ubuntu xenial
24 # with git 2.7.4 and it's definitely present in zesty with git at 2.11.
25 PULL_ADDITION='--rebase --autostash'
30 # the maximum depth that the recursive functions will try to go below the starting directory.
33 # use our splitter tool for lengthy output if it's available.
34 if [ ! -z "$(which splitter 2>/dev/null)" ]; then
35 TO_SPLITTER="$(which splitter)"
36 # calculate the number of columsn in the terminal.
38 TO_SPLITTER+=" --maxcol $(($cols - 1))"
45 #hmmm: move this to core
46 # this makes the status of pipe N into the main return value.
47 function promote_pipe_return()
49 ( exit ${PIPESTATUS[$1]} )
54 # one unpleasantry to take care of first; cygwin barfs aggressively if the TMP directory
55 # is a DOS path, but we need it to be a DOS path for our GFFS testing, so that blows.
56 # to get past this, TMP gets changed below to a hopefully generic and safe place.
57 if [[ "$TMP" =~ .:.* ]]; then
58 echo "making weirdo temporary directory for PCDOS-style path."
59 export TMP=/tmp/rev_control_$USER
61 if [ ! -d "$TMP" ]; then
64 if [ ! -d "$TMP" ]; then
65 echo "could not create the temporary directory TMP in: $TMP"
66 echo "this script will not work properly without an existing TMP directory."
71 # checks the directory provided into the revision control system repository it belongs to.
74 local directory="$1"; shift
78 # make a nice echoer since we want to use it inside conditions below.
79 local nicedir="$directory"
80 if [ $nicedir == "." ]; then
83 local blatt="echo -n checking in '$nicedir'... "
85 do_update "$directory"
86 test_or_die "repository update--this should be fixed before check-in."
88 pushd "$directory" &>/dev/null
89 if [ -f ".no-checkin" ]; then
90 echo "skipping check-in due to presence of .no-checkin sentinel file."
91 elif [ -d "CVS" ]; then
92 if test_writeable "CVS"; then
95 test_or_die "cvs checkin"
97 elif [ -d ".svn" ]; then
98 if test_writeable ".svn"; then
101 test_or_die "svn checkin"
103 elif [ -d ".git" ]; then
104 if test_writeable ".git"; then
107 # put all changed and new files in the commit. not to everyone's liking.
108 git add --all . | $TO_SPLITTER
109 promote_pipe_return 0
110 test_or_die "git add all new files"
112 # see if there are any changes in the local repository.
113 if ! git diff-index --quiet HEAD --; then
114 # tell git about all the files and get a check-in comment.
115 #hmmm: begins to look like, you guessed it, a reusable bit that all commit actions could enjoy.
118 test_or_continue "git commit"
119 if [ $retval -ne 0 ]; then
120 echo -e -n "Commit failed or was aborted:\nShould we continue with other check-ins? [y/N] "
123 if [[ "${line:0:1}" != "y" ]]; then
124 echo "Stopping check-in process due to missing commit and user request."
130 # a new set of steps we have to take to make sure the branch integrity is good.
131 do_careful_git_update "$(\pwd)"
133 # we continue on to the push, even if there were no changes this time, because
134 # there could already be committed changes that haven't been pushed yet.
136 # upload any changes to the upstream repo so others can see them.
137 git push --tags origin "$(my_branch_name)" 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
138 promote_pipe_return 0
139 test_or_die "git push"
143 # nothing there. it's not an error though.
144 echo no repository in $directory
148 restore_terminal_title
153 # shows the local changes in a repository.
156 local directory="$1"; shift
160 pushd "$directory" &>/dev/null
162 # only update if we see a repository living there.
163 if [ -d ".svn" ]; then
165 test_or_die "subversion diff"
166 elif [ -d ".git" ]; then
168 test_or_die "git diff"
169 elif [ -d "CVS" ]; then
171 test_or_die "cvs diff"
176 restore_terminal_title
181 # reports any files that are not already known to the upstream repository.
182 function do_report_new
184 local directory="$1"; shift
188 pushd "$directory" &>/dev/null
190 # only update if we see a repository living there.
191 if [ -f ".no-checkin" ]; then
192 echo "skipping reporting due to presence of .no-checkin sentinel file."
193 elif [ -d ".svn" ]; then
194 # this action so far only makes sense and is needed for svn.
195 bash $FEISTY_MEOW_SCRIPTS/rev_control/svnapply.sh \? echo
196 test_or_die "svn diff"
197 elif [ -d ".git" ]; then
199 test_or_die "git status -u"
204 restore_terminal_title
209 # checks in all the folders in the specified list.
210 function checkin_list()
212 # make the list of directories unique.
213 local list="$(uniquify $*)"
217 # turn repo list back into an array.
218 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
222 for outer in "${repository_list[@]}"; do
223 # check the repository first, since it might be an absolute path.
224 if [[ $outer =~ /.* ]]; then
225 # yep, this path is absolute. just handle it directly.
226 if [ ! -d "$outer" ]; then continue; fi
228 test_or_die "running check-in (absolute) on path: $outer"
231 for inner in $list; do
232 # add in the directory component to see if we can find the folder.
233 local path="$inner/$outer"
234 if [ ! -d "$path" ]; then continue; fi
236 test_or_die "running check-in (relative) on path: $path"
242 restore_terminal_title
245 # does a careful git update on all the folders in the specified list.
246 function puff_out_list()
248 # make the list of directories unique.
249 local list="$(uniquify $*)"
253 # turn repo list back into an array.
254 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
258 #hmmm: once again, seeing some reusable code in this loop...
259 for outer in "${repository_list[@]}"; do
260 # check the repository first, since it might be an absolute path.
261 if [[ $outer =~ /.* ]]; then
262 # yep, this path is absolute. just handle it directly.
263 if [ ! -d "$outer" ]; then continue; fi
264 do_careful_git_update "$outer"
265 test_or_die "running puff-out (absolute) on path: $outer"
268 for inner in $list; do
269 # add in the directory component to see if we can find the folder.
270 local path="$inner/$outer"
271 if [ ! -d "$path" ]; then continue; fi
272 do_careful_git_update "$path"
273 test_or_die "running puff-out (relative) on path: $path"
279 restore_terminal_title
283 ### takes out the first few carriage returns that are in the input.
284 ##function squash_first_few_crs()
287 ##while read input_text; do
289 ##if [ $i -le 5 ]; then
290 ##echo -n "$input_text "
295 ##if [ $i -le 3 ]; then
296 ### if we're still squashing eols, make sure we don't leave them hanging.
301 #hmmm: the below are git specific and should be named that way.
303 function all_branch_names()
305 echo "$(git branch -vv | cut -d ' ' -f2)"
308 # a helpful method that reports the git branch for the current directory's
310 function my_branch_name()
312 echo "$(git branch -vv | grep '\*' | cut -d ' ' -f2)"
315 #this had a -> in it at one point for not matching, didn't it?
316 # this reports the upstream branch for the current repo.
317 ##function parent_branch_name()
319 ##echo "$(git branch -vv | grep \* | cut -d ' ' -f2)"
322 # reports the status of the branch by echoing one of these values:
323 # okay: up to date and everything is good.
324 # needs_pull: this branch needs to be pulled from origins.
325 # needs_push: there are unsaved changes on this branch to push to remote store.
326 # diverged: the branches diverged and are going to need a merge.
327 # reference: https://stackoverflow.com/questions/3258243/check-if-pull-needed-in-git
328 function check_branch_state()
330 local branch="$1"; shift
332 if [ -z "$branch" ]; then
333 echo "No branch was passed to check branch state."
337 local to_return=120 # unknown issue.
339 local local_branch=$(git rev-parse @)
340 local remote_branch=$(git rev-parse "$branch")
341 local merge_base=$(git merge-base @ "$branch")
344 if [ "$local_branch" == "$remote_branch" ]; then
346 elif [ "$local_branch" == "$merge_base" ]; then
348 elif [ "$remote_branch" == "$merge_base" ]; then
359 # only shows the branch state if it's not okay.
360 # note that this is not the same as a conditional branch (ha ha).
361 function show_branch_conditionally()
363 local this_branch="$1"; shift
365 local state=$(check_branch_state "$this_branch")
366 if [ "$state" != "okay" ]; then
367 echo "=> branch '$this_branch' state is not clean: $state"
371 # the git update process just gets more and more complex when you bring in
372 # branches, so we've moved this here to avoid having a ton of code in the
374 function do_careful_git_update()
376 local directory="$1"; shift
377 pushd "$directory" &>/dev/null
378 test_or_die "changing to directory: $directory"
380 if [ ! -d ".git" ]; then
381 # we ignore if they're jumping into a non-useful folder, but also tell them.
382 echo "Directory is not a git repository: $directory"
386 local this_branch="$(my_branch_name)"
388 show_branch_conditionally "$this_branch"
390 # first update all our remote branches to their current state from the repos.
391 git remote update | $TO_SPLITTER
392 promote_pipe_return 0
393 test_or_die "git remote update"
395 show_branch_conditionally "$this_branch"
397 # this code is now doing what i have to do when i repair the repo. and it seems to be good so far.
398 # note that we allow the local branch to be merged with its remote counterpart; otherwise we would
399 # miss changes that happened elsewhere which should be seen in our local copy.
400 local branch_list=$(all_branch_names)
402 for bran in $branch_list; do
403 # echo "synchronizing remote branch: $bran"
404 git checkout "$bran" | $TO_SPLITTER
405 promote_pipe_return 0
406 test_or_die "git switching checkout to remote branch: $bran"
408 show_branch_conditionally "$this_branch"
410 remote_branch_info=$(git ls-remote --heads origin $bran 2>/dev/null)
411 if [ ! -z "$remote_branch_info" ]; then
412 # we are pretty sure the remote branch does exist.
413 git pull $PULL_ADDITION origin "$bran" | $TO_SPLITTER
414 # we may want to choose to do fast forward, to avoid crazy multiple merge histories
415 # without any changes in them. --no-ff
416 promote_pipe_return 0
418 test_or_die "git pull of remote branch: $bran"
420 # now switch back to our branch.
421 git checkout "$this_branch" | $TO_SPLITTER
422 promote_pipe_return 0
423 test_or_die "git checking out our current branch: $this_branch"
425 # now pull down any changes in our own origin in the repo, to stay in synch
426 # with any changes from others.
427 git pull $PULL_ADDITION --all | $TO_SPLITTER
428 #is the above really important when we did this branch already in the loop?
429 #it does an --all, but is that effective or different? should we be doing that in above loop?
431 promote_pipe_return 0
432 test_or_die "git pulling all upstream"
437 # gets the latest versions of the assets from the upstream repository.
440 directory="$1"; shift
444 # make a nice echoer since we want to use it inside conditions below.
445 local nicedir="$directory"
446 if [ $nicedir == "." ]; then
449 local blatt="echo retrieving '$nicedir'..."
451 pushd "$directory" &>/dev/null
452 if [ -d "CVS" ]; then
453 if test_writeable "CVS"; then
455 cvs update . | $TO_SPLITTER
456 promote_pipe_return 0
457 test_or_die "cvs update"
459 elif [ -d ".svn" ]; then
460 if test_writeable ".svn"; then
462 svn update . | $TO_SPLITTER
463 promote_pipe_return 0
464 test_or_die "svn update"
466 elif [ -d ".git" ]; then
467 if test_writeable ".git"; then
469 git pull $PULL_ADDITION 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
470 #ordinary pulls should be allowed to do fast forward: --no-ff
471 promote_pipe_return 0
472 test_or_die "git pull of origin"
475 # this is not an error necessarily; we'll just pretend they planned this.
476 echo no repository in $directory
480 restore_terminal_title
485 # gets all the updates for a list of folders under revision control.
486 function checkout_list()
488 local list="$(uniquify $*)"
492 # turn repo list back into an array.
493 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
497 for outer in "${repository_list[@]}"; do
498 # check the repository first, since it might be an absolute path.
499 if [[ $outer =~ /.* ]]; then
500 # yep, this path is absolute. just handle it directly.
501 if [ ! -d "$outer" ]; then continue; fi
503 test_or_die "running update on: $path"
506 for inner in $list; do
507 # add in the directory component to see if we can find the folder.
508 local path="$inner/$outer"
509 if [ ! -d "$path" ]; then continue; fi
511 test_or_die "running update on: $path"
517 restore_terminal_title
520 # provides a list of absolute paths of revision control directories
521 # that are located under the directory passed as the first parameter.
522 function generate_rev_ctrl_filelist()
524 local dir="$1"; shift
525 pushd "$dir" &>/dev/null
526 local dirhere="$( \cd "$(\dirname "$dir")" && /bin/pwd )"
527 local tempfile=$(mktemp /tmp/zz_checkins.XXXXXX)
529 local additional_filter
530 find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".svn" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
531 find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".git" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
532 # CVS is not well behaved like git and (now) svn, and we seldom use it anymore.
535 # see if they've warned us not to try checking in within vendor hierarchies.
536 if [ ! -z "NO_CHECKIN_VENDOR" ]; then
537 sed -i -e '/.*\/vendor\/.*/d' "$tempfile"
540 local sortfile=$(mktemp /tmp/zz_checkin_sort.XXXXXX)
541 sort <"$tempfile" >"$sortfile"
546 # iterates across a list of directories contained in a file (first parameter).
547 # on each directory name, it performs the action (second parameter) provided.
548 function perform_revctrl_action_on_file()
550 local tempfile="$1"; shift
551 local action="$1"; shift
557 while read -u 3 dirname; do
558 if [ -z "$dirname" ]; then
559 # we often have blank lines in the input file for some reason.
563 pushd "$dirname" &>/dev/null
565 # pass the current directory plus the remaining parameters from function invocation.
567 test_or_die "performing action $action on: $(pwd)"
572 if [ -z "$did_anything" ]; then
573 echo "There was nothing to do the action '$action' on."
576 restore_terminal_title