done refactoring version control library script
[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       # upload any changes to the upstream repo so others can see them.
85       git push 2>&1 
86 #| grep -v "X11 forwarding request failed"
87 #have to do pipestatus if want to keep the above.
88       retval+=$?
89     fi
90   else
91     # nothing there.  it's not an error though.
92     echo no repository in $directory
93     retval=0
94   fi
95   popd &>/dev/null
96
97   restore_terminal_title
98
99   return $retval
100 }
101
102 # shows the local changes in a repository.
103 function do_diff
104 {
105   local directory="$1"; shift
106
107   save_terminal_title
108
109   pushd "$directory" &>/dev/null
110   local retval=0  # normally successful.
111
112   # only update if we see a repository living there.
113   if [ -d ".svn" ]; then
114     svn diff .
115     retval+=$?
116   elif [ -d ".git" ]; then
117     git diff 
118     retval+=$?
119   elif [ -d "CVS" ]; then
120     cvs diff .
121     retval+=$?
122   fi
123
124   popd &>/dev/null
125
126   restore_terminal_title
127
128   return $retval
129 }
130
131 # reports any files that are not already known to the upstream repository.
132 function do_report_new
133 {
134   local directory="$1"; shift
135
136   save_terminal_title
137
138   pushd "$directory" &>/dev/null
139   local retval=0  # normally successful.
140
141   # only update if we see a repository living there.
142   if [ -f ".no-checkin" ]; then
143     echo "skipping reporting due to presence of .no-checkin sentinel file."
144   elif [ -d ".svn" ]; then
145     # this action so far only makes sense and is needed for svn.
146     bash $FEISTY_MEOW_SCRIPTS/rev_control/svnapply.sh \? echo
147     retval=$?
148   elif [ -d ".git" ]; then
149     git status -u
150     retval=$?
151   fi
152
153   popd &>/dev/null
154
155   restore_terminal_title
156
157   return $retval
158 }
159
160 # checks in all the folders in a specified list.
161 function checkin_list()
162 {
163   # make the list of directories unique.
164   local list="$(uniquify $*)"
165
166   save_terminal_title
167
168   # turn repo list back into an array.
169   eval "repository_list=( ${REPOSITORY_LIST[*]} )"
170
171   local outer inner
172
173   for outer in "${repository_list[@]}"; do
174     # check the repository first, since it might be an absolute path.
175     if [[ $outer =~ /.* ]]; then
176       # yep, this path is absolute.  just handle it directly.
177       if [ ! -d "$outer" ]; then continue; fi
178       do_checkin $outer
179       test_or_die "running check-in on: $outer"
180       sep 28
181     else
182       for inner in $list; do
183         # add in the directory component to see if we can find the folder.
184         local path="$inner/$outer"
185         if [ ! -d "$path" ]; then continue; fi
186         do_checkin $path
187         test_or_die "running check-in on: $path"
188         sep 28
189       done
190     fi
191   done
192
193   restore_terminal_title
194 }
195
196 # takes out the first few carriage returns that are in the input.
197 function squash_first_few_crs()
198 {
199   i=0
200   while read input_text; do
201     i=$((i+1))
202     if [ $i -le 5 ]; then
203       echo -n "$input_text  "
204     else
205       echo $input_text
206     fi
207   done
208   if [ $i -le 3 ]; then
209     # if we're still squashing eols, make sure we don't leave them hanging.
210     echo
211   fi
212 }
213
214 # gets the latest versions of the assets from the upstream repository.
215 function do_update()
216 {
217   directory="$1"; shift
218
219   save_terminal_title
220
221   # make a nice echoer since we want to use it inside conditions below.
222   local nicedir="$directory"
223   if [ $nicedir == "." ]; then
224     nicedir=$(\pwd)
225   fi
226   local blatt="echo retrieving '$nicedir'..."
227
228   local retval=0  # plan on success for now.
229   pushd "$directory" &>/dev/null
230   if [ -d "CVS" ]; then
231     if test_writeable "CVS"; then
232       $blatt
233       cvs update . | squash_first_few_crs
234       retval=${PIPESTATUS[0]}
235     fi
236   elif [ -d ".svn" ]; then
237     if test_writeable ".svn"; then
238       $blatt
239       svn update . | squash_first_few_crs
240       retval=${PIPESTATUS[0]}
241     fi
242   elif [ -d ".git" ]; then
243     if test_writeable ".git"; then
244       $blatt
245       git pull 2>&1 | grep -v "X11 forwarding request failed" | squash_first_few_crs
246       retval=${PIPESTATUS[0]}
247     fi
248   else
249     # this is not an error necessarily; we'll just pretend they planned this.
250     echo no repository in $directory
251   fi
252   popd &>/dev/null
253
254   restore_terminal_title
255
256   return $retval
257 }
258
259 # gets all the updates for a list of folders under revision control.
260 function checkout_list()
261 {
262   local list="$(uniquify $*)"
263
264   save_terminal_title
265
266   # turn repo list back into an array.
267   eval "repository_list=( ${REPOSITORY_LIST[*]} )"
268
269   local outer inner
270
271   for outer in "${repository_list[@]}"; do
272     # check the repository first, since it might be an absolute path.
273     if [[ $outer =~ /.* ]]; then
274       # yep, this path is absolute.  just handle it directly.
275       if [ ! -d "$outer" ]; then continue; fi
276       do_update $outer
277       test_or_die "running update on: $path"
278       sep 28
279     else
280       for inner in $list; do
281         # add in the directory component to see if we can find the folder.
282         local path="$inner/$outer"
283         if [ ! -d "$path" ]; then continue; fi
284         do_update $path
285         test_or_die "running update on: $path"
286         sep 28
287       done
288     fi
289   done
290
291   restore_terminal_title
292 }
293
294 # provides a list of absolute paths of revision control directories
295 # that are located under the directory passed as the first parameter.
296 function generate_rev_ctrl_filelist()
297 {
298   local dir="$1"; shift
299   pushd "$dir" &>/dev/null
300   local dirhere="$( \cd "$(\dirname "$dir")" && /bin/pwd )"
301   local tempfile=$(mktemp /tmp/zz_checkins.XXXXXX)
302   echo >$tempfile
303   local additional_filter
304   find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".svn" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
305   find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".git" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
306   # CVS is not well behaved like git and (now) svn, and we seldom use it anymore.
307   popd &>/dev/null
308
309   # see if they've warned us not to try checking in within vendor hierarchies.
310   if [ ! -z "NO_CHECKIN_VENDOR" ]; then
311     sed -i -e '/.*\/vendor\/.*/d' "$tempfile"
312   fi
313
314   local sortfile=$(mktemp /tmp/zz_checkin_sort.XXXXXX)
315   sort <"$tempfile" >"$sortfile"
316   \rm "$tempfile"
317   echo "$sortfile"
318 }
319
320 # iterates across a list of directories contained in a file (first parameter).
321 # on each directory name, it performs the action (second parameter) provided.
322 function perform_revctrl_action_on_file()
323 {
324   local tempfile="$1"; shift
325   local action="$1"; shift
326
327   save_terminal_title
328
329   while read -u 3 dirname; do
330     if [ -z "$dirname" ]; then continue; fi
331     pushd "$dirname" &>/dev/null
332     echo "[$(pwd)]"
333     $action .
334     test_or_die "performing action $action on: $(pwd)"
335     sep 28
336     popd &>/dev/null
337   done 3<"$tempfile"
338
339   restore_terminal_title
340
341   rm $tempfile
342 }
343
344