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 # use our splitter tool for lengthy output if it's available.
18 if [ ! -z "$(which splitter 2>/dev/null)" ]; then
19 TO_SPLITTER="$(which splitter)"
20 # calculate the number of columsn in the terminal.
22 TO_SPLITTER+=" --maxcol $(($cols - 1))"
29 #hmmm: move this to core
30 # this makes the status of pipe N into the main return value.
31 function promote_pipe_return()
33 ( exit ${PIPESTATUS[$1]} )
38 # one unpleasantry to take care of first; cygwin barfs aggressively if the TMP directory
39 # is a DOS path, but we need it to be a DOS path for our GFFS testing, so that blows.
40 # to get past this, TMP gets changed below to a hopefully generic and safe place.
41 if [[ "$TMP" =~ .:.* ]]; then
42 echo "making weirdo temporary directory for PCDOS-style path."
43 export TMP=/tmp/rev_control_$USER
45 if [ ! -d "$TMP" ]; then
48 if [ ! -d "$TMP" ]; then
49 echo "could not create the temporary directory TMP in: $TMP"
50 echo "this script will not work properly without an existing TMP directory."
55 # checks the directory provided into the revision control system repository it belongs to.
58 local directory="$1"; shift
62 # make a nice echoer since we want to use it inside conditions below.
63 local nicedir="$directory"
64 if [ $nicedir == "." ]; then
67 local blatt="echo -n checking in '$nicedir'... "
69 do_update "$directory"
70 test_or_die "repository update--this should be fixed before check-in."
72 pushd "$directory" &>/dev/null
73 if [ -f ".no-checkin" ]; then
74 echo "skipping check-in due to presence of .no-checkin sentinel file."
75 elif [ -d "CVS" ]; then
76 if test_writeable "CVS"; then
79 test_or_die "cvs checkin"
81 elif [ -d ".svn" ]; then
82 if test_writeable ".svn"; then
85 test_or_die "svn checkin"
87 elif [ -d ".git" ]; then
88 if test_writeable ".git"; then
91 # put all changed and new files in the commit. not to everyone's liking.
92 git add --all . | $TO_SPLITTER
94 test_or_die "git add all new files"
96 # see if there are any changes in the local repository.
97 if ! git diff-index --quiet HEAD --; then
98 # tell git about all the files and get a check-in comment.
99 #hmmm: begins to look like, you guessed it, a reusable bit that all commit actions could enjoy.
102 test_or_continue "git commit"
103 if [ $retval -ne 0 ]; then
104 echo -e -n "Commit failed or was aborted:\nShould we continue with other check-ins? [y/N] "
107 if [[ "${line:0:1}" != "y" ]]; then
108 echo "Stopping check-in process due to missing commit and user request."
114 # a new set of steps we have to take to make sure the branch integrity is good.
115 do_careful_git_update "$(\pwd)"
117 # we continue on to the push, even if there were no changes this time, because
118 # there could already be committed changes that haven't been pushed yet.
120 # upload any changes to the upstream repo so others can see them.
121 git push --tags origin "$(my_branch_name)" 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
122 promote_pipe_return 0
123 test_or_die "git push"
127 # nothing there. it's not an error though.
128 echo no repository in $directory
132 restore_terminal_title
137 # shows the local changes in a repository.
140 local directory="$1"; shift
144 pushd "$directory" &>/dev/null
146 # only update if we see a repository living there.
147 if [ -d ".svn" ]; then
149 test_or_die "subversion diff"
150 elif [ -d ".git" ]; then
152 test_or_die "git diff"
153 elif [ -d "CVS" ]; then
155 test_or_die "cvs diff"
160 restore_terminal_title
165 # reports any files that are not already known to the upstream repository.
166 function do_report_new
168 local directory="$1"; shift
172 pushd "$directory" &>/dev/null
174 # only update if we see a repository living there.
175 if [ -f ".no-checkin" ]; then
176 echo "skipping reporting due to presence of .no-checkin sentinel file."
177 elif [ -d ".svn" ]; then
178 # this action so far only makes sense and is needed for svn.
179 bash $FEISTY_MEOW_SCRIPTS/rev_control/svnapply.sh \? echo
180 test_or_die "svn diff"
181 elif [ -d ".git" ]; then
183 test_or_die "git status -u"
188 restore_terminal_title
193 # checks in all the folders in the specified list.
194 function checkin_list()
196 # make the list of directories unique.
197 local list="$(uniquify $*)"
201 # turn repo list back into an array.
202 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
206 for outer in "${repository_list[@]}"; do
207 # check the repository first, since it might be an absolute path.
208 if [[ $outer =~ /.* ]]; then
209 # yep, this path is absolute. just handle it directly.
210 if [ ! -d "$outer" ]; then continue; fi
212 test_or_die "running check-in (absolute) on path: $outer"
215 for inner in $list; do
216 # add in the directory component to see if we can find the folder.
217 local path="$inner/$outer"
218 if [ ! -d "$path" ]; then continue; fi
220 test_or_die "running check-in (relative) on path: $path"
226 restore_terminal_title
229 # does a careful git update on all the folders in the specified list.
230 function puff_out_list()
232 # make the list of directories unique.
233 local list="$(uniquify $*)"
237 # turn repo list back into an array.
238 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
242 #hmmm: once again, seeing some reusable code in this loop...
243 for outer in "${repository_list[@]}"; do
244 # check the repository first, since it might be an absolute path.
245 if [[ $outer =~ /.* ]]; then
246 # yep, this path is absolute. just handle it directly.
247 if [ ! -d "$outer" ]; then continue; fi
248 do_careful_git_update "$outer"
249 test_or_die "running puff-out (absolute) on path: $outer"
252 for inner in $list; do
253 # add in the directory component to see if we can find the folder.
254 local path="$inner/$outer"
255 if [ ! -d "$path" ]; then continue; fi
256 do_careful_git_update "$path"
257 test_or_die "running puff-out (relative) on path: $path"
263 restore_terminal_title
267 ### takes out the first few carriage returns that are in the input.
268 ##function squash_first_few_crs()
271 ##while read input_text; do
273 ##if [ $i -le 5 ]; then
274 ##echo -n "$input_text "
279 ##if [ $i -le 3 ]; then
280 ### if we're still squashing eols, make sure we don't leave them hanging.
285 #hmmm: the below are git specific and should be named that way.
287 function all_branch_names()
289 echo "$(git branch -vv | cut -d ' ' -f2)"
292 # a helpful method that reports the git branch for the current directory's
294 function my_branch_name()
296 echo "$(git branch -vv | grep '\*' | cut -d ' ' -f2)"
299 #this had a -> in it at one point for not matching, didn't it?
300 # this reports the upstream branch for the current repo.
301 ##function parent_branch_name()
303 ##echo "$(git branch -vv | grep \* | cut -d ' ' -f2)"
306 # reports the status of the branch by echoing one of these values:
307 # okay: up to date and everything is good.
308 # needs_pull: this branch needs to be pulled from origins.
309 # needs_push: there are unsaved changes on this branch to push to remote store.
310 # diverged: the branches diverged and are going to need a merge.
311 # reference: https://stackoverflow.com/questions/3258243/check-if-pull-needed-in-git
312 function check_branch_state()
314 local branch="$1"; shift
316 if [ -z "$branch" ]; then
317 echo "No branch was passed to check branch state."
321 local to_return=120 # unknown issue.
323 local local_branch=$(git rev-parse @)
324 local remote_branch=$(git rev-parse "$branch")
325 local merge_base=$(git merge-base @ "$branch")
328 if [ "$local_branch" == "$remote_branch" ]; then
330 elif [ "$local_branch" == "$merge_base" ]; then
332 elif [ "$remote_branch" == "$merge_base" ]; then
343 # only shows the branch state if it's not okay.
344 # note that this is not the same as a conditional branch (ha ha).
345 function show_branch_conditionally()
347 local this_branch="$1"; shift
349 local state=$(check_branch_state "$this_branch")
350 if [ "$state" != "okay" ]; then
351 echo "=> branch '$this_branch' state is not clean: $state"
355 # the git update process just gets more and more complex when you bring in
356 # branches, so we've moved this here to avoid having a ton of code in the
358 function do_careful_git_update()
360 local directory="$1"; shift
361 pushd "$directory" &>/dev/null
362 test_or_die "changing to directory: $directory"
364 if [ ! -d ".git" ]; then
365 # we ignore if they're jumping into a non-useful folder, but also tell them.
366 echo "Directory is not a git repository: $directory"
370 local this_branch="$(my_branch_name)"
372 show_branch_conditionally "$this_branch"
374 # first update all our remote branches to their current state from the repos.
375 git remote update | $TO_SPLITTER
376 promote_pipe_return 0
377 test_or_die "git remote update"
379 show_branch_conditionally "$this_branch"
381 # this code is now doing what i have to do when i repair the repo. and it seems to be good so far.
382 # note that we allow the local branch to be merged with its remote counterpart; otherwise we would
383 # miss changes that happened elsewhere which should be seen in our local copy.
384 local branch_list=$(all_branch_names)
386 for bran in $branch_list; do
387 # echo "synchronizing remote branch: $bran"
388 git checkout "$bran" | $TO_SPLITTER
389 promote_pipe_return 0
390 test_or_die "git switching checkout to remote branch: $bran"
392 show_branch_conditionally "$this_branch"
394 remote_branch_info=$(git ls-remote --heads origin $bran 2>/dev/null)
395 if [ ! -z "$remote_branch_info" ]; then
396 # we are pretty sure the remote branch does exist.
397 git pull --rebase --autostash origin "$bran" | $TO_SPLITTER
398 # we may want to choose to do fast forward, to avoid crazy multiple merge histories
399 # without any changes in them. --no-ff
400 promote_pipe_return 0
402 test_or_die "git pull of remote branch: $bran"
404 # now switch back to our branch.
405 git checkout "$this_branch" | $TO_SPLITTER
406 promote_pipe_return 0
407 test_or_die "git checking out our current branch: $this_branch"
409 # now pull down any changes in our own origin in the repo, to stay in synch
410 # with any changes from others.
411 git pull --rebase --autostash --all | $TO_SPLITTER
412 #is the above really important when we did this branch already in the loop?
413 #it does an --all, but is that effective or different? should we be doing that in above loop?
415 promote_pipe_return 0
416 test_or_die "git pulling all upstream"
421 # gets the latest versions of the assets from the upstream repository.
424 directory="$1"; shift
428 # make a nice echoer since we want to use it inside conditions below.
429 local nicedir="$directory"
430 if [ $nicedir == "." ]; then
433 local blatt="echo retrieving '$nicedir'..."
435 pushd "$directory" &>/dev/null
436 if [ -d "CVS" ]; then
437 if test_writeable "CVS"; then
439 cvs update . | $TO_SPLITTER
440 promote_pipe_return 0
441 test_or_die "cvs update"
443 elif [ -d ".svn" ]; then
444 if test_writeable ".svn"; then
446 svn update . | $TO_SPLITTER
447 promote_pipe_return 0
448 test_or_die "svn update"
450 elif [ -d ".git" ]; then
451 if test_writeable ".git"; then
453 git pull --rebase --autostash 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
454 #ordinary pulls should be allowed to do fast forward: --no-ff
455 promote_pipe_return 0
456 test_or_die "git pull of origin"
459 # this is not an error necessarily; we'll just pretend they planned this.
460 echo no repository in $directory
464 restore_terminal_title
469 # gets all the updates for a list of folders under revision control.
470 function checkout_list()
472 local list="$(uniquify $*)"
476 # turn repo list back into an array.
477 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
481 for outer in "${repository_list[@]}"; do
482 # check the repository first, since it might be an absolute path.
483 if [[ $outer =~ /.* ]]; then
484 # yep, this path is absolute. just handle it directly.
485 if [ ! -d "$outer" ]; then continue; fi
487 test_or_die "running update on: $path"
490 for inner in $list; do
491 # add in the directory component to see if we can find the folder.
492 local path="$inner/$outer"
493 if [ ! -d "$path" ]; then continue; fi
495 test_or_die "running update on: $path"
501 restore_terminal_title
504 # provides a list of absolute paths of revision control directories
505 # that are located under the directory passed as the first parameter.
506 function generate_rev_ctrl_filelist()
508 local dir="$1"; shift
509 pushd "$dir" &>/dev/null
510 local dirhere="$( \cd "$(\dirname "$dir")" && /bin/pwd )"
511 local tempfile=$(mktemp /tmp/zz_checkins.XXXXXX)
513 local additional_filter
514 find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".svn" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
515 find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".git" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
516 # CVS is not well behaved like git and (now) svn, and we seldom use it anymore.
519 # see if they've warned us not to try checking in within vendor hierarchies.
520 if [ ! -z "NO_CHECKIN_VENDOR" ]; then
521 sed -i -e '/.*\/vendor\/.*/d' "$tempfile"
524 local sortfile=$(mktemp /tmp/zz_checkin_sort.XXXXXX)
525 sort <"$tempfile" >"$sortfile"
530 # iterates across a list of directories contained in a file (first parameter).
531 # on each directory name, it performs the action (second parameter) provided.
532 function perform_revctrl_action_on_file()
534 local tempfile="$1"; shift
535 local action="$1"; shift
541 while read -u 3 dirname; do
542 if [ -z "$dirname" ]; then
543 # we often have blank lines in the input file for some reason.
547 pushd "$dirname" &>/dev/null
549 # pass the current directory plus the remaining parameters from function invocation.
551 test_or_die "performing action $action on: $(pwd)"
556 if [ -z "$did_anything" ]; then
557 echo "There was nothing to do the action '$action' on."
560 restore_terminal_title