slugworth
[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 # classic implementation, but only works with one master branch.
82 # fixes will be forthcoming from development branch.
83
84       # snag all new files.  not to everyone's liking.
85       git add --all .
86       test_or_die "git add all new files"
87
88       # see if there are any changes in the local repository.
89       if ! git diff-index --quiet HEAD --; then
90         # tell git about all the files and get a check-in comment.
91         git commit .
92         test_or_die "git commit"
93       fi
94
95 #      # upload the files to the server so others can see them.
96 #      git push 2>&1 | grep -v "X11 forwarding request failed"
97 #      if [ ${PIPESTATUS[0]} -ne 0 ]; then false; fi
98 #      test_or_die "git push"
99
100       # catch if the diff-index failed somehow.
101       test_or_die "git diff-index"
102
103       # we continue on to the push, even if there were no changes this time, because
104       # there could already be committed changes that haven't been pushed yet.
105
106       local myself="$(my_branch_name)"
107 #      local parent="$(parent_branch_name)"
108
109       # upload any changes to the upstream repo so others can see them.
110
111 #      if [ "$myself" != "$parent" ]; then
112 #        git push origin "$(myself)" 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
113 #        test_or_die "git push to origin: $myself"
114 #      else
115 #        # this branch is the same as the parent, so just push.
116
117         git push 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
118         test_or_die "git push"
119 #      fi
120
121     fi
122   else
123     # nothing there.  it's not an error though.
124     echo no repository in $directory
125   fi
126   popd &>/dev/null
127
128   restore_terminal_title
129
130   true;
131 }
132
133 # shows the local changes in a repository.
134 function do_diff
135 {
136   local directory="$1"; shift
137
138   save_terminal_title
139
140   pushd "$directory" &>/dev/null
141
142   # only update if we see a repository living there.
143   if [ -d ".svn" ]; then
144     svn diff .
145     test_or_die "subversion diff"
146   elif [ -d ".git" ]; then
147     git diff 
148     test_or_die "git diff"
149   elif [ -d "CVS" ]; then
150     cvs diff .
151     test_or_die "cvs diff"
152   fi
153
154   popd &>/dev/null
155
156   restore_terminal_title
157
158   true;
159 }
160
161 # reports any files that are not already known to the upstream repository.
162 function do_report_new
163 {
164   local directory="$1"; shift
165
166   save_terminal_title
167
168   pushd "$directory" &>/dev/null
169
170   # only update if we see a repository living there.
171   if [ -f ".no-checkin" ]; then
172     echo "skipping reporting due to presence of .no-checkin sentinel file."
173   elif [ -d ".svn" ]; then
174     # this action so far only makes sense and is needed for svn.
175     bash $FEISTY_MEOW_SCRIPTS/rev_control/svnapply.sh \? echo
176     test_or_die "svn diff"
177   elif [ -d ".git" ]; then
178     git status -u
179     test_or_die "git status -u"
180   fi
181
182   popd &>/dev/null
183
184   restore_terminal_title
185
186   true
187 }
188
189 # checks in all the folders in a specified list.
190 function checkin_list()
191 {
192   # make the list of directories unique.
193   local list="$(uniquify $*)"
194
195   save_terminal_title
196
197   # turn repo list back into an array.
198   eval "repository_list=( ${REPOSITORY_LIST[*]} )"
199
200   local outer inner
201
202   for outer in "${repository_list[@]}"; do
203     # check the repository first, since it might be an absolute path.
204     if [[ $outer =~ /.* ]]; then
205       # yep, this path is absolute.  just handle it directly.
206       if [ ! -d "$outer" ]; then continue; fi
207       do_checkin $outer
208       test_or_die "running check-in (absolute) on path: $outer"
209       sep 28
210     else
211       for inner in $list; do
212         # add in the directory component to see if we can find the folder.
213         local path="$inner/$outer"
214         if [ ! -d "$path" ]; then continue; fi
215         do_checkin $path
216         test_or_die "running check-in (relative) on path: $path"
217         sep 28
218       done
219     fi
220   done
221
222   restore_terminal_title
223 }
224
225 # takes out the first few carriage returns that are in the input.
226 function squash_first_few_crs()
227 {
228   i=0
229   while read input_text; do
230     i=$((i+1))
231     if [ $i -le 5 ]; then
232       echo -n "$input_text  "
233     else
234       echo $input_text
235     fi
236   done
237   if [ $i -le 3 ]; then
238     # if we're still squashing eols, make sure we don't leave them hanging.
239     echo
240   fi
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 | 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 # this exits with 0 for success (normal bash behavior) when up to date.  if the branch is not up to date,
258 # then these values are returned:
259 #DOCUMENT THE VALUES
260 # reference: https://stackoverflow.com/questions/3258243/check-if-pull-needed-in-git
261 function check_branch_state()
262 {
263   local branch="$1"; shift
264
265   local to_return=120  # unknown issue.
266
267 sep
268
269   LOCAL=$(git rev-parse @)
270   REMOTE=$(git rev-parse "$branch")
271   BASE=$(git merge-base @ "$branch")
272 var branch LOCAL REMOTE BASE
273
274   if [ "$LOCAL" == "$REMOTE" ]; then
275     echo "Up-to-date"
276     to_return=0
277   elif [ "$LOCAL" == "$BASE" ]; then
278     echo "Need to pull"
279     to_return=1
280   elif [ "$REMOTE" == "$BASE" ]; then
281     echo "Need to push"
282     to_return=2
283   else
284     echo "Diverged"
285     to_return=3
286   fi
287
288 sep
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 # do_checkin method.
296 function careful_git_update()
297 {
298
299 echo A
300   this_branch="$(my_branch_name)"
301
302 #we want my branch here, don't we?  not like parent or anything?
303   check_branch_state "$this_branch"
304   state=$?
305   test_or_continue "branch state check"
306   echo the branch state is $state
307 #need to instead do something here if fails.
308
309 echo B
310
311   # first update all our remote branches to their current state from the repos.
312   git remote update
313   test_or_die "git remote update"
314
315 echo C
316   # now pull down any changes in our own origin in the repo, to stay in synch
317   # with any changes from others.
318   git pull --no-ff --all
319   test_or_die "git pulling all upstream"
320
321 echo D
322 #the above are just not enough.  now doing what i have to do to repair things.
323 branch_list=$(git branch |grep -v '^\*')
324
325
326 echo E
327
328 echo The rest of pull is not being done yet.
329 return 1
330
331
332
333 # below has older shards of partial knowledge.
334
335 #      reslog=$(git log HEAD..origin/master --oneline)
336 #      if [[ "${reslog}" != "" ]] ; then
337 #        git merge origin/master
338
339 #      # from very helpful page:
340 #      # https://stackoverflow.com/questions/10312521/how-to-fetch-all-git-branches
341 #      for remote in $( git branch -r | grep -v -- '->' ); do
342 #        git branch --track ${remote#origin/} $remote 2>/dev/null
343 ##hmmm: ignoring errors from these, since they are continual.
344 ##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.
345 #      done
346 #
347 ##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.
348 #      git fetch --all 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
349 #      test_or_die "git fetch"
350 #
351 #      git pull --all 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
352 #      test_or_die "git pull"
353
354 }
355
356 # gets the latest versions of the assets from the upstream repository.
357 function do_update()
358 {
359   directory="$1"; shift
360
361   save_terminal_title
362
363   # make a nice echoer since we want to use it inside conditions below.
364   local nicedir="$directory"
365   if [ $nicedir == "." ]; then
366     nicedir=$(\pwd)
367   fi
368   local blatt="echo retrieving '$nicedir'..."
369
370   pushd "$directory" &>/dev/null
371   if [ -d "CVS" ]; then
372     if test_writeable "CVS"; then
373       $blatt
374       cvs update . | $TO_SPLITTER
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       test_or_die "svn update"
382     fi
383   elif [ -d ".git" ]; then
384     if test_writeable ".git"; then
385       $blatt
386
387 # classic implementation, but only works with one master branch.
388 # fixes will be forthcoming from development branch.
389
390 #      git pull 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
391 #      if [ ${PIPESTATUS[0]} -ne 0 ]; then false; fi
392 #      test_or_die "git pull"
393
394 #any parms needed?
395       careful_git_update 
396
397     fi
398   else
399     # this is not an error necessarily; we'll just pretend they planned this.
400     echo no repository in $directory
401   fi
402   popd &>/dev/null
403
404   restore_terminal_title
405
406   true
407 }
408
409 # gets all the updates for a list of folders under revision control.
410 function checkout_list()
411 {
412   local list="$(uniquify $*)"
413
414   save_terminal_title
415
416   # turn repo list back into an array.
417   eval "repository_list=( ${REPOSITORY_LIST[*]} )"
418
419   local outer inner
420
421   for outer in "${repository_list[@]}"; do
422     # check the repository first, since it might be an absolute path.
423     if [[ $outer =~ /.* ]]; then
424       # yep, this path is absolute.  just handle it directly.
425       if [ ! -d "$outer" ]; then continue; fi
426       do_update $outer
427       test_or_die "running update on: $path"
428       sep 28
429     else
430       for inner in $list; do
431         # add in the directory component to see if we can find the folder.
432         local path="$inner/$outer"
433         if [ ! -d "$path" ]; then continue; fi
434         do_update $path
435         test_or_die "running update on: $path"
436         sep 28
437       done
438     fi
439   done
440
441   restore_terminal_title
442 }
443
444 # provides a list of absolute paths of revision control directories
445 # that are located under the directory passed as the first parameter.
446 function generate_rev_ctrl_filelist()
447 {
448   local dir="$1"; shift
449   pushd "$dir" &>/dev/null
450   local dirhere="$( \cd "$(\dirname "$dir")" && /bin/pwd )"
451   local tempfile=$(mktemp /tmp/zz_checkins.XXXXXX)
452   echo >$tempfile
453   local additional_filter
454   find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".svn" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
455   find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".git" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
456   # CVS is not well behaved like git and (now) svn, and we seldom use it anymore.
457   popd &>/dev/null
458
459   # see if they've warned us not to try checking in within vendor hierarchies.
460   if [ ! -z "NO_CHECKIN_VENDOR" ]; then
461     sed -i -e '/.*\/vendor\/.*/d' "$tempfile"
462   fi
463
464   local sortfile=$(mktemp /tmp/zz_checkin_sort.XXXXXX)
465   sort <"$tempfile" >"$sortfile"
466   \rm "$tempfile"
467   echo "$sortfile"
468 }
469
470 # iterates across a list of directories contained in a file (first parameter).
471 # on each directory name, it performs the action (second parameter) provided.
472 function perform_revctrl_action_on_file()
473 {
474   local tempfile="$1"; shift
475   local action="$1"; shift
476
477   save_terminal_title
478
479   while read -u 3 dirname; do
480     if [ -z "$dirname" ]; then continue; fi
481     pushd "$dirname" &>/dev/null
482     echo "[$(pwd)]"
483     $action .
484     test_or_die "performing action $action on: $(pwd)"
485     sep 28
486     popd &>/dev/null
487   done 3<"$tempfile"
488
489   restore_terminal_title
490
491   rm $tempfile
492 }
493