8e9ba78b7ac40b71fb5437cd433ebd4d20961af6
[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 # the name of our "don't check this stuff in" file.
18 export NO_CHECKIN=".no-checkin"
19
20 # use our splitter tool for lengthy output if it's available.
21 if [ ! -z "$(whichable splitter)" ]; then
22   TO_SPLITTER="$(whichable splitter)"
23   # calculate the number of columsn in the terminal.
24   cols=$(get_maxcols)
25   TO_SPLITTER+=" --maxcol $(($cols - 1))"
26 else
27   TO_SPLITTER=cat
28 fi
29
30 ##############
31
32 # one unpleasantry to take care of first; cygwin barfs aggressively if the TMP directory
33 # is a DOS path, but we need it to be a DOS path for our GFFS testing, so that blows.
34 # to get past this, TMP gets changed below to a hopefully generic and safe place.
35 if [[ "$TMP" =~ .:.* ]]; then
36   log_feisty_meow_event "making weirdo temporary directory for PCDOS-style path."
37   export TMP=/tmp/rev_control_$USER
38 fi
39 if [ ! -d "$TMP" ]; then
40   mkdir -p $TMP
41 fi
42 if [ ! -d "$TMP" ]; then
43   echo "could not create the temporary directory TMP in: $TMP"
44   echo "this script will not work properly without an existing TMP directory."
45   echo
46 #hmmm: odd approach to solving the "sourced scripts shouldn't exit or they take down the
47 #      original caller too" issue.
48   echo "hit ctrl-c to stay in this shell now, otherwise it may exit in 10 seconds..."
49   sleep 10
50   exit 1
51 fi
52
53 ##############
54
55 # checks the directory provided into the revision control system repository it belongs to.
56 function do_revctrl_checkin()
57 {
58   local directory="$1"; shift
59
60 #hmmm: another piece of reusable code, to process the directory for printing.
61   # make a nice echoer since we want to use it inside conditions below.
62   local nicedir="$directory"
63   if [ $nicedir == "." ]; then
64     nicedir="$( \cd . && /bin/pwd )"
65 #echo "calculated nicedir as '$nicedir'"
66   fi
67   local blatt_report="echo -ne \nchecking in '$nicedir'...  "
68   local tell_no_checkin="echo -ne \nskipping check-in due to presence of $NO_CHECKIN sentinel file: $nicedir"
69
70   pushd "$directory" &>/dev/null
71 #hmmm: overly elaborate sections below here, but we do want precise handling for git case.
72   if [ -d "CVS" ]; then
73     if test_writeable "CVS"; then
74       do_revctrl_simple_update "$directory"
75       exit_on_error "updating repository; this issue should be fixed before check-in."
76       if [ -f "$NO_CHECKIN" ]; then
77 #        echo -ne "\nskipping check-in due to presence of $NO_CHECKIN sentinel file: $directory"
78         $tell_no_checkin
79       else
80         $blatt_report
81         cvs ci .
82         exit_on_error "cvs checkin"
83       fi
84     fi
85   elif [ -d ".svn" ]; then
86     if test_writeable ".svn"; then
87       do_revctrl_simple_update "$directory"
88       exit_on_error "updating repository; this issue should be fixed before check-in."
89       if [ -f "$NO_CHECKIN" ]; then
90 #        echo -ne "\nskipping check-in due to presence of $NO_CHECKIN sentinel file: $directory"
91         $tell_no_checkin
92       else
93         $blatt_report
94         svn ci .
95         exit_on_error "svn checkin"
96       fi
97     fi
98   elif [ -d ".git" ]; then
99     if test_writeable ".git"; then
100
101       # take steps to make sure the branch integrity is good and we're up to date against remote repos.
102       do_revctrl_careful_update "$(\pwd)"
103
104       if [ -f "$NO_CHECKIN" ]; then
105 #        echo -ne "\nskipping check-in due to presence of $NO_CHECKIN sentinel file: $directory"
106         $tell_no_checkin
107       else
108         $blatt_report
109
110         # put all changed and new files in the commit.  not to everyone's liking.
111         git add --all . | $TO_SPLITTER
112         promote_pipe_return 0
113         exit_on_error "git add all new files"
114
115         # see if there are any changes in the local repository.
116         if ! git diff-index --quiet HEAD --; then
117           # tell git about all the files and get a check-in comment.
118 #hmmm: begins to look like, you guessed it, a reusable bit that all commit actions could enjoy.
119           git commit .
120           retval=$?
121           continue_on_error "git commit"
122           if [ $retval -ne 0 ]; then
123             echo -e -n "Commit failed or was aborted:\nShould we continue with other check-ins? [y/N] "
124             local line
125             read line
126             if [[ "${line:0:1}" != "y" ]]; then
127               echo "Stopping check-in process due to missing commit and user request."
128               exit 1
129             fi
130           fi
131         fi
132
133         # we continue on to the push, even if there were no obvious changes this run, because
134         # there could already be committed changes that haven't been pushed yet.
135
136         # upload any changes to the upstream repo so others can see them.
137         git push --tags origin "$(my_branch_name)" 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
138         promote_pipe_return 0
139         exit_on_error "git push"
140       fi
141     fi
142   else
143     # nothing there.  it's not an error though.
144     log_feisty_meow_event "no repository in $directory"
145   fi
146   popd &>/dev/null
147
148   return 0
149 }
150
151 # shows the local changes in a repository.
152 function do_revctrl_diff
153 {
154   local directory="$1"; shift
155
156   pushd "$directory" &>/dev/null
157
158   # only update if we see a repository living there.
159   if [ -d ".svn" ]; then
160     svn diff .
161     exit_on_error "subversion diff"
162   elif [ -d ".git" ]; then
163     git --no-pager diff 
164     exit_on_error "git diff"
165   elif [ -d "CVS" ]; then
166     cvs diff .
167     exit_on_error "cvs diff"
168   fi
169
170   popd &>/dev/null
171
172   return 0
173 }
174
175 # reports any files that are not already known to the upstream repository.
176 function do_revctrl_report_new
177 {
178   local directory="$1"; shift
179
180   pushd "$directory" &>/dev/null
181
182   # only update if we see a repository living there.
183   if [ -f "$NO_CHECKIN" ]; then
184     echo -ne "\nskipping reporting due to presence of $NO_CHECKIN sentinel file: $directory"
185   elif [ -d ".svn" ]; then
186     # this action so far only makes sense and is needed for svn.
187     bash $FEISTY_MEOW_SCRIPTS/rev_control/svnapply.sh \? echo
188     exit_on_error "svn diff"
189   elif [ -d ".git" ]; then
190     git status -u
191     exit_on_error "git status -u"
192   fi
193
194   popd &>/dev/null
195
196   return 0
197 }
198
199 # checks in all the folders in the specified list.
200 function checkin_list()
201 {
202   # make the list of directories unique.
203   local list="$(uniquify $*)"
204
205   # turn repo list back into an array.
206   eval "repository_list=( ${REPOSITORY_LIST[*]} )"
207
208   local outer inner
209
210   for outer in "${repository_list[@]}"; do
211     # check the repository first, since it might be an absolute path.
212     if [[ $outer =~ /.* ]]; then
213       # yep, this path is absolute.  just handle it directly.
214       if [ ! -d "$outer" ]; then continue; fi
215       do_revctrl_checkin "$outer"
216       exit_on_error "running check-in (absolute) on path: $outer"
217     else
218       for inner in $list; do
219         # add in the directory component to see if we can find the folder.
220         local path="$inner/$outer"
221         if [ ! -d "$path" ]; then continue; fi
222         do_revctrl_checkin "$path"
223         exit_on_error "running check-in (relative) on path: $path"
224       done
225     fi
226   done
227 }
228
229 #hmmm: below functions are git specific and should be named that way.
230
231 function all_branch_names()
232 {
233   echo "$(git branch -vv | cut -d ' ' -f2)"
234 }
235
236 #this had a -> in it at one point for not matching, didn't it?
237 # this reports the upstream branch for the current repo.
238 ##function parent_branch_name()
239 ##{
240   ##echo "$(git branch -vv | grep \* | 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 # reports the status of the branch by echoing one of these values:
251 #   okay: up to date and everything is good.
252 #   needs_pull: this branch needs to be pulled from origins.
253 #   needs_push: there are unsaved changes on this branch to push to remote store.
254 #   diverged: the branches diverged and are going to need a merge.
255 # reference: https://stackoverflow.com/questions/3258243/check-if-pull-needed-in-git
256 function check_branch_state()
257 {
258   local branch="$1"; shift
259
260   if [ -z "$branch" ]; then
261     echo "No branch was passed to check branch state."
262     return 1
263   fi
264
265   local to_return=120  # unknown issue.
266
267   local local_branch=$(git rev-parse HEAD)
268   local remote_branch=$(git rev-parse "$branch")
269   local merge_base=$(git merge-base HEAD "$branch")
270
271   local to_echo=
272   if [ "$local_branch" == "$remote_branch" ]; then
273     to_echo="okay"
274   elif [ "$local_branch" == "$merge_base" ]; then
275     to_echo="needs_pull"
276   elif [ "$remote_branch" == "$merge_base" ]; then
277     to_echo="needs_push"
278   else
279     to_echo="diverged"
280   fi
281
282   echo -n "$to_echo"
283
284   return $to_return
285 }
286
287 # showes the branch currently active in the repository.
288 function show_active_branch()
289 {
290 #hmmm: if no args, assume current dir!?
291
292   for directory in "$@"; do
293     if [ $directory == "." ]; then
294       directory="$( \cd . && /bin/pwd )"
295 #echo "calculated directory as '$directory'"
296     fi
297
298     echo -n -e "$(basename $directory)\n\t=> branch "
299     pushd "$directory" &>/dev/null
300
301 #hmmm: if git...
302     git rev-parse --abbrev-ref HEAD
303 #hmmm: else OTHERS!!!
304
305     popd &>/dev/null
306   done
307 }
308
309 # only shows the branch state if it's not okay.
310 # note that this is not the same as a conditional branch (ha ha).
311 function show_branch_conditionally()
312 {
313   local this_branch="$1"; shift
314
315   local state=$(check_branch_state "$this_branch")
316   if [ "$state" != "okay" ]; then
317     echo "=> branch '$this_branch' state is not clean: $state"
318   fi
319 }
320
321 # the git update process just gets more and more complex when you bring in
322 # branches, so we've moved this here to avoid having a ton of code in the
323 # other methods.
324 function do_revctrl_careful_update()
325 {
326   local directory="$1"; shift
327   pushd "$directory" &>/dev/null
328   exit_on_error "changing to directory: $directory"
329
330   if [ ! -d ".git" ]; then
331     # not a git project, so just boil this down to a getem action.
332     popd &>/dev/null
333     log_feisty_meow_event "skipping careful part and doing simple update on non-git repository: $directory"
334     do_revctrl_simple_update "$directory"
335     return $?
336   fi
337
338 #hmmm: another piece of reusable code, to process the directory for printing.
339   # make a nice echoer since we want to use it inside conditions below.
340   local nicedir="$directory"
341   if [ $nicedir == "." ]; then
342     nicedir=$(\pwd)
343   fi
344   local blatt_report="echo -e \ncarefully retrieving '$nicedir'..."
345   $blatt_report
346
347   local this_branch="$(my_branch_name)"
348
349   show_branch_conditionally "$this_branch"
350
351   # first update all our remote branches to their current state from the repos.
352   git remote update | $TO_SPLITTER
353   promote_pipe_return 0
354   exit_on_error "git remote update"
355
356   show_branch_conditionally "$this_branch"
357
358   # this code is now doing what i have to do when i repair the repo.  and it seems to be good so far.
359   # note that we allow the local branch to be merged with its remote counterpart; otherwise we would
360   # miss changes that happened elsewhere which should be seen in our local copy.
361   local branch_list=$(all_branch_names)
362   local bran
363   for bran in $branch_list; do
364     log_feisty_meow_event "synchronizing remote branch: $bran"
365     git checkout "$bran" | $TO_SPLITTER
366     promote_pipe_return 0
367     exit_on_error "git switching checkout to remote branch: $bran"
368
369     show_branch_conditionally "$this_branch"
370
371     remote_branch_info=$(git ls-remote --heads origin $bran 2>/dev/null)
372     if [ ! -z "$remote_branch_info" ]; then
373       # we are pretty sure the remote branch does exist.
374       git pull --tags origin "$bran" | $TO_SPLITTER
375       promote_pipe_return 0
376     fi
377     exit_on_error "git pull of remote branch: $bran"
378   done
379   # now switch back to our branch.
380   git checkout "$this_branch" | $TO_SPLITTER
381   promote_pipe_return 0
382   exit_on_error "git checking out our current branch: $this_branch"
383
384   # now pull down any changes in our own origin in the repo, to stay in synch
385   # with any changes from others.
386   git fetch --tags --all | $TO_SPLITTER
387 #is the above really important when we did this branch already in the loop?
388 #it does an --all, but is that effective or different?  should we be doing that in above loop?
389   promote_pipe_return 0
390   exit_on_error "git pulling all upstream"
391
392   popd &>/dev/null
393 }
394
395 # gets the latest versions of the assets from the upstream repository.
396 function do_revctrl_simple_update()
397 {
398   directory="$1"; shift
399
400 #hmmm: another piece of reusable code, to process the directory for printing.
401   # make a nice echoer since we want to use it inside conditions below.
402   local nicedir="$directory"
403   if [ $nicedir == "." ]; then
404     nicedir=$(\pwd)
405   fi
406   local blatt_report="echo -e \nretrieving '$nicedir'..."
407
408   pushd "$directory" &>/dev/null
409   if [ -d "CVS" ]; then
410     if test_writeable "CVS"; then
411       $blatt_report
412       cvs update . | $TO_SPLITTER
413       promote_pipe_return 0
414       exit_on_error "cvs update"
415     fi
416   elif [ -d ".svn" ]; then
417     if test_writeable ".svn"; then
418       $blatt_report
419       svn update . | $TO_SPLITTER
420       promote_pipe_return 0
421       exit_on_error "svn update"
422     fi
423   elif [ -d ".git" ]; then
424     if test_writeable ".git"; then
425       $blatt_report
426       git pull --tags 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
427       promote_pipe_return 0
428       exit_on_error "git pull of origin"
429     fi
430   else
431     # this is not an error necessarily; we'll just pretend they planned this.
432     log_feisty_meow_event "no repository in $directory"
433   fi
434   popd &>/dev/null
435
436   return 0
437 }
438
439 # gets all the updates for a list of folders under revision control.
440 function checkout_list()
441 {
442   local list="$(uniquify $*)"
443
444   # turn repo list back into an array.
445   eval "repository_list=( ${REPOSITORY_LIST[*]} )"
446
447   local outer inner
448
449   for outer in "${repository_list[@]}"; do
450     # check the repository first, since it might be an absolute path.
451     if [[ $outer =~ /.* ]]; then
452       # yep, this path is absolute.  just handle it directly.
453       if [ ! -d "$outer" ]; then continue; fi
454       do_revctrl_simple_update $outer
455       exit_on_error "running update on: $path"
456     else
457       for inner in $list; do
458         # add in the directory component to see if we can find the folder.
459         local path="$inner/$outer"
460         if [ ! -d "$path" ]; then continue; fi
461         do_revctrl_simple_update $path
462         exit_on_error "running update on: $path"
463       done
464     fi
465   done
466 }
467
468 # does a careful update on all the folders in the specified list;
469 # it looks in the REPOSITORY_LIST for those names and updates them.
470 # this is just like checkout_list, but it's for the puffing up action
471 # we need to do on git.
472 function puff_out_list()
473 {
474   # make the list of directories unique.
475   local list="$(uniquify $*)"
476
477   # turn repo list back into an array.
478   eval "repository_list=( ${REPOSITORY_LIST[*]} )"
479
480   local outer inner
481
482 #hmmm: once again, seeing some reusable code in this loop...
483   for outer in "${repository_list[@]}"; do
484     # check the repository first, since it might be an absolute path.
485     if [[ $outer =~ /.* ]]; then
486       # yep, this path is absolute.  just handle it directly.
487       if [ ! -d "$outer" ]; then continue; fi
488       do_revctrl_careful_update "$outer"
489       exit_on_error "running puff-out (absolute) on path: $outer"
490     else
491       for inner in $list; do
492         # add in the directory component to see if we can find the folder.
493         local path="$inner/$outer"
494         if [ ! -d "$path" ]; then continue; fi
495         do_revctrl_careful_update "$path"
496         exit_on_error "running puff-out (relative) on path: $path"
497       done
498     fi
499   done
500 }
501
502 # provides a list of absolute paths of revision control directories
503 # that are located under the directory passed as the first parameter.
504 function generate_rev_ctrl_filelist()
505 {
506   local dir="$1"; shift
507   pushd "$dir" &>/dev/null
508   local dirhere="$( \cd "$(\dirname "$dir")" && /bin/pwd )"
509   local tempfile=$(mktemp /tmp/zz_checkins.XXXXXX)
510   echo >$tempfile
511   local additional_filter
512   find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".svn" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
513   find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".git" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
514   # CVS is not well behaved like git and (now) svn, and we seldom use it anymore.
515   popd &>/dev/null
516
517   # see if they've warned us not to try checking in within vendor hierarchies.
518   if [ ! -z "NO_CHECKIN_VENDOR" ]; then
519     sed -i -e '/.*\/vendor\/.*/d' "$tempfile"
520   fi
521
522   local sortfile=$(mktemp /tmp/zz_checkin_sort.XXXXXX)
523   sort <"$tempfile" >"$sortfile"
524   echo "$sortfile"
525   \rm "$tempfile"
526 }
527
528 # iterates across a list of directories contained in a file (first parameter).
529 # on each directory name, it performs the action (second parameter) provided.
530 function perform_revctrl_action_on_file()
531 {
532   local tempfile="$1"; shift
533   local action="$1"; shift
534
535   local did_anything=
536
537   while read -u 3 dirname; do
538     if [ -z "$dirname" ]; then
539       # we often have blank lines in the input file for some reason.
540       continue
541     fi
542     did_anything=yes
543     pushd "$dirname" &>/dev/null
544     echo "[$(pwd)]"
545     # pass the current directory plus the remaining parameters from function invocation.
546     $action . 
547     exit_on_error "performing action $action on: $(pwd)"
548     popd &>/dev/null
549   done 3<"$tempfile"
550
551   if [ -z "$did_anything" ]; then
552     echo "There was nothing to do the action '$action' on."
553   fi
554
555   rm "$tempfile"
556 }
557