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
104 log_given=t$log_given
107 --F=*|--f=*|--fi=*|--fil=*|--file=*)
109 log_given=t$log_given
115 --au=*|--aut=*|--auth=*|--autho=*|--author=*)
116 force_author="${1#*=}"
118 --au|--aut|--auth|--autho|--author)
119 case "$#" in 1) usage ;; esac
123 -e|--e|--ed|--edi|--edit)
126 -i|--i|--in|--inc|--incl|--inclu|--includ|--include)
129 --int|--inte|--inter|--intera|--interac|--interact|--interacti|\
130 --interactiv|--interactive)
133 -o|--o|--on|--onl|--only)
136 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
137 case "$#" in 1) usage ;; esac
139 log_given=m$log_given
140 log_message="${log_message:+${log_message}
146 log_given=m$log_given
147 log_message="${log_message:+${log_message}
152 --m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
153 log_given=m$log_given
154 log_message="${log_message:+${log_message}
159 -n|--n|--no|--no-|--no-v|--no-ve|--no-ver|--no-veri|--no-verif|\
163 --a|--am|--ame|--amen|--amend)
168 case "$#" in 1) usage ;; esac
170 log_given=t$log_given
174 --ree=*|--reed=*|--reedi=*|--reedit=*|--reedit-=*|--reedit-m=*|\
175 --reedit-me=*|--reedit-mes=*|--reedit-mess=*|--reedit-messa=*|\
176 --reedit-messag=*|--reedit-message=*)
177 log_given=t$log_given
181 --ree|--reed|--reedi|--reedit|--reedit-|--reedit-m|--reedit-me|\
182 --reedit-mes|--reedit-mess|--reedit-messa|--reedit-messag|\
184 case "$#" in 1) usage ;; esac
186 log_given=t$log_given
191 case "$#" in 1) usage ;; esac
193 log_given=t$log_given
197 --reu=*|--reus=*|--reuse=*|--reuse-=*|--reuse-m=*|--reuse-me=*|\
198 --reuse-mes=*|--reuse-mess=*|--reuse-messa=*|--reuse-messag=*|\
200 log_given=t$log_given
204 --reu|--reus|--reuse|--reuse-|--reuse-m|--reuse-me|--reuse-mes|\
205 --reuse-mess|--reuse-messa|--reuse-messag|--reuse-message)
206 case "$#" in 1) usage ;; esac
208 log_given=t$log_given
212 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
215 -t|--t|--te|--tem|--temp|--templ|--templa|--templat|--template)
216 case "$#" in 1) usage ;; esac
221 -q|--q|--qu|--qui|--quie|--quiet)
224 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
227 -u|--u|--un|--unt|--untr|--untra|--untrac|--untrack|--untracke|\
228 --untracked|--untracked-|--untracked-f|--untracked-fi|--untracked-fil|\
229 --untracked-file|--untracked-files)
245 case "$edit_flag" in t) no_edit= ;; esac
247 ################################################################
248 # Sanity check options
250 case "$amend,$initial_commit" in
252 die "You do not have anything to amend." ;;
254 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
255 die "You are in the middle of a merge -- cannot amend."
261 die "Only one of -c/-C/-F can be used." ;;
263 die "Option -m cannot be combined with -c/-C/-F." ;;
266 case "$#,$also,$only,$amend" in
268 die "Only one of --include/--only can be used." ;;
270 die "No paths with --include/--only does not make sense." ;;
272 only_include_assumed="# Clever... amending the last one with dirty index." ;;
276 only_include_assumed="# Explicit paths specified without -i nor -o; assuming --only paths..."
281 case "$all,$interactive,$also,$#" in
283 die "Cannot use -a, --interactive or -i at the same time." ;;
285 die "Paths with -a does not make sense." ;;
287 die "Paths with --interactive does not make sense." ;;
289 die "No paths with -i does not make sense." ;;
292 if test ! -z "$templatefile" -a -z "$log_given"
294 if test ! -f "$templatefile"
296 die "Commit template file does not exist."
300 ################################################################
301 # Prepare index to have a tree to be committed
305 if test ! -f "$THIS_INDEX"
307 die 'nothing to commit (use "git add file1 file2" to include for commit)'
312 GIT_INDEX_FILE="$NEXT_INDEX" &&
313 export GIT_INDEX_FILE &&
314 git diff-files --name-only -z |
315 git update-index --remove -z --stdin
320 git ls-files --error-unmatch -- "$@" >/dev/null || exit
322 git diff-files --name-only -z -- "$@" |
325 GIT_INDEX_FILE="$NEXT_INDEX" &&
326 export GIT_INDEX_FILE &&
327 git update-index --remove -z --stdin
331 if test "$interactive" = t; then
332 git add --interactive || exit
338 if test -f "$GIT_DIR/MERGE_HEAD"
340 refuse_partial "Cannot do a partial commit during a merge."
343 TMP_INDEX="$GIT_DIR/tmp-index$$"
345 test -z "$initial_commit" && W=--with-tree=HEAD
346 commit_only=`git ls-files --error-unmatch $W -- "$@"` || exit
348 # Build a temporary index and update the real index
350 if test -z "$initial_commit"
352 GIT_INDEX_FILE="$THIS_INDEX" \
353 git read-tree --index-output="$TMP_INDEX" -i -m HEAD
358 printf '%s\n' "$commit_only" |
359 GIT_INDEX_FILE="$TMP_INDEX" \
360 git update-index --add --remove --stdin &&
363 printf '%s\n' "$commit_only" |
365 GIT_INDEX_FILE="$NEXT_INDEX"
366 export GIT_INDEX_FILE
367 git update-index --add --remove --stdin
374 ################################################################
375 # If we do as-is commit, the index file will be THIS_INDEX,
376 # otherwise NEXT_INDEX after we make this commit. We leave
377 # the index as is if we abort.
379 if test -f "$NEXT_INDEX"
381 USE_INDEX="$NEXT_INDEX"
383 USE_INDEX="$THIS_INDEX"
386 case "$status_only" in
388 # This will silently fail in a read-only repository, which is
390 GIT_INDEX_FILE="$USE_INDEX" git update-index -q --unmerged --refresh
395 GIT_INDEX_FILE="$USE_INDEX" git update-index -q --refresh || exit
399 ################################################################
400 # Grab commit message, write out tree and make commit.
402 if test t = "$verify" && test -x "$GIT_DIR"/hooks/pre-commit
404 GIT_INDEX_FILE="${TMP_INDEX:-${USE_INDEX}}" "$GIT_DIR"/hooks/pre-commit \
408 if test "$log_message" != ''
410 printf '%s\n' "$log_message"
411 elif test "$logfile" != ""
413 if test "$logfile" = -
416 echo >&2 "(reading log message from standard input)"
421 elif test "$use_commit" != ""
423 encoding=$(git config i18n.commitencoding || echo UTF-8)
424 git show -s --pretty=raw --encoding="$encoding" "$use_commit" |
425 sed -e '1,/^$/d' -e 's/^ //'
426 elif test -f "$GIT_DIR/MERGE_MSG"
428 cat "$GIT_DIR/MERGE_MSG"
429 elif test -f "$GIT_DIR/SQUASH_MSG"
431 cat "$GIT_DIR/SQUASH_MSG"
432 elif test "$templatefile" != ""
435 fi | git stripspace >"$GIT_DIR"/COMMIT_EDITMSG
439 sign=$(git-var GIT_COMMITTER_IDENT | sed -e '
443 blank_before_signoff=
444 tail -n 1 "$GIT_DIR"/COMMIT_EDITMSG |
445 grep 'Signed-off-by:' >/dev/null || blank_before_signoff='
447 tail -n 1 "$GIT_DIR"/COMMIT_EDITMSG |
448 grep "$sign"$ >/dev/null ||
449 printf '%s%s\n' "$blank_before_signoff" "$sign" \
450 >>"$GIT_DIR"/COMMIT_EDITMSG
454 if test -f "$GIT_DIR/MERGE_HEAD" && test -z "$no_edit"; then
456 echo "# It looks like you may be committing a MERGE."
457 echo "# If this is not correct, please remove the file"
458 printf '%s\n' "# $GIT_DIR/MERGE_HEAD"
459 echo "# and try again"
461 fi >>"$GIT_DIR"/COMMIT_EDITMSG
464 if test '' != "$use_commit"
466 eval "$(get_author_ident_from_commit "$use_commit")"
467 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
469 if test '' != "$force_author"
471 GIT_AUTHOR_NAME=`expr "z$force_author" : 'z\(.*[^ ]\) *<.*'` &&
472 GIT_AUTHOR_EMAIL=`expr "z$force_author" : '.*\(<.*\)'` &&
473 test '' != "$GIT_AUTHOR_NAME" &&
474 test '' != "$GIT_AUTHOR_EMAIL" ||
475 die "malformed --author parameter"
476 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
480 if test -z "$initial_commit"
483 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
484 rloga='commit (merge)'
485 PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
486 elif test -n "$amend"; then
487 rloga='commit (amend)'
488 PARENTS=$(git cat-file commit HEAD |
489 sed -n -e '/^$/q' -e 's/^parent /-p /p')
491 current="$(git rev-parse --verify HEAD)"
493 if [ -z "$(git ls-files)" ]; then
494 echo >&2 'nothing to commit (use "git add file1 file2" to include for commit)'
498 rloga='commit (initial)'
501 set_reflog_action "$rloga"
503 if test -z "$no_edit"
507 echo "# Please enter the commit message for your changes."
508 echo "# (Comment lines starting with '#' will not be included)"
509 test -z "$only_include_assumed" || echo "$only_include_assumed"
511 } >>"$GIT_DIR"/COMMIT_EDITMSG
513 # we need to check if there is anything to commit
514 run_status >/dev/null
516 if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" ]
518 rm -f "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
526 git-var GIT_AUTHOR_IDENT > /dev/null || die
527 git-var GIT_COMMITTER_IDENT > /dev/null || die
528 git_editor "$GIT_DIR/COMMIT_EDITMSG"
534 if test -x "$GIT_DIR"/hooks/commit-msg
536 "$GIT_DIR"/hooks/commit-msg "$GIT_DIR"/COMMIT_EDITMSG || exit
540 if test -z "$no_edit"
548 ' "$GIT_DIR"/COMMIT_EDITMSG
550 cat "$GIT_DIR"/COMMIT_EDITMSG
552 git stripspace >"$GIT_DIR"/COMMIT_MSG
554 # Test whether the commit message has any content we didn't supply.
556 grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
557 git stripspace > "$GIT_DIR"/COMMIT_BAREMSG
559 # Is the commit message totally empty?
560 if test -s "$GIT_DIR"/COMMIT_BAREMSG
562 if test "$templatefile" != ""
564 # Test whether this is just the unaltered template.
565 if cnt=`sed -e '/^#/d' < "$templatefile" |
567 diff "$GIT_DIR"/COMMIT_BAREMSG - |
574 # No template, so the content in the commit message must
575 # have come from the user.
580 rm -f "$GIT_DIR"/COMMIT_BAREMSG
582 if test "$have_commitmsg" = "t"
584 if test -z "$TMP_INDEX"
586 tree=$(GIT_INDEX_FILE="$USE_INDEX" git write-tree)
588 tree=$(GIT_INDEX_FILE="$TMP_INDEX" git write-tree) &&
591 commit=$(git commit-tree $tree $PARENTS <"$GIT_DIR/COMMIT_MSG") &&
592 rlogm=$(sed -e 1q "$GIT_DIR"/COMMIT_MSG) &&
593 git update-ref -m "$GIT_REFLOG_ACTION: $rlogm" HEAD $commit "$current" &&
594 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" &&
595 if test -f "$NEXT_INDEX"
597 mv "$NEXT_INDEX" "$THIS_INDEX"
602 echo >&2 "* no commit message? aborting commit."
606 rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
614 if test -x "$GIT_DIR"/hooks/post-commit
616 "$GIT_DIR"/hooks/post-commit
620 commit=`git diff-tree --always --shortstat --pretty="format:%h: %s"\
621 --summary --root HEAD --`
622 echo "Created${initial_commit:+ initial} commit $commit"