Allow gcc builds with -Wall -Werror.
[libtool/ericb.git] / clcommit.m4sh
blob351076a3e342a532e4a8e757ba7ad23a67a99c90
1 AS_INIT[]m4_divert_push([HEADER-COPYRIGHT])dnl
2 # @configure_input@
4 # clcommit (GNU @PACKAGE@) version 2.0
5 # Written by Gary V. Vaughan <gary@gnu.org>
6 # and Alexandre Oliva <aoliva@redhat.com>
8 # Copyright (C) 1999, 2000, 2004, 2006, 2008, 2009 Free Software Foundation, Inc.
9 # This is free software; see the source for copying conditions.  There is NO
10 # warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 # Clcommit is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2 of the License, or
15 # (at your option) any later version.
17 # Clcommit is distributed in the hope that it will be useful, but
18 # WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 # GNU General Public License for more details.
22 # You should have received a copy of the GNU General Public License
23 # along with clcommit; see the file COPYING.  If not, a copy
24 # can be downloaded from http://www.gnu.org/licenses/gpl.html,
25 # or obtained by writing to the Free Software Foundation, Inc.,
26 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 # Usage: $progname [OPTION]... [--] [filepattern ...]
30 # -a AUTHOR --author=AUTHOR    override changeset author name with AUTHOR
31 # -C FILE   --changelog=FILE   extract commit message from specified FILE
32 #           --debug            enable verbose shell tracing
33 # -n        --dry-run          don't commit anything
34 #           --fast             same as --force --first
35 # -F file   --file=FILE        read commit message from FILE
36 # -1        --first            extract first entry from ChangeLog, no git diff
37 # -f        --force            don't check (unless *followed* by -n), and just
38 #                              display commit message instead of running $PAGER
39 #           --from=ADDRESS     override default from ADDRESS in commit email
40 # -m msg    --message=STRING   set commit message to STRING
41 #           --msg=STRING          same as -m
42 # -p        --push             push the changes back to origin
43 # -r [FILE] --rcfile[=FILE]    read default option from FILE [./.clcommitrc]
44 # -s addr   --sendmail=ADDRESS send commit email of the differences to ADDRESS
45 #           --signature[=FILE] add FILE to the end of the email [~/.signature]
46 #           --signoff          add a Signed-off-by attribution at the end
47 # -S TEXT   --summary=TEXT     specify a TEXT subject line for the commit email
48 #           --tags             in conjunction with -p, also push tags
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 git-maintained projects
54 # with ChangeLog files.  It will check that there have been no
55 # conflicting commits in the git 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 working directory are considered for check in.
60 # The commit message will be extracted from the differences between a
61 # file named ChangeLog* in the commit list, or named after -C, and the
62 # one in the repository (unless a message was specified with `-m' or
63 # `-F').  An empty message is not accepted (but a blank line is).  If
64 # the message is acceptable, it will be presented for verification
65 # (and possible edition) using the $PAGER environment variable (or
66 # `more', if it is not set, or `cat', if the `-f' switch is given).
67 # If $PAGER exits successfully, the modified files (at that moment)
68 # are checked in, unless `-n' was specified, in which case nothing is
69 # checked in.
71 # Report bugs to <gary@gnu.org>
73 : ${GIT="git"}
74 : ${MAILNOTIFY="mailnotify"}
75 : ${MKSTAMP="mkstamp"}
77 test -f "libltdl/config/$MAILNOTIFY" && MAILNOTIFY="libltdl/config/$MAILNOTIFY"
78 test -f "libltdl/config/$MKSTAMP" && MKSTAMP="libltdl/config/$MKSTAMP"
80 PROGRAM=clcommit
82 m4_divert_pop
83 m4_include([getopt.m4sh])
85 M4SH_VERBATIM([[
86 # Global variables:
87 opt_commit=:
88 opt_first=false
89 opt_push=false
90 opt_tags=false
91 opt_update=:
92 opt_verbose=false
94 git_flags=
95 mailnotify_flags=
96 sendmail_to=
98 exit_cmd=:
100 # try to find out whether read supports -r
101 if echo bt | tr b '\\' | { read -r line; test "X$line" = 'X\t'; } 2>/dev/null
102 then
103   read_r='read -r'
104 else
105   read_r=read
108 # Locations for important files:
109 signature_file=
110 log_dir="`func_mktempdir`"
111 log_file="$log_dir/log"
112 push_conflicts="$log_dir/git_push.log"
114 trap '$RM -r "$log_dir"; exit $EXIT_FAILURE' 1 2 15
116 set -e
118 # Parse options once, thoroughly.  This comes as soon as possible in
119 # the script to make things like `clcommit --version' happen quickly.
121   # sed scripts:
122   my_sed_single_opt='1s/^\(..\).*$/\1/;q'
123   my_sed_single_rest='1s/^..\(.*\)$/\1/;q'
124   my_sed_long_opt='1s/^\(--[^=]*\)=.*/\1/;q'
125   my_sed_long_arg='1s/^--[^=]*=//'
127   # this just eases exit handling
128   while test $# -gt 0; do
129     opt="$1"
130     shift
131     case $opt in
133       --author|-a)      test $# = 0 && func_missing_arg $opt && break
134                         func_quote_for_eval "$1"
135                         git_flags="$git_flags --author=$func_quote_for_eval_result"
136                         shift
137                         ;;
139       --[cC]hange[lL]og|-C)
140                         test $# = 0 && func_missing_arg $opt && break
141                         if test -f "$1"; then :; else
142                           func_error "ChangeLog file \`$1' does not exist"
143                           break
144                         fi
145                         ChangeLog="$1"
146                         shift
147                         ;;
149       --debug)          func_echo "enabling shell trace mode"
150                         mailnotify_flags="$mailnotify_flags --debug"
151                         set -x
152                         ;;
154       --dry-run|-n)     opt_commit=false; opt_update=:          ;;
156       --fast)           set dummy --force --first ${1+"$@"}; shift      ;;
158       --file|-F)        test $# = 0 && func_missing_arg $opt && break
159                         if $opt_first || test -f "$log_file"; then
160                           func_error "you can have at most one of -m, -F and -1"
161                           break
162                         fi
163                         if cat < "$1" > "$log_file"; then :; else
164                           break
165                         fi
166                         shift
167                         ;;
169       --first|-1)       if test -f "$log_File"; then
170                           func_error "you can have at most one of -m, -F and -1"
171                           break
172                         fi
173                         opt_first=:
174                         ;;
176       --force|-f)       opt_update=false; PAGER=cat             ;;
178       --from)           test $# = 0 && func_missing_arg $opt && break
179                         func_quote_for_eval "$1"
180                         mailnotify_flags="$mailnotify_flags --from=$func_quote_for_eval_result"
181                         shift
182                         ;;
184       --message|--msg|-m)
185                         test $# = 0 && func_missing_arg $opt && break
186                         if $opt_first || test -f "$log_file"; then
187                           func_error "you can have at most one of -m, -F and -1"
188                           break
189                         fi
190                         echo "$1" > "$log_file"
191                         shift
192                         ;;
194       --push|-p)        opt_push=:                              ;;
196       --rcfile|-r)      rc_file="./.clcommitrc"
197                         if test $# -gt 0; then
198                           case $1 in
199                             -*)                         ;;
200                             *)  rc_file="$1"; shift     ;;
201                           esac
202                         fi
203                         if test -f "$rc_file"; then :; else
204                           func_error "rcfile \`$rc_file' does not exist"
205                           exit_cmd=exit
206                           break
207                         fi
208                         # The funny quoting allows keeping one option per
209                         # line in $rc_file:
210                         eval set dummy `echo \`cat $rc_file\` \${1+"\$@"}`
211                         shift
212                         ;;
214       --sendmail|-s)    test $# = 0 && func_missing_arg $opt && break
215                         func_quote_for_eval "$1"
216                         sendmail_to="$func_quote_for_eval_result"
217                         shift
218                         ;;
220       --signature)      test $# = 0 && func_missing_arg $opt && break
221                         signature_file="$HOME/.signature"
222                         case $1 in
223                           -*) ;;
224                           *)  signature_file="$1"; shift ;;
225                         esac
226                         if test -f "$signature_file"; then :; else
227                           func_error "\`$signature_file': file not found"
228                           break
229                         fi
230                         ;;
232       --signoff)        git_flags="$git_flags --signoff"        ;;
234       --summary|-S)     test $# = 0 && func_missing_arg $opt && break
235                         summary="$1"
236                         shift
237                         ;;
239       --tags)           opt_tags=:                              ;;
241       --verbose|-v)     opt_verbose=:                           ;;
243       # Separate optargs to long options:
244       --*=*)
245                         arg=`echo "$opt" | $SED "$my_sed_long_arg"`
246                         opt=`echo "$opt" | $SED "$my_sed_long_opt"`
247                         set dummy "$opt" "$arg" ${1+"$@"}
248                         shift
249                         ;;
251       # Separate optargs to short options:
252       -a*|-m*|-F*|-C*|-S*|-s*)
253                         arg=`echo "$opt" |$SED "$my_sed_single_rest"`
254                         opt=`echo "$opt" |$SED "$my_sed_single_opt"`
255                         set dummy "$opt" "$arg" ${1+"$@"}
256                         shift
257                         ;;
259       # Separate non-argument short options:
260       -1*|-f*|-p*|-n*|-q*)
261                         rest=`echo "$opt" |$SED "$my_sed_single_rest"`
262                         opt=`echo "$opt" |$SED "$my_sed_single_opt"`
263                         set dummy "$opt" "-$rest" ${1+"$@"}
264                         shift
265                         ;;
267       -\?|-h)           func_usage                                      ;;
268       --help)           func_help                                       ;;
269       --version)        func_version                                    ;;
270       --)               break                                           ;;
271       -*)               func_fatal_help "unrecognized option \`$opt'"   ;;
272       *)                set dummy "$opt" ${1+"$@"};     shift; break            ;;
273     esac
274   done
276   if test -z "$sendmail_to"; then
278     # can't have a from address without a destination address
279     test -n "$sendmail_from" &&
280       func_error "can't use --from without --sendmail." && exit_cmd=exit
282     # can't use a signature file without a destination address
283     test -n "$signature_file" &&
284       func_error "can't use --signature without --sendmail." && exit_cmd=exit
285   fi
287   $opt_tags && test x"$opt_push" = xfalse &&
288     func_error "can't use --tags without --push." && exit_cmd=exit
290   # Bail if the options were screwed
291   $exit_cmd $EXIT_FAILURE
294 # func_check_conflicts
295 func_check_conflicts ()
297     if $GIT push --dry-run > "$push_conflicts" 2>&1; then :; else
298       cat "$push_conflicts" >&2
299       func_fatal_error "some conflicts were found with upstream repository, aborting..."
300     fi
304 # func_check_commit_msg
305 func_check_commit_msg ()
307     if test -z "$ChangeLog"; then
308       for f in ${1+"$@"}; do
309         case "$f" in
310           ChangeLog* | */ChangeLog*)
311             if test -z "$ChangeLog"; then
312               ChangeLog="$f"
313             else
314               func_fatal_error "multiple ChangeLog files: $ChangeLog and $f"
315             fi
316           ;;
317         esac
318       done
319     fi
321     func_verbose "$progname: checking commit message..."
322     if $opt_first; then
323       skipping=:
324       $SED 's,^,+,' < ${ChangeLog-ChangeLog} |
325         while $read_r line; do
326           case "$line" in
327             "+") if $skipping; then skipping=false; else break; fi;;
328             "+ "*)
329               func_error "*** Warning: lines should start with tabs, not spaces; ignoring line:"
330               echo "$line" | $SED 's/^.//' >&2;;
331             "+  "*)
332               $skipping || echo "$line" ;;
333           esac
334         done |
335           $SED 's,^\+   ,,' > "$log_file" || exit $EXIT_FAILURE
336     else
337       $GIT diff ${ChangeLog-ChangeLog} |
338         while $read_r line; do
339           case $line in
340             "--- "*) :;;
341             "-"*)
342               func_error "*** Warning: the following line in ChangeLog diff is suspicious:"
343               echo "$line" | $SED 's/^.//' >&2;;
344             "+ "*)
345               func_error "*** Warning: lines should start with tabs, not spaces; ignoring line:"
346               echo "$line" | $SED 's/^.//' >&2;;
347             "+") echo ;;
348             "+  "*) echo "$line";;
349           esac
350         done |
351           $SED -e 's,\+ ,,' -e '/./p' -e '/./d' -e '1d' -e '$d' > "$log_file" \
352       || exit $EXIT_FAILURE
353     fi
354     # The sed script above removes "+TAB" from the beginning of a line, then
355     # deletes the first and/or the last line, when they happen to be empty
359 # func_commit
360 func_commit ()
362     ${PAGER-more} "$log_file" || exit $EXIT_FAILURE
364     sleep 1 # give the user some time for a ^C
366     subject=`git status 2>/dev/null | $SED -n 's/^#.*[mad][ode][dl].*ed: *//p'`
367     test $# -gt 0 && subject="$@"
369     test $# -gt 0 || { set dummy -a; shift; }
370     func_verbose "$GIT commit$git_flags -F $log_file ${1+$@}"
371     $GIT commit$git_flags -F $log_file ${1+"$@"} || exit $EXIT_FAILURE
373     if $opt_push; then
374       $GIT push
375       $opt_tags && $GIT push --tags
376     fi
378     :
382 # func_mailnotify
383 func_mailnotify ()
385     notify_file="${log_dir}/notify"
386     func_verbose "Mailing commit notification to $sendmail_to"
388     {
389       echo Subject: $subject
390       test -f .git/config &&
391           echo "Root:           `$SED -n 's,^   url = .*:\(.*\)$,\1,p' .git/config`"
392       test -f $MKSTAMP &&
393           echo "Timestamp:      `$SHELL $MKSTAMP .`"
394       echo "Branch:             `$GIT branch | sed -n 's/\* //p'`"
395       test -f .git/config &&
396           echo "Changes by:     `$SED -n 's,^   url = \([^:]*\):.*$,\1,p' .git/config`"
397       echo ""
398       echo "Log Message:"
399       $SED -e 's,^,     ,' "$log_file"
400       test -f "$signature_file" && {
401         echo '-- '
402         cat "$signature_file"
403       }
404     } > "$notify_file"
406     ${PAGER-more} "$notify_file" || break
408     # Break out the subject line again
409     my_mail_subject=`$SED -e '
410         1{
411             s/^Subject: *//
412             q
413         }' "$notify_file"`
414     my_mail_body=`$SED -e '2,$p;d' "$notify_file"`
415     echo "$my_mail_body" > "$notify_file"
417     func_verbose "mailing commit notification to \"$sendmail_to\""
418     func_quote_for_eval "$my_mail_subject"
419     func_show_eval "$MAILNOTIFY $mailnotify_flags \
420         -s $func_quote_for_eval_result -m 'text/plain' -f '$notify_file' \
421         -- $sendmail_to"
426 ## ----- ##
427 ## main. ##
428 ## ----- ##
431   test -f "$log_file" || func_check_commit_msg
433   grep '[^      ]' < "$log_file" > /dev/null ||
434     func_fatal_error "empty commit message, aborting"
436   if grep '^$' < "$log_file" > /dev/null; then
437     func_error "*** Warning: blank lines should not appear within commit messages."
438     func_error "*** They should be used to separate distinct commits."
439   fi
441   $opt_update && $opt_push && func_check_conflicts
443   # Do not check for empty $log_file again, even though the user might have
444   # zeroed it out.  If s/he did, it was probably intentional.
445   if $opt_commit; then
446     func_commit ${1+"$@"}
447   fi
449   # Send a copy of the log_file if sendmail_to was set:
450   subject=`sed -n '1p' "$log_file"`
451   if test -n "$sendmail_to"; then
452     if ! $opt_push; then
453       func_warning "Mail notification NOT sent for commit to local repository."
454     else
455       func_mailnotify
456     fi
457   fi
459   $RM -r "$log_dir"
462 exit $EXIT_SUCCESS
464 # Local Variables:
465 # mode:shell-script
466 # sh-indentation:2
467 # End: