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 case "$status_only" in
56 git runstatus ${color} \
57 ${verbose:+--verbose} \
59 ${untracked_files:+--untracked}
63 test -z "$TMP_INDEX" || {
64 test -f "$TMP_INDEX" && rm -f "$TMP_INDEX"
69 ################################################################
70 # Command line argument parsing and sanity checking
90 templatefile="`git config commit.template`"
91 while case "$#" in 0) break;; esac
94 -F|--F|-f|--f|--fi|--fil|--file)
95 case "$#" in 1) usage ;; esac
104 log_given=t$log_given
105 logfile=`expr "z$1" : 'z-[Ff]\(.*\)'`
108 --F=*|--f=*|--fi=*|--fil=*|--file=*)
110 log_given=t$log_given
111 logfile=`expr "z$1" : 'z-[^=]*=\(.*\)'`
118 --au=*|--aut=*|--auth=*|--autho=*|--author=*)
119 force_author=`expr "z$1" : 'z-[^=]*=\(.*\)'`
122 --au|--aut|--auth|--autho|--author)
123 case "$#" in 1) usage ;; esac
128 -e|--e|--ed|--edi|--edit)
132 -i|--i|--in|--inc|--incl|--inclu|--includ|--include)
136 --int|--inte|--inter|--intera|--interac|--interact|--interacti|\
137 --interactiv|--interactive)
141 -o|--o|--on|--onl|--only)
145 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
146 case "$#" in 1) usage ;; esac
148 log_given=m$log_given
149 if test "$log_message" = ''
153 log_message="$log_message
161 log_given=m$log_given
162 if test "$log_message" = ''
164 log_message=`expr "z$1" : 'z-m\(.*\)'`
166 log_message="$log_message
168 `expr "z$1" : 'z-m\(.*\)'`"
173 --m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
174 log_given=m$log_given
175 if test "$log_message" = ''
177 log_message=`expr "z$1" : 'z-[^=]*=\(.*\)'`
179 log_message="$log_message
181 `expr "z$1" : 'zq-[^=]*=\(.*\)'`"
186 -n|--n|--no|--no-|--no-v|--no-ve|--no-ver|--no-veri|--no-verif|\
191 --a|--am|--ame|--amen|--amend)
197 case "$#" in 1) usage ;; esac
199 log_given=t$log_given
204 --ree=*|--reed=*|--reedi=*|--reedit=*|--reedit-=*|--reedit-m=*|\
205 --reedit-me=*|--reedit-mes=*|--reedit-mess=*|--reedit-messa=*|\
206 --reedit-messag=*|--reedit-message=*)
207 log_given=t$log_given
208 use_commit=`expr "z$1" : 'z-[^=]*=\(.*\)'`
212 --ree|--reed|--reedi|--reedit|--reedit-|--reedit-m|--reedit-me|\
213 --reedit-mes|--reedit-mess|--reedit-messa|--reedit-messag|\
215 case "$#" in 1) usage ;; esac
217 log_given=t$log_given
223 case "$#" in 1) usage ;; esac
225 log_given=t$log_given
230 --reu=*|--reus=*|--reuse=*|--reuse-=*|--reuse-m=*|--reuse-me=*|\
231 --reuse-mes=*|--reuse-mess=*|--reuse-messa=*|--reuse-messag=*|\
233 log_given=t$log_given
234 use_commit=`expr "z$1" : 'z-[^=]*=\(.*\)'`
238 --reu|--reus|--reuse|--reuse-|--reuse-m|--reuse-me|--reuse-mes|\
239 --reuse-mess|--reuse-messa|--reuse-messag|--reuse-message)
240 case "$#" in 1) usage ;; esac
242 log_given=t$log_given
247 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
251 -t|--t|--te|--tem|--temp|--templ|--templa|--templat|--template)
252 case "$#" in 1) usage ;; esac
258 -q|--q|--qu|--qui|--quie|--quiet)
262 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
266 -u|--u|--un|--unt|--untr|--untra|--untrac|--untrack|--untracke|\
267 --untracked|--untracked-|--untracked-f|--untracked-fi|--untracked-fil|\
268 --untracked-file|--untracked-files)
284 case "$edit_flag" in t) no_edit= ;; esac
286 ################################################################
287 # Sanity check options
289 case "$amend,$initial_commit" in
291 die "You do not have anything to amend." ;;
293 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
294 die "You are in the middle of a merge -- cannot amend."
300 die "Only one of -c/-C/-F can be used." ;;
302 die "Option -m cannot be combined with -c/-C/-F." ;;
305 case "$#,$also,$only,$amend" in
307 die "Only one of --include/--only can be used." ;;
309 die "No paths with --include/--only does not make sense." ;;
311 only_include_assumed="# Clever... amending the last one with dirty index." ;;
315 only_include_assumed="# Explicit paths specified without -i nor -o; assuming --only paths..."
320 case "$all,$interactive,$also,$#" in
322 die "Cannot use -a, --interactive or -i at the same time." ;;
324 die "Paths with -a does not make sense." ;;
326 die "Paths with --interactive does not make sense." ;;
328 die "No paths with -i does not make sense." ;;
331 if test ! -z "$templatefile" -a -z "$log_given"
333 if test ! -f "$templatefile"
335 die "Commit template file does not exist."
339 ################################################################
340 # Prepare index to have a tree to be committed
344 if test ! -f "$THIS_INDEX"
346 die 'nothing to commit (use "git add file1 file2" to include for commit)'
351 GIT_INDEX_FILE="$NEXT_INDEX" &&
352 export GIT_INDEX_FILE &&
353 git diff-files --name-only -z |
354 git update-index --remove -z --stdin
359 git ls-files --error-unmatch -- "$@" >/dev/null || exit
361 git diff-files --name-only -z -- "$@" |
364 GIT_INDEX_FILE="$NEXT_INDEX" &&
365 export GIT_INDEX_FILE &&
366 git update-index --remove -z --stdin
370 if test "$interactive" = t; then
371 git add --interactive || exit
377 if test -f "$GIT_DIR/MERGE_HEAD"
379 refuse_partial "Cannot do a partial commit during a merge."
381 TMP_INDEX="$GIT_DIR/tmp-index$$"
382 commit_only=`git ls-files --error-unmatch -- "$@"` || exit
384 # Build a temporary index and update the real index
386 if test -z "$initial_commit"
388 GIT_INDEX_FILE="$THIS_INDEX" \
389 git read-tree --index-output="$TMP_INDEX" -i -m HEAD
394 printf '%s\n' "$commit_only" |
395 GIT_INDEX_FILE="$TMP_INDEX" \
396 git update-index --add --remove --stdin &&
399 printf '%s\n' "$commit_only" |
401 GIT_INDEX_FILE="$NEXT_INDEX"
402 export GIT_INDEX_FILE
403 git update-index --remove --stdin
410 ################################################################
411 # If we do as-is commit, the index file will be THIS_INDEX,
412 # otherwise NEXT_INDEX after we make this commit. We leave
413 # the index as is if we abort.
415 if test -f "$NEXT_INDEX"
417 USE_INDEX="$NEXT_INDEX"
419 USE_INDEX="$THIS_INDEX"
422 case "$status_only" in
424 # This will silently fail in a read-only repository, which is
426 GIT_INDEX_FILE="$USE_INDEX" git update-index -q --unmerged --refresh
431 GIT_INDEX_FILE="$USE_INDEX" git update-index -q --refresh || exit
435 ################################################################
436 # Grab commit message, write out tree and make commit.
438 if test t = "$verify" && test -x "$GIT_DIR"/hooks/pre-commit
442 GIT_INDEX_FILE="$TMP_INDEX" "$GIT_DIR"/hooks/pre-commit
444 GIT_INDEX_FILE="$USE_INDEX" "$GIT_DIR"/hooks/pre-commit
448 if test "$log_message" != ''
450 printf '%s\n' "$log_message"
451 elif test "$logfile" != ""
453 if test "$logfile" = -
456 echo >&2 "(reading log message from standard input)"
461 elif test "$use_commit" != ""
463 encoding=$(git config i18n.commitencoding || echo UTF-8)
464 git show -s --pretty=raw --encoding="$encoding" "$use_commit" |
465 sed -e '1,/^$/d' -e 's/^ //'
466 elif test -f "$GIT_DIR/MERGE_MSG"
468 cat "$GIT_DIR/MERGE_MSG"
469 elif test -f "$GIT_DIR/SQUASH_MSG"
471 cat "$GIT_DIR/SQUASH_MSG"
472 elif test "$templatefile" != ""
475 fi | git stripspace >"$GIT_DIR"/COMMIT_EDITMSG
479 sign=$(git-var GIT_COMMITTER_IDENT | sed -e '
483 blank_before_signoff=
484 tail -n 1 "$GIT_DIR"/COMMIT_EDITMSG |
485 grep 'Signed-off-by:' >/dev/null || blank_before_signoff='
487 tail -n 1 "$GIT_DIR"/COMMIT_EDITMSG |
488 grep "$sign"$ >/dev/null ||
489 printf '%s%s\n' "$blank_before_signoff" "$sign" \
490 >>"$GIT_DIR"/COMMIT_EDITMSG
494 if test -f "$GIT_DIR/MERGE_HEAD" && test -z "$no_edit"; then
496 echo "# It looks like you may be committing a MERGE."
497 echo "# If this is not correct, please remove the file"
498 printf '%s\n' "# $GIT_DIR/MERGE_HEAD"
499 echo "# and try again"
501 fi >>"$GIT_DIR"/COMMIT_EDITMSG
504 if test '' != "$use_commit"
506 eval "$(get_author_ident_from_commit "$use_commit")"
507 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
509 if test '' != "$force_author"
511 GIT_AUTHOR_NAME=`expr "z$force_author" : 'z\(.*[^ ]\) *<.*'` &&
512 GIT_AUTHOR_EMAIL=`expr "z$force_author" : '.*\(<.*\)'` &&
513 test '' != "$GIT_AUTHOR_NAME" &&
514 test '' != "$GIT_AUTHOR_EMAIL" ||
515 die "malformed --author parameter"
516 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
520 if test -z "$initial_commit"
523 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
524 rloga='commit (merge)'
525 PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
526 elif test -n "$amend"; then
527 rloga='commit (amend)'
528 PARENTS=$(git cat-file commit HEAD |
529 sed -n -e '/^$/q' -e 's/^parent /-p /p')
531 current="$(git rev-parse --verify HEAD)"
533 if [ -z "$(git ls-files)" ]; then
534 echo >&2 'nothing to commit (use "git add file1 file2" to include for commit)'
538 rloga='commit (initial)'
541 set_reflog_action "$rloga"
543 if test -z "$no_edit"
547 echo "# Please enter the commit message for your changes."
548 echo "# (Comment lines starting with '#' will not be included)"
549 test -z "$only_include_assumed" || echo "$only_include_assumed"
551 } >>"$GIT_DIR"/COMMIT_EDITMSG
553 # we need to check if there is anything to commit
554 run_status >/dev/null
556 if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" -a -z "$amend" ]
558 rm -f "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
565 git-var GIT_AUTHOR_IDENT > /dev/null || die
566 git-var GIT_COMMITTER_IDENT > /dev/null || die
567 git_editor "$GIT_DIR/COMMIT_EDITMSG"
573 if test -x "$GIT_DIR"/hooks/commit-msg
575 "$GIT_DIR"/hooks/commit-msg "$GIT_DIR"/COMMIT_EDITMSG || exit
579 if test -z "$no_edit"
587 ' "$GIT_DIR"/COMMIT_EDITMSG
589 cat "$GIT_DIR"/COMMIT_EDITMSG
591 git stripspace >"$GIT_DIR"/COMMIT_MSG
593 # Test whether the commit message has any content we didn't supply.
595 grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
596 git stripspace > "$GIT_DIR"/COMMIT_BAREMSG
598 # Is the commit message totally empty?
599 if test -s "$GIT_DIR"/COMMIT_BAREMSG
601 if test "$templatefile" != ""
603 # Test whether this is just the unaltered template.
604 if cnt=`sed -e '/^#/d' < "$templatefile" |
606 diff "$GIT_DIR"/COMMIT_BAREMSG - |
613 # No template, so the content in the commit message must
614 # have come from the user.
619 rm -f "$GIT_DIR"/COMMIT_BAREMSG
621 if test "$have_commitmsg" = "t"
623 if test -z "$TMP_INDEX"
625 tree=$(GIT_INDEX_FILE="$USE_INDEX" git write-tree)
627 tree=$(GIT_INDEX_FILE="$TMP_INDEX" git write-tree) &&
630 commit=$(git commit-tree $tree $PARENTS <"$GIT_DIR/COMMIT_MSG") &&
631 rlogm=$(sed -e 1q "$GIT_DIR"/COMMIT_MSG) &&
632 git update-ref -m "$GIT_REFLOG_ACTION: $rlogm" HEAD $commit "$current" &&
633 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" &&
634 if test -f "$NEXT_INDEX"
636 mv "$NEXT_INDEX" "$THIS_INDEX"
641 echo >&2 "* no commit message? aborting commit."
645 rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
653 if test -x "$GIT_DIR"/hooks/post-commit
655 "$GIT_DIR"/hooks/post-commit
659 commit=`git diff-tree --always --shortstat --pretty="format:%h: %s"\
660 --summary --root HEAD --`
661 echo "Created${initial_commit:+ initial} commit $commit"