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?"
29 THIS_INDEX="${GIT_INDEX_FILE:-$GIT_DIR/index}"
30 NEXT_INDEX="$GIT_DIR/next-index$$"
33 cp -p "$THIS_INDEX" "$NEXT_INDEX"
37 # If TMP_INDEX is defined, that means we are doing
38 # "--only" partial commit, and that index file is used
39 # to build the tree for the commit. Otherwise, if
40 # NEXT_INDEX exists, that is the index file used to
41 # make the commit. Otherwise we are using as-is commit
42 # so the regular index file is what we use to compare.
43 if test '' != "$TMP_INDEX"
45 GIT_INDEX_FILE="$TMP_INDEX"
47 elif test -f "$NEXT_INDEX"
49 GIT_INDEX_FILE="$NEXT_INDEX"
53 if test "$status_only" = "t" -o "$use_status_color" = "t"; then
58 git runstatus ${color} \
59 ${verbose:+--verbose} \
61 ${untracked_files:+--untracked}
65 test -z "$TMP_INDEX" || {
66 test -f "$TMP_INDEX" && rm -f "$TMP_INDEX"
71 ################################################################
72 # Command line argument parsing and sanity checking
92 templatefile="`git config commit.template`"
96 -F|--F|-f|--f|--fi|--fil|--file)
97 case "$#" in 1) usage ;; esac
100 log_given=t$log_given
105 log_given=t$log_given
108 --F=*|--f=*|--fi=*|--fil=*|--file=*)
110 log_given=t$log_given
116 --au=*|--aut=*|--auth=*|--autho=*|--author=*)
117 force_author="${1#*=}"
119 --au|--aut|--auth|--autho|--author)
120 case "$#" in 1) usage ;; esac
124 -e|--e|--ed|--edi|--edit)
127 -i|--i|--in|--inc|--incl|--inclu|--includ|--include)
130 --int|--inte|--inter|--intera|--interac|--interact|--interacti|\
131 --interactiv|--interactive)
134 -o|--o|--on|--onl|--only)
137 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
138 case "$#" in 1) usage ;; esac
140 log_given=m$log_given
141 log_message="${log_message:+${log_message}
147 log_given=m$log_given
148 log_message="${log_message:+${log_message}
153 --m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
154 log_given=m$log_given
155 log_message="${log_message:+${log_message}
160 -n|--n|--no|--no-|--no-v|--no-ve|--no-ver|--no-veri|--no-verif|\
164 --a|--am|--ame|--amen|--amend)
169 case "$#" in 1) usage ;; esac
171 log_given=t$log_given
175 --ree=*|--reed=*|--reedi=*|--reedit=*|--reedit-=*|--reedit-m=*|\
176 --reedit-me=*|--reedit-mes=*|--reedit-mess=*|--reedit-messa=*|\
177 --reedit-messag=*|--reedit-message=*)
178 log_given=t$log_given
182 --ree|--reed|--reedi|--reedit|--reedit-|--reedit-m|--reedit-me|\
183 --reedit-mes|--reedit-mess|--reedit-messa|--reedit-messag|\
185 case "$#" in 1) usage ;; esac
187 log_given=t$log_given
192 case "$#" in 1) usage ;; esac
194 log_given=t$log_given
198 --reu=*|--reus=*|--reuse=*|--reuse-=*|--reuse-m=*|--reuse-me=*|\
199 --reuse-mes=*|--reuse-mess=*|--reuse-messa=*|--reuse-messag=*|\
201 log_given=t$log_given
205 --reu|--reus|--reuse|--reuse-|--reuse-m|--reuse-me|--reuse-mes|\
206 --reuse-mess|--reuse-messa|--reuse-messag|--reuse-message)
207 case "$#" in 1) usage ;; esac
209 log_given=t$log_given
213 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
216 -t|--t|--te|--tem|--temp|--templ|--templa|--templat|--template)
217 case "$#" in 1) usage ;; esac
222 -q|--q|--qu|--qui|--quie|--quiet)
225 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
228 -u|--u|--un|--unt|--untr|--untra|--untrac|--untrack|--untracke|\
229 --untracked|--untracked-|--untracked-f|--untracked-fi|--untracked-fil|\
230 --untracked-file|--untracked-files)
246 case "$edit_flag" in t) no_edit= ;; esac
248 ################################################################
249 # Sanity check options
251 case "$amend,$initial_commit" in
253 die "You do not have anything to amend." ;;
255 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
256 die "You are in the middle of a merge -- cannot amend."
262 die "Only one of -c/-C/-F can be used." ;;
264 die "Option -m cannot be combined with -c/-C/-F." ;;
267 case "$#,$also,$only,$amend" in
269 die "Only one of --include/--only can be used." ;;
271 die "No paths with --include/--only does not make sense." ;;
273 only_include_assumed="# Clever... amending the last one with dirty index." ;;
277 only_include_assumed="# Explicit paths specified without -i nor -o; assuming --only paths..."
282 case "$all,$interactive,$also,$#" in
284 die "Cannot use -a, --interactive or -i at the same time." ;;
286 die "Paths with -a does not make sense." ;;
288 die "Paths with --interactive does not make sense." ;;
290 die "No paths with -i does not make sense." ;;
293 if test ! -z "$templatefile" -a -z "$log_given"
295 if test ! -f "$templatefile"
297 die "Commit template file does not exist."
301 ################################################################
302 # Prepare index to have a tree to be committed
306 if test ! -f "$THIS_INDEX"
308 die 'nothing to commit (use "git add file1 file2" to include for commit)'
313 GIT_INDEX_FILE="$NEXT_INDEX" &&
314 export GIT_INDEX_FILE &&
315 git diff-files --name-only -z |
316 git update-index --remove -z --stdin
321 git ls-files --error-unmatch -- "$@" >/dev/null || exit
323 git diff-files --name-only -z -- "$@" |
326 GIT_INDEX_FILE="$NEXT_INDEX" &&
327 export GIT_INDEX_FILE &&
328 git update-index --remove -z --stdin
332 if test "$interactive" = t; then
333 git add --interactive || exit
339 if test -f "$GIT_DIR/MERGE_HEAD"
341 refuse_partial "Cannot do a partial commit during a merge."
344 TMP_INDEX="$GIT_DIR/tmp-index$$"
346 test -z "$initial_commit" && W=--with-tree=HEAD
347 commit_only=`git ls-files --error-unmatch $W -- "$@"` || exit
349 # Build a temporary index and update the real index
351 if test -z "$initial_commit"
353 GIT_INDEX_FILE="$THIS_INDEX" \
354 git read-tree --index-output="$TMP_INDEX" -i -m HEAD
359 printf '%s\n' "$commit_only" |
360 GIT_INDEX_FILE="$TMP_INDEX" \
361 git update-index --add --remove --stdin &&
364 printf '%s\n' "$commit_only" |
366 GIT_INDEX_FILE="$NEXT_INDEX"
367 export GIT_INDEX_FILE
368 git update-index --add --remove --stdin
375 ################################################################
376 # If we do as-is commit, the index file will be THIS_INDEX,
377 # otherwise NEXT_INDEX after we make this commit. We leave
378 # the index as is if we abort.
380 if test -f "$NEXT_INDEX"
382 USE_INDEX="$NEXT_INDEX"
384 USE_INDEX="$THIS_INDEX"
387 case "$status_only" in
389 # This will silently fail in a read-only repository, which is
391 GIT_INDEX_FILE="$USE_INDEX" git update-index -q --unmerged --refresh
396 GIT_INDEX_FILE="$USE_INDEX" git update-index -q --refresh || exit
400 ################################################################
401 # Grab commit message, write out tree and make commit.
403 if test t = "$verify" && test -x "$GIT_DIR"/hooks/pre-commit
405 GIT_INDEX_FILE="${TMP_INDEX:-${USE_INDEX}}" "$GIT_DIR"/hooks/pre-commit \
409 if test "$log_message" != ''
411 printf '%s\n' "$log_message"
412 elif test "$logfile" != ""
414 if test "$logfile" = -
417 echo >&2 "(reading log message from standard input)"
422 elif test "$use_commit" != ""
424 encoding=$(git config i18n.commitencoding || echo UTF-8)
425 git show -s --pretty=raw --encoding="$encoding" "$use_commit" |
426 sed -e '1,/^$/d' -e 's/^ //'
427 elif test -f "$GIT_DIR/MERGE_MSG"
429 cat "$GIT_DIR/MERGE_MSG"
430 elif test -f "$GIT_DIR/SQUASH_MSG"
432 cat "$GIT_DIR/SQUASH_MSG"
433 elif test "$templatefile" != ""
436 fi | git stripspace >"$GIT_DIR"/COMMIT_EDITMSG
440 sign=$(git-var GIT_COMMITTER_IDENT | sed -e '
444 blank_before_signoff=
445 tail -n 1 "$GIT_DIR"/COMMIT_EDITMSG |
446 grep 'Signed-off-by:' >/dev/null || blank_before_signoff='
448 tail -n 1 "$GIT_DIR"/COMMIT_EDITMSG |
449 grep "$sign"$ >/dev/null ||
450 printf '%s%s\n' "$blank_before_signoff" "$sign" \
451 >>"$GIT_DIR"/COMMIT_EDITMSG
455 if test -f "$GIT_DIR/MERGE_HEAD" && test -z "$no_edit"; then
457 echo "# It looks like you may be committing a MERGE."
458 echo "# If this is not correct, please remove the file"
459 printf '%s\n' "# $GIT_DIR/MERGE_HEAD"
460 echo "# and try again"
462 fi >>"$GIT_DIR"/COMMIT_EDITMSG
465 if test '' != "$use_commit"
467 eval "$(get_author_ident_from_commit "$use_commit")"
468 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
470 if test '' != "$force_author"
472 GIT_AUTHOR_NAME=`expr "z$force_author" : 'z\(.*[^ ]\) *<.*'` &&
473 GIT_AUTHOR_EMAIL=`expr "z$force_author" : '.*\(<.*\)'` &&
474 test '' != "$GIT_AUTHOR_NAME" &&
475 test '' != "$GIT_AUTHOR_EMAIL" ||
476 die "malformed --author parameter"
477 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
481 if test -z "$initial_commit"
484 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
485 rloga='commit (merge)'
486 PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
487 elif test -n "$amend"; then
488 rloga='commit (amend)'
489 PARENTS=$(git cat-file commit HEAD |
490 sed -n -e '/^$/q' -e 's/^parent /-p /p')
492 current="$(git rev-parse --verify HEAD)"
494 if [ -z "$(git ls-files)" ]; then
495 echo >&2 'nothing to commit (use "git add file1 file2" to include for commit)'
499 rloga='commit (initial)'
502 set_reflog_action "$rloga"
504 if test -z "$no_edit"
508 echo "# Please enter the commit message for your changes."
509 echo "# (Comment lines starting with '#' will not be included)"
510 test -z "$only_include_assumed" || echo "$only_include_assumed"
512 } >>"$GIT_DIR"/COMMIT_EDITMSG
514 # we need to check if there is anything to commit
515 run_status >/dev/null
517 if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" ]
519 rm -f "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
527 git-var GIT_AUTHOR_IDENT > /dev/null || die
528 git-var GIT_COMMITTER_IDENT > /dev/null || die
529 git_editor "$GIT_DIR/COMMIT_EDITMSG"
535 if test -x "$GIT_DIR"/hooks/commit-msg
537 "$GIT_DIR"/hooks/commit-msg "$GIT_DIR"/COMMIT_EDITMSG || exit
541 if test -z "$no_edit"
549 ' "$GIT_DIR"/COMMIT_EDITMSG
551 cat "$GIT_DIR"/COMMIT_EDITMSG
553 git stripspace >"$GIT_DIR"/COMMIT_MSG
555 # Test whether the commit message has any content we didn't supply.
557 grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
558 git stripspace > "$GIT_DIR"/COMMIT_BAREMSG
560 # Is the commit message totally empty?
561 if test -s "$GIT_DIR"/COMMIT_BAREMSG
563 if test "$templatefile" != ""
565 # Test whether this is just the unaltered template.
566 if cnt=`sed -e '/^#/d' < "$templatefile" |
568 diff "$GIT_DIR"/COMMIT_BAREMSG - |
575 # No template, so the content in the commit message must
576 # have come from the user.
581 rm -f "$GIT_DIR"/COMMIT_BAREMSG
583 if test "$have_commitmsg" = "t"
585 if test -z "$TMP_INDEX"
587 tree=$(GIT_INDEX_FILE="$USE_INDEX" git write-tree)
589 tree=$(GIT_INDEX_FILE="$TMP_INDEX" git write-tree) &&
592 commit=$(git commit-tree $tree $PARENTS <"$GIT_DIR/COMMIT_MSG") &&
593 rlogm=$(sed -e 1q "$GIT_DIR"/COMMIT_MSG) &&
594 git update-ref -m "$GIT_REFLOG_ACTION: $rlogm" HEAD $commit "$current" &&
595 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" &&
596 if test -f "$NEXT_INDEX"
598 mv "$NEXT_INDEX" "$THIS_INDEX"
603 echo >&2 "* no commit message? aborting commit."
607 rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
616 if test -x "$GIT_DIR"/hooks/post-commit
618 "$GIT_DIR"/hooks/post-commit
622 commit=`git diff-tree --always --shortstat --pretty="format:%h: %s"\
623 --summary --root HEAD --`
624 echo "Created${initial_commit:+ initial} commit $commit"