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"
12 #hmmm: we need to dump all the outputs in this script into splitter
16 # the maximum depth that the recursive functions will try to go below the starting directory.
19 # use our splitter tool for lengthy output if it's available.
20 if [ ! -z "$(which splitter)" ]; then
21 TO_SPLITTER="$(which splitter)"
28 # one unpleasantry to take care of first; cygwin barfs aggressively if the TMP directory
29 # is a DOS path, but we need it to be a DOS path for our GFFS testing, so that blows.
30 # to get past this, TMP gets changed below to a hopefully generic and safe place.
31 if [[ "$TMP" =~ .:.* ]]; then
32 echo "making weirdo temporary directory for PCDOS-style path."
33 export TMP=/tmp/rev_control_$USER
35 if [ ! -d "$TMP" ]; then
38 if [ ! -d "$TMP" ]; then
39 echo "could not create the temporary directory TMP in: $TMP"
40 echo "this script will not work properly without an existing TMP directory."
45 # checks the directory provided into the revision control system repository it belongs to.
48 local directory="$1"; shift
52 # make a nice echoer since we want to use it inside conditions below.
53 local nicedir="$directory"
54 if [ $nicedir == "." ]; then
57 local blatt="echo checking in '$nicedir'..."
59 do_update "$directory"
60 test_or_die "repository update--this should be fixed before check-in."
62 pushd "$directory" &>/dev/null
63 if [ -f ".no-checkin" ]; then
64 echo "skipping check-in due to presence of .no-checkin sentinel file."
65 elif [ -d "CVS" ]; then
66 if test_writeable "CVS"; then
69 test_or_die "cvs checkin"
71 elif [ -d ".svn" ]; then
72 if test_writeable ".svn"; then
75 test_or_die "svn checkin"
77 elif [ -d ".git" ]; then
78 if test_writeable ".git"; then
81 # put all changed and new files in the commit. not to everyone's liking.
83 test_or_die "git add all new files"
85 # see if there are any changes in the local repository.
86 if ! git diff-index --quiet HEAD --; then
87 # tell git about all the files and get a check-in comment.
89 test_or_die "git commit"
92 # # upload the files to the server so others can see them.
93 # git push 2>&1 | grep -v "X11 forwarding request failed"
94 # if [ ${PIPESTATUS[0]} -ne 0 ]; then false; fi
95 # test_or_die "git push"
97 # a new set of steps we have to take to make sure the branch integrity is good.
100 # we continue on to the push, even if there were no changes this time, because
101 # there could already be committed changes that haven't been pushed yet.
103 # upload any changes to the upstream repo so others can see them.
104 git push 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
105 test_or_die "git push"
109 # nothing there. it's not an error though.
110 echo no repository in $directory
114 restore_terminal_title
119 # shows the local changes in a repository.
122 local directory="$1"; shift
126 pushd "$directory" &>/dev/null
128 # only update if we see a repository living there.
129 if [ -d ".svn" ]; then
131 test_or_die "subversion diff"
132 elif [ -d ".git" ]; then
134 test_or_die "git diff"
135 elif [ -d "CVS" ]; then
137 test_or_die "cvs diff"
142 restore_terminal_title
147 # reports any files that are not already known to the upstream repository.
148 function do_report_new
150 local directory="$1"; shift
154 pushd "$directory" &>/dev/null
156 # only update if we see a repository living there.
157 if [ -f ".no-checkin" ]; then
158 echo "skipping reporting due to presence of .no-checkin sentinel file."
159 elif [ -d ".svn" ]; then
160 # this action so far only makes sense and is needed for svn.
161 bash $FEISTY_MEOW_SCRIPTS/rev_control/svnapply.sh \? echo
162 test_or_die "svn diff"
163 elif [ -d ".git" ]; then
165 test_or_die "git status -u"
170 restore_terminal_title
175 # checks in all the folders in a specified list.
176 function checkin_list()
178 # make the list of directories unique.
179 local list="$(uniquify $*)"
183 # turn repo list back into an array.
184 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
188 for outer in "${repository_list[@]}"; do
189 # check the repository first, since it might be an absolute path.
190 if [[ $outer =~ /.* ]]; then
191 # yep, this path is absolute. just handle it directly.
192 if [ ! -d "$outer" ]; then continue; fi
194 test_or_die "running check-in (absolute) on path: $outer"
197 for inner in $list; do
198 # add in the directory component to see if we can find the folder.
199 local path="$inner/$outer"
200 if [ ! -d "$path" ]; then continue; fi
202 test_or_die "running check-in (relative) on path: $path"
208 restore_terminal_title
211 # takes out the first few carriage returns that are in the input.
212 function squash_first_few_crs()
215 while read input_text; do
217 if [ $i -le 5 ]; then
218 echo -n "$input_text "
223 if [ $i -le 3 ]; then
224 # if we're still squashing eols, make sure we don't leave them hanging.
229 # a helpful method that reports the git branch for the current directory's
231 function my_branch_name()
233 echo "$(git branch | 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 # this exits with 0 for success (normal bash behavior) when up to date. if the branch is not up to date,
244 # then these values are returned:
246 # reference: https://stackoverflow.com/questions/3258243/check-if-pull-needed-in-git
247 function check_branch_state()
249 local branch="$1"; shift
251 local to_return=120 # unknown issue.
255 LOCAL=$(git rev-parse @)
256 REMOTE=$(git rev-parse "$branch")
257 BASE=$(git merge-base @ "$branch")
258 var branch LOCAL REMOTE BASE
260 if [ "$LOCAL" == "$REMOTE" ]; then
263 elif [ "$LOCAL" == "$BASE" ]; then
266 elif [ "$REMOTE" == "$BASE" ]; then
279 # the git update process just gets more and more complex when you bring in
280 # branches, so we've moved this here to avoid having a ton of code in the
282 function careful_git_update()
286 local this_branch="$(my_branch_name)"
288 #we want my branch here, don't we? not like parent or anything?
289 check_branch_state "$this_branch"
291 test_or_continue "branch state check"
292 echo the branch state is $state
293 #need to instead do something here if fails.
294 # above is worse than useless code; in the situations i'm seeing fail, it reports no changes. *&@#*&@#
297 # the above are just not enough. this code is now doing what i have to do when i repair the repo.
298 local branch_list=$(git branch |grep -v '^\*')
300 for bran in $branch_list; do
302 test_or_die "git checking out remote branch: $bran"
304 test_or_die "git pull of remote branch: $bran"
306 # now switch back to our branch.
307 git checkout "$this_branch"
308 test_or_die "git checking out our current branch: $this_branch"
312 # first update all our remote branches to their current state from the repos.
314 test_or_die "git remote update"
317 # now pull down any changes in our own origin in the repo, to stay in synch
318 # with any changes from others.
319 git pull --no-ff --all
320 test_or_die "git pulling all upstream"
324 echo The rest of pull is not being done yet.
329 # below has older shards of partial knowledge.
331 # reslog=$(git log HEAD..origin/master --oneline)
332 # if [[ "${reslog}" != "" ]] ; then
333 # git merge origin/master
335 # # from very helpful page:
336 # # https://stackoverflow.com/questions/10312521/how-to-fetch-all-git-branches
337 # for remote in $( git branch -r | grep -v -- '->' ); do
338 # git branch --track ${remote#origin/} $remote 2>/dev/null
339 ##hmmm: ignoring errors from these, since they are continual.
340 ##hmmm: if we could find a way to not try to track with a local branch when there's already one present, that would be swell. it's probably simple.
343 ##hmmm: well, one time it failed without the fetch. i hope that's because the fetch is actually needed and not because the whole approach is fubar.
344 # git fetch --all 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
345 # test_or_die "git fetch"
347 # git pull --all 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
348 # test_or_die "git pull"
352 # gets the latest versions of the assets from the upstream repository.
355 directory="$1"; shift
359 # make a nice echoer since we want to use it inside conditions below.
360 local nicedir="$directory"
361 if [ $nicedir == "." ]; then
364 local blatt="echo retrieving '$nicedir'..."
366 pushd "$directory" &>/dev/null
367 if [ -d "CVS" ]; then
368 if test_writeable "CVS"; then
370 cvs update . | $TO_SPLITTER
371 test_or_die "cvs update"
373 elif [ -d ".svn" ]; then
374 if test_writeable ".svn"; then
376 svn update . | $TO_SPLITTER
377 test_or_die "svn update"
379 elif [ -d ".git" ]; then
380 if test_writeable ".git"; then
383 # classic implementation, but only works with one master branch.
384 # fixes will be forthcoming from development branch.
386 git pull 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
387 if [ ${PIPESTATUS[0]} -ne 0 ]; then false; fi
388 test_or_die "git pull"
391 ##no! can't be done here or commit fudges up careful_git_update
395 # this is not an error necessarily; we'll just pretend they planned this.
396 echo no repository in $directory
400 restore_terminal_title
405 # gets all the updates for a list of folders under revision control.
406 function checkout_list()
408 local list="$(uniquify $*)"
412 # turn repo list back into an array.
413 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
417 for outer in "${repository_list[@]}"; do
418 # check the repository first, since it might be an absolute path.
419 if [[ $outer =~ /.* ]]; then
420 # yep, this path is absolute. just handle it directly.
421 if [ ! -d "$outer" ]; then continue; fi
423 test_or_die "running update on: $path"
426 for inner in $list; do
427 # add in the directory component to see if we can find the folder.
428 local path="$inner/$outer"
429 if [ ! -d "$path" ]; then continue; fi
431 test_or_die "running update on: $path"
437 restore_terminal_title
440 # provides a list of absolute paths of revision control directories
441 # that are located under the directory passed as the first parameter.
442 function generate_rev_ctrl_filelist()
444 local dir="$1"; shift
445 pushd "$dir" &>/dev/null
446 local dirhere="$( \cd "$(\dirname "$dir")" && /bin/pwd )"
447 local tempfile=$(mktemp /tmp/zz_checkins.XXXXXX)
449 local additional_filter
450 find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".svn" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
451 find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".git" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
452 # CVS is not well behaved like git and (now) svn, and we seldom use it anymore.
455 # see if they've warned us not to try checking in within vendor hierarchies.
456 if [ ! -z "NO_CHECKIN_VENDOR" ]; then
457 sed -i -e '/.*\/vendor\/.*/d' "$tempfile"
460 local sortfile=$(mktemp /tmp/zz_checkin_sort.XXXXXX)
461 sort <"$tempfile" >"$sortfile"
466 # iterates across a list of directories contained in a file (first parameter).
467 # on each directory name, it performs the action (second parameter) provided.
468 function perform_revctrl_action_on_file()
470 local tempfile="$1"; shift
471 local action="$1"; shift
475 while read -u 3 dirname; do
476 if [ -z "$dirname" ]; then continue; fi
477 pushd "$dirname" &>/dev/null
480 test_or_die "performing action $action on: $(pwd)"
485 restore_terminal_title