much more complicated now
[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 #hmmm: we need to dump all the outputs in this script into splitter
10
11 ##############
12
13 # the maximum depth that the recursive functions will try to go below the starting directory.
14 export MAX_DEPTH=5
15
16 # use our splitter tool for lengthy output if it's available.
17 if [ ! -z "$(which splitter)" ]; then
18   TO_SPLITTER='| splitter'
19 fi
20
21 ##############
22
23 # one unpleasantry to take care of first; cygwin barfs aggressively if the TMP directory
24 # is a DOS path, but we need it to be a DOS path for our GFFS testing, so that blows.
25 # to get past this, TMP gets changed below to a hopefully generic and safe place.
26 if [[ "$TMP" =~ .:.* ]]; then
27   echo "making weirdo temporary directory for PCDOS-style path."
28   export TMP=/tmp/rev_control_$USER
29 fi
30 if [ ! -d "$TMP" ]; then
31   mkdir -p $TMP
32 fi
33 if [ ! -d "$TMP" ]; then
34   echo "could not create the temporary directory TMP in: $TMP"
35   echo "this script will not work properly without an existing TMP directory."
36 fi
37 #hmmm: re-address the above code, since it doesn't make a lot of sense to me right now...
38
39
40 ##############
41
42 # checks the directory provided into the revision control system repository it belongs to.
43 function do_checkin()
44 {
45   local directory="$1"; shift
46
47   save_terminal_title
48
49   # make a nice echoer since we want to use it inside conditions below.
50   local nicedir="$directory"
51   if [ $nicedir == "." ]; then
52     nicedir=$(\pwd)
53   fi
54   local blatt="echo checking in '$nicedir'..."
55
56   local retval=0  # normally successful.
57
58   do_update "$directory"
59   retval=$?
60   test_or_die "repository update failed; 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       retval=$?
70     fi
71   elif [ -d ".svn" ]; then
72     if test_writeable ".svn"; then
73       $blatt
74       svn ci .
75       retval=$?
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       retval=$?
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         retval+=$?
89       fi
90       # catch if the diff-index failed somehow.
91       retval+=$?
92
93       local myself="$(my_branch_name)"
94       local parent="$(parent_branch_name)"
95
96       # upload any changes to the upstream repo so others can see them.
97       if [ "$myself" != "$parent" ]; then
98         git push origin "$(myself)" 2>&1 | grep -v "X11 forwarding request failed" $TO_SPLITTER
99         retval+=${PIPESTATUS[0]}
100       else
101         # this branch is the same as the parent, so just push.
102         git push 2>&1 | grep -v "X11 forwarding request failed" $TO_SPLITTER
103         retval+=${PIPESTATUS[0]}
104       fi
105
106     fi
107   else
108     # nothing there.  it's not an error though.
109     echo no repository in $directory
110     retval=0
111   fi
112   popd &>/dev/null
113
114   restore_terminal_title
115
116   return $retval
117 }
118
119 # shows the local changes in a repository.
120 function do_diff
121 {
122   local directory="$1"; shift
123
124   save_terminal_title
125
126   pushd "$directory" &>/dev/null
127   local retval=0  # normally successful.
128
129   # only update if we see a repository living there.
130   if [ -d ".svn" ]; then
131     svn diff .
132     retval+=$?
133   elif [ -d ".git" ]; then
134     git diff 
135     retval+=$?
136   elif [ -d "CVS" ]; then
137     cvs diff .
138     retval+=$?
139   fi
140
141   popd &>/dev/null
142
143   restore_terminal_title
144
145   return $retval
146 }
147
148 # reports any files that are not already known to the upstream repository.
149 function do_report_new
150 {
151   local directory="$1"; shift
152
153   save_terminal_title
154
155   pushd "$directory" &>/dev/null
156   local retval=0  # normally successful.
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     retval=$?
165   elif [ -d ".git" ]; then
166     git status -u
167     retval=$?
168   fi
169
170   popd &>/dev/null
171
172   restore_terminal_title
173
174   return $retval
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 on: $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 on: $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   local retval=0  # plan on success for now.
259   pushd "$directory" &>/dev/null
260   if [ -d "CVS" ]; then
261     if test_writeable "CVS"; then
262       $blatt
263       cvs update . | squash_first_few_crs
264       retval=${PIPESTATUS[0]}
265     fi
266   elif [ -d ".svn" ]; then
267     if test_writeable ".svn"; then
268       $blatt
269       svn update . | squash_first_few_crs
270       retval=${PIPESTATUS[0]}
271     fi
272   elif [ -d ".git" ]; then
273     if test_writeable ".git"; then
274       $blatt
275       retval=0
276
277       # from very helpful page:
278       # https://stackoverflow.com/questions/10312521/how-to-fetch-all-git-branches
279       git branch -r | grep -v '\->' |
280           while read remote; do
281             git branch --track "${remote#origin/}" "$remote"
282             # ensure we notice a failure when adding tracking.
283             retval+=$?
284           done
285       retval+=${PIPESTATUS[0]}$?
286
287       git fetch --all 2>&1 | grep -v "X11 forwarding request failed" $TO_SPLITTER
288       retval+=${PIPESTATUS[0]}
289
290       git pull --all 2>&1 | grep -v "X11 forwarding request failed" $TO_SPLITTER
291       retval+=${PIPESTATUS[0]}
292     fi
293   else
294     # this is not an error necessarily; we'll just pretend they planned this.
295     echo no repository in $directory
296   fi
297   popd &>/dev/null
298
299   restore_terminal_title
300
301   return $retval
302 }
303
304 # gets all the updates for a list of folders under revision control.
305 function checkout_list()
306 {
307   local list="$(uniquify $*)"
308
309   save_terminal_title
310
311   # turn repo list back into an array.
312   eval "repository_list=( ${REPOSITORY_LIST[*]} )"
313
314   local outer inner
315
316   for outer in "${repository_list[@]}"; do
317     # check the repository first, since it might be an absolute path.
318     if [[ $outer =~ /.* ]]; then
319       # yep, this path is absolute.  just handle it directly.
320       if [ ! -d "$outer" ]; then continue; fi
321       do_update $outer
322       test_or_die "running update on: $path"
323       sep 28
324     else
325       for inner in $list; do
326         # add in the directory component to see if we can find the folder.
327         local path="$inner/$outer"
328         if [ ! -d "$path" ]; then continue; fi
329         do_update $path
330         test_or_die "running update on: $path"
331         sep 28
332       done
333     fi
334   done
335
336   restore_terminal_title
337 }
338
339 # provides a list of absolute paths of revision control directories
340 # that are located under the directory passed as the first parameter.
341 function generate_rev_ctrl_filelist()
342 {
343   local dir="$1"; shift
344   pushd "$dir" &>/dev/null
345   local dirhere="$( \cd "$(\dirname "$dir")" && /bin/pwd )"
346   local tempfile=$(mktemp /tmp/zz_checkins.XXXXXX)
347   echo >$tempfile
348   local additional_filter
349   find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".svn" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
350   find $dirhere -follow -maxdepth $MAX_DEPTH -type d -iname ".git" -exec echo {}/.. ';' >>$tempfile 2>/dev/null
351   # CVS is not well behaved like git and (now) svn, and we seldom use it anymore.
352   popd &>/dev/null
353
354   # see if they've warned us not to try checking in within vendor hierarchies.
355   if [ ! -z "NO_CHECKIN_VENDOR" ]; then
356     sed -i -e '/.*\/vendor\/.*/d' "$tempfile"
357   fi
358
359   local sortfile=$(mktemp /tmp/zz_checkin_sort.XXXXXX)
360   sort <"$tempfile" >"$sortfile"
361   \rm "$tempfile"
362   echo "$sortfile"
363 }
364
365 # iterates across a list of directories contained in a file (first parameter).
366 # on each directory name, it performs the action (second parameter) provided.
367 function perform_revctrl_action_on_file()
368 {
369   local tempfile="$1"; shift
370   local action="$1"; shift
371
372   save_terminal_title
373
374   while read -u 3 dirname; do
375     if [ -z "$dirname" ]; then continue; fi
376     pushd "$dirname" &>/dev/null
377     echo "[$(pwd)]"
378     $action .
379     test_or_die "performing action $action on: $(pwd)"
380     sep 28
381     popd &>/dev/null
382   done 3<"$tempfile"
383
384   restore_terminal_title
385
386   rm $tempfile
387 }
388
389