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)" ]; then
19 TO_SPLITTER="$(which splitter)"
21 #hmmm: another reusable chunk here, getting terminal size.
22 # calculate the number of columsn in the terminal.
23 cols=$(stty size | awk '{print $2}')
24 TO_SPLITTER+=" --maxcol $(($cols - 1))"
31 #hmmm: move this to core
32 # this makes the status of pipe N into the main return value.
33 function promote_pipe_return()
35 ( exit ${PIPESTATUS[$1]} )
40 # one unpleasantry to take care of first; cygwin barfs aggressively if the TMP directory
41 # is a DOS path, but we need it to be a DOS path for our GFFS testing, so that blows.
42 # to get past this, TMP gets changed below to a hopefully generic and safe place.
43 if [[ "$TMP" =~ .:.* ]]; then
44 echo "making weirdo temporary directory for PCDOS-style path."
45 export TMP=/tmp/rev_control_$USER
47 if [ ! -d "$TMP" ]; then
50 if [ ! -d "$TMP" ]; then
51 echo "could not create the temporary directory TMP in: $TMP"
52 echo "this script will not work properly without an existing TMP directory."
57 # checks the directory provided into the revision control system repository it belongs to.
60 local directory="$1"; shift
64 # make a nice echoer since we want to use it inside conditions below.
65 local nicedir="$directory"
66 if [ $nicedir == "." ]; then
69 local blatt="echo checking in '$nicedir'..."
71 do_update "$directory"
72 test_or_die "repository update--this should be fixed before check-in."
74 pushd "$directory" &>/dev/null
75 if [ -f ".no-checkin" ]; then
76 echo "skipping check-in due to presence of .no-checkin sentinel file."
77 elif [ -d "CVS" ]; then
78 if test_writeable "CVS"; then
81 test_or_die "cvs checkin"
83 elif [ -d ".svn" ]; then
84 if test_writeable ".svn"; then
87 test_or_die "svn checkin"
89 elif [ -d ".git" ]; then
90 if test_writeable ".git"; then
93 # put all changed and new files in the commit. not to everyone's liking.
94 git add --all . | $TO_SPLITTER
96 test_or_die "git add all new files"
98 # see if there are any changes in the local repository.
99 if ! git diff-index --quiet HEAD --; then
100 # tell git about all the files and get a check-in comment.
102 test_or_die "git commit"
105 # a new set of steps we have to take to make sure the branch integrity is good.
106 do_careful_git_update "$(\pwd)"
108 # we continue on to the push, even if there were no changes this time, because
109 # there could already be committed changes that haven't been pushed yet.
111 # upload any changes to the upstream repo so others can see them.
112 git push origin "$(my_branch_name)" 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
113 promote_pipe_return 0
114 test_or_die "git push"
118 # nothing there. it's not an error though.
119 echo no repository in $directory
123 restore_terminal_title
128 # shows the local changes in a repository.
131 local directory="$1"; shift
135 pushd "$directory" &>/dev/null
137 # only update if we see a repository living there.
138 if [ -d ".svn" ]; then
140 test_or_die "subversion diff"
141 elif [ -d ".git" ]; then
143 test_or_die "git diff"
144 elif [ -d "CVS" ]; then
146 test_or_die "cvs diff"
151 restore_terminal_title
156 # reports any files that are not already known to the upstream repository.
157 function do_report_new
159 local directory="$1"; shift
163 pushd "$directory" &>/dev/null
165 # only update if we see a repository living there.
166 if [ -f ".no-checkin" ]; then
167 echo "skipping reporting due to presence of .no-checkin sentinel file."
168 elif [ -d ".svn" ]; then
169 # this action so far only makes sense and is needed for svn.
170 bash $FEISTY_MEOW_SCRIPTS/rev_control/svnapply.sh \? echo
171 test_or_die "svn diff"
172 elif [ -d ".git" ]; then
174 test_or_die "git status -u"
179 restore_terminal_title
184 # checks in all the folders in a specified list.
185 function checkin_list()
187 # make the list of directories unique.
188 local list="$(uniquify $*)"
192 # turn repo list back into an array.
193 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
197 for outer in "${repository_list[@]}"; do
198 # check the repository first, since it might be an absolute path.
199 if [[ $outer =~ /.* ]]; then
200 # yep, this path is absolute. just handle it directly.
201 if [ ! -d "$outer" ]; then continue; fi
203 test_or_die "running check-in (absolute) on path: $outer"
206 for inner in $list; do
207 # add in the directory component to see if we can find the folder.
208 local path="$inner/$outer"
209 if [ ! -d "$path" ]; then continue; fi
211 test_or_die "running check-in (relative) on path: $path"
217 restore_terminal_title
220 # takes out the first few carriage returns that are in the input.
221 function squash_first_few_crs()
224 while read input_text; do
226 if [ $i -le 5 ]; then
227 echo -n "$input_text "
232 if [ $i -le 3 ]; then
233 # if we're still squashing eols, make sure we don't leave them hanging.
238 #hmmm: the below are git specific and should be named that way.
240 function all_branch_names()
242 echo "$(git branch -vv | cut -d ' ' -f2)"
245 # a helpful method that reports the git branch for the current directory's
247 function my_branch_name()
249 echo "$(git branch -vv | grep '\*' | cut -d ' ' -f2)"
252 #this had a -> in it at one point for not matching, didn't it?
253 # this reports the upstream branch for the current repo.
254 ##function parent_branch_name()
256 ##echo "$(git branch -vv | grep \* | cut -d ' ' -f2)"
259 # reports the status of the branch by echoing one of these values:
260 # okay: up to date and everything is good.
261 # needs_pull: this branch needs to be pulled from origins.
262 # needs_push: there are unsaved changes on this branch to push to remote store.
263 # diverged: the branches diverged and are going to need a merge.
264 # reference: https://stackoverflow.com/questions/3258243/check-if-pull-needed-in-git
265 function check_branch_state()
267 local branch="$1"; shift
269 if [ -z "$branch" ]; then
270 echo "No branch was passed to check branch state."
274 local to_return=120 # unknown issue.
276 local local_branch=$(git rev-parse @)
277 local remote_branch=$(git rev-parse "$branch")
278 local merge_base=$(git merge-base @ "$branch")
280 if [ "$local_branch" == "$remote_branch" ]; then
282 elif [ "$local_branch" == "$merge_base" ]; then
284 elif [ "$remote_branch" == "$merge_base" ]; then
293 # the git update process just gets more and more complex when you bring in
294 # branches, so we've moved this here to avoid having a ton of code in the
296 function do_careful_git_update()
298 local directory="$1"; shift
299 pushd "$directory" &>/dev/null
300 test_or_die "changing to directory: $directory"
302 if [ ! -d ".git" ]; then
303 # we ignore if they're jumping into a non-useful folder, but also tell them.
304 echo "Directory is not a git repository: $directory"
308 local this_branch="$(my_branch_name)"
310 state=$(check_branch_state "$this_branch")
311 echo "=> branch '$this_branch' state prior to remote update is: $state"
313 # first update all our remote branches to their current state from the repos.
314 git remote update | $TO_SPLITTER
315 promote_pipe_return 0
316 test_or_die "git remote update"
318 state=$(check_branch_state "$this_branch")
319 echo "=> branch '$this_branch' state after remote update is: $state"
321 # this code is now doing what i have to do when i repair the repo. and it seems to be good so far.
322 local branch_list=$(all_branch_names)
324 for bran in $branch_list; do
325 # echo "synchronizing remote branch: $bran"
326 git checkout "$bran" | $TO_SPLITTER
327 promote_pipe_return 0
328 test_or_die "git switching checkout to remote branch: $bran"
330 state=$(check_branch_state "$bran")
331 echo "=> branch '$bran' state is: $state"
333 remote_branch_info=$(git ls-remote --heads origin $bran 2>/dev/null)
334 if [ ! -z "$remote_branch_info" ]; then
335 # we are pretty sure the remote branch does exist.
336 git pull --no-ff origin "$bran" | $TO_SPLITTER
337 promote_pipe_return 0
339 echo "=> branch '$bran' state after pull is: $state"
341 test_or_die "git pull of remote branch: $bran"
343 # now switch back to our branch.
344 git checkout "$this_branch" | $TO_SPLITTER
345 promote_pipe_return 0
346 test_or_die "git checking out our current branch: $this_branch"
348 # now pull down any changes in our own origin in the repo, to stay in synch
349 # with any changes from others.
350 git pull --no-ff --all | $TO_SPLITTER
351 promote_pipe_return 0
352 test_or_die "git pulling all upstream"
357 # gets the latest versions of the assets from the upstream repository.
360 directory="$1"; shift
364 # make a nice echoer since we want to use it inside conditions below.
365 local nicedir="$directory"
366 if [ $nicedir == "." ]; then
369 local blatt="echo retrieving '$nicedir'..."
371 pushd "$directory" &>/dev/null
372 if [ -d "CVS" ]; then
373 if test_writeable "CVS"; then
375 cvs update . | $TO_SPLITTER
376 promote_pipe_return 0
377 test_or_die "cvs update"
379 elif [ -d ".svn" ]; then
380 if test_writeable ".svn"; then
382 svn update . | $TO_SPLITTER
383 promote_pipe_return 0
384 test_or_die "svn update"
386 elif [ -d ".git" ]; then
387 if test_writeable ".git"; then
389 git pull --no-ff 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
390 promote_pipe_return 0
391 test_or_die "git pull of origin without fast forwards"
394 # this is not an error necessarily; we'll just pretend they planned this.
395 echo no repository in $directory
399 restore_terminal_title
404 # gets all the updates for a list of folders under revision control.
405 function checkout_list()
407 local list="$(uniquify $*)"
411 # turn repo list back into an array.
412 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
416 for outer in "${repository_list[@]}"; do
417 # check the repository first, since it might be an absolute path.
418 if [[ $outer =~ /.* ]]; then
419 # yep, this path is absolute. just handle it directly.
420 if [ ! -d "$outer" ]; then continue; fi
422 test_or_die "running update on: $path"
425 for inner in $list; do
426 # add in the directory component to see if we can find the folder.
427 local path="$inner/$outer"
428 if [ ! -d "$path" ]; then continue; fi
430 test_or_die "running update on: $path"
436 restore_terminal_title
439 # provides a list of absolute paths of revision control directories
440 # that are located under the directory passed as the first parameter.
441 function generate_rev_ctrl_filelist()
443 local dir="$1"; shift
444 pushd "$dir" &>/dev/null
445 local dirhere="$( \cd "$(\dirname "$dir")" && /bin/pwd )"
446 local tempfile=$(mktemp /tmp/zz_checkins.XXXXXX)
448 local additional_filter
449 find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".svn" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
450 find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".git" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
451 # CVS is not well behaved like git and (now) svn, and we seldom use it anymore.
454 # see if they've warned us not to try checking in within vendor hierarchies.
455 if [ ! -z "NO_CHECKIN_VENDOR" ]; then
456 sed -i -e '/.*\/vendor\/.*/d' "$tempfile"
459 local sortfile=$(mktemp /tmp/zz_checkin_sort.XXXXXX)
460 sort <"$tempfile" >"$sortfile"
465 # iterates across a list of directories contained in a file (first parameter).
466 # on each directory name, it performs the action (second parameter) provided.
467 function perform_revctrl_action_on_file()
469 local tempfile="$1"; shift
470 local action="$1"; shift
476 while read -u 3 dirname; do
477 if [ -z "$dirname" ]; then
478 # we often have blank lines in the input file for some reason.
482 pushd "$dirname" &>/dev/null
484 # pass the current directory plus the remaining parameters from function invocation.
486 test_or_die "performing action $action on: $(pwd)"
491 if [ -z "$did_anything" ]; then
492 echo "There was nothing to do the action '$action' on."
495 restore_terminal_title