more diagnostics
[feisty_meow.git] / scripts / rev_control / version_control.sh
1 #!/bin/bash
2
3 # these are helper functions for doing localized revision control.
4 # this script should be sourced into other scripts that use it.
5
6 # Author: Chris Koeritz
7 # Author: Kevin Wentworth
8
9 source "$FEISTY_MEOW_SCRIPTS/core/launch_feisty_meow.sh"
10 source "$FEISTY_MEOW_SCRIPTS/tty/terminal_titler.sh"
11
12 ##############
13
14 # the maximum depth that the recursive functions will try to go below the starting directory.
15 export MAX_DEPTH=5
16
17 # use our splitter tool for lengthy output if it's available.
18 if [ ! -z "$(which splitter)" ]; then
19   TO_SPLITTER="$(which splitter)"
20
21 #hmmm: another reusable chunk here, getting terminal size.
22   # calculate the number of columsn in the terminal.
23   cols=$(stty size | awk '{print $2}')
24   TO_SPLITTER+=" --maxcol $(($cols - 1))"
25 else
26   TO_SPLITTER=cat
27 fi
28
29 ##############
30
31 #hmmm: move this to core
32 # this makes the status of pipe N into the main return value.
33 function promote_pipe_return()
34 {
35   ( exit ${PIPESTATUS[$1]} )
36 }
37
38 ##############
39
40 # one unpleasantry to take care of first; cygwin barfs aggressively if the TMP directory
41 # is a DOS path, but we need it to be a DOS path for our GFFS testing, so that blows.
42 # to get past this, TMP gets changed below to a hopefully generic and safe place.
43 if [[ "$TMP" =~ .:.* ]]; then
44   echo "making weirdo temporary directory for PCDOS-style path."
45   export TMP=/tmp/rev_control_$USER
46 fi
47 if [ ! -d "$TMP" ]; then
48   mkdir -p $TMP
49 fi
50 if [ ! -d "$TMP" ]; then
51   echo "could not create the temporary directory TMP in: $TMP"
52   echo "this script will not work properly without an existing TMP directory."
53 fi
54
55 ##############
56
57 # checks the directory provided into the revision control system repository it belongs to.
58 function do_checkin()
59 {
60   local directory="$1"; shift
61
62   save_terminal_title
63
64   # make a nice echoer since we want to use it inside conditions below.
65   local nicedir="$directory"
66   if [ $nicedir == "." ]; then
67     nicedir=$(\pwd)
68   fi
69   local blatt="echo checking in '$nicedir'..."
70
71   do_update "$directory"
72   test_or_die "repository update--this should be fixed before check-in."
73
74   pushd "$directory" &>/dev/null
75   if [ -f ".no-checkin" ]; then
76     echo "skipping check-in due to presence of .no-checkin sentinel file."
77   elif [ -d "CVS" ]; then
78     if test_writeable "CVS"; then
79       $blatt
80       cvs ci .
81       test_or_die "cvs checkin"
82     fi
83   elif [ -d ".svn" ]; then
84     if test_writeable ".svn"; then
85       $blatt
86       svn ci .
87       test_or_die "svn checkin"
88     fi
89   elif [ -d ".git" ]; then
90     if test_writeable ".git"; then
91       $blatt
92
93       # put all changed and new files in the commit.  not to everyone's liking.
94       git add --all . | $TO_SPLITTER
95       promote_pipe_return 0
96       test_or_die "git add all new files"
97
98       # see if there are any changes in the local repository.
99       if ! git diff-index --quiet HEAD --; then
100         # tell git about all the files and get a check-in comment.
101         git commit .
102         test_or_die "git commit"
103       fi
104
105       # a new set of steps we have to take to make sure the branch integrity is good.
106       do_careful_git_update "$(\pwd)"
107
108       # we continue on to the push, even if there were no changes this time, because
109       # there could already be committed changes that haven't been pushed yet.
110
111       # upload any changes to the upstream repo so others can see them.
112       git push origin "$(my_branch_name)" 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
113       promote_pipe_return 0
114       test_or_die "git push"
115
116     fi
117   else
118     # nothing there.  it's not an error though.
119     echo no repository in $directory
120   fi
121   popd &>/dev/null
122
123   restore_terminal_title
124
125   return 0
126 }
127
128 # shows the local changes in a repository.
129 function do_diff
130 {
131   local directory="$1"; shift
132
133   save_terminal_title
134
135   pushd "$directory" &>/dev/null
136
137   # only update if we see a repository living there.
138   if [ -d ".svn" ]; then
139     svn diff .
140     test_or_die "subversion diff"
141   elif [ -d ".git" ]; then
142     git diff 
143     test_or_die "git diff"
144   elif [ -d "CVS" ]; then
145     cvs diff .
146     test_or_die "cvs diff"
147   fi
148
149   popd &>/dev/null
150
151   restore_terminal_title
152
153   return 0
154 }
155
156 # reports any files that are not already known to the upstream repository.
157 function do_report_new
158 {
159   local directory="$1"; shift
160
161   save_terminal_title
162
163   pushd "$directory" &>/dev/null
164
165   # only update if we see a repository living there.
166   if [ -f ".no-checkin" ]; then
167     echo "skipping reporting due to presence of .no-checkin sentinel file."
168   elif [ -d ".svn" ]; then
169     # this action so far only makes sense and is needed for svn.
170     bash $FEISTY_MEOW_SCRIPTS/rev_control/svnapply.sh \? echo
171     test_or_die "svn diff"
172   elif [ -d ".git" ]; then
173     git status -u
174     test_or_die "git status -u"
175   fi
176
177   popd &>/dev/null
178
179   restore_terminal_title
180
181   return 0
182 }
183
184 # checks in all the folders in a specified list.
185 function checkin_list()
186 {
187   # make the list of directories unique.
188   local list="$(uniquify $*)"
189
190   save_terminal_title
191
192   # turn repo list back into an array.
193   eval "repository_list=( ${REPOSITORY_LIST[*]} )"
194
195   local outer inner
196
197   for outer in "${repository_list[@]}"; do
198     # check the repository first, since it might be an absolute path.
199     if [[ $outer =~ /.* ]]; then
200       # yep, this path is absolute.  just handle it directly.
201       if [ ! -d "$outer" ]; then continue; fi
202       do_checkin $outer
203       test_or_die "running check-in (absolute) on path: $outer"
204       sep 28
205     else
206       for inner in $list; do
207         # add in the directory component to see if we can find the folder.
208         local path="$inner/$outer"
209         if [ ! -d "$path" ]; then continue; fi
210         do_checkin $path
211         test_or_die "running check-in (relative) on path: $path"
212         sep 28
213       done
214     fi
215   done
216
217   restore_terminal_title
218 }
219
220 # takes out the first few carriage returns that are in the input.
221 function squash_first_few_crs()
222 {
223   i=0
224   while read input_text; do
225     i=$((i+1))
226     if [ $i -le 5 ]; then
227       echo -n "$input_text  "
228     else
229       echo $input_text
230     fi
231   done
232   if [ $i -le 3 ]; then
233     # if we're still squashing eols, make sure we don't leave them hanging.
234     echo
235   fi
236 }
237
238 #hmmm: the below are git specific and should be named that way.
239
240 function all_branch_names()
241 {
242   echo "$(git branch -vv | cut -d ' ' -f2)"
243 }
244
245 # a helpful method that reports the git branch for the current directory's
246 # git repository.
247 function my_branch_name()
248 {
249   echo "$(git branch -vv | grep '\*' | cut -d ' ' -f2)"
250 }
251
252 #this had a -> in it at one point for not matching, didn't it?
253 # this reports the upstream branch for the current repo.
254 ##function parent_branch_name()
255 ##{
256   ##echo "$(git branch -vv | grep \* | cut -d ' ' -f2)"
257 ##}
258
259 # reports the status of the branch by echoing one of these values:
260 #   okay: up to date and everything is good.
261 #   needs_pull: this branch needs to be pulled from origins.
262 #   needs_push: there are unsaved changes on this branch to push to remote store.
263 #   diverged: the branches diverged and are going to need a merge.
264 # reference: https://stackoverflow.com/questions/3258243/check-if-pull-needed-in-git
265 function check_branch_state()
266 {
267   local branch="$1"; shift
268
269   local to_return=120  # unknown issue.
270
271   local local_branch=$(git rev-parse @)
272   local remote_branch=$(git rev-parse "$branch")
273   local merge_base=$(git merge-base @ "$branch")
274
275   if [ "$local_branch" == "$remote_branch" ]; then
276     echo "okay"
277   elif [ "$local_branch" == "$merge_base" ]; then
278     echo "needs_pull"
279   elif [ "$remote_branch" == "$merge_base" ]; then
280     echo "needs_push"
281   else
282     echo "diverged"
283   fi
284
285   return $to_return
286 }
287
288 # the git update process just gets more and more complex when you bring in
289 # branches, so we've moved this here to avoid having a ton of code in the
290 # other methods.
291 function do_careful_git_update()
292 {
293   local directory="$1"; shift
294   pushd "$directory" &>/dev/null
295   test_or_die "changing to directory: $directory"
296
297   if [ ! -d ".git" ]; then
298     # we ignore if they're jumping into a non-useful folder, but also tell them.
299     echo "Directory is not a git repository: $directory"
300     return 0
301   fi
302
303   state=$(check_branch_state "$this_branch")
304   echo "=> branch '$this_branch' state prior to remote update is: $state"
305
306   # first update all our remote branches to their current state from the repos.
307   git remote update | $TO_SPLITTER
308   promote_pipe_return 0
309   test_or_die "git remote update"
310
311   local this_branch="$(my_branch_name)"
312 #appears to be useless; reports no changes when we need to know about remote changes that do exist:
313 #hmmm: trying it out again now that things are better elsewhere.  let's see what it says.
314   state=$(check_branch_state "$this_branch")
315   echo "=> branch '$this_branch' state after remote update is: $state"
316
317   # this code is now doing what i have to do when i repair the repo.  and it seems to be good so far.
318   local branch_list=$(all_branch_names)
319   local bran
320   for bran in $branch_list; do
321 #    echo "synchronizing remote branch: $bran"
322     git checkout "$bran" | $TO_SPLITTER
323     promote_pipe_return 0
324     test_or_die "git switching checkout to remote branch: $bran"
325
326     state=$(check_branch_state "$bran")
327     echo "=> branch '$bran' state is: $state"
328
329     remote_branch_info=$(git ls-remote --heads origin $bran 2>/dev/null)
330     if [ ! -z "$remote_branch_info" ]; then
331       # we are pretty sure the remote branch does exist.
332       git pull --no-ff origin "$bran" | $TO_SPLITTER
333       promote_pipe_return 0
334
335       echo "=> branch '$bran' state after pull is: $state"
336     fi
337     test_or_die "git pull of remote branch: $bran"
338   done
339   # now switch back to our branch.
340   git checkout "$this_branch" | $TO_SPLITTER
341   promote_pipe_return 0
342   test_or_die "git checking out our current branch: $this_branch"
343
344   # now pull down any changes in our own origin in the repo, to stay in synch
345   # with any changes from others.
346   git pull --no-ff --all | $TO_SPLITTER
347   promote_pipe_return 0
348   test_or_die "git pulling all upstream"
349
350   popd &>/dev/null
351 }
352
353 # gets the latest versions of the assets from the upstream repository.
354 function do_update()
355 {
356   directory="$1"; shift
357
358   save_terminal_title
359
360   # make a nice echoer since we want to use it inside conditions below.
361   local nicedir="$directory"
362   if [ $nicedir == "." ]; then
363     nicedir=$(\pwd)
364   fi
365   local blatt="echo retrieving '$nicedir'..."
366
367   pushd "$directory" &>/dev/null
368   if [ -d "CVS" ]; then
369     if test_writeable "CVS"; then
370       $blatt
371       cvs update . | $TO_SPLITTER
372       promote_pipe_return 0
373       test_or_die "cvs update"
374     fi
375   elif [ -d ".svn" ]; then
376     if test_writeable ".svn"; then
377       $blatt
378       svn update . | $TO_SPLITTER
379       promote_pipe_return 0
380       test_or_die "svn update"
381     fi
382   elif [ -d ".git" ]; then
383     if test_writeable ".git"; then
384       $blatt
385       git pull --no-ff 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
386       promote_pipe_return 0
387       test_or_die "git pull of origin without fast forwards"
388     fi
389   else
390     # this is not an error necessarily; we'll just pretend they planned this.
391     echo no repository in $directory
392   fi
393   popd &>/dev/null
394
395   restore_terminal_title
396
397   return 0
398 }
399
400 # gets all the updates for a list of folders under revision control.
401 function checkout_list()
402 {
403   local list="$(uniquify $*)"
404
405   save_terminal_title
406
407   # turn repo list back into an array.
408   eval "repository_list=( ${REPOSITORY_LIST[*]} )"
409
410   local outer inner
411
412   for outer in "${repository_list[@]}"; do
413     # check the repository first, since it might be an absolute path.
414     if [[ $outer =~ /.* ]]; then
415       # yep, this path is absolute.  just handle it directly.
416       if [ ! -d "$outer" ]; then continue; fi
417       do_update $outer
418       test_or_die "running update on: $path"
419       sep 28
420     else
421       for inner in $list; do
422         # add in the directory component to see if we can find the folder.
423         local path="$inner/$outer"
424         if [ ! -d "$path" ]; then continue; fi
425         do_update $path
426         test_or_die "running update on: $path"
427         sep 28
428       done
429     fi
430   done
431
432   restore_terminal_title
433 }
434
435 # provides a list of absolute paths of revision control directories
436 # that are located under the directory passed as the first parameter.
437 function generate_rev_ctrl_filelist()
438 {
439   local dir="$1"; shift
440   pushd "$dir" &>/dev/null
441   local dirhere="$( \cd "$(\dirname "$dir")" && /bin/pwd )"
442   local tempfile=$(mktemp /tmp/zz_checkins.XXXXXX)
443   echo >$tempfile
444   local additional_filter
445   find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".svn" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
446   find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".git" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
447   # CVS is not well behaved like git and (now) svn, and we seldom use it anymore.
448   popd &>/dev/null
449
450   # see if they've warned us not to try checking in within vendor hierarchies.
451   if [ ! -z "NO_CHECKIN_VENDOR" ]; then
452     sed -i -e '/.*\/vendor\/.*/d' "$tempfile"
453   fi
454
455   local sortfile=$(mktemp /tmp/zz_checkin_sort.XXXXXX)
456   sort <"$tempfile" >"$sortfile"
457   echo "$sortfile"
458   \rm "$tempfile"
459 }
460
461 # iterates across a list of directories contained in a file (first parameter).
462 # on each directory name, it performs the action (second parameter) provided.
463 function perform_revctrl_action_on_file()
464 {
465   local tempfile="$1"; shift
466   local action="$1"; shift
467
468   save_terminal_title
469
470   local did_anything=
471
472   while read -u 3 dirname; do
473     if [ -z "$dirname" ]; then
474       # we often have blank lines in the input file for some reason.
475       continue
476     fi
477     did_anything=yes
478     pushd "$dirname" &>/dev/null
479     echo "[$(pwd)]"
480     # pass the current directory plus the remaining parameters from function invocation.
481     $action . 
482     test_or_die "performing action $action on: $(pwd)"
483     sep 28
484     popd &>/dev/null
485   done 3<"$tempfile"
486
487   if [ -z "$did_anything" ]; then
488     echo "There was nothing to do the action '$action' on."
489   fi
490
491   restore_terminal_title
492
493   rm "$tempfile"
494 }
495