96d4e4dca23b81245b2bbe2f2c73286ae5da5ff5
[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 #hmmm: we need to dump all the outputs in this script into splitter
13
14 ##############
15
16 # the maximum depth that the recursive functions will try to go below the starting directory.
17 export MAX_DEPTH=5
18
19 # use our splitter tool for lengthy output if it's available.
20 if [ ! -z "$(which splitter)" ]; then
21   TO_SPLITTER="$(which splitter)"
22 else
23   TO_SPLITTER=cat
24 fi
25
26 ##############
27
28 # one unpleasantry to take care of first; cygwin barfs aggressively if the TMP directory
29 # is a DOS path, but we need it to be a DOS path for our GFFS testing, so that blows.
30 # to get past this, TMP gets changed below to a hopefully generic and safe place.
31 if [[ "$TMP" =~ .:.* ]]; then
32   echo "making weirdo temporary directory for PCDOS-style path."
33   export TMP=/tmp/rev_control_$USER
34 fi
35 if [ ! -d "$TMP" ]; then
36   mkdir -p $TMP
37 fi
38 if [ ! -d "$TMP" ]; then
39   echo "could not create the temporary directory TMP in: $TMP"
40   echo "this script will not work properly without an existing TMP directory."
41 fi
42
43 ##############
44
45 # checks the directory provided into the revision control system repository it belongs to.
46 function do_checkin()
47 {
48   local directory="$1"; shift
49
50   save_terminal_title
51
52   # make a nice echoer since we want to use it inside conditions below.
53   local nicedir="$directory"
54   if [ $nicedir == "." ]; then
55     nicedir=$(\pwd)
56   fi
57   local blatt="echo checking in '$nicedir'..."
58
59   do_update "$directory"
60   test_or_die "repository update--this should be fixed before check-in."
61
62   pushd "$directory" &>/dev/null
63   if [ -f ".no-checkin" ]; then
64     echo "skipping check-in due to presence of .no-checkin sentinel file."
65   elif [ -d "CVS" ]; then
66     if test_writeable "CVS"; then
67       $blatt
68       cvs ci .
69       test_or_die "cvs checkin"
70     fi
71   elif [ -d ".svn" ]; then
72     if test_writeable ".svn"; then
73       $blatt
74       svn ci .
75       test_or_die "svn checkin"
76     fi
77   elif [ -d ".git" ]; then
78     if test_writeable ".git"; then
79       $blatt
80
81       # put all changed and new files in the commit.  not to everyone's liking.
82       git add --all .
83       test_or_die "git add all new files"
84
85       # see if there are any changes in the local repository.
86       if ! git diff-index --quiet HEAD --; then
87         # tell git about all the files and get a check-in comment.
88         git commit .
89         test_or_die "git commit"
90       fi
91
92 #      # upload the files to the server so others can see them.
93 #      git push 2>&1 | grep -v "X11 forwarding request failed"
94 #      if [ ${PIPESTATUS[0]} -ne 0 ]; then false; fi
95 #      test_or_die "git push"
96
97       # a new set of steps we have to take to make sure the branch integrity is good.
98       careful_git_update 
99
100       # we continue on to the push, even if there were no changes this time, because
101       # there could already be committed changes that haven't been pushed yet.
102
103       # upload any changes to the upstream repo so others can see them.
104       git push 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
105       test_or_die "git push"
106
107     fi
108   else
109     # nothing there.  it's not an error though.
110     echo no repository in $directory
111   fi
112   popd &>/dev/null
113
114   restore_terminal_title
115
116   true;
117 }
118
119 # shows the local changes in a repository.
120 function do_diff
121 {
122   local directory="$1"; shift
123
124   save_terminal_title
125
126   pushd "$directory" &>/dev/null
127
128   # only update if we see a repository living there.
129   if [ -d ".svn" ]; then
130     svn diff .
131     test_or_die "subversion diff"
132   elif [ -d ".git" ]; then
133     git diff 
134     test_or_die "git diff"
135   elif [ -d "CVS" ]; then
136     cvs diff .
137     test_or_die "cvs diff"
138   fi
139
140   popd &>/dev/null
141
142   restore_terminal_title
143
144   true;
145 }
146
147 # reports any files that are not already known to the upstream repository.
148 function do_report_new
149 {
150   local directory="$1"; shift
151
152   save_terminal_title
153
154   pushd "$directory" &>/dev/null
155
156   # only update if we see a repository living there.
157   if [ -f ".no-checkin" ]; then
158     echo "skipping reporting due to presence of .no-checkin sentinel file."
159   elif [ -d ".svn" ]; then
160     # this action so far only makes sense and is needed for svn.
161     bash $FEISTY_MEOW_SCRIPTS/rev_control/svnapply.sh \? echo
162     test_or_die "svn diff"
163   elif [ -d ".git" ]; then
164     git status -u
165     test_or_die "git status -u"
166   fi
167
168   popd &>/dev/null
169
170   restore_terminal_title
171
172   true
173 }
174
175 # checks in all the folders in a specified list.
176 function checkin_list()
177 {
178   # make the list of directories unique.
179   local list="$(uniquify $*)"
180
181   save_terminal_title
182
183   # turn repo list back into an array.
184   eval "repository_list=( ${REPOSITORY_LIST[*]} )"
185
186   local outer inner
187
188   for outer in "${repository_list[@]}"; do
189     # check the repository first, since it might be an absolute path.
190     if [[ $outer =~ /.* ]]; then
191       # yep, this path is absolute.  just handle it directly.
192       if [ ! -d "$outer" ]; then continue; fi
193       do_checkin $outer
194       test_or_die "running check-in (absolute) on path: $outer"
195       sep 28
196     else
197       for inner in $list; do
198         # add in the directory component to see if we can find the folder.
199         local path="$inner/$outer"
200         if [ ! -d "$path" ]; then continue; fi
201         do_checkin $path
202         test_or_die "running check-in (relative) on path: $path"
203         sep 28
204       done
205     fi
206   done
207
208   restore_terminal_title
209 }
210
211 # takes out the first few carriage returns that are in the input.
212 function squash_first_few_crs()
213 {
214   i=0
215   while read input_text; do
216     i=$((i+1))
217     if [ $i -le 5 ]; then
218       echo -n "$input_text  "
219     else
220       echo $input_text
221     fi
222   done
223   if [ $i -le 3 ]; then
224     # if we're still squashing eols, make sure we don't leave them hanging.
225     echo
226   fi
227 }
228
229 # a helpful method that reports the git branch for the current directory's
230 # git repository.
231 function my_branch_name()
232 {
233   echo "$(git branch | grep '\*' | 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 # this exits with 0 for success (normal bash behavior) when up to date.  if the branch is not up to date,
244 # then these values are returned:
245 #DOCUMENT THE VALUES
246 # reference: https://stackoverflow.com/questions/3258243/check-if-pull-needed-in-git
247 function check_branch_state()
248 {
249   local branch="$1"; shift
250
251   local to_return=120  # unknown issue.
252
253 sep
254
255   LOCAL=$(git rev-parse @)
256   REMOTE=$(git rev-parse "$branch")
257   BASE=$(git merge-base @ "$branch")
258 var branch LOCAL REMOTE BASE
259
260   if [ "$LOCAL" == "$REMOTE" ]; then
261     echo "Up-to-date"
262     to_return=0
263   elif [ "$LOCAL" == "$BASE" ]; then
264     echo "Need to pull"
265     to_return=1
266   elif [ "$REMOTE" == "$BASE" ]; then
267     echo "Need to push"
268     to_return=2
269   else
270     echo "Diverged"
271     to_return=3
272   fi
273
274 sep
275
276   return $to_return
277 }
278
279 # the git update process just gets more and more complex when you bring in
280 # branches, so we've moved this here to avoid having a ton of code in the
281 # do_checkin method.
282 function careful_git_update()
283 {
284
285   local this_branch="$(my_branch_name)"
286
287 #we want my branch here, don't we?  not like parent or anything?
288   check_branch_state "$this_branch"
289   state=$?
290   test_or_continue "branch state check"
291   echo the branch state is $state
292 #need to instead do something here if fails.
293 # above is worse than useless code; in the situations i'm seeing fail, it reports no changes.  *&@#*&@#
294
295 echo DOING BRANCH WALKER
296   # the above are just not enough.  this code is now doing what i have to do when i repair the repo.
297   local branch_list=$(git branch |grep -v '^\*')
298   local bran
299   for bran in $branch_list; do
300 echo GETTING LATEST ON: $bran
301     git checkout "$bran"
302     test_or_die "git checking out remote branch: $bran"
303     git pull --no-ff
304     test_or_die "git pull of remote branch: $bran"
305   done
306   # now switch back to our branch.
307   git checkout "$this_branch"
308   test_or_die "git checking out our current branch: $this_branch"
309
310 echo NOW REMOTE UPDATE
311
312   # first update all our remote branches to their current state from the repos.
313   git remote update
314   test_or_die "git remote update"
315
316 echo NOW THE FULL PULL
317   # now pull down any changes in our own origin in the repo, to stay in synch
318   # with any changes from others.
319   git pull --no-ff --all
320   test_or_die "git pulling all upstream"
321
322 echo DONE CAREFUL UPDATE
323
324   return 0
325
326 # below has older shards of partial knowledge.
327
328 #      reslog=$(git log HEAD..origin/master --oneline)
329 #      if [[ "${reslog}" != "" ]] ; then
330 #        git merge origin/master
331
332 #      # from very helpful page:
333 #      # https://stackoverflow.com/questions/10312521/how-to-fetch-all-git-branches
334 #      for remote in $( git branch -r | grep -v -- '->' ); do
335 #        git branch --track ${remote#origin/} $remote 2>/dev/null
336 ##hmmm: ignoring errors from these, since they are continual.
337 ##hmmm: if we could find a way to not try to track with a local branch when there's already one present, that would be swell.  it's probably simple.
338 #      done
339 #
340 ##hmmm: well, one time it failed without the fetch.  i hope that's because the fetch is actually needed and not because the whole approach is fubar.
341 #      git fetch --all 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
342 #      test_or_die "git fetch"
343 #
344 #      git pull --all 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
345 #      test_or_die "git pull"
346
347 }
348
349 # gets the latest versions of the assets from the upstream repository.
350 function do_update()
351 {
352   directory="$1"; shift
353
354   save_terminal_title
355
356   # make a nice echoer since we want to use it inside conditions below.
357   local nicedir="$directory"
358   if [ $nicedir == "." ]; then
359     nicedir=$(\pwd)
360   fi
361   local blatt="echo retrieving '$nicedir'..."
362
363   pushd "$directory" &>/dev/null
364   if [ -d "CVS" ]; then
365     if test_writeable "CVS"; then
366       $blatt
367       cvs update . | $TO_SPLITTER
368       test_or_die "cvs update"
369     fi
370   elif [ -d ".svn" ]; then
371     if test_writeable ".svn"; then
372       $blatt
373       svn update . | $TO_SPLITTER
374       test_or_die "svn update"
375     fi
376   elif [ -d ".git" ]; then
377     if test_writeable ".git"; then
378       $blatt
379       git pull --no-ff origin 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
380       if [ ${PIPESTATUS[0]} -ne 0 ]; then false; fi
381       test_or_die "git pull of origin without fast forwards"
382     fi
383   else
384     # this is not an error necessarily; we'll just pretend they planned this.
385     echo no repository in $directory
386   fi
387   popd &>/dev/null
388
389   restore_terminal_title
390
391   true
392 }
393
394 # gets all the updates for a list of folders under revision control.
395 function checkout_list()
396 {
397   local list="$(uniquify $*)"
398
399   save_terminal_title
400
401   # turn repo list back into an array.
402   eval "repository_list=( ${REPOSITORY_LIST[*]} )"
403
404   local outer inner
405
406   for outer in "${repository_list[@]}"; do
407     # check the repository first, since it might be an absolute path.
408     if [[ $outer =~ /.* ]]; then
409       # yep, this path is absolute.  just handle it directly.
410       if [ ! -d "$outer" ]; then continue; fi
411       do_update $outer
412       test_or_die "running update on: $path"
413       sep 28
414     else
415       for inner in $list; do
416         # add in the directory component to see if we can find the folder.
417         local path="$inner/$outer"
418         if [ ! -d "$path" ]; then continue; fi
419         do_update $path
420         test_or_die "running update on: $path"
421         sep 28
422       done
423     fi
424   done
425
426   restore_terminal_title
427 }
428
429 # provides a list of absolute paths of revision control directories
430 # that are located under the directory passed as the first parameter.
431 function generate_rev_ctrl_filelist()
432 {
433   local dir="$1"; shift
434   pushd "$dir" &>/dev/null
435   local dirhere="$( \cd "$(\dirname "$dir")" && /bin/pwd )"
436   local tempfile=$(mktemp /tmp/zz_checkins.XXXXXX)
437   echo >$tempfile
438   local additional_filter
439   find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".svn" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
440   find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".git" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
441   # CVS is not well behaved like git and (now) svn, and we seldom use it anymore.
442   popd &>/dev/null
443
444   # see if they've warned us not to try checking in within vendor hierarchies.
445   if [ ! -z "NO_CHECKIN_VENDOR" ]; then
446     sed -i -e '/.*\/vendor\/.*/d' "$tempfile"
447   fi
448
449   local sortfile=$(mktemp /tmp/zz_checkin_sort.XXXXXX)
450   sort <"$tempfile" >"$sortfile"
451   \rm "$tempfile"
452   echo "$sortfile"
453 }
454
455 # iterates across a list of directories contained in a file (first parameter).
456 # on each directory name, it performs the action (second parameter) provided.
457 function perform_revctrl_action_on_file()
458 {
459   local tempfile="$1"; shift
460   local action="$1"; shift
461
462   save_terminal_title
463
464   while read -u 3 dirname; do
465     if [ -z "$dirname" ]; then continue; fi
466     pushd "$dirname" &>/dev/null
467     echo "[$(pwd)]"
468     $action .
469     test_or_die "performing action $action on: $(pwd)"
470     sep 28
471     popd &>/dev/null
472   done 3<"$tempfile"
473
474   restore_terminal_title
475
476   rm $tempfile
477 }
478