abstracted get_maxcols as new function
[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 # the git update process just gets more and more complex when you bring in
292 # branches, so we've moved this here to avoid having a ton of code in the
293 # other methods.
294 function do_careful_git_update()
295 {
296   local directory="$1"; shift
297   pushd "$directory" &>/dev/null
298   test_or_die "changing to directory: $directory"
299
300   if [ ! -d ".git" ]; then
301     # we ignore if they're jumping into a non-useful folder, but also tell them.
302     echo "Directory is not a git repository: $directory"
303     return 0
304   fi
305
306   local this_branch="$(my_branch_name)"
307
308   state=$(check_branch_state "$this_branch")
309   echo "=> branch '$this_branch' state prior to remote update is: $state"
310
311   # first update all our remote branches to their current state from the repos.
312   git remote update | $TO_SPLITTER
313   promote_pipe_return 0
314   test_or_die "git remote update"
315
316   state=$(check_branch_state "$this_branch")
317   echo "=> branch '$this_branch' state after remote update is: $state"
318
319   # this code is now doing what i have to do when i repair the repo.  and it seems to be good so far.
320   local branch_list=$(all_branch_names)
321   local bran
322   for bran in $branch_list; do
323 #    echo "synchronizing remote branch: $bran"
324     git checkout "$bran" | $TO_SPLITTER
325     promote_pipe_return 0
326     test_or_die "git switching checkout to remote branch: $bran"
327
328     state=$(check_branch_state "$bran")
329     echo "=> branch '$bran' state is: $state"
330
331     remote_branch_info=$(git ls-remote --heads origin $bran 2>/dev/null)
332     if [ ! -z "$remote_branch_info" ]; then
333       # we are pretty sure the remote branch does exist.
334       git pull --no-ff origin "$bran" | $TO_SPLITTER
335       promote_pipe_return 0
336
337       echo "=> branch '$bran' state after pull is: $state"
338     fi
339     test_or_die "git pull of remote branch: $bran"
340   done
341   # now switch back to our branch.
342   git checkout "$this_branch" | $TO_SPLITTER
343   promote_pipe_return 0
344   test_or_die "git checking out our current branch: $this_branch"
345
346   # now pull down any changes in our own origin in the repo, to stay in synch
347   # with any changes from others.
348   git pull --no-ff --all | $TO_SPLITTER
349   promote_pipe_return 0
350   test_or_die "git pulling all upstream"
351
352   popd &>/dev/null
353 }
354
355 # gets the latest versions of the assets from the upstream repository.
356 function do_update()
357 {
358   directory="$1"; shift
359
360   save_terminal_title
361
362   # make a nice echoer since we want to use it inside conditions below.
363   local nicedir="$directory"
364   if [ $nicedir == "." ]; then
365     nicedir=$(\pwd)
366   fi
367   local blatt="echo retrieving '$nicedir'..."
368
369   pushd "$directory" &>/dev/null
370   if [ -d "CVS" ]; then
371     if test_writeable "CVS"; then
372       $blatt
373       cvs update . | $TO_SPLITTER
374       promote_pipe_return 0
375       test_or_die "cvs update"
376     fi
377   elif [ -d ".svn" ]; then
378     if test_writeable ".svn"; then
379       $blatt
380       svn update . | $TO_SPLITTER
381       promote_pipe_return 0
382       test_or_die "svn update"
383     fi
384   elif [ -d ".git" ]; then
385     if test_writeable ".git"; then
386       $blatt
387       git pull --no-ff 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
388       promote_pipe_return 0
389       test_or_die "git pull of origin without fast forwards"
390     fi
391   else
392     # this is not an error necessarily; we'll just pretend they planned this.
393     echo no repository in $directory
394   fi
395   popd &>/dev/null
396
397   restore_terminal_title
398
399   return 0
400 }
401
402 # gets all the updates for a list of folders under revision control.
403 function checkout_list()
404 {
405   local list="$(uniquify $*)"
406
407   save_terminal_title
408
409   # turn repo list back into an array.
410   eval "repository_list=( ${REPOSITORY_LIST[*]} )"
411
412   local outer inner
413
414   for outer in "${repository_list[@]}"; do
415     # check the repository first, since it might be an absolute path.
416     if [[ $outer =~ /.* ]]; then
417       # yep, this path is absolute.  just handle it directly.
418       if [ ! -d "$outer" ]; then continue; fi
419       do_update $outer
420       test_or_die "running update on: $path"
421       sep 28
422     else
423       for inner in $list; do
424         # add in the directory component to see if we can find the folder.
425         local path="$inner/$outer"
426         if [ ! -d "$path" ]; then continue; fi
427         do_update $path
428         test_or_die "running update on: $path"
429         sep 28
430       done
431     fi
432   done
433
434   restore_terminal_title
435 }
436
437 # provides a list of absolute paths of revision control directories
438 # that are located under the directory passed as the first parameter.
439 function generate_rev_ctrl_filelist()
440 {
441   local dir="$1"; shift
442   pushd "$dir" &>/dev/null
443   local dirhere="$( \cd "$(\dirname "$dir")" && /bin/pwd )"
444   local tempfile=$(mktemp /tmp/zz_checkins.XXXXXX)
445   echo >$tempfile
446   local additional_filter
447   find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".svn" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
448   find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".git" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
449   # CVS is not well behaved like git and (now) svn, and we seldom use it anymore.
450   popd &>/dev/null
451
452   # see if they've warned us not to try checking in within vendor hierarchies.
453   if [ ! -z "NO_CHECKIN_VENDOR" ]; then
454     sed -i -e '/.*\/vendor\/.*/d' "$tempfile"
455   fi
456
457   local sortfile=$(mktemp /tmp/zz_checkin_sort.XXXXXX)
458   sort <"$tempfile" >"$sortfile"
459   echo "$sortfile"
460   \rm "$tempfile"
461 }
462
463 # iterates across a list of directories contained in a file (first parameter).
464 # on each directory name, it performs the action (second parameter) provided.
465 function perform_revctrl_action_on_file()
466 {
467   local tempfile="$1"; shift
468   local action="$1"; shift
469
470   save_terminal_title
471
472   local did_anything=
473
474   while read -u 3 dirname; do
475     if [ -z "$dirname" ]; then
476       # we often have blank lines in the input file for some reason.
477       continue
478     fi
479     did_anything=yes
480     pushd "$dirname" &>/dev/null
481     echo "[$(pwd)]"
482     # pass the current directory plus the remaining parameters from function invocation.
483     $action . 
484     test_or_die "performing action $action on: $(pwd)"
485     sep 28
486     popd &>/dev/null
487   done 3<"$tempfile"
488
489   if [ -z "$did_anything" ]; then
490     echo "There was nothing to do the action '$action' on."
491   fi
492
493   restore_terminal_title
494
495   rm "$tempfile"
496 }
497