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