* config/ltmain.m4sh: Make sure that we $show what we will $run,
[libtool.git] / clcommit.m4sh
blob6cb10c02bfba50d2d526178aa3dbf15fd41d8e8e
1 m4_define([_m4_divert(SCRIPT)], 100)
2 m4_divert_push([SCRIPT])#!/bin/sh
3 # @configure_input@
5 # clcommit (GNU @PACKAGE@) version 0.12
6 # Written by Gary V. Vaughan <gary@gnu.org>
7 # and Alexandre Oliva <aoliva@redhat.com>
9 # Copyright (C) 1999, 2000, 2004 Free Software Foundation, Inc.
10 # This is free software; see the source for copying conditions.  There is NO
11 # warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 2 of the License, or
16 # (at your option) any later version.
18 # This program is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 # General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, a copy can be downloaded from
25 # http://www.gnu.org/copyleft/gpl.html, or by writing to the Free
26 # Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 # MA 02111-1307, USA.
29 # Usage: $progname [OPTION]... [--] [file|dir ...]
31 # -C file --changelog=file   extract commit message from specified ChangeLog
32 # -zN     --compress=N       set compression level (0-9, 0=none, 9=max)
33 #         --debug            enable verbose shell tracing
34 # -n      --dry-run          don't commit anything
35 #         --fast             same as --force --first
36 # -F file --file=file        read commit message from file
37 # -1      --first            extract first entry from ChangeLog, no cvs diff
38 # -f      --force            don't check (unless *followed* by -n), and just
39 #                            display commit message instead of running $PAGER
40 #         --from=addr        override default from address in commit email
41 # -l      --local            don't descend into subdirectories
42 # -m msg  --message=msg      set commit message
43 #         --msg=msg          same as -m
44 # -q      --quiet            run cvs in quiet mode
45 # -s addr --sendmail=addr    send a commit email of the differences to ADDR
46 #         --signature[=file] add FILE to the end of the email (~/.signature)
47 # -S TEXT --summary=TEXT     specify a TEXT summary for the commit (tla only)
48 # -t      --tla              use tla as the scm (instead of cvs)
49 # -v      --verbose          run in verbose mode
50 #         --version          print version information
51 # -h,-?   --help             print short or long help message
53 # This script eases checking in changes to CVS-maintained projects
54 # with ChangeLog files.  It will check that there have been no
55 # conflicting commits in the CVS repository and print which files it
56 # is going to commit to stderr.  A list of files to compare and to
57 # check in can be given in the command line.  If it is not given, all
58 # files in the current directory (and below, unless `-l' is given) are
59 # considered for check in.
61 # The commit message will be extracted from the differences between a
62 # file named ChangeLog* in the commit list, or named after -C, and the
63 # one in the repository (unless a message was specified with `-m' or
64 # `-F').  An empty message is not accepted (but a blank line is).  If
65 # the message is acceptable, it will be presented for verification
66 # (and possible edition) using the $PAGER environment variable (or
67 # `more', if it is not set, or `cat', if the `-f' switch is given).
68 # If $PAGER exits successfully, the modified files (at that moment)
69 # are checked in, unless `-n' was specified, in which case nothing is
70 # checked in.
72 # Report bugs to <gary@gnu.org>
74 : ${CVS="cvs"}
75 : ${TLA="tla"}
76 : ${MAILNOTIFY="mailnotify"}
77 : ${MKSTAMP="mkstamp"}
79 test -f "config/$MAILNOTIFY" && MAILNOTIFY="config/MAILNOTIFY"
80 test -f "config/$MKSTAMP" && MKSTAMP="config/$MKSTAMP"
82 PROGRAM=clcommit
84 AS_SHELL_SANITIZE
85 $as_unset CDPATH
87 m4_include([getopt.m4sh])
89 # Global variables:
90 cvs_flags=
91 update_flags=
92 commit_flags=
93 opt_commit=:
94 opt_update=:
95 opt_verbose=false
96 opt_first=false
97 opt_tla=false
99 mailnotify_flags=
100 sendmail_to=
101 exit_cmd=:
103 # Locations for important files
104 signature_file=
105 log_file="${TMPDIR-/tmp}/$progname-$$"
107 $RM "${TMPDIR-/tmp}/$progname"*
108 trap '$RM "${log_file}"*; exit $EXIT_FAILURE' 1 2 15
110 set -e
112 # Parse options once, thoroughly.  This comes as soon as possible in
113 # the script to make things like `clcommit --version' happen quickly.
115   # sed scripts:
116   my_sed_single_opt='1s/^\(..\).*$/\1/;q'
117   my_sed_single_rest='1s/^..\(.*\)$/\1/;q'
118   my_sed_long_opt='1s/^\(--[[^=]]*\)=.*/\1/;q'
119   my_sed_long_arg='1s/^--[[^=]]*=//'
121   # this just eases exit handling
122   while test $# -gt 0; do
123     opt="$1"
124     shift
125     case $opt in
127       --debug)          func_echo "enabling shell trace mode"
128                         mailnotify_flags="$mailnotify_flags --debug"
129                         set -x
130                         ;;
132       --fast)           set -- --force --first ${1+"$@"}        ;;
134       -f|--force)       opt_update=false; PAGER=cat             ;;
136       --from)           test $# = 0 && func_missing_arg $opt && break
137                         mailnotify_flags="$mailnotify_flags --from '"$1"'"
138                         shift
139                         ;;
141       -l|--local)       update_flags="$update_flags -l"
142                         commit_flags="$commit_flags -l"
143                         ;;
145       -m|--message|--msg)
146                         test $# = 0 && func_missing_arg $opt && break
147                         if $opt_first || test -f "$log_file"; then
148                           func_error "you can have at most one of -m, -F and -1"
149                           break
150                         fi
151                         echo "$1" > "$log_file"
152                         shift
153                         ;;
155       -F|--file)        test $# = 0 && func_missing_arg $opt && break
156                         if $opt_first || test -f "$log_file"; then
157                           func_error "you can have at most one of -m, -F and -1"
158                           break
159                         fi
160                         if cat < "$1" > "$log_file"; then :; else
161                           break
162                         fi
163                         shift
164                         ;;
166       -1|--first)       if test -f "$log_File"; then
167                           func_error "you can have at most one of -m, -F and -1"
168                           break
169                         fi
170                         opt_first=:
171                         ;;
173       -C|--[[cC]]hange[[lL]]og)
174                         test $# = 0 && func_missing_arg $opt && break
175                         if test -f "$1"; then :; else
176                           func_error "ChangeLog file \`$1' does not exist"
177                           break
178                         fi
179                         ChangeLog="$1"
180                         shift
181                         ;;
183       -n|--dry-run)     opt_commit=false; opt_update=:          ;;
185       -q|--quiet)       cvs_flags="$cvs_flags -q"               ;;
187       -s|--sendmail)    test $# = 0 && func_missing_arg $opt && break
188                         sendmail_to="$1"
189                         shift
190                         ;;
192       --signature)      test $# = 0 && func_missing_arg $opt && break
193                         signature_file="$HOME/.signature"
194                         case $1 in
195                           -*) ;;
196                           *)  signature_file="$1"; shift ;;
197                         esac
198                         if test -f "$signature_file"; then :; else
199                           func_error "\`$signature_file': file not found"
200                           break
201                         fi
202                         ;;
204       -S|--summary)     test $# = 0 && func_missing_arg $opt && break
205                         summary="$1"
206                         shift
207                         ;;
209       -t|--tla)         opt_tla=:                               ;;
211       -v|--verbose)     opt_verbose=:                           ;;
213       -z|--compress)
214                         test $# = 0 && func_missing_arg $opt && break
215                         case "$1" in
216                           [[0-9]]) :;;
217                           *)  func_error "invalid argument for $opt"
218                               break
219                               ;;
220                         esac
221                         cvs_flags="$cvs_flags -z$1"
222                         shift
223                         ;;
225       # Separate optargs to long options:
226       --message=*|--msg=*|--from=*|--file=*|--[[Cc]]hange[[Ll]]og=*|--compress=*|--summary=*|--sendmail=*|--signature=*)
227                         arg=`echo "$opt" | $SED "$my_sed_long_arg"`
228                         opt=`echo "$opt" | $SED "$my_sed_long_opt"`
229                         set -- "$opt" "$arg" ${1+"$@"}
230                         ;;
232       # Separate optargs to short options:
233       -m*|-F*|-C*|-S*|-s*|-z*)
234                         arg=`echo "$opt" |$SED "$my_sed_single_rest"`
235                         opt=`echo "$opt" |$SED "$my_sed_single_opt"`
236                         set -- "$opt" "$arg" ${1+"$@"}
237                         ;;
239       # Separate non-argument short options:
240       -f*|-1*|-n*|-q*)
241                         rest=`echo "$opt" |$SED "$my_sed_single_rest"`
242                         opt=`echo "$opt" |$SED "$my_sed_single_opt"`
243                         set -- "$opt" "-$rest" ${1+"$@"}
244                         ;;
246       -\?|-h)           func_usage                                      ;;
247       --help)           func_help                                       ;;
248       --version)        func_version                                    ;;
249       --)               break                                           ;;
250       -*)               func_fatal_help "unrecognized option \`$opt'"   ;;
251       *)                set -- "$opt" ${1+"$@"};        break           ;;
252     esac
253   done
255   if test -z "$sendmail_to"; then
257     # can't have a from address without a destination address
258     test -n "$sendmail_from" &&
259       func_error "can't use --from without --sendmail." && exit_cmd=exit
261     # can't use a signature file without a destination address
262     test -n "$signature_file" &&
263       func_error "can't use --signature without --sendmail." && exit_cmd=exit
264   fi
266   # Bail if the options were screwed
267   $exit_cmd $EXIT_FAILURE
270 # func_check_conflicts
271 func_check_conflicts ()
273     func_verbose "$progname: checking for conflicts..."
274     if $opt_tla; then
275       if ( $TLA changes |
276             while read line; do
277               echo "$line"
278               echo "$line" >&3
279             done | grep '^C'
280           ) 3>&1 >/dev/null; then
281         func_fatal_error "some conflicts were found with arch archive, aborting..."
282       fi
283     fi
285     if test -f CVS/Entries; then
286       if ( $CVS $cvs_flags -q -n update $update_flags ${1+"$@"} |
287             while read line; do
288               echo "$line"
289               echo "$line" >&3
290             done | grep '^C'
291           ) 3>&1 >/dev/null; then
292         func_fatal_error "some conflicts were found with CVS repository, aborting..."
293       fi
294     fi
298 # func_check_commit_msg
299 func_check_commit_msg ()
301     if test -z "$ChangeLog"; then
302       for f in ${1+"$@"}; do
303         case "$f" in
304           ChangeLog* | */ChangeLog*)
305             if test -z "$ChangeLog"; then
306               ChangeLog="$f"
307             else
308               func_fatal_error "multiple ChangeLog files: $ChangeLog and $f"
309             fi
310           ;;
311         esac
312       done
313     fi
315     func_verbose "$progname: checking commit message..."
316     if $opt_first; then
317       skipping=:
318       $SED 's,^,+,' < ${ChangeLog-ChangeLog} |
319         while read line; do
320           case "$line" in
321             "+") if $skipping; then skipping=false; else break; fi;;
322             "+ "*)
323               func_error "*** Warning: lines should start with tabs, not spaces; ignoring line:"
324               echo "$line" | $SED 's/^.//' >&2;;
325             "+  "*)
326               $skipping || echo "$line" ;;
327           esac
328         done |
329           $SED 's,^\+   ,,' > "$log_file" || exit $EXIT_FAILURE
330     else
331       if $opt_tla; then
332         cmd="$TLA file-diffs"
333       else
334         cmd="$CVS $cvs_flags diff -u"
335       fi
336       $cmd ${ChangeLog-ChangeLog} |
337         while read line; do
338           case $line in
339             "--- "*) :;;
340             "-"*)
341               func_error "*** Warning: the following line in ChangeLog diff is suspicious:"
342               echo "$line" | $SED 's/^.//' >&2;;
343             "+ "*)
344               func_error "*** Warning: lines should start with tabs, not spaces; ignoring line:"
345               echo "$line" | $SED 's/^.//' >&2;;
346             "+") echo ;;
347             "+  "*) echo "$line";;
348           esac
349         done |
350           $SED -e 's,\+ ,,' -e '/./p' -e '/./d' -e '1d' -e '$d' > "$log_file" \
351       || exit $EXIT_FAILURE
352     fi
353     # The sed script above removes "+TAB" from the beginning of a line, then
354     # deletes the first and/or the last line, when they happen to be empty
358 # func_commit
359 func_commit ()
361     if $opt_tla; then
362       tla_log=`$TLA make-log`
363       test -n "$summary" || summary=`$SED -e '1{s/^Summary: *//;q;}'`
364       echo "Summary: $summary" > "$tla_log$$T" &&
365         $SED 1d "$tla_log" >> "$tla_log$$T" &&
366         cat < "$log_file" >> "$tla_log$$T" &&
367         mv "$tla_log$$T" "$tla_log"
368       ${PAGER-more} "$tla_log" || exit $EXIT_FAILURE
370       sleep 1 # give the user some time for a ^C
372       # Propagate any user edits back to the cvs log message
373       $SED -e '/^[[A-Z]][[-a-zA-Z]]*:/d' -e '1d' < $tla_log > $log_file
375     else
376       ${PAGER-more} "$log_file" || exit $EXIT_FAILURE
378       sleep 1 # give the user some time for a ^C
380       subject=`cvs -nq up 2>/dev/null | grep '^[[MAD]] ' | $SED 's/^. //'`
381       test $# -gt 0 && subject="$@"
382     fi
384     if test -f CVS/Entries; then
385       func_verbose "$CVS $cvs_flags commit $commit_flags -F $log_file ${1+$@}"
386       $CVS $cvs_flags commit $commit_flags -F $log_file ${1+"$@"} || exit $EXIT_FAILURE
387     fi
389     # Need to do the tla commit *after* cvs commit to make sure the
390     # ChangeLog timestamps stay in synch.
391     $opt_tla && $TLA commit
395 # func_mailnotify
396 func_mailnotify ()
398     notify_file="${log_file}.2"
399     func_verbose "Mailing commit notification to $sendmail_to"
401     {
402       echo Subject: $subject
403       test -f '{arch}/=tagging-method' &&
404           echo "Tree version:   `$TLA tree-version`"
405       test -f CVS/Root &&
406           echo "CVSROOT:        `$SED -e 's,.*:,,g' CVS/Root`"
407       test -f $MKSTAMP &&
408           echo "TIMESTAMP:      `$SHELL $MKSTAMP < ./ChangeLog`"
409       test -f CVS/Repository &&
410           echo "Module name:    `cat CVS/Repository`"
411       test -f CVS/Tag &&
412           echo "Branch:         `$SED -e 's,^T,,;1q' CVS/Tag`"
413       test -f CVS/Root &&
414           echo "Changes by:     `$SED -e 's,^:[[a-z]]*:,,;s,:.*$,,g' CVS/Root`"
415       echo ""
416       echo "Log Message:"
417       $SED -e 's,^,     ,' "$log_file"
418       test -f "$signature_file" && cat "$signature_file"
419     } > "$notify_file"
421     ${PAGER-more} "$notify_file" || break
423     # Break out the subject line again
424     my_mail_subject=`$SED -e '1{s/^Subject: *//;q;}' "$notify_file"`
425     my_mail_body=`$SED -e '2,$p;d' "$notify_file"`
426     echo "$my_mail_body" > "$notify_file"
428     func_verbose "mailing commit notification to \"$sendmail_to\""
429     eval func_verbose_eval $MAILNOTIFY $mailnotify_flags -s "'$my_mail_subject'" \
430         -m "text/plain" -f "$notify_file" -- "'$sendmail_to'"
435 ## ----- ##
436 ## main. ##
437 ## ----- ##
440   $opt_update && func_check_conflicts
442   test -f "$log_file" || func_check_commit_msg
444   grep '[[^     ]]' < "$log_file" > /dev/null ||
445     func_fatal_error "empty commit message, aborting"
447   if grep '^$' < "$log_file" > /dev/null; then
448     func_error "*** Warning: blank lines should not appear within commit messages."
449     func_error "*** They should be used to separate distinct commits."
450   fi
452   # Do not check for empty $log_file again, even though the user might have
453   # zeroed it out.  If s/he did, it was probably intentional.
454   if $opt_commit; then
455     func_commit
456   fi
458   # Need to set the subject line *after* tla commit, or the
459   # patch revision changes underneath us!
460   subject="$summary"
461   $opt_tla && \
462     subject="$subject [["`$TLA tree-version|$SED $basename`--`$TLA revisions|$SED -e '$p;d'`"]]"
464   # Send a copy of the log_file if sendmail_to was set:
465   if test -n "$sendmail_to"; then
466     func_mailnotify
467   fi
469   $RM "${log_file}"*
472 exit $EXIT_SUCCESS
474 # Local Variables:
475 # mode:shell-script
476 # sh-indentation:2
477 # End: