1224480f3823fe27a3ec7c6a8e79ebbc689b707b
[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 else
21   TO_SPLITTER=cat
22 fi
23
24 ##############
25
26 # one unpleasantry to take care of first; cygwin barfs aggressively if the TMP directory
27 # is a DOS path, but we need it to be a DOS path for our GFFS testing, so that blows.
28 # to get past this, TMP gets changed below to a hopefully generic and safe place.
29 if [[ "$TMP" =~ .:.* ]]; then
30   echo "making weirdo temporary directory for PCDOS-style path."
31   export TMP=/tmp/rev_control_$USER
32 fi
33 if [ ! -d "$TMP" ]; then
34   mkdir -p $TMP
35 fi
36 if [ ! -d "$TMP" ]; then
37   echo "could not create the temporary directory TMP in: $TMP"
38   echo "this script will not work properly without an existing TMP directory."
39 fi
40
41 ##############
42
43 # checks the directory provided into the revision control system repository it belongs to.
44 function do_checkin()
45 {
46   local directory="$1"; shift
47
48   save_terminal_title
49
50   # make a nice echoer since we want to use it inside conditions below.
51   local nicedir="$directory"
52   if [ $nicedir == "." ]; then
53     nicedir=$(\pwd)
54   fi
55   local blatt="echo checking in '$nicedir'..."
56
57   do_update "$directory"
58   test_or_die "repository update--this should be fixed before check-in."
59
60   pushd "$directory" &>/dev/null
61   if [ -f ".no-checkin" ]; then
62     echo "skipping check-in due to presence of .no-checkin sentinel file."
63   elif [ -d "CVS" ]; then
64     if test_writeable "CVS"; then
65       $blatt
66       cvs ci .
67       test_or_die "cvs checkin"
68     fi
69   elif [ -d ".svn" ]; then
70     if test_writeable ".svn"; then
71       $blatt
72       svn ci .
73       test_or_die "svn checkin"
74     fi
75   elif [ -d ".git" ]; then
76     if test_writeable ".git"; then
77       $blatt
78
79       # put all changed and new files in the commit.  not to everyone's liking.
80       git add --all .
81       test_or_die "git add all new files"
82
83       # see if there are any changes in the local repository.
84       if ! git diff-index --quiet HEAD --; then
85         # tell git about all the files and get a check-in comment.
86         git commit .
87         test_or_die "git commit"
88       fi
89
90       # a new set of steps we have to take to make sure the branch integrity is good.
91       do_careful_git_update 
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       # upload any changes to the upstream repo so others can see them.
97       git push origin "$(my_branch_name)" 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
98       test_or_die "git push"
99
100     fi
101   else
102     # nothing there.  it's not an error though.
103     echo no repository in $directory
104   fi
105   popd &>/dev/null
106
107   restore_terminal_title
108
109   return 0
110 }
111
112 # shows the local changes in a repository.
113 function do_diff
114 {
115   local directory="$1"; shift
116
117   save_terminal_title
118
119   pushd "$directory" &>/dev/null
120
121   # only update if we see a repository living there.
122   if [ -d ".svn" ]; then
123     svn diff .
124     test_or_die "subversion diff"
125   elif [ -d ".git" ]; then
126     git diff 
127     test_or_die "git diff"
128   elif [ -d "CVS" ]; then
129     cvs diff .
130     test_or_die "cvs diff"
131   fi
132
133   popd &>/dev/null
134
135   restore_terminal_title
136
137   return 0
138 }
139
140 # reports any files that are not already known to the upstream repository.
141 function do_report_new
142 {
143   local directory="$1"; shift
144
145   save_terminal_title
146
147   pushd "$directory" &>/dev/null
148
149   # only update if we see a repository living there.
150   if [ -f ".no-checkin" ]; then
151     echo "skipping reporting due to presence of .no-checkin sentinel file."
152   elif [ -d ".svn" ]; then
153     # this action so far only makes sense and is needed for svn.
154     bash $FEISTY_MEOW_SCRIPTS/rev_control/svnapply.sh \? echo
155     test_or_die "svn diff"
156   elif [ -d ".git" ]; then
157     git status -u
158     test_or_die "git status -u"
159   fi
160
161   popd &>/dev/null
162
163   restore_terminal_title
164
165   return 0
166 }
167
168 # checks in all the folders in a specified list.
169 function checkin_list()
170 {
171   # make the list of directories unique.
172   local list="$(uniquify $*)"
173
174   save_terminal_title
175
176   # turn repo list back into an array.
177   eval "repository_list=( ${REPOSITORY_LIST[*]} )"
178
179   local outer inner
180
181   for outer in "${repository_list[@]}"; do
182     # check the repository first, since it might be an absolute path.
183     if [[ $outer =~ /.* ]]; then
184       # yep, this path is absolute.  just handle it directly.
185       if [ ! -d "$outer" ]; then continue; fi
186       do_checkin $outer
187       test_or_die "running check-in (absolute) on path: $outer"
188       sep 28
189     else
190       for inner in $list; do
191         # add in the directory component to see if we can find the folder.
192         local path="$inner/$outer"
193         if [ ! -d "$path" ]; then continue; fi
194         do_checkin $path
195         test_or_die "running check-in (relative) on path: $path"
196         sep 28
197       done
198     fi
199   done
200
201   restore_terminal_title
202 }
203
204 # takes out the first few carriage returns that are in the input.
205 function squash_first_few_crs()
206 {
207   i=0
208   while read input_text; do
209     i=$((i+1))
210     if [ $i -le 5 ]; then
211       echo -n "$input_text  "
212     else
213       echo $input_text
214     fi
215   done
216   if [ $i -le 3 ]; then
217     # if we're still squashing eols, make sure we don't leave them hanging.
218     echo
219   fi
220 }
221
222 #hmmm: the below are git specific and should be named that way.
223
224 function all_branch_names()
225 {
226   echo "$(git branch -vv | cut -d ' ' -f2)"
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 -vv | 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 # other methods.
282 function do_careful_git_update()
283 {
284   local this_branch="$(my_branch_name)"
285
286 #proposition: this step didn't help before, and it seems redundant now.
287 #...
288 #hmmm, maybe it is needed.  and people did seem to want it first, so trying that.
289   # first update all our remote branches to their current state from the repos.
290   git remote update
291   test_or_die "git remote update"
292
293 #appears to be useless; reports no changes when we need to know about remote changes that do exist:
294 #  check_branch_state "$this_branch"
295 #  state=$?
296 #  test_or_continue "branch state check"
297 #  echo the branch state is $state
298
299   # this code is now doing what i have to do when i repair the repo.  and it seems to be good so far.
300   local branch_list=$(all_branch_names)
301   local bran
302   for bran in $branch_list; do
303 #    echo "synchronizing remote branch: $bran"
304     git checkout "$bran"
305     test_or_die "git checking out remote branch: $bran"
306     git pull --no-ff
307     test_or_die "git pull of remote branch: $bran"
308   done
309   # now switch back to our branch.
310   git checkout "$this_branch"
311   test_or_die "git checking out our current branch: $this_branch"
312
313   # now pull down any changes in our own origin in the repo, to stay in synch
314   # with any changes from others.
315   git pull --no-ff --all
316   test_or_die "git pulling all upstream"
317 }
318
319 # gets the latest versions of the assets from the upstream repository.
320 function do_update()
321 {
322   directory="$1"; shift
323
324   save_terminal_title
325
326   # make a nice echoer since we want to use it inside conditions below.
327   local nicedir="$directory"
328   if [ $nicedir == "." ]; then
329     nicedir=$(\pwd)
330   fi
331   local blatt="echo retrieving '$nicedir'..."
332
333   pushd "$directory" &>/dev/null
334   if [ -d "CVS" ]; then
335     if test_writeable "CVS"; then
336       $blatt
337       cvs update . | $TO_SPLITTER
338       test_or_die "cvs update"
339     fi
340   elif [ -d ".svn" ]; then
341     if test_writeable ".svn"; then
342       $blatt
343       svn update . | $TO_SPLITTER
344       test_or_die "svn update"
345     fi
346   elif [ -d ".git" ]; then
347     if test_writeable ".git"; then
348       $blatt
349       git pull --no-ff 2>&1 | grep -v "X11 forwarding request failed" | $TO_SPLITTER
350       if [ ${PIPESTATUS[0]} -ne 0 ]; then false; fi
351       test_or_die "git pull of origin without fast forwards"
352     fi
353   else
354     # this is not an error necessarily; we'll just pretend they planned this.
355     echo no repository in $directory
356   fi
357   popd &>/dev/null
358
359   restore_terminal_title
360
361   return 0
362 }
363
364 # gets all the updates for a list of folders under revision control.
365 function checkout_list()
366 {
367   local list="$(uniquify $*)"
368
369   save_terminal_title
370
371   # turn repo list back into an array.
372   eval "repository_list=( ${REPOSITORY_LIST[*]} )"
373
374   local outer inner
375
376   for outer in "${repository_list[@]}"; do
377     # check the repository first, since it might be an absolute path.
378     if [[ $outer =~ /.* ]]; then
379       # yep, this path is absolute.  just handle it directly.
380       if [ ! -d "$outer" ]; then continue; fi
381       do_update $outer
382       test_or_die "running update on: $path"
383       sep 28
384     else
385       for inner in $list; do
386         # add in the directory component to see if we can find the folder.
387         local path="$inner/$outer"
388         if [ ! -d "$path" ]; then continue; fi
389         do_update $path
390         test_or_die "running update on: $path"
391         sep 28
392       done
393     fi
394   done
395
396   restore_terminal_title
397 }
398
399 # provides a list of absolute paths of revision control directories
400 # that are located under the directory passed as the first parameter.
401 function generate_rev_ctrl_filelist()
402 {
403   local dir="$1"; shift
404   pushd "$dir" &>/dev/null
405   local dirhere="$( \cd "$(\dirname "$dir")" && /bin/pwd )"
406   local tempfile=$(mktemp /tmp/zz_checkins.XXXXXX)
407   echo >$tempfile
408   local additional_filter
409   find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".svn" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
410   find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".git" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
411   # CVS is not well behaved like git and (now) svn, and we seldom use it anymore.
412   popd &>/dev/null
413
414   # see if they've warned us not to try checking in within vendor hierarchies.
415   if [ ! -z "NO_CHECKIN_VENDOR" ]; then
416     sed -i -e '/.*\/vendor\/.*/d' "$tempfile"
417   fi
418
419   local sortfile=$(mktemp /tmp/zz_checkin_sort.XXXXXX)
420   sort <"$tempfile" >"$sortfile"
421   \rm "$tempfile"
422   echo "$sortfile"
423 }
424
425 # iterates across a list of directories contained in a file (first parameter).
426 # on each directory name, it performs the action (second parameter) provided.
427 function perform_revctrl_action_on_file()
428 {
429   local tempfile="$1"; shift
430   local action="$1"; shift
431
432   save_terminal_title
433
434   local did_anything=
435
436   while read -u 3 dirname; do
437     if [ -z "$dirname" ]; then
438       # we often have blank lines in the input file for some reason.
439       continue
440     fi
441     did_anything=yes
442     pushd "$dirname" &>/dev/null
443     echo "[$(pwd)]"
444     $action .
445     test_or_die "performing action $action on: $(pwd)"
446     sep 28
447     popd &>/dev/null
448   done 3<"$tempfile"
449
450   if [ -z "$did_anything" ]; then
451     echo "There was nothing to do the action '$action' on."
452   fi
453
454   restore_terminal_title
455
456   rm $tempfile
457 }
458