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