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)"
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 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.
100 test_or_die "git commit"
103 # a new set of steps we have to take to make sure the branch integrity is good.
104 do_careful_git_update "$(\pwd)"
106 # we continue on to the push, even if there were no changes this time, because
107 # there could already be committed changes that haven't been pushed yet.
109 # upload any changes to the upstream repo so others can see them.
110 git push origin "$(my_branch_name)" 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
111 promote_pipe_return 0
112 test_or_die "git push"
116 # nothing there. it's not an error though.
117 echo no repository in $directory
121 restore_terminal_title
126 # shows the local changes in a repository.
129 local directory="$1"; shift
133 pushd "$directory" &>/dev/null
135 # only update if we see a repository living there.
136 if [ -d ".svn" ]; then
138 test_or_die "subversion diff"
139 elif [ -d ".git" ]; then
141 test_or_die "git diff"
142 elif [ -d "CVS" ]; then
144 test_or_die "cvs diff"
149 restore_terminal_title
154 # reports any files that are not already known to the upstream repository.
155 function do_report_new
157 local directory="$1"; shift
161 pushd "$directory" &>/dev/null
163 # only update if we see a repository living there.
164 if [ -f ".no-checkin" ]; then
165 echo "skipping reporting due to presence of .no-checkin sentinel file."
166 elif [ -d ".svn" ]; then
167 # this action so far only makes sense and is needed for svn.
168 bash $FEISTY_MEOW_SCRIPTS/rev_control/svnapply.sh \? echo
169 test_or_die "svn diff"
170 elif [ -d ".git" ]; then
172 test_or_die "git status -u"
177 restore_terminal_title
182 # checks in all the folders in a specified list.
183 function checkin_list()
185 # make the list of directories unique.
186 local list="$(uniquify $*)"
190 # turn repo list back into an array.
191 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
195 for outer in "${repository_list[@]}"; do
196 # check the repository first, since it might be an absolute path.
197 if [[ $outer =~ /.* ]]; then
198 # yep, this path is absolute. just handle it directly.
199 if [ ! -d "$outer" ]; then continue; fi
201 test_or_die "running check-in (absolute) on path: $outer"
204 for inner in $list; do
205 # add in the directory component to see if we can find the folder.
206 local path="$inner/$outer"
207 if [ ! -d "$path" ]; then continue; fi
209 test_or_die "running check-in (relative) on path: $path"
215 restore_terminal_title
218 # takes out the first few carriage returns that are in the input.
219 function squash_first_few_crs()
222 while read input_text; do
224 if [ $i -le 5 ]; then
225 echo -n "$input_text "
230 if [ $i -le 3 ]; then
231 # if we're still squashing eols, make sure we don't leave them hanging.
236 #hmmm: the below are git specific and should be named that way.
238 function all_branch_names()
240 echo "$(git branch -vv | cut -d ' ' -f2)"
243 # a helpful method that reports the git branch for the current directory's
245 function my_branch_name()
247 echo "$(git branch -vv | grep '\*' | cut -d ' ' -f2)"
250 #this had a -> in it at one point for not matching, didn't it?
251 # this reports the upstream branch for the current repo.
252 ##function parent_branch_name()
254 ##echo "$(git branch -vv | grep \* | cut -d ' ' -f2)"
257 # reports the status of the branch by echoing one of these values:
258 # okay: up to date and everything is good.
259 # needs_pull: this branch needs to be pulled from origins.
260 # needs_push: there are unsaved changes on this branch to push to remote store.
261 # diverged: the branches diverged and are going to need a merge.
262 # reference: https://stackoverflow.com/questions/3258243/check-if-pull-needed-in-git
263 function check_branch_state()
265 local branch="$1"; shift
267 if [ -z "$branch" ]; then
268 echo "No branch was passed to check branch state."
272 local to_return=120 # unknown issue.
274 local local_branch=$(git rev-parse @)
275 local remote_branch=$(git rev-parse "$branch")
276 local merge_base=$(git merge-base @ "$branch")
278 if [ "$local_branch" == "$remote_branch" ]; then
280 elif [ "$local_branch" == "$merge_base" ]; then
282 elif [ "$remote_branch" == "$merge_base" ]; then
291 # only shows the branch state if it's not okay.
292 # note that this is not the same as a conditional branch (ha ha).
293 function show_branch_conditionally()
295 local this_branch="$1"; shift
297 state=$(check_branch_state "$this_branch")
298 if [ "$state" != "okay" ]; then
299 echo "=> branch '$this_branch' state is not clean: $state"
303 # the git update process just gets more and more complex when you bring in
304 # branches, so we've moved this here to avoid having a ton of code in the
306 function do_careful_git_update()
308 local directory="$1"; shift
309 pushd "$directory" &>/dev/null
310 test_or_die "changing to directory: $directory"
312 if [ ! -d ".git" ]; then
313 # we ignore if they're jumping into a non-useful folder, but also tell them.
314 echo "Directory is not a git repository: $directory"
318 local this_branch="$(my_branch_name)"
320 show_branch_conditionally "$this_branch"
322 # first update all our remote branches to their current state from the repos.
323 git remote update | $TO_SPLITTER
324 promote_pipe_return 0
325 test_or_die "git remote update"
327 show_branch_conditionally "$this_branch"
329 # this code is now doing what i have to do when i repair the repo. and it seems to be good so far.
330 local branch_list=$(all_branch_names)
332 for bran in $branch_list; do
333 # echo "synchronizing remote branch: $bran"
334 git checkout "$bran" | $TO_SPLITTER
335 promote_pipe_return 0
336 test_or_die "git switching checkout to remote branch: $bran"
338 show_branch_conditionally "$this_branch"
340 remote_branch_info=$(git ls-remote --heads origin $bran 2>/dev/null)
341 if [ ! -z "$remote_branch_info" ]; then
342 # we are pretty sure the remote branch does exist.
343 git pull --no-ff origin "$bran" | $TO_SPLITTER
344 promote_pipe_return 0
346 echo "=> branch '$bran' state after pull is: $state"
348 test_or_die "git pull of remote branch: $bran"
350 # now switch back to our branch.
351 git checkout "$this_branch" | $TO_SPLITTER
352 promote_pipe_return 0
353 test_or_die "git checking out our current branch: $this_branch"
355 # now pull down any changes in our own origin in the repo, to stay in synch
356 # with any changes from others.
357 git pull --no-ff --all | $TO_SPLITTER
358 promote_pipe_return 0
359 test_or_die "git pulling all upstream"
364 # gets the latest versions of the assets from the upstream repository.
367 directory="$1"; shift
371 # make a nice echoer since we want to use it inside conditions below.
372 local nicedir="$directory"
373 if [ $nicedir == "." ]; then
376 local blatt="echo retrieving '$nicedir'..."
378 pushd "$directory" &>/dev/null
379 if [ -d "CVS" ]; then
380 if test_writeable "CVS"; then
382 cvs update . | $TO_SPLITTER
383 promote_pipe_return 0
384 test_or_die "cvs update"
386 elif [ -d ".svn" ]; then
387 if test_writeable ".svn"; then
389 svn update . | $TO_SPLITTER
390 promote_pipe_return 0
391 test_or_die "svn update"
393 elif [ -d ".git" ]; then
394 if test_writeable ".git"; then
396 git pull --no-ff 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
397 promote_pipe_return 0
398 test_or_die "git pull of origin without fast forwards"
401 # this is not an error necessarily; we'll just pretend they planned this.
402 echo no repository in $directory
406 restore_terminal_title
411 # gets all the updates for a list of folders under revision control.
412 function checkout_list()
414 local list="$(uniquify $*)"
418 # turn repo list back into an array.
419 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
423 for outer in "${repository_list[@]}"; do
424 # check the repository first, since it might be an absolute path.
425 if [[ $outer =~ /.* ]]; then
426 # yep, this path is absolute. just handle it directly.
427 if [ ! -d "$outer" ]; then continue; fi
429 test_or_die "running update on: $path"
432 for inner in $list; do
433 # add in the directory component to see if we can find the folder.
434 local path="$inner/$outer"
435 if [ ! -d "$path" ]; then continue; fi
437 test_or_die "running update on: $path"
443 restore_terminal_title
446 # provides a list of absolute paths of revision control directories
447 # that are located under the directory passed as the first parameter.
448 function generate_rev_ctrl_filelist()
450 local dir="$1"; shift
451 pushd "$dir" &>/dev/null
452 local dirhere="$( \cd "$(\dirname "$dir")" && /bin/pwd )"
453 local tempfile=$(mktemp /tmp/zz_checkins.XXXXXX)
455 local additional_filter
456 find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".svn" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
457 find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".git" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
458 # CVS is not well behaved like git and (now) svn, and we seldom use it anymore.
461 # see if they've warned us not to try checking in within vendor hierarchies.
462 if [ ! -z "NO_CHECKIN_VENDOR" ]; then
463 sed -i -e '/.*\/vendor\/.*/d' "$tempfile"
466 local sortfile=$(mktemp /tmp/zz_checkin_sort.XXXXXX)
467 sort <"$tempfile" >"$sortfile"
472 # iterates across a list of directories contained in a file (first parameter).
473 # on each directory name, it performs the action (second parameter) provided.
474 function perform_revctrl_action_on_file()
476 local tempfile="$1"; shift
477 local action="$1"; shift
483 while read -u 3 dirname; do
484 if [ -z "$dirname" ]; then
485 # we often have blank lines in the input file for some reason.
489 pushd "$dirname" &>/dev/null
491 # pass the current directory plus the remaining parameters from function invocation.
493 test_or_die "performing action $action on: $(pwd)"
498 if [ -z "$did_anything" ]; then
499 echo "There was nothing to do the action '$action' on."
502 restore_terminal_title