1 AS_INIT[]m4_divert_push([HEADER-COPYRIGHT])dnl
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
71 # Report bugs to <gary@gnu.org>
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"
83 m4_include([getopt.m4sh])
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
108 # Locations for important files:
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
118 # Parse options once, thoroughly. This comes as soon as possible in
119 # the script to make things like `clcommit --version' happen quickly.
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
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"
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"
149 --debug) func_echo "enabling shell trace mode"
150 mailnotify_flags="$mailnotify_flags --debug"
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"
163 if cat < "$1" > "$log_file"; then :; else
169 --first|-1) if test -f "$log_File"; then
170 func_error "you can have at most one of -m, -F and -1"
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"
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"
190 echo "$1" > "$log_file"
194 --push|-p) opt_push=: ;;
196 --rcfile|-r) rc_file="./.clcommitrc"
197 if test $# -gt 0; then
200 *) rc_file="$1"; shift ;;
203 if test -f "$rc_file"; then :; else
204 func_error "rcfile \`$rc_file' does not exist"
208 # The funny quoting allows keeping one option per
210 eval set dummy `echo \`cat $rc_file\` \${1+"\$@"}`
214 --sendmail|-s) test $# = 0 && func_missing_arg $opt && break
215 func_quote_for_eval "$1"
216 sendmail_to="$func_quote_for_eval_result"
220 --signature) test $# = 0 && func_missing_arg $opt && break
221 signature_file="$HOME/.signature"
224 *) signature_file="$1"; shift ;;
226 if test -f "$signature_file"; then :; else
227 func_error "\`$signature_file': file not found"
232 --signoff) git_flags="$git_flags --signoff" ;;
234 --summary|-S) test $# = 0 && func_missing_arg $opt && break
239 --tags) opt_tags=: ;;
241 --verbose|-v) opt_verbose=: ;;
243 # Separate optargs to long options:
245 arg=`echo "$opt" | $SED "$my_sed_long_arg"`
246 opt=`echo "$opt" | $SED "$my_sed_long_opt"`
247 set dummy "$opt" "$arg" ${1+"$@"}
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+"$@"}
259 # Separate non-argument short options:
261 rest=`echo "$opt" |$SED "$my_sed_single_rest"`
262 opt=`echo "$opt" |$SED "$my_sed_single_opt"`
263 set dummy "$opt" "-$rest" ${1+"$@"}
267 -\?|-h) func_usage ;;
269 --version) func_version ;;
271 -*) func_fatal_help "unrecognized option \`$opt'" ;;
272 *) set dummy "$opt" ${1+"$@"}; shift; break ;;
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
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..."
304 # func_check_commit_msg
305 func_check_commit_msg ()
307 if test -z "$ChangeLog"; then
308 for f in ${1+"$@"}; do
310 ChangeLog* | */ChangeLog*)
311 if test -z "$ChangeLog"; then
314 func_fatal_error "multiple ChangeLog files: $ChangeLog and $f"
321 func_verbose "$progname: checking commit message..."
324 $SED 's,^,+,' < ${ChangeLog-ChangeLog} |
325 while $read_r line; do
327 "+") if $skipping; then skipping=false; else break; fi;;
329 func_error "*** Warning: lines should start with tabs, not spaces; ignoring line:"
330 echo "$line" | $SED 's/^.//' >&2;;
332 $skipping || echo "$line" ;;
335 $SED 's,^\+ ,,' > "$log_file" || exit $EXIT_FAILURE
337 $GIT diff ${ChangeLog-ChangeLog} |
338 while $read_r line; do
342 func_error "*** Warning: the following line in ChangeLog diff is suspicious:"
343 echo "$line" | $SED 's/^.//' >&2;;
345 func_error "*** Warning: lines should start with tabs, not spaces; ignoring line:"
346 echo "$line" | $SED 's/^.//' >&2;;
348 "+ "*) echo "$line";;
351 $SED -e 's,\+ ,,' -e '/./p' -e '/./d' -e '1d' -e '$d' > "$log_file" \
352 || exit $EXIT_FAILURE
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
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
375 $opt_tags && $GIT push --tags
385 notify_file="${log_dir}/notify"
386 func_verbose "Mailing commit notification to $sendmail_to"
389 echo Subject: $subject
390 test -f .git/config &&
391 echo "Root: `$SED -n 's,^ url = .*:\(.*\)$,\1,p' .git/config`"
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`"
399 $SED -e 's,^, ,' "$log_file"
400 test -f "$signature_file" && {
402 cat "$signature_file"
406 ${PAGER-more} "$notify_file" || break
408 # Break out the subject line again
409 my_mail_subject=`$SED -e '
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' \
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."
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.
446 func_commit ${1+"$@"}
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
453 func_warning "Mail notification NOT sent for commit to local repository."