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 # check git version to see if we can use autostash.
15 # this appears to be an ubuntu issue, where xenial did not provide it even though the
16 # feature appeared in git 2.6 and xenial claims it has git version 2.7.4. eventually,
17 # this version test can go away.
18 gitvertest="$(git version | sed -e 's/git version [0-9]\.//' | sed -e 's/\.[0-9][0-9]*$//' )"
19 if (( $gitvertest >= 11 )); then
20 # auto-stash is not available until 2.6 for git, but ubuntu is misreporting or using a
21 # differing version number somehow. we are sure autostash was missing on ubuntu xenial
22 # with git 2.7.4 and it's definitely present in zesty with git at 2.11.
23 # PULL_ADDITION='--rebase --autostash'
24 #although initially attractive, above set of flags seems to lose history we don't want to
31 # the maximum depth that the recursive functions will try to go below the starting directory.
34 # use our splitter tool for lengthy output if it's available.
35 if [ ! -z "$(which splitter 2>/dev/null)" ]; then
36 TO_SPLITTER="$(which splitter)"
37 # calculate the number of columsn in the terminal.
39 TO_SPLITTER+=" --maxcol $(($cols - 1))"
46 #hmmm: move this to core
47 # this makes the status of pipe N into the main return value.
48 function promote_pipe_return()
50 ( exit ${PIPESTATUS[$1]} )
55 # one unpleasantry to take care of first; cygwin barfs aggressively if the TMP directory
56 # is a DOS path, but we need it to be a DOS path for our GFFS testing, so that blows.
57 # to get past this, TMP gets changed below to a hopefully generic and safe place.
58 if [[ "$TMP" =~ .:.* ]]; then
59 echo "making weirdo temporary directory for PCDOS-style path."
60 export TMP=/tmp/rev_control_$USER
62 if [ ! -d "$TMP" ]; then
65 if [ ! -d "$TMP" ]; then
66 echo "could not create the temporary directory TMP in: $TMP"
67 echo "this script will not work properly without an existing TMP directory."
72 # checks the directory provided into the revision control system repository it belongs to.
75 local directory="$1"; shift
79 # make a nice echoer since we want to use it inside conditions below.
80 local nicedir="$directory"
81 if [ $nicedir == "." ]; then
84 local blatt="echo -n checking in '$nicedir'... "
86 do_update "$directory"
87 test_or_die "repository update--this should be fixed before check-in."
89 pushd "$directory" &>/dev/null
90 if [ -f ".no-checkin" ]; then
91 echo "skipping check-in due to presence of .no-checkin sentinel file."
92 elif [ -d "CVS" ]; then
93 if test_writeable "CVS"; then
96 test_or_die "cvs checkin"
98 elif [ -d ".svn" ]; then
99 if test_writeable ".svn"; then
102 test_or_die "svn checkin"
104 elif [ -d ".git" ]; then
105 if test_writeable ".git"; then
108 # put all changed and new files in the commit. not to everyone's liking.
109 git add --all . | $TO_SPLITTER
110 promote_pipe_return 0
111 test_or_die "git add all new files"
113 # see if there are any changes in the local repository.
114 if ! git diff-index --quiet HEAD --; then
115 # tell git about all the files and get a check-in comment.
116 #hmmm: begins to look like, you guessed it, a reusable bit that all commit actions could enjoy.
119 test_or_continue "git commit"
120 if [ $retval -ne 0 ]; then
121 echo -e -n "Commit failed or was aborted:\nShould we continue with other check-ins? [y/N] "
124 if [[ "${line:0:1}" != "y" ]]; then
125 echo "Stopping check-in process due to missing commit and user request."
131 # a new set of steps we have to take to make sure the branch integrity is good.
132 do_careful_git_update "$(\pwd)"
134 # we continue on to the push, even if there were no changes this time, because
135 # there could already be committed changes that haven't been pushed yet.
137 # upload any changes to the upstream repo so others can see them.
138 git push --tags origin "$(my_branch_name)" 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
139 promote_pipe_return 0
140 test_or_die "git push"
144 # nothing there. it's not an error though.
145 echo no repository in $directory
149 restore_terminal_title
154 # shows the local changes in a repository.
157 local directory="$1"; shift
161 pushd "$directory" &>/dev/null
163 # only update if we see a repository living there.
164 if [ -d ".svn" ]; then
166 test_or_die "subversion diff"
167 elif [ -d ".git" ]; then
169 test_or_die "git diff"
170 elif [ -d "CVS" ]; then
172 test_or_die "cvs diff"
177 restore_terminal_title
182 # reports any files that are not already known to the upstream repository.
183 function do_report_new
185 local directory="$1"; shift
189 pushd "$directory" &>/dev/null
191 # only update if we see a repository living there.
192 if [ -f ".no-checkin" ]; then
193 echo "skipping reporting due to presence of .no-checkin sentinel file."
194 elif [ -d ".svn" ]; then
195 # this action so far only makes sense and is needed for svn.
196 bash $FEISTY_MEOW_SCRIPTS/rev_control/svnapply.sh \? echo
197 test_or_die "svn diff"
198 elif [ -d ".git" ]; then
200 test_or_die "git status -u"
205 restore_terminal_title
210 # checks in all the folders in the specified list.
211 function checkin_list()
213 # make the list of directories unique.
214 local list="$(uniquify $*)"
218 # turn repo list back into an array.
219 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
223 for outer in "${repository_list[@]}"; do
224 # check the repository first, since it might be an absolute path.
225 if [[ $outer =~ /.* ]]; then
226 # yep, this path is absolute. just handle it directly.
227 if [ ! -d "$outer" ]; then continue; fi
229 test_or_die "running check-in (absolute) on path: $outer"
232 for inner in $list; do
233 # add in the directory component to see if we can find the folder.
234 local path="$inner/$outer"
235 if [ ! -d "$path" ]; then continue; fi
237 test_or_die "running check-in (relative) on path: $path"
243 restore_terminal_title
246 # does a careful git update on all the folders in the specified list.
247 function puff_out_list()
249 # make the list of directories unique.
250 local list="$(uniquify $*)"
254 # turn repo list back into an array.
255 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
259 #hmmm: once again, seeing some reusable code in this loop...
260 for outer in "${repository_list[@]}"; do
261 # check the repository first, since it might be an absolute path.
262 if [[ $outer =~ /.* ]]; then
263 # yep, this path is absolute. just handle it directly.
264 if [ ! -d "$outer" ]; then continue; fi
265 do_careful_git_update "$outer"
266 test_or_die "running puff-out (absolute) on path: $outer"
269 for inner in $list; do
270 # add in the directory component to see if we can find the folder.
271 local path="$inner/$outer"
272 if [ ! -d "$path" ]; then continue; fi
273 do_careful_git_update "$path"
274 test_or_die "running puff-out (relative) on path: $path"
280 restore_terminal_title
284 ### takes out the first few carriage returns that are in the input.
285 ##function squash_first_few_crs()
288 ##while read input_text; do
290 ##if [ $i -le 5 ]; then
291 ##echo -n "$input_text "
296 ##if [ $i -le 3 ]; then
297 ### if we're still squashing eols, make sure we don't leave them hanging.
302 #hmmm: the below are git specific and should be named that way.
304 function all_branch_names()
306 echo "$(git branch -vv | cut -d ' ' -f2)"
309 # a helpful method that reports the git branch for the current directory's
311 function my_branch_name()
313 echo "$(git branch -vv | grep '\*' | cut -d ' ' -f2)"
316 #this had a -> in it at one point for not matching, didn't it?
317 # this reports the upstream branch for the current repo.
318 ##function parent_branch_name()
320 ##echo "$(git branch -vv | grep \* | cut -d ' ' -f2)"
323 # reports the status of the branch by echoing one of these values:
324 # okay: up to date and everything is good.
325 # needs_pull: this branch needs to be pulled from origins.
326 # needs_push: there are unsaved changes on this branch to push to remote store.
327 # diverged: the branches diverged and are going to need a merge.
328 # reference: https://stackoverflow.com/questions/3258243/check-if-pull-needed-in-git
329 function check_branch_state()
331 local branch="$1"; shift
333 if [ -z "$branch" ]; then
334 echo "No branch was passed to check branch state."
338 local to_return=120 # unknown issue.
340 local local_branch=$(git rev-parse @)
341 local remote_branch=$(git rev-parse "$branch")
342 local merge_base=$(git merge-base @ "$branch")
345 if [ "$local_branch" == "$remote_branch" ]; then
347 elif [ "$local_branch" == "$merge_base" ]; then
349 elif [ "$remote_branch" == "$merge_base" ]; then
360 # only shows the branch state if it's not okay.
361 # note that this is not the same as a conditional branch (ha ha).
362 function show_branch_conditionally()
364 local this_branch="$1"; shift
366 local state=$(check_branch_state "$this_branch")
367 if [ "$state" != "okay" ]; then
368 echo "=> branch '$this_branch' state is not clean: $state"
372 # the git update process just gets more and more complex when you bring in
373 # branches, so we've moved this here to avoid having a ton of code in the
375 function do_careful_git_update()
377 local directory="$1"; shift
378 pushd "$directory" &>/dev/null
379 test_or_die "changing to directory: $directory"
381 if [ ! -d ".git" ]; then
382 # we ignore if they're jumping into a non-useful folder, but also tell them.
383 echo "Directory is not a git repository: $directory"
387 local this_branch="$(my_branch_name)"
389 show_branch_conditionally "$this_branch"
391 # first update all our remote branches to their current state from the repos.
392 git remote update | $TO_SPLITTER
393 promote_pipe_return 0
394 test_or_die "git remote update"
396 show_branch_conditionally "$this_branch"
398 # this code is now doing what i have to do when i repair the repo. and it seems to be good so far.
399 # note that we allow the local branch to be merged with its remote counterpart; otherwise we would
400 # miss changes that happened elsewhere which should be seen in our local copy.
401 local branch_list=$(all_branch_names)
403 for bran in $branch_list; do
404 # echo "synchronizing remote branch: $bran"
405 git checkout "$bran" | $TO_SPLITTER
406 promote_pipe_return 0
407 test_or_die "git switching checkout to remote branch: $bran"
409 show_branch_conditionally "$this_branch"
411 remote_branch_info=$(git ls-remote --heads origin $bran 2>/dev/null)
412 if [ ! -z "$remote_branch_info" ]; then
413 # we are pretty sure the remote branch does exist.
414 git pull $PULL_ADDITION origin "$bran" | $TO_SPLITTER
415 # we may want to choose to do fast forward, to avoid crazy multiple merge histories
416 # without any changes in them. --no-ff
417 promote_pipe_return 0
419 test_or_die "git pull of remote branch: $bran"
421 # now switch back to our branch.
422 git checkout "$this_branch" | $TO_SPLITTER
423 promote_pipe_return 0
424 test_or_die "git checking out our current branch: $this_branch"
426 # now pull down any changes in our own origin in the repo, to stay in synch
427 # with any changes from others.
428 git pull $PULL_ADDITION --all | $TO_SPLITTER
429 #is the above really important when we did this branch already in the loop?
430 #it does an --all, but is that effective or different? should we be doing that in above loop?
432 promote_pipe_return 0
433 test_or_die "git pulling all upstream"
438 # gets the latest versions of the assets from the upstream repository.
441 directory="$1"; shift
445 # make a nice echoer since we want to use it inside conditions below.
446 local nicedir="$directory"
447 if [ $nicedir == "." ]; then
450 local blatt="echo retrieving '$nicedir'..."
452 pushd "$directory" &>/dev/null
453 if [ -d "CVS" ]; then
454 if test_writeable "CVS"; then
456 cvs update . | $TO_SPLITTER
457 promote_pipe_return 0
458 test_or_die "cvs update"
460 elif [ -d ".svn" ]; then
461 if test_writeable ".svn"; then
463 svn update . | $TO_SPLITTER
464 promote_pipe_return 0
465 test_or_die "svn update"
467 elif [ -d ".git" ]; then
468 if test_writeable ".git"; then
470 git pull $PULL_ADDITION 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
471 #ordinary pulls should be allowed to do fast forward: --no-ff
472 promote_pipe_return 0
473 test_or_die "git pull of origin"
476 # this is not an error necessarily; we'll just pretend they planned this.
477 echo no repository in $directory
481 restore_terminal_title
486 # gets all the updates for a list of folders under revision control.
487 function checkout_list()
489 local list="$(uniquify $*)"
493 # turn repo list back into an array.
494 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
498 for outer in "${repository_list[@]}"; do
499 # check the repository first, since it might be an absolute path.
500 if [[ $outer =~ /.* ]]; then
501 # yep, this path is absolute. just handle it directly.
502 if [ ! -d "$outer" ]; then continue; fi
504 test_or_die "running update on: $path"
507 for inner in $list; do
508 # add in the directory component to see if we can find the folder.
509 local path="$inner/$outer"
510 if [ ! -d "$path" ]; then continue; fi
512 test_or_die "running update on: $path"
518 restore_terminal_title
521 # provides a list of absolute paths of revision control directories
522 # that are located under the directory passed as the first parameter.
523 function generate_rev_ctrl_filelist()
525 local dir="$1"; shift
526 pushd "$dir" &>/dev/null
527 local dirhere="$( \cd "$(\dirname "$dir")" && /bin/pwd )"
528 local tempfile=$(mktemp /tmp/zz_checkins.XXXXXX)
530 local additional_filter
531 find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".svn" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
532 find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".git" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
533 # CVS is not well behaved like git and (now) svn, and we seldom use it anymore.
536 # see if they've warned us not to try checking in within vendor hierarchies.
537 if [ ! -z "NO_CHECKIN_VENDOR" ]; then
538 sed -i -e '/.*\/vendor\/.*/d' "$tempfile"
541 local sortfile=$(mktemp /tmp/zz_checkin_sort.XXXXXX)
542 sort <"$tempfile" >"$sortfile"
547 # iterates across a list of directories contained in a file (first parameter).
548 # on each directory name, it performs the action (second parameter) provided.
549 function perform_revctrl_action_on_file()
551 local tempfile="$1"; shift
552 local action="$1"; shift
558 while read -u 3 dirname; do
559 if [ -z "$dirname" ]; then
560 # we often have blank lines in the input file for some reason.
564 pushd "$dirname" &>/dev/null
566 # pass the current directory plus the remaining parameters from function invocation.
568 test_or_die "performing action $action on: $(pwd)"
573 if [ -z "$did_anything" ]; then
574 echo "There was nothing to do the action '$action' on."
577 restore_terminal_title