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'
28 # the maximum depth that the recursive functions will try to go below the starting directory.
31 # use our splitter tool for lengthy output if it's available.
32 if [ ! -z "$(which splitter 2>/dev/null)" ]; then
33 TO_SPLITTER="$(which splitter)"
34 # calculate the number of columsn in the terminal.
36 TO_SPLITTER+=" --maxcol $(($cols - 1))"
43 #hmmm: move this to core
44 # this makes the status of pipe N into the main return value.
45 function promote_pipe_return()
47 ( exit ${PIPESTATUS[$1]} )
52 # one unpleasantry to take care of first; cygwin barfs aggressively if the TMP directory
53 # is a DOS path, but we need it to be a DOS path for our GFFS testing, so that blows.
54 # to get past this, TMP gets changed below to a hopefully generic and safe place.
55 if [[ "$TMP" =~ .:.* ]]; then
56 echo "making weirdo temporary directory for PCDOS-style path."
57 export TMP=/tmp/rev_control_$USER
59 if [ ! -d "$TMP" ]; then
62 if [ ! -d "$TMP" ]; then
63 echo "could not create the temporary directory TMP in: $TMP"
64 echo "this script will not work properly without an existing TMP directory."
69 # checks the directory provided into the revision control system repository it belongs to.
72 local directory="$1"; shift
76 # make a nice echoer since we want to use it inside conditions below.
77 local nicedir="$directory"
78 if [ $nicedir == "." ]; then
81 local blatt="echo -n checking in '$nicedir'... "
83 do_update "$directory"
84 test_or_die "repository update--this should be fixed before check-in."
86 pushd "$directory" &>/dev/null
87 if [ -f ".no-checkin" ]; then
88 echo "skipping check-in due to presence of .no-checkin sentinel file."
89 elif [ -d "CVS" ]; then
90 if test_writeable "CVS"; then
93 test_or_die "cvs checkin"
95 elif [ -d ".svn" ]; then
96 if test_writeable ".svn"; then
99 test_or_die "svn checkin"
101 elif [ -d ".git" ]; then
102 if test_writeable ".git"; then
105 # put all changed and new files in the commit. not to everyone's liking.
106 git add --all . | $TO_SPLITTER
107 promote_pipe_return 0
108 test_or_die "git add all new files"
110 # see if there are any changes in the local repository.
111 if ! git diff-index --quiet HEAD --; then
112 # tell git about all the files and get a check-in comment.
113 #hmmm: begins to look like, you guessed it, a reusable bit that all commit actions could enjoy.
116 test_or_continue "git commit"
117 if [ $retval -ne 0 ]; then
118 echo -e -n "Commit failed or was aborted:\nShould we continue with other check-ins? [y/N] "
121 if [[ "${line:0:1}" != "y" ]]; then
122 echo "Stopping check-in process due to missing commit and user request."
128 # a new set of steps we have to take to make sure the branch integrity is good.
129 do_careful_git_update "$(\pwd)"
131 # we continue on to the push, even if there were no changes this time, because
132 # there could already be committed changes that haven't been pushed yet.
134 # upload any changes to the upstream repo so others can see them.
135 git push --tags origin "$(my_branch_name)" 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
136 promote_pipe_return 0
137 test_or_die "git push"
141 # nothing there. it's not an error though.
142 echo no repository in $directory
146 restore_terminal_title
151 # shows the local changes in a repository.
154 local directory="$1"; shift
158 pushd "$directory" &>/dev/null
160 # only update if we see a repository living there.
161 if [ -d ".svn" ]; then
163 test_or_die "subversion diff"
164 elif [ -d ".git" ]; then
166 test_or_die "git diff"
167 elif [ -d "CVS" ]; then
169 test_or_die "cvs diff"
174 restore_terminal_title
179 # reports any files that are not already known to the upstream repository.
180 function do_report_new
182 local directory="$1"; shift
186 pushd "$directory" &>/dev/null
188 # only update if we see a repository living there.
189 if [ -f ".no-checkin" ]; then
190 echo "skipping reporting due to presence of .no-checkin sentinel file."
191 elif [ -d ".svn" ]; then
192 # this action so far only makes sense and is needed for svn.
193 bash $FEISTY_MEOW_SCRIPTS/rev_control/svnapply.sh \? echo
194 test_or_die "svn diff"
195 elif [ -d ".git" ]; then
197 test_or_die "git status -u"
202 restore_terminal_title
207 # checks in all the folders in the specified list.
208 function checkin_list()
210 # make the list of directories unique.
211 local list="$(uniquify $*)"
215 # turn repo list back into an array.
216 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
220 for outer in "${repository_list[@]}"; do
221 # check the repository first, since it might be an absolute path.
222 if [[ $outer =~ /.* ]]; then
223 # yep, this path is absolute. just handle it directly.
224 if [ ! -d "$outer" ]; then continue; fi
226 test_or_die "running check-in (absolute) on path: $outer"
229 for inner in $list; do
230 # add in the directory component to see if we can find the folder.
231 local path="$inner/$outer"
232 if [ ! -d "$path" ]; then continue; fi
234 test_or_die "running check-in (relative) on path: $path"
240 restore_terminal_title
243 # does a careful git update on all the folders in the specified list.
244 function puff_out_list()
246 # make the list of directories unique.
247 local list="$(uniquify $*)"
251 # turn repo list back into an array.
252 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
256 #hmmm: once again, seeing some reusable code in this loop...
257 for outer in "${repository_list[@]}"; do
258 # check the repository first, since it might be an absolute path.
259 if [[ $outer =~ /.* ]]; then
260 # yep, this path is absolute. just handle it directly.
261 if [ ! -d "$outer" ]; then continue; fi
262 do_careful_git_update "$outer"
263 test_or_die "running puff-out (absolute) on path: $outer"
266 for inner in $list; do
267 # add in the directory component to see if we can find the folder.
268 local path="$inner/$outer"
269 if [ ! -d "$path" ]; then continue; fi
270 do_careful_git_update "$path"
271 test_or_die "running puff-out (relative) on path: $path"
277 restore_terminal_title
281 ### takes out the first few carriage returns that are in the input.
282 ##function squash_first_few_crs()
285 ##while read input_text; do
287 ##if [ $i -le 5 ]; then
288 ##echo -n "$input_text "
293 ##if [ $i -le 3 ]; then
294 ### if we're still squashing eols, make sure we don't leave them hanging.
299 #hmmm: the below are git specific and should be named that way.
301 function all_branch_names()
303 echo "$(git branch -vv | cut -d ' ' -f2)"
306 # a helpful method that reports the git branch for the current directory's
308 function my_branch_name()
310 echo "$(git branch -vv | grep '\*' | cut -d ' ' -f2)"
313 #this had a -> in it at one point for not matching, didn't it?
314 # this reports the upstream branch for the current repo.
315 ##function parent_branch_name()
317 ##echo "$(git branch -vv | grep \* | cut -d ' ' -f2)"
320 # reports the status of the branch by echoing one of these values:
321 # okay: up to date and everything is good.
322 # needs_pull: this branch needs to be pulled from origins.
323 # needs_push: there are unsaved changes on this branch to push to remote store.
324 # diverged: the branches diverged and are going to need a merge.
325 # reference: https://stackoverflow.com/questions/3258243/check-if-pull-needed-in-git
326 function check_branch_state()
328 local branch="$1"; shift
330 if [ -z "$branch" ]; then
331 echo "No branch was passed to check branch state."
335 local to_return=120 # unknown issue.
337 local local_branch=$(git rev-parse @)
338 local remote_branch=$(git rev-parse "$branch")
339 local merge_base=$(git merge-base @ "$branch")
342 if [ "$local_branch" == "$remote_branch" ]; then
344 elif [ "$local_branch" == "$merge_base" ]; then
346 elif [ "$remote_branch" == "$merge_base" ]; then
357 # only shows the branch state if it's not okay.
358 # note that this is not the same as a conditional branch (ha ha).
359 function show_branch_conditionally()
361 local this_branch="$1"; shift
363 local state=$(check_branch_state "$this_branch")
364 if [ "$state" != "okay" ]; then
365 echo "=> branch '$this_branch' state is not clean: $state"
369 # the git update process just gets more and more complex when you bring in
370 # branches, so we've moved this here to avoid having a ton of code in the
372 function do_careful_git_update()
374 local directory="$1"; shift
375 pushd "$directory" &>/dev/null
376 test_or_die "changing to directory: $directory"
378 if [ ! -d ".git" ]; then
379 # we ignore if they're jumping into a non-useful folder, but also tell them.
380 echo "Directory is not a git repository: $directory"
384 local this_branch="$(my_branch_name)"
386 show_branch_conditionally "$this_branch"
388 # first update all our remote branches to their current state from the repos.
389 git remote update | $TO_SPLITTER
390 promote_pipe_return 0
391 test_or_die "git remote update"
393 show_branch_conditionally "$this_branch"
395 # this code is now doing what i have to do when i repair the repo. and it seems to be good so far.
396 # note that we allow the local branch to be merged with its remote counterpart; otherwise we would
397 # miss changes that happened elsewhere which should be seen in our local copy.
398 local branch_list=$(all_branch_names)
400 for bran in $branch_list; do
401 # echo "synchronizing remote branch: $bran"
402 git checkout "$bran" | $TO_SPLITTER
403 promote_pipe_return 0
404 test_or_die "git switching checkout to remote branch: $bran"
406 show_branch_conditionally "$this_branch"
408 remote_branch_info=$(git ls-remote --heads origin $bran 2>/dev/null)
409 if [ ! -z "$remote_branch_info" ]; then
410 # we are pretty sure the remote branch does exist.
411 git pull $PULL_ADDITION origin "$bran" | $TO_SPLITTER
412 # we may want to choose to do fast forward, to avoid crazy multiple merge histories
413 # without any changes in them. --no-ff
414 promote_pipe_return 0
416 test_or_die "git pull of remote branch: $bran"
418 # now switch back to our branch.
419 git checkout "$this_branch" | $TO_SPLITTER
420 promote_pipe_return 0
421 test_or_die "git checking out our current branch: $this_branch"
423 # now pull down any changes in our own origin in the repo, to stay in synch
424 # with any changes from others.
425 git pull $PULL_ADDITION --all | $TO_SPLITTER
426 #is the above really important when we did this branch already in the loop?
427 #it does an --all, but is that effective or different? should we be doing that in above loop?
429 promote_pipe_return 0
430 test_or_die "git pulling all upstream"
435 # gets the latest versions of the assets from the upstream repository.
438 directory="$1"; shift
442 # make a nice echoer since we want to use it inside conditions below.
443 local nicedir="$directory"
444 if [ $nicedir == "." ]; then
447 local blatt="echo retrieving '$nicedir'..."
449 pushd "$directory" &>/dev/null
450 if [ -d "CVS" ]; then
451 if test_writeable "CVS"; then
453 cvs update . | $TO_SPLITTER
454 promote_pipe_return 0
455 test_or_die "cvs update"
457 elif [ -d ".svn" ]; then
458 if test_writeable ".svn"; then
460 svn update . | $TO_SPLITTER
461 promote_pipe_return 0
462 test_or_die "svn update"
464 elif [ -d ".git" ]; then
465 if test_writeable ".git"; then
467 git pull $PULL_ADDITION 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
468 #ordinary pulls should be allowed to do fast forward: --no-ff
469 promote_pipe_return 0
470 test_or_die "git pull of origin"
473 # this is not an error necessarily; we'll just pretend they planned this.
474 echo no repository in $directory
478 restore_terminal_title
483 # gets all the updates for a list of folders under revision control.
484 function checkout_list()
486 local list="$(uniquify $*)"
490 # turn repo list back into an array.
491 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
495 for outer in "${repository_list[@]}"; do
496 # check the repository first, since it might be an absolute path.
497 if [[ $outer =~ /.* ]]; then
498 # yep, this path is absolute. just handle it directly.
499 if [ ! -d "$outer" ]; then continue; fi
501 test_or_die "running update on: $path"
504 for inner in $list; do
505 # add in the directory component to see if we can find the folder.
506 local path="$inner/$outer"
507 if [ ! -d "$path" ]; then continue; fi
509 test_or_die "running update on: $path"
515 restore_terminal_title
518 # provides a list of absolute paths of revision control directories
519 # that are located under the directory passed as the first parameter.
520 function generate_rev_ctrl_filelist()
522 local dir="$1"; shift
523 pushd "$dir" &>/dev/null
524 local dirhere="$( \cd "$(\dirname "$dir")" && /bin/pwd )"
525 local tempfile=$(mktemp /tmp/zz_checkins.XXXXXX)
527 local additional_filter
528 find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".svn" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
529 find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".git" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
530 # CVS is not well behaved like git and (now) svn, and we seldom use it anymore.
533 # see if they've warned us not to try checking in within vendor hierarchies.
534 if [ ! -z "NO_CHECKIN_VENDOR" ]; then
535 sed -i -e '/.*\/vendor\/.*/d' "$tempfile"
538 local sortfile=$(mktemp /tmp/zz_checkin_sort.XXXXXX)
539 sort <"$tempfile" >"$sortfile"
544 # iterates across a list of directories contained in a file (first parameter).
545 # on each directory name, it performs the action (second parameter) provided.
546 function perform_revctrl_action_on_file()
548 local tempfile="$1"; shift
549 local action="$1"; shift
555 while read -u 3 dirname; do
556 if [ -z "$dirname" ]; then
557 # we often have blank lines in the input file for some reason.
561 pushd "$dirname" &>/dev/null
563 # pass the current directory plus the remaining parameters from function invocation.
565 test_or_die "performing action $action on: $(pwd)"
570 if [ -z "$did_anything" ]; then
571 echo "There was nothing to do the action '$action' on."
574 restore_terminal_title