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 local to_return=120 # unknown issue.
271 local local_branch=$(git rev-parse @)
272 local remote_branch=$(git rev-parse "$branch")
273 local merge_base=$(git merge-base @ "$branch")
275 if [ "$local_branch" == "$remote_branch" ]; then
277 elif [ "$local_branch" == "$merge_base" ]; then
279 elif [ "$remote_branch" == "$merge_base" ]; then
288 # the git update process just gets more and more complex when you bring in
289 # branches, so we've moved this here to avoid having a ton of code in the
291 function do_careful_git_update()
293 local directory="$1"; shift
294 pushd "$directory" &>/dev/null
295 test_or_die "changing to directory: $directory"
297 if [ ! -d ".git" ]; then
298 # we ignore if they're jumping into a non-useful folder, but also tell them.
299 echo "Directory is not a git repository: $directory"
303 # first update all our remote branches to their current state from the repos.
304 git remote update | $TO_SPLITTER
305 promote_pipe_return 0
306 test_or_die "git remote update"
308 local this_branch="$(my_branch_name)"
309 #appears to be useless; reports no changes when we need to know about remote changes that do exist:
310 #hmmm: trying it out again now that things are better elsewhere. let's see what it says.
311 state=$(check_branch_state "$this_branch")
312 echo "=> branch '$this_branch' state is: $state"
314 # this code is now doing what i have to do when i repair the repo. and it seems to be good so far.
315 local branch_list=$(all_branch_names)
317 for bran in $branch_list; do
318 # echo "synchronizing remote branch: $bran"
319 git checkout "$bran" | $TO_SPLITTER
320 promote_pipe_return 0
321 test_or_die "git switching checkout to remote branch: $bran"
323 state=$(check_branch_state "$bran")
324 echo "=> branch '$bran' state is: $state"
326 remote_branch_info=$(git ls-remote --heads origin $bran 2>/dev/null)
327 if [ ! -z "$remote_branch_info" ]; then
328 # we are pretty sure the remote branch does exist.
329 git pull --no-ff origin "$bran" | $TO_SPLITTER
330 promote_pipe_return 0
332 test_or_die "git pull of remote branch: $bran"
334 # now switch back to our branch.
335 git checkout "$this_branch" | $TO_SPLITTER
336 promote_pipe_return 0
337 test_or_die "git checking out our current branch: $this_branch"
339 # now pull down any changes in our own origin in the repo, to stay in synch
340 # with any changes from others.
341 git pull --no-ff --all | $TO_SPLITTER
342 promote_pipe_return 0
343 test_or_die "git pulling all upstream"
348 # gets the latest versions of the assets from the upstream repository.
351 directory="$1"; shift
355 # make a nice echoer since we want to use it inside conditions below.
356 local nicedir="$directory"
357 if [ $nicedir == "." ]; then
360 local blatt="echo retrieving '$nicedir'..."
362 pushd "$directory" &>/dev/null
363 if [ -d "CVS" ]; then
364 if test_writeable "CVS"; then
366 cvs update . | $TO_SPLITTER
367 promote_pipe_return 0
368 test_or_die "cvs update"
370 elif [ -d ".svn" ]; then
371 if test_writeable ".svn"; then
373 svn update . | $TO_SPLITTER
374 promote_pipe_return 0
375 test_or_die "svn update"
377 elif [ -d ".git" ]; then
378 if test_writeable ".git"; then
380 git pull --no-ff 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
381 promote_pipe_return 0
382 test_or_die "git pull of origin without fast forwards"
385 # this is not an error necessarily; we'll just pretend they planned this.
386 echo no repository in $directory
390 restore_terminal_title
395 # gets all the updates for a list of folders under revision control.
396 function checkout_list()
398 local list="$(uniquify $*)"
402 # turn repo list back into an array.
403 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
407 for outer in "${repository_list[@]}"; do
408 # check the repository first, since it might be an absolute path.
409 if [[ $outer =~ /.* ]]; then
410 # yep, this path is absolute. just handle it directly.
411 if [ ! -d "$outer" ]; then continue; fi
413 test_or_die "running update on: $path"
416 for inner in $list; do
417 # add in the directory component to see if we can find the folder.
418 local path="$inner/$outer"
419 if [ ! -d "$path" ]; then continue; fi
421 test_or_die "running update on: $path"
427 restore_terminal_title
430 # provides a list of absolute paths of revision control directories
431 # that are located under the directory passed as the first parameter.
432 function generate_rev_ctrl_filelist()
434 local dir="$1"; shift
435 pushd "$dir" &>/dev/null
436 local dirhere="$( \cd "$(\dirname "$dir")" && /bin/pwd )"
437 local tempfile=$(mktemp /tmp/zz_checkins.XXXXXX)
439 local additional_filter
440 find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".svn" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
441 find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".git" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
442 # CVS is not well behaved like git and (now) svn, and we seldom use it anymore.
445 # see if they've warned us not to try checking in within vendor hierarchies.
446 if [ ! -z "NO_CHECKIN_VENDOR" ]; then
447 sed -i -e '/.*\/vendor\/.*/d' "$tempfile"
450 local sortfile=$(mktemp /tmp/zz_checkin_sort.XXXXXX)
451 sort <"$tempfile" >"$sortfile"
456 # iterates across a list of directories contained in a file (first parameter).
457 # on each directory name, it performs the action (second parameter) provided.
458 function perform_revctrl_action_on_file()
460 local tempfile="$1"; shift
461 local action="$1"; shift
467 while read -u 3 dirname; do
468 if [ -z "$dirname" ]; then
469 # we often have blank lines in the input file for some reason.
473 pushd "$dirname" &>/dev/null
475 # pass the current directory plus the remaining parameters from function invocation.
477 test_or_die "performing action $action on: $(pwd)"
482 if [ -z "$did_anything" ]; then
483 echo "There was nothing to do the action '$action' on."
486 restore_terminal_title