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 update on all the folders in the specified list;
247 # it looks in the REPOSITORY_LIST for those names and updates them.
248 # this is just like checkout_list, but it's for the puffing up action
249 # we need to do on git.
250 function puff_out_list()
252 # make the list of directories unique.
253 local list="$(uniquify $*)"
257 # turn repo list back into an array.
258 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
262 #hmmm: once again, seeing some reusable code in this loop...
263 for outer in "${repository_list[@]}"; do
264 # check the repository first, since it might be an absolute path.
265 if [[ $outer =~ /.* ]]; then
266 # yep, this path is absolute. just handle it directly.
267 if [ ! -d "$outer" ]; then continue; fi
268 do_careful_git_update "$outer"
269 test_or_die "running puff-out (absolute) on path: $outer"
272 for inner in $list; do
273 # add in the directory component to see if we can find the folder.
274 local path="$inner/$outer"
275 if [ ! -d "$path" ]; then continue; fi
276 do_careful_git_update "$path"
277 test_or_die "running puff-out (relative) on path: $path"
283 restore_terminal_title
287 ### takes out the first few carriage returns that are in the input.
288 ##function squash_first_few_crs()
291 ##while read input_text; do
293 ##if [ $i -le 5 ]; then
294 ##echo -n "$input_text "
299 ##if [ $i -le 3 ]; then
300 ### if we're still squashing eols, make sure we don't leave them hanging.
305 #hmmm: the below are git specific and should be named that way.
307 function all_branch_names()
309 echo "$(git branch -vv | cut -d ' ' -f2)"
312 # a helpful method that reports the git branch for the current directory's
314 function my_branch_name()
316 echo "$(git branch -vv | grep '\*' | cut -d ' ' -f2)"
319 #this had a -> in it at one point for not matching, didn't it?
320 # this reports the upstream branch for the current repo.
321 ##function parent_branch_name()
323 ##echo "$(git branch -vv | grep \* | cut -d ' ' -f2)"
326 # reports the status of the branch by echoing one of these values:
327 # okay: up to date and everything is good.
328 # needs_pull: this branch needs to be pulled from origins.
329 # needs_push: there are unsaved changes on this branch to push to remote store.
330 # diverged: the branches diverged and are going to need a merge.
331 # reference: https://stackoverflow.com/questions/3258243/check-if-pull-needed-in-git
332 function check_branch_state()
334 local branch="$1"; shift
336 if [ -z "$branch" ]; then
337 echo "No branch was passed to check branch state."
341 local to_return=120 # unknown issue.
343 local local_branch=$(git rev-parse @)
344 local remote_branch=$(git rev-parse "$branch")
345 local merge_base=$(git merge-base @ "$branch")
348 if [ "$local_branch" == "$remote_branch" ]; then
350 elif [ "$local_branch" == "$merge_base" ]; then
352 elif [ "$remote_branch" == "$merge_base" ]; then
363 # only shows the branch state if it's not okay.
364 # note that this is not the same as a conditional branch (ha ha).
365 function show_branch_conditionally()
367 local this_branch="$1"; shift
369 local state=$(check_branch_state "$this_branch")
370 if [ "$state" != "okay" ]; then
371 echo "=> branch '$this_branch' state is not clean: $state"
375 # the git update process just gets more and more complex when you bring in
376 # branches, so we've moved this here to avoid having a ton of code in the
378 function do_careful_git_update()
380 local directory="$1"; shift
381 pushd "$directory" &>/dev/null
382 test_or_die "changing to directory: $directory"
384 if [ ! -d ".git" ]; then
386 # # we ignore if they're jumping into a non-useful folder, but also tell them.
387 # echo "Directory is not a git repository: $directory"
390 # new and better approach; just boil down to a getem action.
396 local this_branch="$(my_branch_name)"
398 show_branch_conditionally "$this_branch"
400 # first update all our remote branches to their current state from the repos.
401 git remote update | $TO_SPLITTER
402 promote_pipe_return 0
403 test_or_die "git remote update"
405 show_branch_conditionally "$this_branch"
407 # this code is now doing what i have to do when i repair the repo. and it seems to be good so far.
408 # note that we allow the local branch to be merged with its remote counterpart; otherwise we would
409 # miss changes that happened elsewhere which should be seen in our local copy.
410 local branch_list=$(all_branch_names)
412 for bran in $branch_list; do
413 # echo "synchronizing remote branch: $bran"
414 git checkout "$bran" | $TO_SPLITTER
415 promote_pipe_return 0
416 test_or_die "git switching checkout to remote branch: $bran"
418 show_branch_conditionally "$this_branch"
420 remote_branch_info=$(git ls-remote --heads origin $bran 2>/dev/null)
421 if [ ! -z "$remote_branch_info" ]; then
422 # we are pretty sure the remote branch does exist.
423 git pull --tags $PULL_ADDITION origin "$bran" | $TO_SPLITTER
424 # we may want to choose to do fast forward, to avoid crazy multiple merge histories
425 # without any changes in them. --no-ff
426 promote_pipe_return 0
428 test_or_die "git pull of remote branch: $bran"
430 # now switch back to our branch.
431 git checkout "$this_branch" | $TO_SPLITTER
432 promote_pipe_return 0
433 test_or_die "git checking out our current branch: $this_branch"
435 # now pull down any changes in our own origin in the repo, to stay in synch
436 # with any changes from others.
437 git pull --tags $PULL_ADDITION --all | $TO_SPLITTER
438 #is the above really important when we did this branch already in the loop?
439 #it does an --all, but is that effective or different? should we be doing that in above loop?
441 promote_pipe_return 0
442 test_or_die "git pulling all upstream"
447 # gets the latest versions of the assets from the upstream repository.
450 directory="$1"; shift
454 # make a nice echoer since we want to use it inside conditions below.
455 local nicedir="$directory"
456 if [ $nicedir == "." ]; then
459 local blatt="echo retrieving '$nicedir'..."
461 pushd "$directory" &>/dev/null
462 if [ -d "CVS" ]; then
463 if test_writeable "CVS"; then
465 cvs update . | $TO_SPLITTER
466 promote_pipe_return 0
467 test_or_die "cvs update"
469 elif [ -d ".svn" ]; then
470 if test_writeable ".svn"; then
472 svn update . | $TO_SPLITTER
473 promote_pipe_return 0
474 test_or_die "svn update"
476 elif [ -d ".git" ]; then
477 if test_writeable ".git"; then
479 git pull --tags $PULL_ADDITION 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
480 #ordinary pulls should be allowed to do fast forward: --no-ff
481 promote_pipe_return 0
482 test_or_die "git pull of origin"
485 # this is not an error necessarily; we'll just pretend they planned this.
486 echo no repository in $directory
490 restore_terminal_title
495 # gets all the updates for a list of folders under revision control.
496 function checkout_list()
498 local list="$(uniquify $*)"
502 # turn repo list back into an array.
503 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
507 for outer in "${repository_list[@]}"; do
508 # check the repository first, since it might be an absolute path.
509 if [[ $outer =~ /.* ]]; then
510 # yep, this path is absolute. just handle it directly.
511 if [ ! -d "$outer" ]; then continue; fi
513 test_or_die "running update on: $path"
516 for inner in $list; do
517 # add in the directory component to see if we can find the folder.
518 local path="$inner/$outer"
519 if [ ! -d "$path" ]; then continue; fi
521 test_or_die "running update on: $path"
527 restore_terminal_title
530 # provides a list of absolute paths of revision control directories
531 # that are located under the directory passed as the first parameter.
532 function generate_rev_ctrl_filelist()
534 local dir="$1"; shift
535 pushd "$dir" &>/dev/null
536 local dirhere="$( \cd "$(\dirname "$dir")" && /bin/pwd )"
537 local tempfile=$(mktemp /tmp/zz_checkins.XXXXXX)
539 local additional_filter
540 find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".svn" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
541 find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".git" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
542 # CVS is not well behaved like git and (now) svn, and we seldom use it anymore.
545 # see if they've warned us not to try checking in within vendor hierarchies.
546 if [ ! -z "NO_CHECKIN_VENDOR" ]; then
547 sed -i -e '/.*\/vendor\/.*/d' "$tempfile"
550 local sortfile=$(mktemp /tmp/zz_checkin_sort.XXXXXX)
551 sort <"$tempfile" >"$sortfile"
556 # iterates across a list of directories contained in a file (first parameter).
557 # on each directory name, it performs the action (second parameter) provided.
558 function perform_revctrl_action_on_file()
560 local tempfile="$1"; shift
561 local action="$1"; shift
567 while read -u 3 dirname; do
568 if [ -z "$dirname" ]; then
569 # we often have blank lines in the input file for some reason.
573 pushd "$dirname" &>/dev/null
575 # pass the current directory plus the remaining parameters from function invocation.
577 test_or_die "performing action $action on: $(pwd)"
582 if [ -z "$did_anything" ]; then
583 echo "There was nothing to do the action '$action' on."
586 restore_terminal_title