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