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 local branch_list=$(all_branch_names)
384 for bran in $branch_list; do
385 # echo "synchronizing remote branch: $bran"
386 if [ "$this_branch" == "$bran" ]; then
387 echo "skipping redundant update on initial branch: $bran"
390 git checkout "$bran" | $TO_SPLITTER
391 promote_pipe_return 0
392 test_or_die "git switching checkout to remote branch: $bran"
394 show_branch_conditionally "$this_branch"
396 remote_branch_info=$(git ls-remote --heads origin $bran 2>/dev/null)
397 if [ ! -z "$remote_branch_info" ]; then
398 # we are pretty sure the remote branch does exist.
399 git pull --no-ff origin "$bran" | $TO_SPLITTER
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 --no-ff --all | $TO_SPLITTER
412 promote_pipe_return 0
413 test_or_die "git pulling all upstream"
418 # gets the latest versions of the assets from the upstream repository.
421 directory="$1"; shift
425 # make a nice echoer since we want to use it inside conditions below.
426 local nicedir="$directory"
427 if [ $nicedir == "." ]; then
430 local blatt="echo retrieving '$nicedir'..."
432 pushd "$directory" &>/dev/null
433 if [ -d "CVS" ]; then
434 if test_writeable "CVS"; then
436 cvs update . | $TO_SPLITTER
437 promote_pipe_return 0
438 test_or_die "cvs update"
440 elif [ -d ".svn" ]; then
441 if test_writeable ".svn"; then
443 svn update . | $TO_SPLITTER
444 promote_pipe_return 0
445 test_or_die "svn update"
447 elif [ -d ".git" ]; then
448 if test_writeable ".git"; then
450 git pull --no-ff 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
451 promote_pipe_return 0
452 test_or_die "git pull of origin without fast forwards"
455 # this is not an error necessarily; we'll just pretend they planned this.
456 echo no repository in $directory
460 restore_terminal_title
465 # gets all the updates for a list of folders under revision control.
466 function checkout_list()
468 local list="$(uniquify $*)"
472 # turn repo list back into an array.
473 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
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
483 test_or_die "running update on: $path"
486 for inner in $list; do
487 # add in the directory component to see if we can find the folder.
488 local path="$inner/$outer"
489 if [ ! -d "$path" ]; then continue; fi
491 test_or_die "running update on: $path"
497 restore_terminal_title
500 # provides a list of absolute paths of revision control directories
501 # that are located under the directory passed as the first parameter.
502 function generate_rev_ctrl_filelist()
504 local dir="$1"; shift
505 pushd "$dir" &>/dev/null
506 local dirhere="$( \cd "$(\dirname "$dir")" && /bin/pwd )"
507 local tempfile=$(mktemp /tmp/zz_checkins.XXXXXX)
509 local additional_filter
510 find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".svn" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
511 find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".git" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
512 # CVS is not well behaved like git and (now) svn, and we seldom use it anymore.
515 # see if they've warned us not to try checking in within vendor hierarchies.
516 if [ ! -z "NO_CHECKIN_VENDOR" ]; then
517 sed -i -e '/.*\/vendor\/.*/d' "$tempfile"
520 local sortfile=$(mktemp /tmp/zz_checkin_sort.XXXXXX)
521 sort <"$tempfile" >"$sortfile"
526 # iterates across a list of directories contained in a file (first parameter).
527 # on each directory name, it performs the action (second parameter) provided.
528 function perform_revctrl_action_on_file()
530 local tempfile="$1"; shift
531 local action="$1"; shift
537 while read -u 3 dirname; do
538 if [ -z "$dirname" ]; then
539 # we often have blank lines in the input file for some reason.
543 pushd "$dirname" &>/dev/null
545 # pass the current directory plus the remaining parameters from function invocation.
547 test_or_die "performing action $action on: $(pwd)"
552 if [ -z "$did_anything" ]; then
553 echo "There was nothing to do the action '$action' on."
556 restore_terminal_title