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 # one unpleasantry to take care of first; cygwin barfs aggressively if the TMP directory
47 # is a DOS path, but we need it to be a DOS path for our GFFS testing, so that blows.
48 # to get past this, TMP gets changed below to a hopefully generic and safe place.
49 if [[ "$TMP" =~ .:.* ]]; then
50 log_feisty_meow_event "making weirdo temporary directory for PCDOS-style path."
51 export TMP=/tmp/rev_control_$USER
53 if [ ! -d "$TMP" ]; then
56 if [ ! -d "$TMP" ]; then
57 echo "could not create the temporary directory TMP in: $TMP"
58 echo "this script will not work properly without an existing TMP directory."
60 #hmmm: odd approach to solving the "sourced scripts shouldn't exit or they take down the
61 # original caller too" issue.
62 echo "hit ctrl-c to stay in this shell now, otherwise it may exit in 10 seconds..."
69 # checks the directory provided into the revision control system repository it belongs to.
70 function do_revctrl_checkin()
72 local directory="$1"; shift
74 #hmmm: another piece of reusable code, to process the directory for printing.
75 # make a nice echoer since we want to use it inside conditions below.
76 local nicedir="$directory"
77 if [ $nicedir == "." ]; then
80 local blatt_report="echo -ne \nchecking in '$nicedir'... "
81 local tell_no_checkin="echo -ne \nskipping check-in due to presence of .no-checkin sentinel file: $nicedir"
83 pushd "$directory" &>/dev/null
84 #hmmm: overly elaborate sections below here, but we do want precise handling for git case.
86 if test_writeable "CVS"; then
87 do_revctrl_simple_update "$directory"
88 exit_on_error "updating repository; this issue should be fixed before check-in."
89 if [ -f ".no-checkin" ]; then
90 # echo -ne "\nskipping check-in due to presence of .no-checkin sentinel file: $directory"
95 exit_on_error "cvs checkin"
98 elif [ -d ".svn" ]; then
99 if test_writeable ".svn"; then
100 do_revctrl_simple_update "$directory"
101 exit_on_error "updating repository; this issue should be fixed before check-in."
102 if [ -f ".no-checkin" ]; then
103 # echo -ne "\nskipping check-in due to presence of .no-checkin sentinel file: $directory"
108 exit_on_error "svn checkin"
111 elif [ -d ".git" ]; then
112 if test_writeable ".git"; then
114 # take steps to make sure the branch integrity is good and we're up to date against remote repos.
115 do_revctrl_careful_update "$(\pwd)"
117 if [ -f ".no-checkin" ]; then
118 # echo -ne "\nskipping check-in due to presence of .no-checkin sentinel file: $directory"
123 # put all changed and new files in the commit. not to everyone's liking.
124 git add --all . | $TO_SPLITTER
125 promote_pipe_return 0
126 exit_on_error "git add all new files"
128 # see if there are any changes in the local repository.
129 if ! git diff-index --quiet HEAD --; then
130 # tell git about all the files and get a check-in comment.
131 #hmmm: begins to look like, you guessed it, a reusable bit that all commit actions could enjoy.
134 continue_on_error "git commit"
135 if [ $retval -ne 0 ]; then
136 echo -e -n "Commit failed or was aborted:\nShould we continue with other check-ins? [y/N] "
139 if [[ "${line:0:1}" != "y" ]]; then
140 echo "Stopping check-in process due to missing commit and user request."
146 # we continue on to the push, even if there were no obvious changes this run, because
147 # there could already be committed changes that haven't been pushed yet.
149 # upload any changes to the upstream repo so others can see them.
150 git push --tags origin "$(my_branch_name)" 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
151 promote_pipe_return 0
152 exit_on_error "git push"
156 # nothing there. it's not an error though.
157 log_feisty_meow_event "no repository in $directory"
164 # shows the local changes in a repository.
165 function do_revctrl_diff
167 local directory="$1"; shift
169 pushd "$directory" &>/dev/null
171 # only update if we see a repository living there.
172 if [ -d ".svn" ]; then
174 exit_on_error "subversion diff"
175 elif [ -d ".git" ]; then
177 exit_on_error "git diff"
178 elif [ -d "CVS" ]; then
180 exit_on_error "cvs diff"
188 # reports any files that are not already known to the upstream repository.
189 function do_revctrl_report_new
191 local directory="$1"; shift
193 pushd "$directory" &>/dev/null
195 # only update if we see a repository living there.
196 if [ -f ".no-checkin" ]; then
197 echo -ne "\nskipping reporting due to presence of .no-checkin sentinel file: $directory"
198 elif [ -d ".svn" ]; then
199 # this action so far only makes sense and is needed for svn.
200 bash $FEISTY_MEOW_SCRIPTS/rev_control/svnapply.sh \? echo
201 exit_on_error "svn diff"
202 elif [ -d ".git" ]; then
204 exit_on_error "git status -u"
212 # checks in all the folders in the specified list.
213 function checkin_list()
215 # make the list of directories unique.
216 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
228 do_revctrl_checkin "$outer"
229 exit_on_error "running check-in (absolute) on path: $outer"
231 for inner in $list; do
232 # add in the directory component to see if we can find the folder.
233 local path="$inner/$outer"
234 if [ ! -d "$path" ]; then continue; fi
235 do_revctrl_checkin "$path"
236 exit_on_error "running check-in (relative) on path: $path"
242 #hmmm: below functions are git specific and should be named that way.
244 function all_branch_names()
246 echo "$(git branch -vv | cut -d ' ' -f2)"
249 #this had a -> in it at one point for not matching, didn't it?
250 # this reports the upstream branch for the current repo.
251 ##function parent_branch_name()
253 ##echo "$(git branch -vv | grep \* | 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 # reports the status of the branch by echoing one of these values:
264 # okay: up to date and everything is good.
265 # needs_pull: this branch needs to be pulled from origins.
266 # needs_push: there are unsaved changes on this branch to push to remote store.
267 # diverged: the branches diverged and are going to need a merge.
268 # reference: https://stackoverflow.com/questions/3258243/check-if-pull-needed-in-git
269 function check_branch_state()
271 local branch="$1"; shift
273 if [ -z "$branch" ]; then
274 echo "No branch was passed to check branch state."
278 local to_return=120 # unknown issue.
280 local local_branch=$(git rev-parse @)
281 local remote_branch=$(git rev-parse "$branch")
282 local merge_base=$(git merge-base @ "$branch")
285 if [ "$local_branch" == "$remote_branch" ]; then
287 elif [ "$local_branch" == "$merge_base" ]; then
289 elif [ "$remote_branch" == "$merge_base" ]; then
300 # only shows the branch state if it's not okay.
301 # note that this is not the same as a conditional branch (ha ha).
302 function show_branch_conditionally()
304 local this_branch="$1"; shift
306 local state=$(check_branch_state "$this_branch")
307 if [ "$state" != "okay" ]; then
308 echo "=> branch '$this_branch' state is not clean: $state"
312 # the git update process just gets more and more complex when you bring in
313 # branches, so we've moved this here to avoid having a ton of code in the
315 function do_revctrl_careful_update()
317 local directory="$1"; shift
318 pushd "$directory" &>/dev/null
319 exit_on_error "changing to directory: $directory"
321 if [ ! -d ".git" ]; then
322 # not a git project, so just boil this down to a getem action.
324 log_feisty_meow_event "skipping careful part and doing simple update on non-git repository: $directory"
325 do_revctrl_simple_update "$directory"
329 #hmmm: another piece of reusable code, to process the directory for printing.
330 # make a nice echoer since we want to use it inside conditions below.
331 local nicedir="$directory"
332 if [ $nicedir == "." ]; then
335 local blatt_report="echo -e \ncarefully retrieving '$nicedir'..."
338 local this_branch="$(my_branch_name)"
340 show_branch_conditionally "$this_branch"
342 # first update all our remote branches to their current state from the repos.
343 git remote update | $TO_SPLITTER
344 promote_pipe_return 0
345 exit_on_error "git remote update"
347 show_branch_conditionally "$this_branch"
349 # this code is now doing what i have to do when i repair the repo. and it seems to be good so far.
350 # note that we allow the local branch to be merged with its remote counterpart; otherwise we would
351 # miss changes that happened elsewhere which should be seen in our local copy.
352 local branch_list=$(all_branch_names)
354 for bran in $branch_list; do
355 log_feisty_meow_event "synchronizing remote branch: $bran"
356 git checkout "$bran" | $TO_SPLITTER
357 promote_pipe_return 0
358 exit_on_error "git switching checkout to remote branch: $bran"
360 show_branch_conditionally "$this_branch"
362 remote_branch_info=$(git ls-remote --heads origin $bran 2>/dev/null)
363 if [ ! -z "$remote_branch_info" ]; then
364 # we are pretty sure the remote branch does exist.
365 git pull --tags $PULL_ADDITION origin "$bran" | $TO_SPLITTER
366 promote_pipe_return 0
368 exit_on_error "git pull of remote branch: $bran"
370 # now switch back to our branch.
371 git checkout "$this_branch" | $TO_SPLITTER
372 promote_pipe_return 0
373 exit_on_error "git checking out our current branch: $this_branch"
375 # now pull down any changes in our own origin in the repo, to stay in synch
376 # with any changes from others.
377 git pull --tags $PULL_ADDITION --all | $TO_SPLITTER
378 #is the above really important when we did this branch already in the loop?
379 #it does an --all, but is that effective or different? should we be doing that in above loop?
380 promote_pipe_return 0
381 exit_on_error "git pulling all upstream"
386 # gets the latest versions of the assets from the upstream repository.
387 function do_revctrl_simple_update()
389 directory="$1"; shift
391 #hmmm: another piece of reusable code, to process the directory for printing.
392 # make a nice echoer since we want to use it inside conditions below.
393 local nicedir="$directory"
394 if [ $nicedir == "." ]; then
397 local blatt_report="echo -e \nretrieving '$nicedir'..."
399 pushd "$directory" &>/dev/null
400 if [ -d "CVS" ]; then
401 if test_writeable "CVS"; then
403 cvs update . | $TO_SPLITTER
404 promote_pipe_return 0
405 exit_on_error "cvs update"
407 elif [ -d ".svn" ]; then
408 if test_writeable ".svn"; then
410 svn update . | $TO_SPLITTER
411 promote_pipe_return 0
412 exit_on_error "svn update"
414 elif [ -d ".git" ]; then
415 if test_writeable ".git"; then
417 git pull --tags $PULL_ADDITION 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
418 promote_pipe_return 0
419 exit_on_error "git pull of origin"
422 # this is not an error necessarily; we'll just pretend they planned this.
423 log_feisty_meow_event "no repository in $directory"
430 # gets all the updates for a list of folders under revision control.
431 function checkout_list()
433 local list="$(uniquify $*)"
435 # turn repo list back into an array.
436 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
440 for outer in "${repository_list[@]}"; do
441 # check the repository first, since it might be an absolute path.
442 if [[ $outer =~ /.* ]]; then
443 # yep, this path is absolute. just handle it directly.
444 if [ ! -d "$outer" ]; then continue; fi
445 do_revctrl_simple_update $outer
446 exit_on_error "running update on: $path"
448 for inner in $list; do
449 # add in the directory component to see if we can find the folder.
450 local path="$inner/$outer"
451 if [ ! -d "$path" ]; then continue; fi
452 do_revctrl_simple_update $path
453 exit_on_error "running update on: $path"
459 # does a careful update on all the folders in the specified list;
460 # it looks in the REPOSITORY_LIST for those names and updates them.
461 # this is just like checkout_list, but it's for the puffing up action
462 # we need to do on git.
463 function puff_out_list()
465 # make the list of directories unique.
466 local list="$(uniquify $*)"
468 # turn repo list back into an array.
469 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
473 #hmmm: once again, seeing some reusable code in this loop...
474 for outer in "${repository_list[@]}"; do
475 # check the repository first, since it might be an absolute path.
476 if [[ $outer =~ /.* ]]; then
477 # yep, this path is absolute. just handle it directly.
478 if [ ! -d "$outer" ]; then continue; fi
479 do_revctrl_careful_update "$outer"
480 exit_on_error "running puff-out (absolute) on path: $outer"
482 for inner in $list; do
483 # add in the directory component to see if we can find the folder.
484 local path="$inner/$outer"
485 if [ ! -d "$path" ]; then continue; fi
486 do_revctrl_careful_update "$path"
487 exit_on_error "running puff-out (relative) on path: $path"
493 # provides a list of absolute paths of revision control directories
494 # that are located under the directory passed as the first parameter.
495 function generate_rev_ctrl_filelist()
497 local dir="$1"; shift
498 pushd "$dir" &>/dev/null
499 local dirhere="$( \cd "$(\dirname "$dir")" && /bin/pwd )"
500 local tempfile=$(mktemp /tmp/zz_checkins.XXXXXX)
502 local additional_filter
503 find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".svn" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
504 find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".git" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
505 # CVS is not well behaved like git and (now) svn, and we seldom use it anymore.
508 # see if they've warned us not to try checking in within vendor hierarchies.
509 if [ ! -z "NO_CHECKIN_VENDOR" ]; then
510 sed -i -e '/.*\/vendor\/.*/d' "$tempfile"
513 local sortfile=$(mktemp /tmp/zz_checkin_sort.XXXXXX)
514 sort <"$tempfile" >"$sortfile"
519 # iterates across a list of directories contained in a file (first parameter).
520 # on each directory name, it performs the action (second parameter) provided.
521 function perform_revctrl_action_on_file()
523 local tempfile="$1"; shift
524 local action="$1"; shift
528 while read -u 3 dirname; do
529 if [ -z "$dirname" ]; then
530 # we often have blank lines in the input file for some reason.
534 pushd "$dirname" &>/dev/null
536 # pass the current directory plus the remaining parameters from function invocation.
538 exit_on_error "performing action $action on: $(pwd)"
542 if [ -z "$did_anything" ]; then
543 echo "There was nothing to do the action '$action' on."