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"
94 # # upload the files to the server so others can see them.
95 # git push 2>&1 | grep -v "X11 forwarding request failed"
96 # if [ ${PIPESTATUS[0]} -ne 0 ]; then false; fi
97 # test_or_die "git push"
99 # catch if the diff-index failed somehow.
100 test_or_die "git diff-index"
102 # we continue on to the push, even if there were no changes this time, because
103 # there could already be committed changes that haven't been pushed yet.
105 local myself="$(my_branch_name)"
106 local parent="$(parent_branch_name)"
108 # upload any changes to the upstream repo so others can see them.
109 if [ "$myself" != "$parent" ]; then
110 git push origin "$(myself)" 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
111 test_or_die "git push to origin: $myself"
113 # this branch is the same as the parent, so just push.
114 git push 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
115 test_or_die "normal git push"
120 # nothing there. it's not an error though.
121 echo no repository in $directory
125 restore_terminal_title
130 # shows the local changes in a repository.
133 local directory="$1"; shift
137 pushd "$directory" &>/dev/null
139 # only update if we see a repository living there.
140 if [ -d ".svn" ]; then
142 test_or_die "subversion diff"
143 elif [ -d ".git" ]; then
145 test_or_die "git diff"
146 elif [ -d "CVS" ]; then
148 test_or_die "cvs diff"
153 restore_terminal_title
158 # reports any files that are not already known to the upstream repository.
159 function do_report_new
161 local directory="$1"; shift
165 pushd "$directory" &>/dev/null
167 # only update if we see a repository living there.
168 if [ -f ".no-checkin" ]; then
169 echo "skipping reporting due to presence of .no-checkin sentinel file."
170 elif [ -d ".svn" ]; then
171 # this action so far only makes sense and is needed for svn.
172 bash $FEISTY_MEOW_SCRIPTS/rev_control/svnapply.sh \? echo
173 test_or_die "svn diff"
174 elif [ -d ".git" ]; then
176 test_or_die "git status -u"
181 restore_terminal_title
186 # checks in all the folders in a specified list.
187 function checkin_list()
189 # make the list of directories unique.
190 local list="$(uniquify $*)"
194 # turn repo list back into an array.
195 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
199 for outer in "${repository_list[@]}"; do
200 # check the repository first, since it might be an absolute path.
201 if [[ $outer =~ /.* ]]; then
202 # yep, this path is absolute. just handle it directly.
203 if [ ! -d "$outer" ]; then continue; fi
205 test_or_die "running check-in (absolute) on path: $outer"
208 for inner in $list; do
209 # add in the directory component to see if we can find the folder.
210 local path="$inner/$outer"
211 if [ ! -d "$path" ]; then continue; fi
213 test_or_die "running check-in (relative) on path: $path"
219 restore_terminal_title
222 # takes out the first few carriage returns that are in the input.
223 function squash_first_few_crs()
226 while read input_text; do
228 if [ $i -le 5 ]; then
229 echo -n "$input_text "
234 if [ $i -le 3 ]; then
235 # if we're still squashing eols, make sure we don't leave them hanging.
240 # a helpful method that reports the git branch for the current directory's
242 function my_branch_name()
244 echo "$(git branch | grep \* | cut -d ' ' -f2)"
247 # this reports the upstream branch for the current repo.
248 function parent_branch_name()
250 echo "$(git branch -vv | grep \* | cut -d ' ' -f2)"
253 # gets the latest versions of the assets from the upstream repository.
256 directory="$1"; shift
260 # make a nice echoer since we want to use it inside conditions below.
261 local nicedir="$directory"
262 if [ $nicedir == "." ]; then
265 local blatt="echo retrieving '$nicedir'..."
267 pushd "$directory" &>/dev/null
268 if [ -d "CVS" ]; then
269 if test_writeable "CVS"; then
271 cvs update . | $TO_SPLITTER
272 test_or_die "cvs update"
274 elif [ -d ".svn" ]; then
275 if test_writeable ".svn"; then
277 svn update . | $TO_SPLITTER
278 test_or_die "svn update"
280 elif [ -d ".git" ]; then
281 if test_writeable ".git"; then
284 # classic implementation, but only works with one master branch.
285 # fixes will be forthcoming from development branch.
287 # git pull 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
288 # if [ ${PIPESTATUS[0]} -ne 0 ]; then false; fi
289 # test_or_die "git pull"
292 #let's start over clean here...
295 test_or_die "git remote update"
297 # from: https://stackoverflow.com/questions/3258243/check-if-pull-needed-in-git
298 UPSTREAM=$(parent_branch_name)
299 #argh: original UPSTREAM='${1:-'\''@{u}'\''}'
300 LOCAL=$(git rev-parse @)
301 REMOTE=$(git rev-parse "$UPSTREAM")
302 BASE=$(git merge-base @ "$UPSTREAM")
303 var UPSTREAM LOCAL REMOTE BASE
305 if [ "$LOCAL" == "$REMOTE" ]; then
307 elif [ "$LOCAL" == "$BASE" ]; then
309 elif [ "$REMOTE" == "$BASE" ]; then
315 echo The rest of pull is not being done yet.
318 git pull --no-ff origin
319 test_or_die "git fetch origin"
322 # reslog=$(git log HEAD..origin/master --oneline)
323 # if [[ "${reslog}" != "" ]] ; then
324 # git merge origin/master
328 # # from very helpful page:
329 # # https://stackoverflow.com/questions/10312521/how-to-fetch-all-git-branches
330 # for remote in $( git branch -r | grep -v -- '->' ); do
331 # git branch --track ${remote#origin/} $remote 2>/dev/null
332 ##hmmm: ignoring errors from these, since they are continual.
333 ##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.
336 ##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.
337 # git fetch --all 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
338 # test_or_die "git fetch"
340 # git pull --all 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
341 # test_or_die "git pull"
344 # this is not an error necessarily; we'll just pretend they planned this.
345 echo no repository in $directory
349 restore_terminal_title
354 # gets all the updates for a list of folders under revision control.
355 function checkout_list()
357 local list="$(uniquify $*)"
361 # turn repo list back into an array.
362 eval "repository_list=( ${REPOSITORY_LIST[*]} )"
366 for outer in "${repository_list[@]}"; do
367 # check the repository first, since it might be an absolute path.
368 if [[ $outer =~ /.* ]]; then
369 # yep, this path is absolute. just handle it directly.
370 if [ ! -d "$outer" ]; then continue; fi
372 test_or_die "running update on: $path"
375 for inner in $list; do
376 # add in the directory component to see if we can find the folder.
377 local path="$inner/$outer"
378 if [ ! -d "$path" ]; then continue; fi
380 test_or_die "running update on: $path"
386 restore_terminal_title
389 # provides a list of absolute paths of revision control directories
390 # that are located under the directory passed as the first parameter.
391 function generate_rev_ctrl_filelist()
393 local dir="$1"; shift
394 pushd "$dir" &>/dev/null
395 local dirhere="$( \cd "$(\dirname "$dir")" && /bin/pwd )"
396 local tempfile=$(mktemp /tmp/zz_checkins.XXXXXX)
398 local additional_filter
399 find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".svn" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
400 find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".git" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
401 # CVS is not well behaved like git and (now) svn, and we seldom use it anymore.
404 # see if they've warned us not to try checking in within vendor hierarchies.
405 if [ ! -z "NO_CHECKIN_VENDOR" ]; then
406 sed -i -e '/.*\/vendor\/.*/d' "$tempfile"
409 local sortfile=$(mktemp /tmp/zz_checkin_sort.XXXXXX)
410 sort <"$tempfile" >"$sortfile"
415 # iterates across a list of directories contained in a file (first parameter).
416 # on each directory name, it performs the action (second parameter) provided.
417 function perform_revctrl_action_on_file()
419 local tempfile="$1"; shift
420 local action="$1"; shift
424 while read -u 3 dirname; do
425 if [ -z "$dirname" ]; then continue; fi
426 pushd "$dirname" &>/dev/null
429 test_or_die "performing action $action on: $(pwd)"
434 restore_terminal_title