handling branches a little better
[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 source "$FEISTY_MEOW_SCRIPTS/core/launch_feisty_meow.sh"
7 source "$FEISTY_MEOW_SCRIPTS/tty/terminal_titler.sh"
8
9 ##############
10
11 # the maximum depth that the recursive functions will try to go below the starting directory.
12 export MAX_DEPTH=5
13
14 # one unpleasantry to take care of first; cygwin barfs aggressively if the TMP directory
15 # is a DOS path, but we need it to be a DOS path for our GFFS testing, so that blows.
16 # to get past this, TMP gets changed below to a hopefully generic and safe place.
17 if [[ "$TMP" =~ .:.* ]]; then
18   echo "making weirdo temporary directory for PCDOS-style path."
19   export TMP=/tmp/rev_control_$USER
20 fi
21 if [ ! -d "$TMP" ]; then
22   mkdir -p $TMP
23 fi
24 if [ ! -d "$TMP" ]; then
25   echo "could not create the temporary directory TMP in: $TMP"
26   echo "this script will not work properly without an existing TMP directory."
27 fi
28 #hmmm: re-address the above code, since it doesn't make a lot of sense to me right now...
29
30
31 ##############
32
33 # checks the directory provided into the revision control system repository it belongs to.
34 function do_checkin()
35 {
36   local directory="$1"; shift
37
38   save_terminal_title
39
40   # make a nice echoer since we want to use it inside conditions below.
41   local nicedir="$directory"
42   if [ $nicedir == "." ]; then
43     nicedir=$(\pwd)
44   fi
45   local blatt="echo checking in '$nicedir'..."
46
47   local retval=0  # normally successful.
48
49   do_update "$directory"
50   retval=$?
51   test_or_die "repository update failed; this should be fixed before check-in."
52
53   pushd "$directory" &>/dev/null
54   if [ -f ".no-checkin" ]; then
55     echo "skipping check-in due to presence of .no-checkin sentinel file."
56   elif [ -d "CVS" ]; then
57     if test_writeable "CVS"; then
58       $blatt
59       cvs ci .
60       retval=$?
61     fi
62   elif [ -d ".svn" ]; then
63     if test_writeable ".svn"; then
64       $blatt
65       svn ci .
66       retval=$?
67     fi
68   elif [ -d ".git" ]; then
69     if test_writeable ".git"; then
70       $blatt
71       # snag all new files.  not to everyone's liking.
72       git add --all .
73       retval=$?
74
75       # see if there are any changes in the local repository.
76       if ! git diff-index --quiet HEAD --; then
77         # tell git about all the files and get a check-in comment.
78         git commit .
79         retval+=$?
80       fi
81       # catch if the diff-index failed somehow.
82       retval+=$?
83
84 #push the changes to where?  locally?
85       git push 2>&1 | grep -v "X11 forwarding request failed" | squash_first_few_crs
86       retval+=${PIPESTATUS[0]}
87
88       # upload any changes to the upstream repo so others can see them.
89       if [ "$(my_branch_name)" != "master" ]; then
90         git push origin "$(my_branch_name)" 2>&1 | grep -v "X11 forwarding request failed" | squash_first_few_crs
91         retval+=${PIPESTATUS[0]}
92       fi
93
94     fi
95   else
96     # nothing there.  it's not an error though.
97     echo no repository in $directory
98     retval=0
99   fi
100   popd &>/dev/null
101
102   restore_terminal_title
103
104   return $retval
105 }
106
107 # shows the local changes in a repository.
108 function do_diff
109 {
110   local directory="$1"; shift
111
112   save_terminal_title
113
114   pushd "$directory" &>/dev/null
115   local retval=0  # normally successful.
116
117   # only update if we see a repository living there.
118   if [ -d ".svn" ]; then
119     svn diff .
120     retval+=$?
121   elif [ -d ".git" ]; then
122     git diff 
123     retval+=$?
124   elif [ -d "CVS" ]; then
125     cvs diff .
126     retval+=$?
127   fi
128
129   popd &>/dev/null
130
131   restore_terminal_title
132
133   return $retval
134 }
135
136 # reports any files that are not already known to the upstream repository.
137 function do_report_new
138 {
139   local directory="$1"; shift
140
141   save_terminal_title
142
143   pushd "$directory" &>/dev/null
144   local retval=0  # normally successful.
145
146   # only update if we see a repository living there.
147   if [ -f ".no-checkin" ]; then
148     echo "skipping reporting due to presence of .no-checkin sentinel file."
149   elif [ -d ".svn" ]; then
150     # this action so far only makes sense and is needed for svn.
151     bash $FEISTY_MEOW_SCRIPTS/rev_control/svnapply.sh \? echo
152     retval=$?
153   elif [ -d ".git" ]; then
154     git status -u
155     retval=$?
156   fi
157
158   popd &>/dev/null
159
160   restore_terminal_title
161
162   return $retval
163 }
164
165 # checks in all the folders in a specified list.
166 function checkin_list()
167 {
168   # make the list of directories unique.
169   local list="$(uniquify $*)"
170
171   save_terminal_title
172
173   # turn repo list back into an array.
174   eval "repository_list=( ${REPOSITORY_LIST[*]} )"
175
176   local outer inner
177
178   for outer in "${repository_list[@]}"; do
179     # check the repository first, since it might be an absolute path.
180     if [[ $outer =~ /.* ]]; then
181       # yep, this path is absolute.  just handle it directly.
182       if [ ! -d "$outer" ]; then continue; fi
183       do_checkin $outer
184       test_or_die "running check-in on: $outer"
185       sep 28
186     else
187       for inner in $list; do
188         # add in the directory component to see if we can find the folder.
189         local path="$inner/$outer"
190         if [ ! -d "$path" ]; then continue; fi
191         do_checkin $path
192         test_or_die "running check-in on: $path"
193         sep 28
194       done
195     fi
196   done
197
198   restore_terminal_title
199 }
200
201 # takes out the first few carriage returns that are in the input.
202 function squash_first_few_crs()
203 {
204   i=0
205   while read input_text; do
206     i=$((i+1))
207     if [ $i -le 5 ]; then
208       echo -n "$input_text  "
209     else
210       echo $input_text
211     fi
212   done
213   if [ $i -le 3 ]; then
214     # if we're still squashing eols, make sure we don't leave them hanging.
215     echo
216   fi
217 }
218
219 # a helpful method that reports the git branch for the current directory's
220 # git repository.
221 function my_branch_name()
222 {
223   echo "$(git branch | grep \* | cut -d ' ' -f2)"
224 }
225
226 # this reports the upstream branch for the current repo.
227 function parent_branch_name()
228 {
229   echo "$(git branch -vv | grep \* | cut -d ' ' -f2)"
230 }
231
232 # gets the latest versions of the assets from the upstream repository.
233 function do_update()
234 {
235   directory="$1"; shift
236
237   save_terminal_title
238
239   # make a nice echoer since we want to use it inside conditions below.
240   local nicedir="$directory"
241   if [ $nicedir == "." ]; then
242     nicedir=$(\pwd)
243   fi
244   local blatt="echo retrieving '$nicedir'..."
245
246   local retval=0  # plan on success for now.
247   pushd "$directory" &>/dev/null
248   if [ -d "CVS" ]; then
249     if test_writeable "CVS"; then
250       $blatt
251       cvs update . | squash_first_few_crs
252       retval=${PIPESTATUS[0]}
253     fi
254   elif [ -d ".svn" ]; then
255     if test_writeable ".svn"; then
256       $blatt
257       svn update . | squash_first_few_crs
258       retval=${PIPESTATUS[0]}
259     fi
260   elif [ -d ".git" ]; then
261     if test_writeable ".git"; then
262       $blatt
263       retval=0
264
265       git pull origin "$(parent_branch_name)" 2>&1 | grep -v "X11 forwarding request failed" | squash_first_few_crs
266       retval+=${PIPESTATUS[0]}
267
268       git pull origin "$(my_branch_name)" 2>&1 | grep -v "X11 forwarding request failed" | squash_first_few_crs
269       retval+=${PIPESTATUS[0]}
270     fi
271   else
272     # this is not an error necessarily; we'll just pretend they planned this.
273     echo no repository in $directory
274   fi
275   popd &>/dev/null
276
277   restore_terminal_title
278
279   return $retval
280 }
281
282 # gets all the updates for a list of folders under revision control.
283 function checkout_list()
284 {
285   local list="$(uniquify $*)"
286
287   save_terminal_title
288
289   # turn repo list back into an array.
290   eval "repository_list=( ${REPOSITORY_LIST[*]} )"
291
292   local outer inner
293
294   for outer in "${repository_list[@]}"; do
295     # check the repository first, since it might be an absolute path.
296     if [[ $outer =~ /.* ]]; then
297       # yep, this path is absolute.  just handle it directly.
298       if [ ! -d "$outer" ]; then continue; fi
299       do_update $outer
300       test_or_die "running update on: $path"
301       sep 28
302     else
303       for inner in $list; do
304         # add in the directory component to see if we can find the folder.
305         local path="$inner/$outer"
306         if [ ! -d "$path" ]; then continue; fi
307         do_update $path
308         test_or_die "running update on: $path"
309         sep 28
310       done
311     fi
312   done
313
314   restore_terminal_title
315 }
316
317 # provides a list of absolute paths of revision control directories
318 # that are located under the directory passed as the first parameter.
319 function generate_rev_ctrl_filelist()
320 {
321   local dir="$1"; shift
322   pushd "$dir" &>/dev/null
323   local dirhere="$( \cd "$(\dirname "$dir")" && /bin/pwd )"
324   local tempfile=$(mktemp /tmp/zz_checkins.XXXXXX)
325   echo >$tempfile
326   local additional_filter
327   find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".svn" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
328   find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".git" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
329   # CVS is not well behaved like git and (now) svn, and we seldom use it anymore.
330   popd &>/dev/null
331
332   # see if they've warned us not to try checking in within vendor hierarchies.
333   if [ ! -z "NO_CHECKIN_VENDOR" ]; then
334     sed -i -e '/.*\/vendor\/.*/d' "$tempfile"
335   fi
336
337   local sortfile=$(mktemp /tmp/zz_checkin_sort.XXXXXX)
338   sort <"$tempfile" >"$sortfile"
339   \rm "$tempfile"
340   echo "$sortfile"
341 }
342
343 # iterates across a list of directories contained in a file (first parameter).
344 # on each directory name, it performs the action (second parameter) provided.
345 function perform_revctrl_action_on_file()
346 {
347   local tempfile="$1"; shift
348   local action="$1"; shift
349
350   save_terminal_title
351
352   while read -u 3 dirname; do
353     if [ -z "$dirname" ]; then continue; fi
354     pushd "$dirname" &>/dev/null
355     echo "[$(pwd)]"
356     $action .
357     test_or_die "performing action $action on: $(pwd)"
358     sep 28
359     popd &>/dev/null
360   done 3<"$tempfile"
361
362   restore_terminal_title
363
364   rm $tempfile
365 }
366
367