3 # Copyright (c) 2005 Linus Torvalds
4 # Copyright (c) 2006 Junio C Hamano
6 USAGE
='[-a | --interactive] [-s] [-v] [--no-verify] [-m <message> | -F <logfile> | (-C|-c) <commit> | --amend] [-u] [-e] [--author <author>] [--template <file>] [[-i | -o] <path>...]'
11 git rev-parse
--verify HEAD
>/dev
/null
2>&1 || initial_commit
=t
24 echo >&2 "You might have meant to say 'git commit -i paths...', perhaps?"
28 THIS_INDEX
="$GIT_DIR/index"
29 NEXT_INDEX
="$GIT_DIR/next-index$$"
32 cp -p "$THIS_INDEX" "$NEXT_INDEX"
36 # If TMP_INDEX is defined, that means we are doing
37 # "--only" partial commit, and that index file is used
38 # to build the tree for the commit. Otherwise, if
39 # NEXT_INDEX exists, that is the index file used to
40 # make the commit. Otherwise we are using as-is commit
41 # so the regular index file is what we use to compare.
42 if test '' != "$TMP_INDEX"
44 GIT_INDEX_FILE
="$TMP_INDEX"
46 elif test -f "$NEXT_INDEX"
48 GIT_INDEX_FILE
="$NEXT_INDEX"
52 if test "$status_only" = "t" -o "$use_status_color" = "t"; then
57 git runstatus
${color} \
58 ${verbose:+--verbose} \
60 ${untracked_files:+--untracked}
64 test -z "$TMP_INDEX" || {
65 test -f "$TMP_INDEX" && rm -f "$TMP_INDEX"
70 ################################################################
71 # Command line argument parsing and sanity checking
91 templatefile
="`git config commit.template`"
95 -F|
--F|
-f|
--f|
--fi|
--fil|
--file)
96 case "$#" in 1) usage
;; esac
105 log_given
=t
$log_given
106 logfile
=`expr "z$1" : 'z-[Ff]\(.*\)'`
109 --F=*|
--f=*|
--fi=*|
--fil=*|
--file=*)
111 log_given
=t
$log_given
112 logfile
=`expr "z$1" : 'z-[^=]*=\(.*\)'`
119 --au=*|
--aut=*|
--auth=*|
--autho=*|
--author=*)
120 force_author
=`expr "z$1" : 'z-[^=]*=\(.*\)'`
123 --au|
--aut|
--auth|
--autho|
--author)
124 case "$#" in 1) usage
;; esac
129 -e|
--e|
--ed|
--edi|
--edit)
133 -i|
--i|
--in|
--inc|
--incl|
--inclu|
--includ|
--include)
137 --int|
--inte|
--inter|
--intera|
--interac|
--interact|
--interacti|\
138 --interactiv|
--interactive)
142 -o|
--o|
--on|
--onl|
--only)
146 -m|
--m|
--me|
--mes|
--mess|
--messa|
--messag|
--message)
147 case "$#" in 1) usage
;; esac
149 log_given
=m
$log_given
150 if test "$log_message" = ''
154 log_message
="$log_message
162 log_given
=m
$log_given
163 if test "$log_message" = ''
165 log_message
=`expr "z$1" : 'z-m\(.*\)'`
167 log_message
="$log_message
169 `expr "z
$1" : 'z-m\(.*\)'`"
174 --m=*|
--me=*|
--mes=*|
--mess=*|
--messa=*|
--messag=*|
--message=*)
175 log_given
=m
$log_given
176 if test "$log_message" = ''
178 log_message
=`expr "z$1" : 'z-[^=]*=\(.*\)'`
180 log_message
="$log_message
182 `expr "z
$1" : 'zq-[^=]*=\(.*\)'`"
187 -n|
--n|
--no|
--no-|
--no-v|
--no-ve|
--no-ver|
--no-veri|
--no-verif|\
192 --a|
--am|
--ame|
--amen|
--amend)
198 case "$#" in 1) usage
;; esac
200 log_given
=t
$log_given
205 --ree=*|
--reed=*|
--reedi=*|
--reedit=*|
--reedit-=*|
--reedit-m=*|\
206 --reedit-me=*|
--reedit-mes=*|
--reedit-mess=*|
--reedit-messa=*|\
207 --reedit-messag=*|
--reedit-message=*)
208 log_given
=t
$log_given
209 use_commit
=`expr "z$1" : 'z-[^=]*=\(.*\)'`
213 --ree|
--reed|
--reedi|
--reedit|
--reedit-|
--reedit-m|
--reedit-me|\
214 --reedit-mes|
--reedit-mess|
--reedit-messa|
--reedit-messag|\
216 case "$#" in 1) usage
;; esac
218 log_given
=t
$log_given
224 case "$#" in 1) usage
;; esac
226 log_given
=t
$log_given
231 --reu=*|
--reus=*|
--reuse=*|
--reuse-=*|
--reuse-m=*|
--reuse-me=*|\
232 --reuse-mes=*|
--reuse-mess=*|
--reuse-messa=*|
--reuse-messag=*|\
234 log_given
=t
$log_given
235 use_commit
=`expr "z$1" : 'z-[^=]*=\(.*\)'`
239 --reu|
--reus|
--reuse|
--reuse-|
--reuse-m|
--reuse-me|
--reuse-mes|\
240 --reuse-mess|
--reuse-messa|
--reuse-messag|
--reuse-message)
241 case "$#" in 1) usage
;; esac
243 log_given
=t
$log_given
248 -s|
--s|
--si|
--sig|
--sign|
--signo|
--signof|
--signoff)
252 -t|
--t|
--te|
--tem|
--temp|
--templ|
--templa|
--templat|
--template)
253 case "$#" in 1) usage
;; esac
259 -q|
--q|
--qu|
--qui|
--quie|
--quiet)
263 -v|
--v|
--ve|
--ver|
--verb|
--verbo|
--verbos|
--verbose)
267 -u|
--u|
--un|
--unt|
--untr|
--untra|
--untrac|
--untrack|
--untracke|\
268 --untracked|
--untracked-|
--untracked-f|
--untracked-fi|
--untracked-fil|\
269 --untracked-file|
--untracked-files)
285 case "$edit_flag" in t
) no_edit
= ;; esac
287 ################################################################
288 # Sanity check options
290 case "$amend,$initial_commit" in
292 die
"You do not have anything to amend." ;;
294 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
295 die
"You are in the middle of a merge -- cannot amend."
301 die
"Only one of -c/-C/-F can be used." ;;
303 die
"Option -m cannot be combined with -c/-C/-F." ;;
306 case "$#,$also,$only,$amend" in
308 die
"Only one of --include/--only can be used." ;;
310 die
"No paths with --include/--only does not make sense." ;;
312 only_include_assumed
="# Clever... amending the last one with dirty index." ;;
316 only_include_assumed
="# Explicit paths specified without -i nor -o; assuming --only paths..."
321 case "$all,$interactive,$also,$#" in
323 die
"Cannot use -a, --interactive or -i at the same time." ;;
325 die
"Paths with -a does not make sense." ;;
327 die
"Paths with --interactive does not make sense." ;;
329 die
"No paths with -i does not make sense." ;;
332 if test ! -z "$templatefile" -a -z "$log_given"
334 if test ! -f "$templatefile"
336 die
"Commit template file does not exist."
340 ################################################################
341 # Prepare index to have a tree to be committed
345 if test ! -f "$THIS_INDEX"
347 die
'nothing to commit (use "git add file1 file2" to include for commit)'
352 GIT_INDEX_FILE
="$NEXT_INDEX" &&
353 export GIT_INDEX_FILE
&&
354 git diff-files
--name-only -z |
355 git update-index
--remove -z --stdin
360 git ls-files
--error-unmatch -- "$@" >/dev
/null ||
exit
362 git diff-files
--name-only -z -- "$@" |
365 GIT_INDEX_FILE
="$NEXT_INDEX" &&
366 export GIT_INDEX_FILE
&&
367 git update-index
--remove -z --stdin
371 if test "$interactive" = t
; then
372 git add
--interactive ||
exit
378 if test -f "$GIT_DIR/MERGE_HEAD"
380 refuse_partial
"Cannot do a partial commit during a merge."
383 TMP_INDEX
="$GIT_DIR/tmp-index$$"
385 test -z "$initial_commit" && W
=--with-tree=HEAD
386 commit_only
=`git ls-files --error-unmatch $W -- "$@"` ||
exit
388 # Build a temporary index and update the real index
390 if test -z "$initial_commit"
392 GIT_INDEX_FILE
="$THIS_INDEX" \
393 git read-tree
--index-output="$TMP_INDEX" -i -m HEAD
398 printf '%s\n' "$commit_only" |
399 GIT_INDEX_FILE
="$TMP_INDEX" \
400 git update-index
--add --remove --stdin &&
403 printf '%s\n' "$commit_only" |
405 GIT_INDEX_FILE
="$NEXT_INDEX"
406 export GIT_INDEX_FILE
407 git update-index
--add --remove --stdin
414 ################################################################
415 # If we do as-is commit, the index file will be THIS_INDEX,
416 # otherwise NEXT_INDEX after we make this commit. We leave
417 # the index as is if we abort.
419 if test -f "$NEXT_INDEX"
421 USE_INDEX
="$NEXT_INDEX"
423 USE_INDEX
="$THIS_INDEX"
426 case "$status_only" in
428 # This will silently fail in a read-only repository, which is
430 GIT_INDEX_FILE
="$USE_INDEX" git update-index
-q --unmerged --refresh
435 GIT_INDEX_FILE
="$USE_INDEX" git update-index
-q --refresh ||
exit
439 ################################################################
440 # Grab commit message, write out tree and make commit.
442 if test t
= "$verify" && test -x "$GIT_DIR"/hooks
/pre-commit
446 GIT_INDEX_FILE
="$TMP_INDEX" "$GIT_DIR"/hooks
/pre-commit
448 GIT_INDEX_FILE
="$USE_INDEX" "$GIT_DIR"/hooks
/pre-commit
452 if test "$log_message" != ''
454 printf '%s\n' "$log_message"
455 elif test "$logfile" != ""
457 if test "$logfile" = -
460 echo >&2 "(reading log message from standard input)"
465 elif test "$use_commit" != ""
467 encoding
=$
(git config i18n.commitencoding ||
echo UTF-8
)
468 git show
-s --pretty=raw
--encoding="$encoding" "$use_commit" |
469 sed -e '1,/^$/d' -e 's/^ //'
470 elif test -f "$GIT_DIR/MERGE_MSG"
472 cat "$GIT_DIR/MERGE_MSG"
473 elif test -f "$GIT_DIR/SQUASH_MSG"
475 cat "$GIT_DIR/SQUASH_MSG"
476 elif test "$templatefile" != ""
479 fi | git stripspace
>"$GIT_DIR"/COMMIT_EDITMSG
483 sign
=$
(git-var GIT_COMMITTER_IDENT |
sed -e '
487 blank_before_signoff
=
488 tail -n 1 "$GIT_DIR"/COMMIT_EDITMSG |
489 grep 'Signed-off-by:' >/dev
/null || blank_before_signoff
='
491 tail -n 1 "$GIT_DIR"/COMMIT_EDITMSG |
492 grep "$sign"$
>/dev
/null ||
493 printf '%s%s\n' "$blank_before_signoff" "$sign" \
494 >>"$GIT_DIR"/COMMIT_EDITMSG
498 if test -f "$GIT_DIR/MERGE_HEAD" && test -z "$no_edit"; then
500 echo "# It looks like you may be committing a MERGE."
501 echo "# If this is not correct, please remove the file"
502 printf '%s\n' "# $GIT_DIR/MERGE_HEAD"
503 echo "# and try again"
505 fi >>"$GIT_DIR"/COMMIT_EDITMSG
508 if test '' != "$use_commit"
510 eval "$(get_author_ident_from_commit "$use_commit")"
511 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
513 if test '' != "$force_author"
515 GIT_AUTHOR_NAME
=`expr "z$force_author" : 'z\(.*[^ ]\) *<.*'` &&
516 GIT_AUTHOR_EMAIL
=`expr "z$force_author" : '.*\(<.*\)'` &&
517 test '' != "$GIT_AUTHOR_NAME" &&
518 test '' != "$GIT_AUTHOR_EMAIL" ||
519 die
"malformed --author parameter"
520 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
524 if test -z "$initial_commit"
527 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
528 rloga
='commit (merge)'
529 PARENTS
="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
530 elif test -n "$amend"; then
531 rloga
='commit (amend)'
532 PARENTS
=$
(git cat-file commit HEAD |
533 sed -n -e '/^$/q' -e 's/^parent /-p /p')
535 current
="$(git rev-parse --verify HEAD)"
537 if [ -z "$(git ls-files)" ]; then
538 echo >&2 'nothing to commit (use "git add file1 file2" to include for commit)'
542 rloga
='commit (initial)'
545 set_reflog_action
"$rloga"
547 if test -z "$no_edit"
551 echo "# Please enter the commit message for your changes."
552 echo "# (Comment lines starting with '#' will not be included)"
553 test -z "$only_include_assumed" ||
echo "$only_include_assumed"
555 } >>"$GIT_DIR"/COMMIT_EDITMSG
557 # we need to check if there is anything to commit
558 run_status
>/dev
/null
560 if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" ]
562 rm -f "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
570 git-var GIT_AUTHOR_IDENT
> /dev
/null || die
571 git-var GIT_COMMITTER_IDENT
> /dev
/null || die
572 git_editor
"$GIT_DIR/COMMIT_EDITMSG"
578 if test -x "$GIT_DIR"/hooks
/commit-msg
580 "$GIT_DIR"/hooks
/commit-msg
"$GIT_DIR"/COMMIT_EDITMSG ||
exit
584 if test -z "$no_edit"
592 ' "$GIT_DIR"/COMMIT_EDITMSG
594 cat "$GIT_DIR"/COMMIT_EDITMSG
596 git stripspace
>"$GIT_DIR"/COMMIT_MSG
598 # Test whether the commit message has any content we didn't supply.
600 grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
601 git stripspace
> "$GIT_DIR"/COMMIT_BAREMSG
603 # Is the commit message totally empty?
604 if test -s "$GIT_DIR"/COMMIT_BAREMSG
606 if test "$templatefile" != ""
608 # Test whether this is just the unaltered template.
609 if cnt
=`sed -e '/^#/d' < "$templatefile" |
611 diff "$GIT_DIR"/COMMIT_BAREMSG - |
618 # No template, so the content in the commit message must
619 # have come from the user.
624 rm -f "$GIT_DIR"/COMMIT_BAREMSG
626 if test "$have_commitmsg" = "t"
628 if test -z "$TMP_INDEX"
630 tree
=$
(GIT_INDEX_FILE
="$USE_INDEX" git write-tree
)
632 tree
=$
(GIT_INDEX_FILE
="$TMP_INDEX" git write-tree
) &&
635 commit
=$
(git commit-tree
$tree $PARENTS <"$GIT_DIR/COMMIT_MSG") &&
636 rlogm
=$
(sed -e 1q
"$GIT_DIR"/COMMIT_MSG
) &&
637 git update-ref
-m "$GIT_REFLOG_ACTION: $rlogm" HEAD
$commit "$current" &&
638 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" &&
639 if test -f "$NEXT_INDEX"
641 mv "$NEXT_INDEX" "$THIS_INDEX"
646 echo >&2 "* no commit message? aborting commit."
650 rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
658 if test -x "$GIT_DIR"/hooks
/post-commit
660 "$GIT_DIR"/hooks
/post-commit
664 commit
=`git diff-tree --always --shortstat --pretty="format:%h: %s"\
665 --summary --root HEAD --`
666 echo "Created${initial_commit:+ initial} commit $commit"