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