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 state=$(check_branch_state "$this_branch")
309 echo "=> branch '$this_branch' state prior to remote update is: $state"
311 # first update all our remote branches to their current state from the repos.
312 git remote update | $TO_SPLITTER
313 promote_pipe_return 0
314 test_or_die "git remote update"
316 local this_branch="$(my_branch_name)"
317 #appears to be useless; reports no changes when we need to know about remote changes that do exist:
318 #hmmm: trying it out again now that things are better elsewhere. let's see what it says.
319 state=$(check_branch_state "$this_branch")
320 echo "=> branch '$this_branch' state after remote update is: $state"
322 # this code is now doing what i have to do when i repair the repo. and it seems to be good so far.
323 local branch_list=$(all_branch_names)
325 for bran in $branch_list; do
326 # echo "synchronizing remote branch: $bran"
327 git checkout "$bran" | $TO_SPLITTER
328 promote_pipe_return 0
329 test_or_die "git switching checkout to remote branch: $bran"
331 state=$(check_branch_state "$bran")
332 echo "=> branch '$bran' state is: $state"
334 remote_branch_info=$(git ls-remote --heads origin $bran 2>/dev/null)
335 if [ ! -z "$remote_branch_info" ]; then
336 # we are pretty sure the remote branch does exist.
337 git pull --no-ff origin "$bran" | $TO_SPLITTER
338 promote_pipe_return 0
340 echo "=> branch '$bran' state after pull is: $state"
342 test_or_die "git pull of remote branch: $bran"
344 # now switch back to our branch.
345 git checkout "$this_branch" | $TO_SPLITTER
346 promote_pipe_return 0
347 test_or_die "git checking out our current branch: $this_branch"
349 # now pull down any changes in our own origin in the repo, to stay in synch
350 # with any changes from others.
351 git pull --no-ff --all | $TO_SPLITTER
352 promote_pipe_return 0
353 test_or_die "git pulling all upstream"
358 # gets the latest versions of the assets from the upstream repository.
361 directory="$1"; shift
365 # make a nice echoer since we want to use it inside conditions below.
366 local nicedir="$directory"
367 if [ $nicedir == "." ]; then
370 local blatt="echo retrieving '$nicedir'..."
372 pushd "$directory" &>/dev/null
373 if [ -d "CVS" ]; then
374 if test_writeable "CVS"; then
376 cvs update . | $TO_SPLITTER
377 promote_pipe_return 0
378 test_or_die "cvs update"
380 elif [ -d ".svn" ]; then
381 if test_writeable ".svn"; then
383 svn update . | $TO_SPLITTER
384 promote_pipe_return 0
385 test_or_die "svn update"
387 elif [ -d ".git" ]; then
388 if test_writeable ".git"; then
390 git pull --no-ff 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
391 promote_pipe_return 0
392 test_or_die "git pull of origin without fast forwards"
395 # this is not an error necessarily; we'll just pretend they planned this.
396 echo no repository in $directory
400 restore_terminal_title
405 # gets all the updates for a list of folders under revision control.
406 function checkout_list()
408 local list="$(uniquify $*)"
412 # turn repo list back into an array.
413 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
417 for outer in "${repository_list[@]}"; do
418 # check the repository first, since it might be an absolute path.
419 if [[ $outer =~ /.* ]]; then
420 # yep, this path is absolute. just handle it directly.
421 if [ ! -d "$outer" ]; then continue; fi
423 test_or_die "running update on: $path"
426 for inner in $list; do
427 # add in the directory component to see if we can find the folder.
428 local path="$inner/$outer"
429 if [ ! -d "$path" ]; then continue; fi
431 test_or_die "running update on: $path"
437 restore_terminal_title
440 # provides a list of absolute paths of revision control directories
441 # that are located under the directory passed as the first parameter.
442 function generate_rev_ctrl_filelist()
444 local dir="$1"; shift
445 pushd "$dir" &>/dev/null
446 local dirhere="$( \cd "$(\dirname "$dir")" && /bin/pwd )"
447 local tempfile=$(mktemp /tmp/zz_checkins.XXXXXX)
449 local additional_filter
450 find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".svn" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
451 find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".git" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
452 # CVS is not well behaved like git and (now) svn, and we seldom use it anymore.
455 # see if they've warned us not to try checking in within vendor hierarchies.
456 if [ ! -z "NO_CHECKIN_VENDOR" ]; then
457 sed -i -e '/.*\/vendor\/.*/d' "$tempfile"
460 local sortfile=$(mktemp /tmp/zz_checkin_sort.XXXXXX)
461 sort <"$tempfile" >"$sortfile"
466 # iterates across a list of directories contained in a file (first parameter).
467 # on each directory name, it performs the action (second parameter) provided.
468 function perform_revctrl_action_on_file()
470 local tempfile="$1"; shift
471 local action="$1"; shift
477 while read -u 3 dirname; do
478 if [ -z "$dirname" ]; then
479 # we often have blank lines in the input file for some reason.
483 pushd "$dirname" &>/dev/null
485 # pass the current directory plus the remaining parameters from function invocation.
487 test_or_die "performing action $action on: $(pwd)"
492 if [ -z "$did_anything" ]; then
493 echo "There was nothing to do the action '$action' on."
496 restore_terminal_title