b5ad517097d7c5e935791eabc15beb6e79131593
[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       local myself="$(my_branch_name)"
85       local parent="$(parent_branch_name)"
86
87       # upload any changes to the upstream repo so others can see them.
88       if [ "$myself" != "$parent" ]; then
89         git push origin "$(myself)" 2>&1 | grep -v "X11 forwarding request failed" | squash_first_few_crs
90         retval+=${PIPESTATUS[0]}
91       else
92         # this branch is the same as the parent, so just push.
93         git push 2>&1 | grep -v "X11 forwarding request failed" | squash_first_few_crs
94         retval+=${PIPESTATUS[0]}
95       fi
96
97     fi
98   else
99     # nothing there.  it's not an error though.
100     echo no repository in $directory
101     retval=0
102   fi
103   popd &>/dev/null
104
105   restore_terminal_title
106
107   return $retval
108 }
109
110 # shows the local changes in a repository.
111 function do_diff
112 {
113   local directory="$1"; shift
114
115   save_terminal_title
116
117   pushd "$directory" &>/dev/null
118   local retval=0  # normally successful.
119
120   # only update if we see a repository living there.
121   if [ -d ".svn" ]; then
122     svn diff .
123     retval+=$?
124   elif [ -d ".git" ]; then
125     git diff 
126     retval+=$?
127   elif [ -d "CVS" ]; then
128     cvs diff .
129     retval+=$?
130   fi
131
132   popd &>/dev/null
133
134   restore_terminal_title
135
136   return $retval
137 }
138
139 # reports any files that are not already known to the upstream repository.
140 function do_report_new
141 {
142   local directory="$1"; shift
143
144   save_terminal_title
145
146   pushd "$directory" &>/dev/null
147   local retval=0  # normally successful.
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     retval=$?
156   elif [ -d ".git" ]; then
157     git status -u
158     retval=$?
159   fi
160
161   popd &>/dev/null
162
163   restore_terminal_title
164
165   return $retval
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 on: $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 on: $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 # a helpful method that reports the git branch for the current directory's
223 # git repository.
224 function my_branch_name()
225 {
226   echo "$(git branch | grep \* | cut -d ' ' -f2)"
227 }
228
229 # this reports the upstream branch for the current repo.
230 function parent_branch_name()
231 {
232   echo "$(git branch -vv | grep \* | cut -d ' ' -f2)"
233 }
234
235 # gets the latest versions of the assets from the upstream repository.
236 function do_update()
237 {
238   directory="$1"; shift
239
240   save_terminal_title
241
242   # make a nice echoer since we want to use it inside conditions below.
243   local nicedir="$directory"
244   if [ $nicedir == "." ]; then
245     nicedir=$(\pwd)
246   fi
247   local blatt="echo retrieving '$nicedir'..."
248
249   local retval=0  # plan on success for now.
250   pushd "$directory" &>/dev/null
251   if [ -d "CVS" ]; then
252     if test_writeable "CVS"; then
253       $blatt
254       cvs update . | squash_first_few_crs
255       retval=${PIPESTATUS[0]}
256     fi
257   elif [ -d ".svn" ]; then
258     if test_writeable ".svn"; then
259       $blatt
260       svn update . | squash_first_few_crs
261       retval=${PIPESTATUS[0]}
262     fi
263   elif [ -d ".git" ]; then
264     if test_writeable ".git"; then
265       $blatt
266       retval=0
267 #      local myself="$(my_branch_name)"
268 #      local parent="$(parent_branch_name)"
269 #
270 #      if [ "$myself" != "$parent" ]; then
271 #        git pull origin "$parent" 2>&1 | grep -v "X11 forwarding request failed" | squash_first_few_crs
272 #        retval+=${PIPESTATUS[0]}
273 #      else
274
275         git pull --all 2>&1 | grep -v "X11 forwarding request failed" | squash_first_few_crs
276         retval+=${PIPESTATUS[0]}
277
278 #      fi
279     fi
280   else
281     # this is not an error necessarily; we'll just pretend they planned this.
282     echo no repository in $directory
283   fi
284   popd &>/dev/null
285
286   restore_terminal_title
287
288   return $retval
289 }
290
291 # gets all the updates for a list of folders under revision control.
292 function checkout_list()
293 {
294   local list="$(uniquify $*)"
295
296   save_terminal_title
297
298   # turn repo list back into an array.
299   eval "repository_list=( ${REPOSITORY_LIST[*]} )"
300
301   local outer inner
302
303   for outer in "${repository_list[@]}"; do
304     # check the repository first, since it might be an absolute path.
305     if [[ $outer =~ /.* ]]; then
306       # yep, this path is absolute.  just handle it directly.
307       if [ ! -d "$outer" ]; then continue; fi
308       do_update $outer
309       test_or_die "running update on: $path"
310       sep 28
311     else
312       for inner in $list; do
313         # add in the directory component to see if we can find the folder.
314         local path="$inner/$outer"
315         if [ ! -d "$path" ]; then continue; fi
316         do_update $path
317         test_or_die "running update on: $path"
318         sep 28
319       done
320     fi
321   done
322
323   restore_terminal_title
324 }
325
326 # provides a list of absolute paths of revision control directories
327 # that are located under the directory passed as the first parameter.
328 function generate_rev_ctrl_filelist()
329 {
330   local dir="$1"; shift
331   pushd "$dir" &>/dev/null
332   local dirhere="$( \cd "$(\dirname "$dir")" && /bin/pwd )"
333   local tempfile=$(mktemp /tmp/zz_checkins.XXXXXX)
334   echo >$tempfile
335   local additional_filter
336   find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".svn" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
337   find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".git" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
338   # CVS is not well behaved like git and (now) svn, and we seldom use it anymore.
339   popd &>/dev/null
340
341   # see if they've warned us not to try checking in within vendor hierarchies.
342   if [ ! -z "NO_CHECKIN_VENDOR" ]; then
343     sed -i -e '/.*\/vendor\/.*/d' "$tempfile"
344   fi
345
346   local sortfile=$(mktemp /tmp/zz_checkin_sort.XXXXXX)
347   sort <"$tempfile" >"$sortfile"
348   \rm "$tempfile"
349   echo "$sortfile"
350 }
351
352 # iterates across a list of directories contained in a file (first parameter).
353 # on each directory name, it performs the action (second parameter) provided.
354 function perform_revctrl_action_on_file()
355 {
356   local tempfile="$1"; shift
357   local action="$1"; shift
358
359   save_terminal_title
360
361   while read -u 3 dirname; do
362     if [ -z "$dirname" ]; then continue; fi
363     pushd "$dirname" &>/dev/null
364     echo "[$(pwd)]"
365     $action .
366     test_or_die "performing action $action on: $(pwd)"
367     sep 28
368     popd &>/dev/null
369   done 3<"$tempfile"
370
371   restore_terminal_title
372
373   rm $tempfile
374 }
375
376