497e0f8c390989607b1928556546c07b21374cb2
[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 2>/dev/null)" ]; 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 -n 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 #hmmm: begins to look like, you guessed it, a reusable bit that all commit actions could enjoy.
100         git commit .
101         retval=$?
102         test_or_continue "git commit"
103         if [ $retval -ne 0 ]; then
104           echo -e -n "Commit failed or was aborted:\nShould we continue with other check-ins? [y/N] "
105           local line
106           read line
107           if [[ "${line:0:1}" != "y" ]]; then
108             echo "Stopping check-in process due to missing commit and user request."
109             exit 1
110           fi
111         fi
112       fi
113
114       # a new set of steps we have to take to make sure the branch integrity is good.
115       do_careful_git_update "$(\pwd)"
116
117       # we continue on to the push, even if there were no changes this time, because
118       # there could already be committed changes that haven't been pushed yet.
119
120       # upload any changes to the upstream repo so others can see them.
121       git push --tags origin "$(my_branch_name)" 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
122       promote_pipe_return 0
123       test_or_die "git push"
124
125     fi
126   else
127     # nothing there.  it's not an error though.
128     echo no repository in $directory
129   fi
130   popd &>/dev/null
131
132   restore_terminal_title
133
134   return 0
135 }
136
137 # shows the local changes in a repository.
138 function do_diff
139 {
140   local directory="$1"; shift
141
142   save_terminal_title
143
144   pushd "$directory" &>/dev/null
145
146   # only update if we see a repository living there.
147   if [ -d ".svn" ]; then
148     svn diff .
149     test_or_die "subversion diff"
150   elif [ -d ".git" ]; then
151     git diff 
152     test_or_die "git diff"
153   elif [ -d "CVS" ]; then
154     cvs diff .
155     test_or_die "cvs diff"
156   fi
157
158   popd &>/dev/null
159
160   restore_terminal_title
161
162   return 0
163 }
164
165 # reports any files that are not already known to the upstream repository.
166 function do_report_new
167 {
168   local directory="$1"; shift
169
170   save_terminal_title
171
172   pushd "$directory" &>/dev/null
173
174   # only update if we see a repository living there.
175   if [ -f ".no-checkin" ]; then
176     echo "skipping reporting due to presence of .no-checkin sentinel file."
177   elif [ -d ".svn" ]; then
178     # this action so far only makes sense and is needed for svn.
179     bash $FEISTY_MEOW_SCRIPTS/rev_control/svnapply.sh \? echo
180     test_or_die "svn diff"
181   elif [ -d ".git" ]; then
182     git status -u
183     test_or_die "git status -u"
184   fi
185
186   popd &>/dev/null
187
188   restore_terminal_title
189
190   return 0
191 }
192
193 # checks in all the folders in the specified list.
194 function checkin_list()
195 {
196   # make the list of directories unique.
197   local list="$(uniquify $*)"
198
199   save_terminal_title
200
201   # turn repo list back into an array.
202   eval "repository_list=( ${REPOSITORY_LIST[*]} )"
203
204   local outer inner
205
206   for outer in "${repository_list[@]}"; do
207     # check the repository first, since it might be an absolute path.
208     if [[ $outer =~ /.* ]]; then
209       # yep, this path is absolute.  just handle it directly.
210       if [ ! -d "$outer" ]; then continue; fi
211       do_checkin "$outer"
212       test_or_die "running check-in (absolute) on path: $outer"
213       sep 28
214     else
215       for inner in $list; do
216         # add in the directory component to see if we can find the folder.
217         local path="$inner/$outer"
218         if [ ! -d "$path" ]; then continue; fi
219         do_checkin "$path"
220         test_or_die "running check-in (relative) on path: $path"
221         sep 28
222       done
223     fi
224   done
225
226   restore_terminal_title
227 }
228
229 # does a careful git update on all the folders in the specified list.
230 function puff_out_list()
231 {
232   # make the list of directories unique.
233   local list="$(uniquify $*)"
234
235   save_terminal_title
236
237   # turn repo list back into an array.
238   eval "repository_list=( ${REPOSITORY_LIST[*]} )"
239
240   local outer inner
241
242 #hmmm: once again, seeing some reusable code in this loop...
243   for outer in "${repository_list[@]}"; do
244     # check the repository first, since it might be an absolute path.
245     if [[ $outer =~ /.* ]]; then
246       # yep, this path is absolute.  just handle it directly.
247       if [ ! -d "$outer" ]; then continue; fi
248       do_careful_git_update "$outer"
249       test_or_die "running puff-out (absolute) on path: $outer"
250       sep 28
251     else
252       for inner in $list; do
253         # add in the directory component to see if we can find the folder.
254         local path="$inner/$outer"
255         if [ ! -d "$path" ]; then continue; fi
256         do_careful_git_update "$path"
257         test_or_die "running puff-out (relative) on path: $path"
258         sep 28
259       done
260     fi
261   done
262
263   restore_terminal_title
264 }
265
266 #hmmm: to go below.
267 ### takes out the first few carriage returns that are in the input.
268 ##function squash_first_few_crs()
269 ##{
270   ##i=0
271   ##while read input_text; do
272     ##i=$((i+1))
273     ##if [ $i -le 5 ]; then
274       ##echo -n "$input_text  "
275     ##else
276       ##echo $input_text
277     ##fi
278   ##done
279   ##if [ $i -le 3 ]; then
280     ### if we're still squashing eols, make sure we don't leave them hanging.
281     ##echo
282   ##fi
283 ##}
284
285 #hmmm: the below are git specific and should be named that way.
286
287 function all_branch_names()
288 {
289   echo "$(git branch -vv | cut -d ' ' -f2)"
290 }
291
292 # a helpful method that reports the git branch for the current directory's
293 # git repository.
294 function my_branch_name()
295 {
296   echo "$(git branch -vv | grep '\*' | cut -d ' ' -f2)"
297 }
298
299 #this had a -> in it at one point for not matching, didn't it?
300 # this reports the upstream branch for the current repo.
301 ##function parent_branch_name()
302 ##{
303   ##echo "$(git branch -vv | grep \* | cut -d ' ' -f2)"
304 ##}
305
306 # reports the status of the branch by echoing one of these values:
307 #   okay: up to date and everything is good.
308 #   needs_pull: this branch needs to be pulled from origins.
309 #   needs_push: there are unsaved changes on this branch to push to remote store.
310 #   diverged: the branches diverged and are going to need a merge.
311 # reference: https://stackoverflow.com/questions/3258243/check-if-pull-needed-in-git
312 function check_branch_state()
313 {
314   local branch="$1"; shift
315
316   if [ -z "$branch" ]; then
317     echo "No branch was passed to check branch state."
318     return 1
319   fi
320
321   local to_return=120  # unknown issue.
322
323   local local_branch=$(git rev-parse @)
324   local remote_branch=$(git rev-parse "$branch")
325   local merge_base=$(git merge-base @ "$branch")
326
327   local to_echo=
328   if [ "$local_branch" == "$remote_branch" ]; then
329     to_echo="okay"
330   elif [ "$local_branch" == "$merge_base" ]; then
331     to_echo="needs_pull"
332   elif [ "$remote_branch" == "$merge_base" ]; then
333     to_echo="needs_push"
334   else
335     to_echo="diverged"
336   fi
337
338   echo -n "$to_echo"
339
340   return $to_return
341 }
342
343 # only shows the branch state if it's not okay.
344 # note that this is not the same as a conditional branch (ha ha).
345 function show_branch_conditionally()
346 {
347   local this_branch="$1"; shift
348
349   local state=$(check_branch_state "$this_branch")
350   if [ "$state" != "okay" ]; then
351     echo "=> branch '$this_branch' state is not clean: $state"
352   fi
353 }
354
355 # the git update process just gets more and more complex when you bring in
356 # branches, so we've moved this here to avoid having a ton of code in the
357 # other methods.
358 function do_careful_git_update()
359 {
360   local directory="$1"; shift
361   pushd "$directory" &>/dev/null
362   test_or_die "changing to directory: $directory"
363
364   if [ ! -d ".git" ]; then
365     # we ignore if they're jumping into a non-useful folder, but also tell them.
366     echo "Directory is not a git repository: $directory"
367     return 0
368   fi
369
370   local this_branch="$(my_branch_name)"
371
372   show_branch_conditionally "$this_branch"
373
374   # first update all our remote branches to their current state from the repos.
375   git remote update | $TO_SPLITTER
376   promote_pipe_return 0
377   test_or_die "git remote update"
378
379   show_branch_conditionally "$this_branch"
380
381   # this code is now doing what i have to do when i repair the repo.  and it seems to be good so far.
382   # note that we allow the local branch to be merged with its remote counterpart; otherwise we would
383   # miss changes that happened elsewhere which should be seen in our local copy.
384   local branch_list=$(all_branch_names)
385   local bran
386   for bran in $branch_list; do
387 #    echo "synchronizing remote branch: $bran"
388     git checkout "$bran" | $TO_SPLITTER
389     promote_pipe_return 0
390     test_or_die "git switching checkout to remote branch: $bran"
391
392     show_branch_conditionally "$this_branch"
393
394     remote_branch_info=$(git ls-remote --heads origin $bran 2>/dev/null)
395     if [ ! -z "$remote_branch_info" ]; then
396       # we are pretty sure the remote branch does exist.
397       git pull origin "$bran" | $TO_SPLITTER
398 # we may want to choose to do fast forward, to avoid crazy multiple merge histories
399 # without any changes in them.  --no-ff
400       promote_pipe_return 0
401     fi
402     test_or_die "git pull of remote branch: $bran"
403   done
404   # now switch back to our branch.
405   git checkout "$this_branch" | $TO_SPLITTER
406   promote_pipe_return 0
407   test_or_die "git checking out our current branch: $this_branch"
408
409   # now pull down any changes in our own origin in the repo, to stay in synch
410   # with any changes from others.
411   git pull --all | $TO_SPLITTER
412 #is the above really important when we did this branch already in the loop?
413 #it does an --all, but is that effective or different?  should we be doing that in above loop?
414 # --no-ff   
415   promote_pipe_return 0
416   test_or_die "git pulling all upstream"
417
418   popd &>/dev/null
419 }
420
421 # gets the latest versions of the assets from the upstream repository.
422 function do_update()
423 {
424   directory="$1"; shift
425
426   save_terminal_title
427
428   # make a nice echoer since we want to use it inside conditions below.
429   local nicedir="$directory"
430   if [ $nicedir == "." ]; then
431     nicedir=$(\pwd)
432   fi
433   local blatt="echo retrieving '$nicedir'..."
434
435   pushd "$directory" &>/dev/null
436   if [ -d "CVS" ]; then
437     if test_writeable "CVS"; then
438       $blatt
439       cvs update . | $TO_SPLITTER
440       promote_pipe_return 0
441       test_or_die "cvs update"
442     fi
443   elif [ -d ".svn" ]; then
444     if test_writeable ".svn"; then
445       $blatt
446       svn update . | $TO_SPLITTER
447       promote_pipe_return 0
448       test_or_die "svn update"
449     fi
450   elif [ -d ".git" ]; then
451     if test_writeable ".git"; then
452       $blatt
453       git pull 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
454 #ordinary pulls should be allowed to do fast forward: --no-ff 
455       promote_pipe_return 0
456       test_or_die "git pull of origin"
457     fi
458   else
459     # this is not an error necessarily; we'll just pretend they planned this.
460     echo no repository in $directory
461   fi
462   popd &>/dev/null
463
464   restore_terminal_title
465
466   return 0
467 }
468
469 # gets all the updates for a list of folders under revision control.
470 function checkout_list()
471 {
472   local list="$(uniquify $*)"
473
474   save_terminal_title
475
476   # turn repo list back into an array.
477   eval "repository_list=( ${REPOSITORY_LIST[*]} )"
478
479   local outer inner
480
481   for outer in "${repository_list[@]}"; do
482     # check the repository first, since it might be an absolute path.
483     if [[ $outer =~ /.* ]]; then
484       # yep, this path is absolute.  just handle it directly.
485       if [ ! -d "$outer" ]; then continue; fi
486       do_update $outer
487       test_or_die "running update on: $path"
488       sep 28
489     else
490       for inner in $list; do
491         # add in the directory component to see if we can find the folder.
492         local path="$inner/$outer"
493         if [ ! -d "$path" ]; then continue; fi
494         do_update $path
495         test_or_die "running update on: $path"
496         sep 28
497       done
498     fi
499   done
500
501   restore_terminal_title
502 }
503
504 # provides a list of absolute paths of revision control directories
505 # that are located under the directory passed as the first parameter.
506 function generate_rev_ctrl_filelist()
507 {
508   local dir="$1"; shift
509   pushd "$dir" &>/dev/null
510   local dirhere="$( \cd "$(\dirname "$dir")" && /bin/pwd )"
511   local tempfile=$(mktemp /tmp/zz_checkins.XXXXXX)
512   echo >$tempfile
513   local additional_filter
514   find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".svn" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
515   find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".git" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
516   # CVS is not well behaved like git and (now) svn, and we seldom use it anymore.
517   popd &>/dev/null
518
519   # see if they've warned us not to try checking in within vendor hierarchies.
520   if [ ! -z "NO_CHECKIN_VENDOR" ]; then
521     sed -i -e '/.*\/vendor\/.*/d' "$tempfile"
522   fi
523
524   local sortfile=$(mktemp /tmp/zz_checkin_sort.XXXXXX)
525   sort <"$tempfile" >"$sortfile"
526   echo "$sortfile"
527   \rm "$tempfile"
528 }
529
530 # iterates across a list of directories contained in a file (first parameter).
531 # on each directory name, it performs the action (second parameter) provided.
532 function perform_revctrl_action_on_file()
533 {
534   local tempfile="$1"; shift
535   local action="$1"; shift
536
537   save_terminal_title
538
539   local did_anything=
540
541   while read -u 3 dirname; do
542     if [ -z "$dirname" ]; then
543       # we often have blank lines in the input file for some reason.
544       continue
545     fi
546     did_anything=yes
547     pushd "$dirname" &>/dev/null
548     echo "[$(pwd)]"
549     # pass the current directory plus the remaining parameters from function invocation.
550     $action . 
551     test_or_die "performing action $action on: $(pwd)"
552     sep 28
553     popd &>/dev/null
554   done 3<"$tempfile"
555
556   if [ -z "$did_anything" ]; then
557     echo "There was nothing to do the action '$action' on."
558   fi
559
560   restore_terminal_title
561
562   rm "$tempfile"
563 }
564