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 log_feisty_meow_event "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."
69 #hmmm: odd approach to solving the "sourced scripts shouldn't exit or they take down the
70 # original caller too" issue.
71 echo "hit ctrl-c to stay in this shell now, otherwise it may exit in 10 seconds..."
78 # checks the directory provided into the revision control system repository it belongs to.
79 function do_revctrl_checkin()
81 local directory="$1"; shift
83 # make a nice echoer since we want to use it inside conditions below.
84 local nicedir="$directory"
85 if [ $nicedir == "." ]; then
88 local blatt="echo -ne \nchecking in '$nicedir'... "
90 #hmmm: we don't do a puff out on this directory to carefully update the git repo. is there a good reason why?
91 # we know we at least have to get the remote version synched up to avoid severe tire damage, so it's sensible at
92 # least to call do_revctrl_simple_update, but is there a reason not to call the careful git update instead?
93 #AHA, below we do call a careful git update, which is at least partially redundant with calling do_revctrl_simple_update here.
94 # how about pushing the do_revctrl_simple_update down into the two cases that use it and just not calling it for the git case?
95 #hmmm: trying the better seeming approach below now.
97 pushd "$directory" &>/dev/null
98 if [ -f ".no-checkin" ]; then
99 echo "skipping check-in due to presence of .no-checkin sentinel file."
100 elif [ -d "CVS" ]; then
101 if test_writeable "CVS"; then
102 do_revctrl_simple_update "$directory"
103 exit_on_error "updating repository; this issue should be fixed before check-in."
106 exit_on_error "cvs checkin"
108 elif [ -d ".svn" ]; then
109 if test_writeable ".svn"; then
110 do_revctrl_simple_update "$directory"
111 exit_on_error "updating repository; this issue should be fixed before check-in."
114 exit_on_error "svn checkin"
116 elif [ -d ".git" ]; then
117 if test_writeable ".git"; then
119 #hmmm: trying this in front; i have a bad feeling we used to do it like this and there were problems from not committing first!
120 # a new set of steps we have to take to make sure the branch integrity is good.
121 do_revctrl_careful_update "$(\pwd)"
125 # put all changed and new files in the commit. not to everyone's liking.
126 git add --all . | $TO_SPLITTER
127 promote_pipe_return 0
128 exit_on_error "git add all new files"
130 #hmmm: was there a reason to commit before doing the puffing out careful git update? i seem to think there was.
131 # would be nice to remember that or recapture the reason, so we can be sure we're not committing too early.
132 # see if there are any changes in the local repository.
133 if ! git diff-index --quiet HEAD --; then
134 # tell git about all the files and get a check-in comment.
135 #hmmm: begins to look like, you guessed it, a reusable bit that all commit actions could enjoy.
138 continue_on_error "git commit"
139 if [ $retval -ne 0 ]; then
140 echo -e -n "Commit failed or was aborted:\nShould we continue with other check-ins? [y/N] "
143 if [[ "${line:0:1}" != "y" ]]; then
144 echo "Stopping check-in process due to missing commit and user request."
150 # a new set of steps we have to take to make sure the branch integrity is good.
151 #hold do_revctrl_careful_update "$(\pwd)"
153 # we continue on to the push, even if there were no changes this time, because
154 # there could already be committed changes that haven't been pushed yet.
156 # upload any changes to the upstream repo so others can see them.
157 git push --tags origin "$(my_branch_name)" 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
158 promote_pipe_return 0
159 exit_on_error "git push"
163 # nothing there. it's not an error though.
164 log_feisty_meow_event "no repository in $directory"
171 # shows the local changes in a repository.
172 function do_revctrl_diff
174 local directory="$1"; shift
176 pushd "$directory" &>/dev/null
178 # only update if we see a repository living there.
179 if [ -d ".svn" ]; then
181 exit_on_error "subversion diff"
182 elif [ -d ".git" ]; then
184 exit_on_error "git diff"
185 elif [ -d "CVS" ]; then
187 exit_on_error "cvs diff"
195 # reports any files that are not already known to the upstream repository.
196 function do_revctrl_report_new
198 local directory="$1"; shift
200 pushd "$directory" &>/dev/null
202 # only update if we see a repository living there.
203 if [ -f ".no-checkin" ]; then
204 echo "skipping reporting due to presence of .no-checkin sentinel file."
205 elif [ -d ".svn" ]; then
206 # this action so far only makes sense and is needed for svn.
207 bash $FEISTY_MEOW_SCRIPTS/rev_control/svnapply.sh \? echo
208 exit_on_error "svn diff"
209 elif [ -d ".git" ]; then
211 exit_on_error "git status -u"
219 # checks in all the folders in the specified list.
220 function checkin_list()
222 # make the list of directories unique.
223 local list="$(uniquify $*)"
225 # turn repo list back into an array.
226 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
230 for outer in "${repository_list[@]}"; do
231 # check the repository first, since it might be an absolute path.
232 if [[ $outer =~ /.* ]]; then
233 # yep, this path is absolute. just handle it directly.
234 if [ ! -d "$outer" ]; then continue; fi
235 do_revctrl_checkin "$outer"
236 exit_on_error "running check-in (absolute) on path: $outer"
238 for inner in $list; do
239 # add in the directory component to see if we can find the folder.
240 local path="$inner/$outer"
241 if [ ! -d "$path" ]; then continue; fi
242 do_revctrl_checkin "$path"
243 exit_on_error "running check-in (relative) on path: $path"
249 #hmmm: below functions are git specific and should be named that way.
251 function all_branch_names()
253 echo "$(git branch -vv | cut -d ' ' -f2)"
256 # a helpful method that reports the git branch for the current directory's
258 function my_branch_name()
260 echo "$(git branch -vv | grep '\*' | cut -d ' ' -f2)"
263 #this had a -> in it at one point for not matching, didn't it?
264 # this reports the upstream branch for the current repo.
265 ##function parent_branch_name()
267 ##echo "$(git branch -vv | grep \* | cut -d ' ' -f2)"
270 # reports the status of the branch by echoing one of these values:
271 # okay: up to date and everything is good.
272 # needs_pull: this branch needs to be pulled from origins.
273 # needs_push: there are unsaved changes on this branch to push to remote store.
274 # diverged: the branches diverged and are going to need a merge.
275 # reference: https://stackoverflow.com/questions/3258243/check-if-pull-needed-in-git
276 function check_branch_state()
278 local branch="$1"; shift
280 if [ -z "$branch" ]; then
281 echo "No branch was passed to check branch state."
285 local to_return=120 # unknown issue.
287 local local_branch=$(git rev-parse @)
288 local remote_branch=$(git rev-parse "$branch")
289 local merge_base=$(git merge-base @ "$branch")
292 if [ "$local_branch" == "$remote_branch" ]; then
294 elif [ "$local_branch" == "$merge_base" ]; then
296 elif [ "$remote_branch" == "$merge_base" ]; then
307 # only shows the branch state if it's not okay.
308 # note that this is not the same as a conditional branch (ha ha).
309 function show_branch_conditionally()
311 local this_branch="$1"; shift
313 local state=$(check_branch_state "$this_branch")
314 if [ "$state" != "okay" ]; then
315 echo "=> branch '$this_branch' state is not clean: $state"
319 # the git update process just gets more and more complex when you bring in
320 # branches, so we've moved this here to avoid having a ton of code in the
322 function do_revctrl_careful_update()
324 local directory="$1"; shift
325 pushd "$directory" &>/dev/null
326 exit_on_error "changing to directory: $directory"
328 if [ ! -d ".git" ]; then
329 # not a git project, so just boil this down to a getem action.
331 log_feisty_meow_event "skipping careful part and doing simple update on non-git repository: $directory"
332 do_revctrl_simple_update "$directory"
336 #hmmm: another piece of reusable code, to process the directory for printing.
337 # make a nice echoer since we want to use it inside conditions below.
338 local nicedir="$directory"
339 if [ $nicedir == "." ]; then
342 local blatt="echo -e \ncarefully retrieving '$nicedir'..."
345 local this_branch="$(my_branch_name)"
347 show_branch_conditionally "$this_branch"
349 # first update all our remote branches to their current state from the repos.
350 git remote update | $TO_SPLITTER
351 promote_pipe_return 0
352 exit_on_error "git remote update"
354 show_branch_conditionally "$this_branch"
356 # this code is now doing what i have to do when i repair the repo. and it seems to be good so far.
357 # note that we allow the local branch to be merged with its remote counterpart; otherwise we would
358 # miss changes that happened elsewhere which should be seen in our local copy.
359 local branch_list=$(all_branch_names)
361 for bran in $branch_list; do
362 # echo "synchronizing remote branch: $bran"
363 git checkout "$bran" | $TO_SPLITTER
364 promote_pipe_return 0
365 exit_on_error "git switching checkout to remote branch: $bran"
367 show_branch_conditionally "$this_branch"
369 remote_branch_info=$(git ls-remote --heads origin $bran 2>/dev/null)
370 if [ ! -z "$remote_branch_info" ]; then
371 # we are pretty sure the remote branch does exist.
372 git pull --tags $PULL_ADDITION origin "$bran" | $TO_SPLITTER
373 # we may want to choose to do fast forward, to avoid crazy multiple merge histories
374 # without any changes in them. --no-ff
375 promote_pipe_return 0
377 exit_on_error "git pull of remote branch: $bran"
379 # now switch back to our branch.
380 git checkout "$this_branch" | $TO_SPLITTER
381 promote_pipe_return 0
382 exit_on_error "git checking out our current branch: $this_branch"
384 # now pull down any changes in our own origin in the repo, to stay in synch
385 # with any changes from others.
386 git pull --tags $PULL_ADDITION --all | $TO_SPLITTER
387 #is the above really important when we did this branch already in the loop?
388 #it does an --all, but is that effective or different? should we be doing that in above loop?
390 promote_pipe_return 0
391 exit_on_error "git pulling all upstream"
396 # gets the latest versions of the assets from the upstream repository.
397 function do_revctrl_simple_update()
399 directory="$1"; shift
401 # make a nice echoer since we want to use it inside conditions below.
402 local nicedir="$directory"
403 if [ $nicedir == "." ]; then
406 local blatt="echo -e \nretrieving '$nicedir'..."
408 pushd "$directory" &>/dev/null
409 if [ -d "CVS" ]; then
410 if test_writeable "CVS"; then
412 cvs update . | $TO_SPLITTER
413 promote_pipe_return 0
414 exit_on_error "cvs update"
416 elif [ -d ".svn" ]; then
417 if test_writeable ".svn"; then
419 svn update . | $TO_SPLITTER
420 promote_pipe_return 0
421 exit_on_error "svn update"
423 elif [ -d ".git" ]; then
424 if test_writeable ".git"; then
426 git pull --tags $PULL_ADDITION 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
427 #ordinary pulls should be allowed to do fast forward: --no-ff
428 promote_pipe_return 0
429 exit_on_error "git pull of origin"
432 # this is not an error necessarily; we'll just pretend they planned this.
433 log_feisty_meow_event "no repository in $directory"
440 # gets all the updates for a list of folders under revision control.
441 function checkout_list()
443 local list="$(uniquify $*)"
445 # turn repo list back into an array.
446 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
450 for outer in "${repository_list[@]}"; do
451 # check the repository first, since it might be an absolute path.
452 if [[ $outer =~ /.* ]]; then
453 # yep, this path is absolute. just handle it directly.
454 if [ ! -d "$outer" ]; then continue; fi
455 do_revctrl_simple_update $outer
456 exit_on_error "running update on: $path"
458 for inner in $list; do
459 # add in the directory component to see if we can find the folder.
460 local path="$inner/$outer"
461 if [ ! -d "$path" ]; then continue; fi
462 do_revctrl_simple_update $path
463 exit_on_error "running update on: $path"
469 # does a careful update on all the folders in the specified list;
470 # it looks in the REPOSITORY_LIST for those names and updates them.
471 # this is just like checkout_list, but it's for the puffing up action
472 # we need to do on git.
473 function puff_out_list()
475 # make the list of directories unique.
476 local list="$(uniquify $*)"
478 # turn repo list back into an array.
479 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
483 #hmmm: once again, seeing some reusable code in this loop...
484 for outer in "${repository_list[@]}"; do
485 # check the repository first, since it might be an absolute path.
486 if [[ $outer =~ /.* ]]; then
487 # yep, this path is absolute. just handle it directly.
488 if [ ! -d "$outer" ]; then continue; fi
489 do_revctrl_careful_update "$outer"
490 exit_on_error "running puff-out (absolute) on path: $outer"
492 for inner in $list; do
493 # add in the directory component to see if we can find the folder.
494 local path="$inner/$outer"
495 if [ ! -d "$path" ]; then continue; fi
496 do_revctrl_careful_update "$path"
497 exit_on_error "running puff-out (relative) on path: $path"
503 # provides a list of absolute paths of revision control directories
504 # that are located under the directory passed as the first parameter.
505 function generate_rev_ctrl_filelist()
507 local dir="$1"; shift
508 pushd "$dir" &>/dev/null
509 local dirhere="$( \cd "$(\dirname "$dir")" && /bin/pwd )"
510 local tempfile=$(mktemp /tmp/zz_checkins.XXXXXX)
512 local additional_filter
513 find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".svn" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
514 find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".git" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
515 # CVS is not well behaved like git and (now) svn, and we seldom use it anymore.
518 # see if they've warned us not to try checking in within vendor hierarchies.
519 if [ ! -z "NO_CHECKIN_VENDOR" ]; then
520 sed -i -e '/.*\/vendor\/.*/d' "$tempfile"
523 local sortfile=$(mktemp /tmp/zz_checkin_sort.XXXXXX)
524 sort <"$tempfile" >"$sortfile"
529 # iterates across a list of directories contained in a file (first parameter).
530 # on each directory name, it performs the action (second parameter) provided.
531 function perform_revctrl_action_on_file()
533 local tempfile="$1"; shift
534 local action="$1"; shift
538 while read -u 3 dirname; do
539 if [ -z "$dirname" ]; then
540 # we often have blank lines in the input file for some reason.
544 pushd "$dirname" &>/dev/null
546 # pass the current directory plus the remaining parameters from function invocation.
548 exit_on_error "performing action $action on: $(pwd)"
552 if [ -z "$did_anything" ]; then
553 echo "There was nothing to do the action '$action' on."