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="echo -ne \nchecking in '$nicedir'... "
82 pushd "$directory" &>/dev/null
83 if [ -f ".no-checkin" ]; then
84 echo -ne "\nskipping check-in due to presence of .no-checkin sentinel file: $directory"
85 elif [ -d "CVS" ]; then
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."
91 exit_on_error "cvs checkin"
93 elif [ -d ".svn" ]; then
94 if test_writeable ".svn"; then
95 do_revctrl_simple_update "$directory"
96 exit_on_error "updating repository; this issue should be fixed before check-in."
99 exit_on_error "svn checkin"
101 elif [ -d ".git" ]; then
102 if test_writeable ".git"; then
104 # take steps to make sure the branch integrity is good and we're up to date against remote repos.
105 do_revctrl_careful_update "$(\pwd)"
109 # put all changed and new files in the commit. not to everyone's liking.
110 git add --all . | $TO_SPLITTER
111 promote_pipe_return 0
112 exit_on_error "git add all new files"
114 # see if there are any changes in the local repository.
115 if ! git diff-index --quiet HEAD --; then
116 # tell git about all the files and get a check-in comment.
117 #hmmm: begins to look like, you guessed it, a reusable bit that all commit actions could enjoy.
120 continue_on_error "git commit"
121 if [ $retval -ne 0 ]; then
122 echo -e -n "Commit failed or was aborted:\nShould we continue with other check-ins? [y/N] "
125 if [[ "${line:0:1}" != "y" ]]; then
126 echo "Stopping check-in process due to missing commit and user request."
132 # we continue on to the push, even if there were no obvious changes this run, because
133 # there could already be committed changes that haven't been pushed yet.
135 # upload any changes to the upstream repo so others can see them.
136 git push --tags origin "$(my_branch_name)" 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
137 promote_pipe_return 0
138 exit_on_error "git push"
142 # nothing there. it's not an error though.
143 log_feisty_meow_event "no repository in $directory"
150 # shows the local changes in a repository.
151 function do_revctrl_diff
153 local directory="$1"; shift
155 pushd "$directory" &>/dev/null
157 # only update if we see a repository living there.
158 if [ -d ".svn" ]; then
160 exit_on_error "subversion diff"
161 elif [ -d ".git" ]; then
163 exit_on_error "git diff"
164 elif [ -d "CVS" ]; then
166 exit_on_error "cvs diff"
174 # reports any files that are not already known to the upstream repository.
175 function do_revctrl_report_new
177 local directory="$1"; shift
179 pushd "$directory" &>/dev/null
181 # only update if we see a repository living there.
182 if [ -f ".no-checkin" ]; then
183 echo -ne "\nskipping reporting due to presence of .no-checkin sentinel file: $directory"
184 elif [ -d ".svn" ]; then
185 # this action so far only makes sense and is needed for svn.
186 bash $FEISTY_MEOW_SCRIPTS/rev_control/svnapply.sh \? echo
187 exit_on_error "svn diff"
188 elif [ -d ".git" ]; then
190 exit_on_error "git status -u"
198 # checks in all the folders in the specified list.
199 function checkin_list()
201 # make the list of directories unique.
202 local list="$(uniquify $*)"
204 # turn repo list back into an array.
205 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
209 for outer in "${repository_list[@]}"; do
210 # check the repository first, since it might be an absolute path.
211 if [[ $outer =~ /.* ]]; then
212 # yep, this path is absolute. just handle it directly.
213 if [ ! -d "$outer" ]; then continue; fi
214 do_revctrl_checkin "$outer"
215 exit_on_error "running check-in (absolute) on path: $outer"
217 for inner in $list; do
218 # add in the directory component to see if we can find the folder.
219 local path="$inner/$outer"
220 if [ ! -d "$path" ]; then continue; fi
221 do_revctrl_checkin "$path"
222 exit_on_error "running check-in (relative) on path: $path"
228 #hmmm: below functions are git specific and should be named that way.
230 function all_branch_names()
232 echo "$(git branch -vv | cut -d ' ' -f2)"
235 #this had a -> in it at one point for not matching, didn't it?
236 # this reports the upstream branch for the current repo.
237 ##function parent_branch_name()
239 ##echo "$(git branch -vv | grep \* | cut -d ' ' -f2)"
242 # a helpful method that reports the git branch for the current directory's
244 function my_branch_name()
246 echo "$(git branch -vv | grep '\*' | cut -d ' ' -f2)"
249 # reports the status of the branch by echoing one of these values:
250 # okay: up to date and everything is good.
251 # needs_pull: this branch needs to be pulled from origins.
252 # needs_push: there are unsaved changes on this branch to push to remote store.
253 # diverged: the branches diverged and are going to need a merge.
254 # reference: https://stackoverflow.com/questions/3258243/check-if-pull-needed-in-git
255 function check_branch_state()
257 local branch="$1"; shift
259 if [ -z "$branch" ]; then
260 echo "No branch was passed to check branch state."
264 local to_return=120 # unknown issue.
266 local local_branch=$(git rev-parse @)
267 local remote_branch=$(git rev-parse "$branch")
268 local merge_base=$(git merge-base @ "$branch")
271 if [ "$local_branch" == "$remote_branch" ]; then
273 elif [ "$local_branch" == "$merge_base" ]; then
275 elif [ "$remote_branch" == "$merge_base" ]; then
286 # only shows the branch state if it's not okay.
287 # note that this is not the same as a conditional branch (ha ha).
288 function show_branch_conditionally()
290 local this_branch="$1"; shift
292 local state=$(check_branch_state "$this_branch")
293 if [ "$state" != "okay" ]; then
294 echo "=> branch '$this_branch' state is not clean: $state"
298 # the git update process just gets more and more complex when you bring in
299 # branches, so we've moved this here to avoid having a ton of code in the
301 function do_revctrl_careful_update()
303 local directory="$1"; shift
304 pushd "$directory" &>/dev/null
305 exit_on_error "changing to directory: $directory"
307 if [ ! -d ".git" ]; then
308 # not a git project, so just boil this down to a getem action.
310 log_feisty_meow_event "skipping careful part and doing simple update on non-git repository: $directory"
311 do_revctrl_simple_update "$directory"
315 #hmmm: another piece of reusable code, to process the directory for printing.
316 # make a nice echoer since we want to use it inside conditions below.
317 local nicedir="$directory"
318 if [ $nicedir == "." ]; then
321 local blatt="echo -e \ncarefully retrieving '$nicedir'..."
324 local this_branch="$(my_branch_name)"
326 show_branch_conditionally "$this_branch"
328 # first update all our remote branches to their current state from the repos.
329 git remote update | $TO_SPLITTER
330 promote_pipe_return 0
331 exit_on_error "git remote update"
333 show_branch_conditionally "$this_branch"
335 # this code is now doing what i have to do when i repair the repo. and it seems to be good so far.
336 # note that we allow the local branch to be merged with its remote counterpart; otherwise we would
337 # miss changes that happened elsewhere which should be seen in our local copy.
338 local branch_list=$(all_branch_names)
340 for bran in $branch_list; do
341 log_feisty_meow_event "synchronizing remote branch: $bran"
342 git checkout "$bran" | $TO_SPLITTER
343 promote_pipe_return 0
344 exit_on_error "git switching checkout to remote branch: $bran"
346 show_branch_conditionally "$this_branch"
348 remote_branch_info=$(git ls-remote --heads origin $bran 2>/dev/null)
349 if [ ! -z "$remote_branch_info" ]; then
350 # we are pretty sure the remote branch does exist.
351 git pull --tags $PULL_ADDITION origin "$bran" | $TO_SPLITTER
352 promote_pipe_return 0
354 exit_on_error "git pull of remote branch: $bran"
356 # now switch back to our branch.
357 git checkout "$this_branch" | $TO_SPLITTER
358 promote_pipe_return 0
359 exit_on_error "git checking out our current branch: $this_branch"
361 # now pull down any changes in our own origin in the repo, to stay in synch
362 # with any changes from others.
363 git pull --tags $PULL_ADDITION --all | $TO_SPLITTER
364 #is the above really important when we did this branch already in the loop?
365 #it does an --all, but is that effective or different? should we be doing that in above loop?
366 promote_pipe_return 0
367 exit_on_error "git pulling all upstream"
372 # gets the latest versions of the assets from the upstream repository.
373 function do_revctrl_simple_update()
375 directory="$1"; shift
377 #hmmm: another piece of reusable code, to process the directory for printing.
378 # make a nice echoer since we want to use it inside conditions below.
379 local nicedir="$directory"
380 if [ $nicedir == "." ]; then
383 local blatt="echo -e \nretrieving '$nicedir'..."
385 pushd "$directory" &>/dev/null
386 if [ -d "CVS" ]; then
387 if test_writeable "CVS"; then
389 cvs update . | $TO_SPLITTER
390 promote_pipe_return 0
391 exit_on_error "cvs update"
393 elif [ -d ".svn" ]; then
394 if test_writeable ".svn"; then
396 svn update . | $TO_SPLITTER
397 promote_pipe_return 0
398 exit_on_error "svn update"
400 elif [ -d ".git" ]; then
401 if test_writeable ".git"; then
403 git pull --tags $PULL_ADDITION 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
404 promote_pipe_return 0
405 exit_on_error "git pull of origin"
408 # this is not an error necessarily; we'll just pretend they planned this.
409 log_feisty_meow_event "no repository in $directory"
416 # gets all the updates for a list of folders under revision control.
417 function checkout_list()
419 local list="$(uniquify $*)"
421 # turn repo list back into an array.
422 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
426 for outer in "${repository_list[@]}"; do
427 # check the repository first, since it might be an absolute path.
428 if [[ $outer =~ /.* ]]; then
429 # yep, this path is absolute. just handle it directly.
430 if [ ! -d "$outer" ]; then continue; fi
431 do_revctrl_simple_update $outer
432 exit_on_error "running update on: $path"
434 for inner in $list; do
435 # add in the directory component to see if we can find the folder.
436 local path="$inner/$outer"
437 if [ ! -d "$path" ]; then continue; fi
438 do_revctrl_simple_update $path
439 exit_on_error "running update on: $path"
445 # does a careful update on all the folders in the specified list;
446 # it looks in the REPOSITORY_LIST for those names and updates them.
447 # this is just like checkout_list, but it's for the puffing up action
448 # we need to do on git.
449 function puff_out_list()
451 # make the list of directories unique.
452 local list="$(uniquify $*)"
454 # turn repo list back into an array.
455 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
459 #hmmm: once again, seeing some reusable code in this loop...
460 for outer in "${repository_list[@]}"; do
461 # check the repository first, since it might be an absolute path.
462 if [[ $outer =~ /.* ]]; then
463 # yep, this path is absolute. just handle it directly.
464 if [ ! -d "$outer" ]; then continue; fi
465 do_revctrl_careful_update "$outer"
466 exit_on_error "running puff-out (absolute) on path: $outer"
468 for inner in $list; do
469 # add in the directory component to see if we can find the folder.
470 local path="$inner/$outer"
471 if [ ! -d "$path" ]; then continue; fi
472 do_revctrl_careful_update "$path"
473 exit_on_error "running puff-out (relative) on path: $path"
479 # provides a list of absolute paths of revision control directories
480 # that are located under the directory passed as the first parameter.
481 function generate_rev_ctrl_filelist()
483 local dir="$1"; shift
484 pushd "$dir" &>/dev/null
485 local dirhere="$( \cd "$(\dirname "$dir")" && /bin/pwd )"
486 local tempfile=$(mktemp /tmp/zz_checkins.XXXXXX)
488 local additional_filter
489 find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".svn" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
490 find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".git" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
491 # CVS is not well behaved like git and (now) svn, and we seldom use it anymore.
494 # see if they've warned us not to try checking in within vendor hierarchies.
495 if [ ! -z "NO_CHECKIN_VENDOR" ]; then
496 sed -i -e '/.*\/vendor\/.*/d' "$tempfile"
499 local sortfile=$(mktemp /tmp/zz_checkin_sort.XXXXXX)
500 sort <"$tempfile" >"$sortfile"
505 # iterates across a list of directories contained in a file (first parameter).
506 # on each directory name, it performs the action (second parameter) provided.
507 function perform_revctrl_action_on_file()
509 local tempfile="$1"; shift
510 local action="$1"; shift
514 while read -u 3 dirname; do
515 if [ -z "$dirname" ]; then
516 # we often have blank lines in the input file for some reason.
520 pushd "$dirname" &>/dev/null
522 # pass the current directory plus the remaining parameters from function invocation.
524 exit_on_error "performing action $action on: $(pwd)"
528 if [ -z "$did_anything" ]; then
529 echo "There was nothing to do the action '$action' on."