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 # classic implementation, but only works with one master branch.
82 # fixes will be forthcoming from development branch.
84 # snag all new files. not to everyone's liking.
86 test_or_die "git add all new files"
88 # see if there are any changes in the local repository.
89 if ! git diff-index --quiet HEAD --; then
90 # tell git about all the files and get a check-in comment.
92 test_or_die "git commit"
95 # # upload the files to the server so others can see them.
96 # git push 2>&1 | grep -v "X11 forwarding request failed"
97 # if [ ${PIPESTATUS[0]} -ne 0 ]; then false; fi
98 # test_or_die "git push"
100 # catch if the diff-index failed somehow.
101 test_or_die "git diff-index"
103 # we continue on to the push, even if there were no changes this time, because
104 # there could already be committed changes that haven't been pushed yet.
106 local myself="$(my_branch_name)"
107 # local parent="$(parent_branch_name)"
109 # upload any changes to the upstream repo so others can see them.
111 # if [ "$myself" != "$parent" ]; then
112 # git push origin "$(myself)" 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
113 # test_or_die "git push to origin: $myself"
115 # # this branch is the same as the parent, so just push.
117 git push 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
118 test_or_die "git push"
123 # nothing there. it's not an error though.
124 echo no repository in $directory
128 restore_terminal_title
133 # shows the local changes in a repository.
136 local directory="$1"; shift
140 pushd "$directory" &>/dev/null
142 # only update if we see a repository living there.
143 if [ -d ".svn" ]; then
145 test_or_die "subversion diff"
146 elif [ -d ".git" ]; then
148 test_or_die "git diff"
149 elif [ -d "CVS" ]; then
151 test_or_die "cvs diff"
156 restore_terminal_title
161 # reports any files that are not already known to the upstream repository.
162 function do_report_new
164 local directory="$1"; shift
168 pushd "$directory" &>/dev/null
170 # only update if we see a repository living there.
171 if [ -f ".no-checkin" ]; then
172 echo "skipping reporting due to presence of .no-checkin sentinel file."
173 elif [ -d ".svn" ]; then
174 # this action so far only makes sense and is needed for svn.
175 bash $FEISTY_MEOW_SCRIPTS/rev_control/svnapply.sh \? echo
176 test_or_die "svn diff"
177 elif [ -d ".git" ]; then
179 test_or_die "git status -u"
184 restore_terminal_title
189 # checks in all the folders in a specified list.
190 function checkin_list()
192 # make the list of directories unique.
193 local list="$(uniquify $*)"
197 # turn repo list back into an array.
198 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
202 for outer in "${repository_list[@]}"; do
203 # check the repository first, since it might be an absolute path.
204 if [[ $outer =~ /.* ]]; then
205 # yep, this path is absolute. just handle it directly.
206 if [ ! -d "$outer" ]; then continue; fi
208 test_or_die "running check-in (absolute) on path: $outer"
211 for inner in $list; do
212 # add in the directory component to see if we can find the folder.
213 local path="$inner/$outer"
214 if [ ! -d "$path" ]; then continue; fi
216 test_or_die "running check-in (relative) on path: $path"
222 restore_terminal_title
225 # takes out the first few carriage returns that are in the input.
226 function squash_first_few_crs()
229 while read input_text; do
231 if [ $i -le 5 ]; then
232 echo -n "$input_text "
237 if [ $i -le 3 ]; then
238 # if we're still squashing eols, make sure we don't leave them hanging.
243 # a helpful method that reports the git branch for the current directory's
245 function my_branch_name()
247 echo "$(git branch | grep '\*' | cut -d ' ' -f2)"
250 #this had a -> in it at one point for not matching, didn't it?
251 # this reports the upstream branch for the current repo.
252 ##function parent_branch_name()
254 ##echo "$(git branch -vv | grep \* | cut -d ' ' -f2)"
257 # this exits with 0 for success (normal bash behavior) when up to date. if the branch is not up to date,
258 # then these values are returned:
260 # reference: https://stackoverflow.com/questions/3258243/check-if-pull-needed-in-git
261 function check_branch_state()
263 local branch="$1"; shift
265 local to_return=120 # unknown issue.
269 LOCAL=$(git rev-parse @)
270 REMOTE=$(git rev-parse "$branch")
271 BASE=$(git merge-base @ "$branch")
272 var branch LOCAL REMOTE BASE
274 if [ "$LOCAL" == "$REMOTE" ]; then
277 elif [ "$LOCAL" == "$BASE" ]; then
280 elif [ "$REMOTE" == "$BASE" ]; then
293 # the git update process just gets more and more complex when you bring in
294 # branches, so we've moved this here to avoid having a ton of code in the
296 function careful_git_update()
300 this_branch="$(my_branch_name)"
302 #we want my branch here, don't we? not like parent or anything?
303 check_branch_state "$this_branch"
305 test_or_continue "branch state check"
306 echo the branch state is $state
307 #need to instead do something here if fails.
311 # first update all our remote branches to their current state from the repos.
313 test_or_die "git remote update"
316 # now pull down any changes in our own origin in the repo, to stay in synch
317 # with any changes from others.
318 git pull --no-ff --all
319 test_or_die "git pulling all upstream"
322 #the above are just not enough. now doing what i have to do to repair things.
323 branch_list=$(git branch |grep -v '^\*')
328 echo The rest of pull is not being done yet.
333 # below has older shards of partial knowledge.
335 # reslog=$(git log HEAD..origin/master --oneline)
336 # if [[ "${reslog}" != "" ]] ; then
337 # git merge origin/master
339 # # from very helpful page:
340 # # https://stackoverflow.com/questions/10312521/how-to-fetch-all-git-branches
341 # for remote in $( git branch -r | grep -v -- '->' ); do
342 # git branch --track ${remote#origin/} $remote 2>/dev/null
343 ##hmmm: ignoring errors from these, since they are continual.
344 ##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.
347 ##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.
348 # git fetch --all 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
349 # test_or_die "git fetch"
351 # git pull --all 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
352 # test_or_die "git pull"
356 # gets the latest versions of the assets from the upstream repository.
359 directory="$1"; shift
363 # make a nice echoer since we want to use it inside conditions below.
364 local nicedir="$directory"
365 if [ $nicedir == "." ]; then
368 local blatt="echo retrieving '$nicedir'..."
370 pushd "$directory" &>/dev/null
371 if [ -d "CVS" ]; then
372 if test_writeable "CVS"; then
374 cvs update . | $TO_SPLITTER
375 test_or_die "cvs update"
377 elif [ -d ".svn" ]; then
378 if test_writeable ".svn"; then
380 svn update . | $TO_SPLITTER
381 test_or_die "svn update"
383 elif [ -d ".git" ]; then
384 if test_writeable ".git"; then
387 # classic implementation, but only works with one master branch.
388 # fixes will be forthcoming from development branch.
390 # git pull 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
391 # if [ ${PIPESTATUS[0]} -ne 0 ]; then false; fi
392 # test_or_die "git pull"
399 # this is not an error necessarily; we'll just pretend they planned this.
400 echo no repository in $directory
404 restore_terminal_title
409 # gets all the updates for a list of folders under revision control.
410 function checkout_list()
412 local list="$(uniquify $*)"
416 # turn repo list back into an array.
417 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
421 for outer in "${repository_list[@]}"; do
422 # check the repository first, since it might be an absolute path.
423 if [[ $outer =~ /.* ]]; then
424 # yep, this path is absolute. just handle it directly.
425 if [ ! -d "$outer" ]; then continue; fi
427 test_or_die "running update on: $path"
430 for inner in $list; do
431 # add in the directory component to see if we can find the folder.
432 local path="$inner/$outer"
433 if [ ! -d "$path" ]; then continue; fi
435 test_or_die "running update on: $path"
441 restore_terminal_title
444 # provides a list of absolute paths of revision control directories
445 # that are located under the directory passed as the first parameter.
446 function generate_rev_ctrl_filelist()
448 local dir="$1"; shift
449 pushd "$dir" &>/dev/null
450 local dirhere="$( \cd "$(\dirname "$dir")" && /bin/pwd )"
451 local tempfile=$(mktemp /tmp/zz_checkins.XXXXXX)
453 local additional_filter
454 find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".svn" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
455 find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".git" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
456 # CVS is not well behaved like git and (now) svn, and we seldom use it anymore.
459 # see if they've warned us not to try checking in within vendor hierarchies.
460 if [ ! -z "NO_CHECKIN_VENDOR" ]; then
461 sed -i -e '/.*\/vendor\/.*/d' "$tempfile"
464 local sortfile=$(mktemp /tmp/zz_checkin_sort.XXXXXX)
465 sort <"$tempfile" >"$sortfile"
470 # iterates across a list of directories contained in a file (first parameter).
471 # on each directory name, it performs the action (second parameter) provided.
472 function perform_revctrl_action_on_file()
474 local tempfile="$1"; shift
475 local action="$1"; shift
479 while read -u 3 dirname; do
480 if [ -z "$dirname" ]; then continue; fi
481 pushd "$dirname" &>/dev/null
484 test_or_die "performing action $action on: $(pwd)"
489 restore_terminal_title