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