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 # the maximum depth that the recursive functions will try to go below the starting directory.
17 # use our splitter tool for lengthy output if it's available.
18 if [ ! -z "$(which splitter)" ]; then
19 TO_SPLITTER="$(which splitter)"
26 # one unpleasantry to take care of first; cygwin barfs aggressively if the TMP directory
27 # is a DOS path, but we need it to be a DOS path for our GFFS testing, so that blows.
28 # to get past this, TMP gets changed below to a hopefully generic and safe place.
29 if [[ "$TMP" =~ .:.* ]]; then
30 echo "making weirdo temporary directory for PCDOS-style path."
31 export TMP=/tmp/rev_control_$USER
33 if [ ! -d "$TMP" ]; then
36 if [ ! -d "$TMP" ]; then
37 echo "could not create the temporary directory TMP in: $TMP"
38 echo "this script will not work properly without an existing TMP directory."
43 # checks the directory provided into the revision control system repository it belongs to.
46 local directory="$1"; shift
50 # make a nice echoer since we want to use it inside conditions below.
51 local nicedir="$directory"
52 if [ $nicedir == "." ]; then
55 local blatt="echo checking in '$nicedir'..."
57 do_update "$directory"
58 test_or_die "repository update--this should be fixed before check-in."
60 pushd "$directory" &>/dev/null
61 if [ -f ".no-checkin" ]; then
62 echo "skipping check-in due to presence of .no-checkin sentinel file."
63 elif [ -d "CVS" ]; then
64 if test_writeable "CVS"; then
67 test_or_die "cvs checkin"
69 elif [ -d ".svn" ]; then
70 if test_writeable ".svn"; then
73 test_or_die "svn checkin"
75 elif [ -d ".git" ]; then
76 if test_writeable ".git"; then
79 # put all changed and new files in the commit. not to everyone's liking.
81 test_or_die "git add all new files"
83 # see if there are any changes in the local repository.
84 if ! git diff-index --quiet HEAD --; then
85 # tell git about all the files and get a check-in comment.
87 test_or_die "git commit"
90 # a new set of steps we have to take to make sure the branch integrity is good.
91 do_careful_git_update "$(\pwd)"
93 # we continue on to the push, even if there were no changes this time, because
94 # there could already be committed changes that haven't been pushed yet.
96 # upload any changes to the upstream repo so others can see them.
97 git push origin "$(my_branch_name)" 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
98 test_or_die "git push"
102 # nothing there. it's not an error though.
103 echo no repository in $directory
107 restore_terminal_title
112 # shows the local changes in a repository.
115 local directory="$1"; shift
119 pushd "$directory" &>/dev/null
121 # only update if we see a repository living there.
122 if [ -d ".svn" ]; then
124 test_or_die "subversion diff"
125 elif [ -d ".git" ]; then
127 test_or_die "git diff"
128 elif [ -d "CVS" ]; then
130 test_or_die "cvs diff"
135 restore_terminal_title
140 # reports any files that are not already known to the upstream repository.
141 function do_report_new
143 local directory="$1"; shift
147 pushd "$directory" &>/dev/null
149 # only update if we see a repository living there.
150 if [ -f ".no-checkin" ]; then
151 echo "skipping reporting due to presence of .no-checkin sentinel file."
152 elif [ -d ".svn" ]; then
153 # this action so far only makes sense and is needed for svn.
154 bash $FEISTY_MEOW_SCRIPTS/rev_control/svnapply.sh \? echo
155 test_or_die "svn diff"
156 elif [ -d ".git" ]; then
158 test_or_die "git status -u"
163 restore_terminal_title
168 # checks in all the folders in a specified list.
169 function checkin_list()
171 # make the list of directories unique.
172 local list="$(uniquify $*)"
176 # turn repo list back into an array.
177 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
181 for outer in "${repository_list[@]}"; do
182 # check the repository first, since it might be an absolute path.
183 if [[ $outer =~ /.* ]]; then
184 # yep, this path is absolute. just handle it directly.
185 if [ ! -d "$outer" ]; then continue; fi
187 test_or_die "running check-in (absolute) on path: $outer"
190 for inner in $list; do
191 # add in the directory component to see if we can find the folder.
192 local path="$inner/$outer"
193 if [ ! -d "$path" ]; then continue; fi
195 test_or_die "running check-in (relative) on path: $path"
201 restore_terminal_title
204 # takes out the first few carriage returns that are in the input.
205 function squash_first_few_crs()
208 while read input_text; do
210 if [ $i -le 5 ]; then
211 echo -n "$input_text "
216 if [ $i -le 3 ]; then
217 # if we're still squashing eols, make sure we don't leave them hanging.
222 #hmmm: the below are git specific and should be named that way.
224 function all_branch_names()
226 echo "$(git branch -vv | cut -d ' ' -f2)"
229 # a helpful method that reports the git branch for the current directory's
231 function my_branch_name()
233 echo "$(git branch -vv | grep '\*' | cut -d ' ' -f2)"
236 #this had a -> in it at one point for not matching, didn't it?
237 # this reports the upstream branch for the current repo.
238 ##function parent_branch_name()
240 ##echo "$(git branch -vv | grep \* | cut -d ' ' -f2)"
243 # reports the status of the branch by echoing one of these values:
244 # okay: up to date and everything is good.
245 # needs_pull: this branch needs to be pulled from origins.
246 # needs_push: there are unsaved changes on this branch to push to remote store.
247 # diverged: the branches diverged and are going to need a merge.
248 # reference: https://stackoverflow.com/questions/3258243/check-if-pull-needed-in-git
249 function check_branch_state()
251 local branch="$1"; shift
253 local to_return=120 # unknown issue.
255 local local_branch=$(git rev-parse @)
256 local remote_branch=$(git rev-parse "$branch")
257 local merge_base=$(git merge-base @ "$branch")
259 if [ "$local_branch" == "$remote_branch" ]; then
261 elif [ "$local_branch" == "$merge_base" ]; then
263 elif [ "$remote_branch" == "$merge_base" ]; then
272 # the git update process just gets more and more complex when you bring in
273 # branches, so we've moved this here to avoid having a ton of code in the
275 function do_careful_git_update()
277 local directory="$1"; shift
278 pushd "$directory" &>/dev/null
279 test_or_die "changing to directory: $directory"
281 if [ ! -d ".git" ]; then
282 # we ignore if they're jumping into a non-useful folder, but also tell them.
283 echo "Directory is not a git repository: $directory"
287 # first update all our remote branches to their current state from the repos.
289 test_or_die "git remote update"
291 local this_branch="$(my_branch_name)"
292 #appears to be useless; reports no changes when we need to know about remote changes that do exist:
293 #hmmm: trying it out again now that things are better elsewhere. let's see what it says.
294 state=$(check_branch_state "$this_branch")
295 echo "=> branch '$this_branch' state is: $state"
297 # this code is now doing what i have to do when i repair the repo. and it seems to be good so far.
298 local branch_list=$(all_branch_names)
300 for bran in $branch_list; do
301 # echo "synchronizing remote branch: $bran"
303 test_or_die "git switching checkout to remote branch: $bran"
305 state=$(check_branch_state "$bran")
306 echo "=> branch '$bran' state is: $state"
308 remote_branch_info=$(git ls-remote --heads origin $bran 2>/dev/null)
309 if [ ! -z "$remote_branch_info" ]; then
310 # we are pretty sure the remote branch does exist.
311 git pull --no-ff origin "$bran"
313 test_or_die "git pull of remote branch: $bran"
315 # now switch back to our branch.
316 git checkout "$this_branch"
317 test_or_die "git checking out our current branch: $this_branch"
319 # now pull down any changes in our own origin in the repo, to stay in synch
320 # with any changes from others.
321 git pull --no-ff --all
322 test_or_die "git pulling all upstream"
327 # gets the latest versions of the assets from the upstream repository.
330 directory="$1"; shift
334 # make a nice echoer since we want to use it inside conditions below.
335 local nicedir="$directory"
336 if [ $nicedir == "." ]; then
339 local blatt="echo retrieving '$nicedir'..."
341 pushd "$directory" &>/dev/null
342 if [ -d "CVS" ]; then
343 if test_writeable "CVS"; then
345 cvs update . | $TO_SPLITTER
346 test_or_die "cvs update"
348 elif [ -d ".svn" ]; then
349 if test_writeable ".svn"; then
351 svn update . | $TO_SPLITTER
352 test_or_die "svn update"
354 elif [ -d ".git" ]; then
355 if test_writeable ".git"; then
357 git pull --no-ff 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
358 if [ ${PIPESTATUS[0]} -ne 0 ]; then false; fi
359 test_or_die "git pull of origin without fast forwards"
362 # this is not an error necessarily; we'll just pretend they planned this.
363 echo no repository in $directory
367 restore_terminal_title
372 # gets all the updates for a list of folders under revision control.
373 function checkout_list()
375 local list="$(uniquify $*)"
379 # turn repo list back into an array.
380 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
384 for outer in "${repository_list[@]}"; do
385 # check the repository first, since it might be an absolute path.
386 if [[ $outer =~ /.* ]]; then
387 # yep, this path is absolute. just handle it directly.
388 if [ ! -d "$outer" ]; then continue; fi
390 test_or_die "running update on: $path"
393 for inner in $list; do
394 # add in the directory component to see if we can find the folder.
395 local path="$inner/$outer"
396 if [ ! -d "$path" ]; then continue; fi
398 test_or_die "running update on: $path"
404 restore_terminal_title
407 # provides a list of absolute paths of revision control directories
408 # that are located under the directory passed as the first parameter.
409 function generate_rev_ctrl_filelist()
411 local dir="$1"; shift
412 pushd "$dir" &>/dev/null
413 local dirhere="$( \cd "$(\dirname "$dir")" && /bin/pwd )"
414 local tempfile=$(mktemp /tmp/zz_checkins.XXXXXX)
416 local additional_filter
417 find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".svn" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
418 find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".git" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
419 # CVS is not well behaved like git and (now) svn, and we seldom use it anymore.
422 # see if they've warned us not to try checking in within vendor hierarchies.
423 if [ ! -z "NO_CHECKIN_VENDOR" ]; then
424 sed -i -e '/.*\/vendor\/.*/d' "$tempfile"
427 local sortfile=$(mktemp /tmp/zz_checkin_sort.XXXXXX)
428 sort <"$tempfile" >"$sortfile"
433 # iterates across a list of directories contained in a file (first parameter).
434 # on each directory name, it performs the action (second parameter) provided.
435 function perform_revctrl_action_on_file()
437 local tempfile="$1"; shift
438 local action="$1"; shift
444 while read -u 3 dirname; do
445 if [ -z "$dirname" ]; then
446 # we often have blank lines in the input file for some reason.
450 pushd "$dirname" &>/dev/null
452 # pass the current directory plus the remaining parameters from function invocation.
454 test_or_die "performing action $action on: $(pwd)"
459 if [ -z "$did_anything" ]; then
460 echo "There was nothing to do the action '$action' on."
463 restore_terminal_title