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 # the maximum depth that the recursive functions will try to go below the starting directory.
17 # the name of our "don't check this stuff in" file.
18 export NO_CHECKIN=".no-checkin"
20 # use our splitter tool for lengthy output if it's available.
21 if [ ! -z "$(whichable splitter)" ]; then
22 TO_SPLITTER="$(whichable splitter)"
23 # calculate the number of columsn in the terminal.
25 TO_SPLITTER+=" --maxcol $(($cols - 1))"
32 # one unpleasantry to take care of first; cygwin barfs aggressively if the TMP directory
33 # is a DOS path, but we need it to be a DOS path for our GFFS testing, so that blows.
34 # to get past this, TMP gets changed below to a hopefully generic and safe place.
35 if [[ "$TMP" =~ .:.* ]]; then
36 log_feisty_meow_event "making weirdo temporary directory for PCDOS-style path."
37 export TMP=/tmp/rev_control_$USER
39 if [ ! -d "$TMP" ]; then
42 if [ ! -d "$TMP" ]; then
43 echo "could not create the temporary directory TMP in: $TMP"
44 echo "this script will not work properly without an existing TMP directory."
46 #hmmm: odd approach to solving the "sourced scripts shouldn't exit or they take down the
47 # original caller too" issue.
48 echo "hit ctrl-c to stay in this shell now, otherwise it may exit in 10 seconds..."
55 # checks the directory provided into the revision control system repository it belongs to.
56 function do_revctrl_checkin()
58 local directory="$1"; shift
60 #hmmm: another piece of reusable code, to process the directory for printing.
61 # make a nice echoer since we want to use it inside conditions below.
62 local nicedir="$directory"
63 if [ $nicedir == "." ]; then
66 local blatt_report="echo -ne \nchecking in '$nicedir'... "
67 local tell_no_checkin="echo -ne \nskipping check-in due to presence of $NO_CHECKIN sentinel file: $nicedir"
69 pushd "$directory" &>/dev/null
70 #hmmm: overly elaborate sections below here, but we do want precise handling for git case.
72 if test_writeable "CVS"; then
73 do_revctrl_simple_update "$directory"
74 exit_on_error "updating repository; this issue should be fixed before check-in."
75 if [ -f "$NO_CHECKIN" ]; then
76 # echo -ne "\nskipping check-in due to presence of $NO_CHECKIN sentinel file: $directory"
81 exit_on_error "cvs checkin"
84 elif [ -d ".svn" ]; then
85 if test_writeable ".svn"; then
86 do_revctrl_simple_update "$directory"
87 exit_on_error "updating repository; this issue should be fixed before check-in."
88 if [ -f "$NO_CHECKIN" ]; then
89 # echo -ne "\nskipping check-in due to presence of $NO_CHECKIN sentinel file: $directory"
94 exit_on_error "svn checkin"
97 elif [ -d ".git" ]; then
98 if test_writeable ".git"; then
100 # take steps to make sure the branch integrity is good and we're up to date against remote repos.
101 do_revctrl_careful_update "$(\pwd)"
103 if [ -f "$NO_CHECKIN" ]; then
104 # echo -ne "\nskipping check-in due to presence of $NO_CHECKIN sentinel file: $directory"
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"
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.
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] "
125 if [[ "${line:0:1}" != "y" ]]; then
126 echo "Stopping check-in process due to missing commit and user request."
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.
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"
142 # nothing there. it's not an error though.
143 log_feisty_meow_event "no repository in $directory"
150 # shows the local changes in a repository.
151 function do_revctrl_diff
153 local directory="$1"; shift
155 pushd "$directory" &>/dev/null
157 # only update if we see a repository living there.
158 if [ -d ".svn" ]; then
160 exit_on_error "subversion diff"
161 elif [ -d ".git" ]; then
163 exit_on_error "git diff"
164 elif [ -d "CVS" ]; then
166 exit_on_error "cvs diff"
174 # reports any files that are not already known to the upstream repository.
175 function do_revctrl_report_new
177 local directory="$1"; shift
179 pushd "$directory" &>/dev/null
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
190 exit_on_error "git status -u"
198 # checks in all the folders in the specified list.
199 function checkin_list()
201 # make the list of directories unique.
202 local list="$(uniquify $*)"
204 # turn repo list back into an array.
205 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
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"
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"
228 #hmmm: below functions are git specific and should be named that way.
230 function all_branch_names()
232 echo "$(git branch -vv | cut -d ' ' -f2)"
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()
239 ##echo "$(git branch -vv | grep \* | cut -d ' ' -f2)"
242 # a helpful method that reports the git branch for the current directory's
244 function my_branch_name()
246 echo "$(git branch -vv | grep '\*' | cut -d ' ' -f2)"
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()
257 local branch="$1"; shift
259 if [ -z "$branch" ]; then
260 echo "No branch was passed to check branch state."
264 local to_return=120 # unknown issue.
266 local local_branch=$(git rev-parse HEAD)
267 local remote_branch=$(git rev-parse "$branch")
268 local merge_base=$(git merge-base HEAD "$branch")
271 if [ "$local_branch" == "$remote_branch" ]; then
273 elif [ "$local_branch" == "$merge_base" ]; then
275 elif [ "$remote_branch" == "$merge_base" ]; then
286 # showes the branch currently active in the repository.
287 function show_active_branch()
289 #hmmm: if no args, assume current dir!
291 for directory in "$@"; do
292 echo -n "active branch for '$directory': "
293 pushd "$directory" &>/dev/null
296 git rev-parse --abbrev-ref HEAD
297 #hmmm: else OTHERS!!!
303 # only shows the branch state if it's not okay.
304 # note that this is not the same as a conditional branch (ha ha).
305 function show_branch_conditionally()
307 local this_branch="$1"; shift
309 local state=$(check_branch_state "$this_branch")
310 if [ "$state" != "okay" ]; then
311 echo "=> branch '$this_branch' state is not clean: $state"
315 # the git update process just gets more and more complex when you bring in
316 # branches, so we've moved this here to avoid having a ton of code in the
318 function do_revctrl_careful_update()
320 local directory="$1"; shift
321 pushd "$directory" &>/dev/null
322 exit_on_error "changing to directory: $directory"
324 if [ ! -d ".git" ]; then
325 # not a git project, so just boil this down to a getem action.
327 log_feisty_meow_event "skipping careful part and doing simple update on non-git repository: $directory"
328 do_revctrl_simple_update "$directory"
332 #hmmm: another piece of reusable code, to process the directory for printing.
333 # make a nice echoer since we want to use it inside conditions below.
334 local nicedir="$directory"
335 if [ $nicedir == "." ]; then
338 local blatt_report="echo -e \ncarefully retrieving '$nicedir'..."
341 local this_branch="$(my_branch_name)"
343 show_branch_conditionally "$this_branch"
345 # first update all our remote branches to their current state from the repos.
346 git remote update | $TO_SPLITTER
347 promote_pipe_return 0
348 exit_on_error "git remote update"
350 show_branch_conditionally "$this_branch"
352 # this code is now doing what i have to do when i repair the repo. and it seems to be good so far.
353 # note that we allow the local branch to be merged with its remote counterpart; otherwise we would
354 # miss changes that happened elsewhere which should be seen in our local copy.
355 local branch_list=$(all_branch_names)
357 for bran in $branch_list; do
358 log_feisty_meow_event "synchronizing remote branch: $bran"
359 git checkout "$bran" | $TO_SPLITTER
360 promote_pipe_return 0
361 exit_on_error "git switching checkout to remote branch: $bran"
363 show_branch_conditionally "$this_branch"
365 remote_branch_info=$(git ls-remote --heads origin $bran 2>/dev/null)
366 if [ ! -z "$remote_branch_info" ]; then
367 # we are pretty sure the remote branch does exist.
368 git pull --tags origin "$bran" | $TO_SPLITTER
369 promote_pipe_return 0
371 exit_on_error "git pull of remote branch: $bran"
373 # now switch back to our branch.
374 git checkout "$this_branch" | $TO_SPLITTER
375 promote_pipe_return 0
376 exit_on_error "git checking out our current branch: $this_branch"
378 # now pull down any changes in our own origin in the repo, to stay in synch
379 # with any changes from others.
380 git fetch --tags --all | $TO_SPLITTER
381 #is the above really important when we did this branch already in the loop?
382 #it does an --all, but is that effective or different? should we be doing that in above loop?
383 promote_pipe_return 0
384 exit_on_error "git pulling all upstream"
389 # gets the latest versions of the assets from the upstream repository.
390 function do_revctrl_simple_update()
392 directory="$1"; shift
394 #hmmm: another piece of reusable code, to process the directory for printing.
395 # make a nice echoer since we want to use it inside conditions below.
396 local nicedir="$directory"
397 if [ $nicedir == "." ]; then
400 local blatt_report="echo -e \nretrieving '$nicedir'..."
402 pushd "$directory" &>/dev/null
403 if [ -d "CVS" ]; then
404 if test_writeable "CVS"; then
406 cvs update . | $TO_SPLITTER
407 promote_pipe_return 0
408 exit_on_error "cvs update"
410 elif [ -d ".svn" ]; then
411 if test_writeable ".svn"; then
413 svn update . | $TO_SPLITTER
414 promote_pipe_return 0
415 exit_on_error "svn update"
417 elif [ -d ".git" ]; then
418 if test_writeable ".git"; then
420 git pull --tags 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
421 promote_pipe_return 0
422 exit_on_error "git pull of origin"
425 # this is not an error necessarily; we'll just pretend they planned this.
426 log_feisty_meow_event "no repository in $directory"
433 # gets all the updates for a list of folders under revision control.
434 function checkout_list()
436 local list="$(uniquify $*)"
438 # turn repo list back into an array.
439 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
443 for outer in "${repository_list[@]}"; do
444 # check the repository first, since it might be an absolute path.
445 if [[ $outer =~ /.* ]]; then
446 # yep, this path is absolute. just handle it directly.
447 if [ ! -d "$outer" ]; then continue; fi
448 do_revctrl_simple_update $outer
449 exit_on_error "running update on: $path"
451 for inner in $list; do
452 # add in the directory component to see if we can find the folder.
453 local path="$inner/$outer"
454 if [ ! -d "$path" ]; then continue; fi
455 do_revctrl_simple_update $path
456 exit_on_error "running update on: $path"
462 # does a careful update on all the folders in the specified list;
463 # it looks in the REPOSITORY_LIST for those names and updates them.
464 # this is just like checkout_list, but it's for the puffing up action
465 # we need to do on git.
466 function puff_out_list()
468 # make the list of directories unique.
469 local list="$(uniquify $*)"
471 # turn repo list back into an array.
472 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
476 #hmmm: once again, seeing some reusable code in this loop...
477 for outer in "${repository_list[@]}"; do
478 # check the repository first, since it might be an absolute path.
479 if [[ $outer =~ /.* ]]; then
480 # yep, this path is absolute. just handle it directly.
481 if [ ! -d "$outer" ]; then continue; fi
482 do_revctrl_careful_update "$outer"
483 exit_on_error "running puff-out (absolute) on path: $outer"
485 for inner in $list; do
486 # add in the directory component to see if we can find the folder.
487 local path="$inner/$outer"
488 if [ ! -d "$path" ]; then continue; fi
489 do_revctrl_careful_update "$path"
490 exit_on_error "running puff-out (relative) on path: $path"
496 # provides a list of absolute paths of revision control directories
497 # that are located under the directory passed as the first parameter.
498 function generate_rev_ctrl_filelist()
500 local dir="$1"; shift
501 pushd "$dir" &>/dev/null
502 local dirhere="$( \cd "$(\dirname "$dir")" && /bin/pwd )"
503 local tempfile=$(mktemp /tmp/zz_checkins.XXXXXX)
505 local additional_filter
506 find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".svn" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
507 find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".git" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
508 # CVS is not well behaved like git and (now) svn, and we seldom use it anymore.
511 # see if they've warned us not to try checking in within vendor hierarchies.
512 if [ ! -z "NO_CHECKIN_VENDOR" ]; then
513 sed -i -e '/.*\/vendor\/.*/d' "$tempfile"
516 local sortfile=$(mktemp /tmp/zz_checkin_sort.XXXXXX)
517 sort <"$tempfile" >"$sortfile"
522 # iterates across a list of directories contained in a file (first parameter).
523 # on each directory name, it performs the action (second parameter) provided.
524 function perform_revctrl_action_on_file()
526 local tempfile="$1"; shift
527 local action="$1"; shift
531 while read -u 3 dirname; do
532 if [ -z "$dirname" ]; then
533 # we often have blank lines in the input file for some reason.
537 pushd "$dirname" &>/dev/null
539 # pass the current directory plus the remaining parameters from function invocation.
541 exit_on_error "performing action $action on: $(pwd)"
545 if [ -z "$did_anything" ]; then
546 echo "There was nothing to do the action '$action' on."