Prepare for 2.2.6b release.
[libtool.git] / clcommit.m4sh
blob4eefd071421349435e915eb0ee0c54da7540811c
1 m4_define([_m4_divert(SCRIPT)], 100)
2 m4_divert_push([SCRIPT])#!/bin/sh
3 # @configure_input@
5 # clcommit (GNU @PACKAGE@) version 2.0
6 # Written by Gary V. Vaughan <gary@gnu.org>
7 # and Alexandre Oliva <aoliva@redhat.com>
9 # Copyright (C) 1999, 2000, 2004, 2006, 2008 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 # Clcommit 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 # Clcommit 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
21 # GNU General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with clcommit; see the file COPYING.  If not, a copy
25 # can be downloaded from http://www.gnu.org/licenses/gpl.html,
26 # or obtained by writing to the Free Software Foundation, Inc.,
27 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 # Usage: $progname [OPTION]... [--] [filepattern ...]
31 # -a AUTHOR --author=AUTHOR    override changeset author name with AUTHOR
32 # -C FILE   --changelog=FILE   extract commit message from specified FILE
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 git diff
38 # -f        --force            don't check (unless *followed* by -n), and just
39 #                              display commit message instead of running $PAGER
40 #           --from=ADDRESS     override default from ADDRESS in commit email
41 # -m msg    --message=STRING   set commit message to STRING
42 #           --msg=STRING          same as -m
43 # -p        --push             push the changes back to origin
44 # -r [FILE] --rcfile[=FILE]    read default option from FILE [./.clcommitrc]
45 # -s addr   --sendmail=ADDRESS send commit email of the differences to ADDRESS
46 #           --signature[=FILE] add FILE to the end of the email [~/.signature]
47 #           --signoff          add a Signed-off-by attribution at the end
48 # -S TEXT   --summary=TEXT     specify a TEXT subject line for the commit email
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 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 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 AS_SHELL_SANITIZE
83 $as_unset CDPATH
85 m4_include([getopt.m4sh])
87 M4SH_VERBATIM([[
88 # Global variables:
89 opt_commit=:
90 opt_first=false
91 opt_push=false
92 opt_update=:
93 opt_verbose=false
95 git_flags=
96 mailnotify_flags=
97 sendmail_to=
99 exit_cmd=:
101 # try to find out whether read supports -r
102 if echo bt | tr b '\\' | { read -r line; test "X$line" = 'X\t'; } 2>/dev/null
103 then
104   read_r='read -r'
105 else
106   read_r=read
109 # Locations for important files:
110 signature_file=
111 log_dir="`func_mktempdir`"
112 log_file="$log_dir/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       --verbose|-v)     opt_verbose=:                           ;;
241       # Separate optargs to long options:
242       --*=*)
243                         arg=`echo "$opt" | $SED "$my_sed_long_arg"`
244                         opt=`echo "$opt" | $SED "$my_sed_long_opt"`
245                         set dummy "$opt" "$arg" ${1+"$@"}
246                         shift
247                         ;;
249       # Separate optargs to short options:
250       -a*|-m*|-F*|-C*|-S*|-s*)
251                         arg=`echo "$opt" |$SED "$my_sed_single_rest"`
252                         opt=`echo "$opt" |$SED "$my_sed_single_opt"`
253                         set dummy "$opt" "$arg" ${1+"$@"}
254                         shift
255                         ;;
257       # Separate non-argument short options:
258       -1*|-f*|-p*|-n*|-q*)
259                         rest=`echo "$opt" |$SED "$my_sed_single_rest"`
260                         opt=`echo "$opt" |$SED "$my_sed_single_opt"`
261                         set dummy "$opt" "-$rest" ${1+"$@"}
262                         shift
263                         ;;
265       -\?|-h)           func_usage                                      ;;
266       --help)           func_help                                       ;;
267       --version)        func_version                                    ;;
268       --)               break                                           ;;
269       -*)               func_fatal_help "unrecognized option \`$opt'"   ;;
270       *)                set dummy "$opt" ${1+"$@"};     shift; break            ;;
271     esac
272   done
274   if test -z "$sendmail_to"; then
276     # can't have a from address without a destination address
277     test -n "$sendmail_from" &&
278       func_error "can't use --from without --sendmail." && exit_cmd=exit
280     # can't use a signature file without a destination address
281     test -n "$signature_file" &&
282       func_error "can't use --signature without --sendmail." && exit_cmd=exit
283   fi
285   # Bail if the options were screwed
286   $exit_cmd $EXIT_FAILURE
289 # func_check_conflicts
290 func_check_conflicts ()
292     # HELP?!?  How to check for git push conflicts?
293     if false; then
294       func_fatal_error "some conflicts were found with CVS repository, aborting..."
295     fi
299 # func_check_commit_msg
300 func_check_commit_msg ()
302     if test -z "$ChangeLog"; then
303       for f in ${1+"$@"}; do
304         case "$f" in
305           ChangeLog* | */ChangeLog*)
306             if test -z "$ChangeLog"; then
307               ChangeLog="$f"
308             else
309               func_fatal_error "multiple ChangeLog files: $ChangeLog and $f"
310             fi
311           ;;
312         esac
313       done
314     fi
316     func_verbose "$progname: checking commit message..."
317     if $opt_first; then
318       skipping=:
319       $SED 's,^,+,' < ${ChangeLog-ChangeLog} |
320         while $read_r line; do
321           case "$line" in
322             "+") if $skipping; then skipping=false; else break; fi;;
323             "+ "*)
324               func_error "*** Warning: lines should start with tabs, not spaces; ignoring line:"
325               echo "$line" | $SED 's/^.//' >&2;;
326             "+  "*)
327               $skipping || echo "$line" ;;
328           esac
329         done |
330           $SED 's,^\+   ,,' > "$log_file" || exit $EXIT_FAILURE
331     else
332       $GIT diff ${ChangeLog-ChangeLog} |
333         while $read_r line; do
334           case $line in
335             "--- "*) :;;
336             "-"*)
337               func_error "*** Warning: the following line in ChangeLog diff is suspicious:"
338               echo "$line" | $SED 's/^.//' >&2;;
339             "+ "*)
340               func_error "*** Warning: lines should start with tabs, not spaces; ignoring line:"
341               echo "$line" | $SED 's/^.//' >&2;;
342             "+") echo ;;
343             "+  "*) echo "$line";;
344           esac
345         done |
346           $SED -e 's,\+ ,,' -e '/./p' -e '/./d' -e '1d' -e '$d' > "$log_file" \
347       || exit $EXIT_FAILURE
348     fi
349     # The sed script above removes "+TAB" from the beginning of a line, then
350     # deletes the first and/or the last line, when they happen to be empty
354 # func_commit
355 func_commit ()
357     ${PAGER-more} "$log_file" || exit $EXIT_FAILURE
359     sleep 1 # give the user some time for a ^C
361     subject=`git status 2>/dev/null | $SED -n 's/^#.*[mad][ode][dl].*ed: *//p'`
362     test $# -gt 0 && subject="$@"
364     test $# -gt 0 || { set dummy -a; shift; }
365     func_verbose "$GIT commit$git_flags -F $log_file ${1+$@}"
366     $GIT commit$git_flags -F $log_file ${1+"$@"} || exit $EXIT_FAILURE
368     $opt_push && $GIT push
370     :
374 # func_mailnotify
375 func_mailnotify ()
377     notify_file="${log_dir}/notify"
378     func_verbose "Mailing commit notification to $sendmail_to"
380     {
381       echo Subject: $subject
382       test -f .git/config &&
383           echo "Root:           `$SED -n 's,^   url = .*:\(.*\)$,\1,p' .git/config`"
384       test -f $MKSTAMP &&
385           echo "Timestamp:      `$SHELL $MKSTAMP .`"
386       echo "Branch:             `$GIT branch | sed -n 's/\* //p'`"
387       test -f .git/config &&
388           echo "Changes by:     `$SED -n 's,^   url = \([^:]*\):.*$,\1,p' .git/config`"
389       echo ""
390       echo "Log Message:"
391       $SED -e 's,^,     ,' "$log_file"
392       test -f "$signature_file" && {
393         echo '-- '
394         cat "$signature_file"
395       }
396     } > "$notify_file"
398     ${PAGER-more} "$notify_file" || break
400     # Break out the subject line again
401     my_mail_subject=`$SED -e '
402         1{
403             s/^Subject: *//
404             q
405         }' "$notify_file"`
406     my_mail_body=`$SED -e '2,$p;d' "$notify_file"`
407     echo "$my_mail_body" > "$notify_file"
409     func_verbose "mailing commit notification to \"$sendmail_to\""
410     func_quote_for_eval "$my_mail_subject"
411     func_show_eval "$MAILNOTIFY $mailnotify_flags \
412         -s $func_quote_for_eval_result -m 'text/plain' -f '$notify_file' \
413         -- $sendmail_to"
418 ## ----- ##
419 ## main. ##
420 ## ----- ##
423   $opt_update && func_check_conflicts
425   test -f "$log_file" || func_check_commit_msg
427   grep '[^      ]' < "$log_file" > /dev/null ||
428     func_fatal_error "empty commit message, aborting"
430   if grep '^$' < "$log_file" > /dev/null; then
431     func_error "*** Warning: blank lines should not appear within commit messages."
432     func_error "*** They should be used to separate distinct commits."
433   fi
435   # Do not check for empty $log_file again, even though the user might have
436   # zeroed it out.  If s/he did, it was probably intentional.
437   if $opt_commit; then
438     func_commit ${1+"$@"}
439   fi
441   subject=`sed -n '1p' "$log_file"`
443   # Send a copy of the log_file if sendmail_to was set:
444   if test -n "$sendmail_to"; then
445     if ! $opt_push; then
446       func_warning "Mail notification NOT sent for commit to local repository."
447     else
448       func_mailnotify
449     fi
450   fi
452   $RM -r "$log_dir"
455 exit $EXIT_SUCCESS
457 # Local Variables:
458 # mode:shell-script
459 # sh-indentation:2
460 # End: