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>...]'
12 git rev-parse --verify HEAD >/dev/null 2>&1 || initial_commit=t
25 echo >&2 "You might have meant to say 'git commit -i paths...', perhaps?"
30 THIS_INDEX="${GIT_INDEX_FILE:-$GIT_DIR/index}"
31 NEXT_INDEX="$GIT_DIR/next-index$$"
34 cp -p "$THIS_INDEX" "$NEXT_INDEX"
38 # If TMP_INDEX is defined, that means we are doing
39 # "--only" partial commit, and that index file is used
40 # to build the tree for the commit. Otherwise, if
41 # NEXT_INDEX exists, that is the index file used to
42 # make the commit. Otherwise we are using as-is commit
43 # so the regular index file is what we use to compare.
44 if test '' != "$TMP_INDEX"
46 GIT_INDEX_FILE="$TMP_INDEX"
48 elif test -f "$NEXT_INDEX"
50 GIT_INDEX_FILE="$NEXT_INDEX"
54 if test "$status_only" = "t" || test "$use_status_color" = "t"; then
59 git runstatus ${color} \
60 ${verbose:+--verbose} \
62 ${untracked_files:+--untracked}
66 test -z "$TMP_INDEX" || {
67 test -f "$TMP_INDEX" && rm -f "$TMP_INDEX"
72 ################################################################
73 # Command line argument parsing and sanity checking
94 templatefile="$(git config commit.template)"
98 -F|--F|-f|--f|--fi|--fil|--file)
99 case "$#" in 1) usage ;; esac
102 log_given=t$log_given
107 log_given=t$log_given
110 --F=*|--f=*|--fi=*|--fil=*|--file=*)
112 log_given=t$log_given
118 --allo|--allow|--allow-|--allow-e|--allow-em|--allow-emp|\
119 --allow-empt|--allow-empty)
122 --au=*|--aut=*|--auth=*|--autho=*|--author=*)
123 force_author="${1#*=}"
125 --au|--aut|--auth|--autho|--author)
126 case "$#" in 1) usage ;; esac
130 -e|--e|--ed|--edi|--edit)
133 -i|--i|--in|--inc|--incl|--inclu|--includ|--include)
136 --int|--inte|--inter|--intera|--interac|--interact|--interacti|\
137 --interactiv|--interactive)
140 -o|--o|--on|--onl|--only)
143 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
144 case "$#" in 1) usage ;; esac
146 log_given=m$log_given
147 log_message="${log_message:+${log_message}
153 log_given=m$log_given
154 log_message="${log_message:+${log_message}
159 --m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
160 log_given=m$log_given
161 log_message="${log_message:+${log_message}
166 -n|--n|--no|--no-|--no-v|--no-ve|--no-ver|--no-veri|--no-verif|\
170 --a|--am|--ame|--amen|--amend)
175 case "$#" in 1) usage ;; esac
177 log_given=t$log_given
181 --ree=*|--reed=*|--reedi=*|--reedit=*|--reedit-=*|--reedit-m=*|\
182 --reedit-me=*|--reedit-mes=*|--reedit-mess=*|--reedit-messa=*|\
183 --reedit-messag=*|--reedit-message=*)
184 log_given=t$log_given
188 --ree|--reed|--reedi|--reedit|--reedit-|--reedit-m|--reedit-me|\
189 --reedit-mes|--reedit-mess|--reedit-messa|--reedit-messag|\
191 case "$#" in 1) usage ;; esac
193 log_given=t$log_given
198 case "$#" in 1) usage ;; esac
200 log_given=t$log_given
204 --reu=*|--reus=*|--reuse=*|--reuse-=*|--reuse-m=*|--reuse-me=*|\
205 --reuse-mes=*|--reuse-mess=*|--reuse-messa=*|--reuse-messag=*|\
207 log_given=t$log_given
211 --reu|--reus|--reuse|--reuse-|--reuse-m|--reuse-me|--reuse-mes|\
212 --reuse-mess|--reuse-messa|--reuse-messag|--reuse-message)
213 case "$#" in 1) usage ;; esac
215 log_given=t$log_given
219 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
222 -t|--t|--te|--tem|--temp|--templ|--templa|--templat|--template)
223 case "$#" in 1) usage ;; esac
228 -q|--q|--qu|--qui|--quie|--quiet)
231 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
234 -u|--u|--un|--unt|--untr|--untra|--untrac|--untrack|--untracke|\
235 --untracked|--untracked-|--untracked-f|--untracked-fi|--untracked-fil|\
236 --untracked-file|--untracked-files)
252 case "$edit_flag" in t) no_edit= ;; esac
254 ################################################################
255 # Sanity check options
257 case "$amend,$initial_commit" in
259 die "You do not have anything to amend." ;;
261 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
262 die "You are in the middle of a merge -- cannot amend."
268 die "Only one of -c/-C/-F can be used." ;;
270 die "Option -m cannot be combined with -c/-C/-F." ;;
273 case "$#,$also,$only,$amend" in
275 die "Only one of --include/--only can be used." ;;
277 die "No paths with --include/--only does not make sense." ;;
279 only_include_assumed="# Clever... amending the last one with dirty index." ;;
283 only_include_assumed="# Explicit paths specified without -i or -o; assuming --only paths..."
288 case "$all,$interactive,$also,$#" in
290 die "Cannot use -a, --interactive or -i at the same time." ;;
292 die "Paths with -a does not make sense." ;;
294 die "Paths with --interactive does not make sense." ;;
296 die "No paths with -i does not make sense." ;;
299 if test ! -z "$templatefile" && test -z "$log_given"
301 if test ! -f "$templatefile"
303 die "Commit template file does not exist."
307 ################################################################
308 # Prepare index to have a tree to be committed
312 if test ! -f "$THIS_INDEX"
314 die 'nothing to commit (use "git add file1 file2" to include for commit)'
319 GIT_INDEX_FILE="$NEXT_INDEX" &&
320 export GIT_INDEX_FILE &&
321 git diff-files --name-only -z |
322 git update-index --remove -z --stdin
327 git ls-files --error-unmatch -- "$@" >/dev/null || exit
329 git diff-files --name-only -z -- "$@" |
332 GIT_INDEX_FILE="$NEXT_INDEX" &&
333 export GIT_INDEX_FILE &&
334 git update-index --remove -z --stdin
338 if test "$interactive" = t; then
339 git add --interactive || exit
345 if test -f "$GIT_DIR/MERGE_HEAD"
347 refuse_partial "Cannot do a partial commit during a merge."
350 TMP_INDEX="$GIT_DIR/tmp-index$$"
352 test -z "$initial_commit" && W=--with-tree=HEAD
353 commit_only=$(git ls-files --error-unmatch $W -- "$@") || exit
355 # Build a temporary index and update the real index
357 if test -z "$initial_commit"
359 GIT_INDEX_FILE="$THIS_INDEX" \
360 git read-tree --index-output="$TMP_INDEX" -i -m HEAD
365 printf '%s\n' "$commit_only" |
366 GIT_INDEX_FILE="$TMP_INDEX" \
367 git update-index --add --remove --stdin &&
370 printf '%s\n' "$commit_only" |
372 GIT_INDEX_FILE="$NEXT_INDEX"
373 export GIT_INDEX_FILE
374 git update-index --add --remove --stdin
381 ################################################################
382 # If we do as-is commit, the index file will be THIS_INDEX,
383 # otherwise NEXT_INDEX after we make this commit. We leave
384 # the index as is if we abort.
386 if test -f "$NEXT_INDEX"
388 USE_INDEX="$NEXT_INDEX"
390 USE_INDEX="$THIS_INDEX"
393 case "$status_only" in
395 # This will silently fail in a read-only repository, which is
397 GIT_INDEX_FILE="$USE_INDEX" git update-index -q --unmerged --refresh
402 GIT_INDEX_FILE="$USE_INDEX" git update-index -q --refresh || exit
406 ################################################################
407 # Grab commit message, write out tree and make commit.
409 if test t = "$verify" && test -x "$GIT_DIR"/hooks/pre-commit
411 GIT_INDEX_FILE="${TMP_INDEX:-${USE_INDEX}}" "$GIT_DIR"/hooks/pre-commit \
415 if test "$log_message" != ''
417 printf '%s\n' "$log_message"
418 elif test "$logfile" != ""
420 if test "$logfile" = -
423 echo >&2 "(reading log message from standard input)"
428 elif test "$use_commit" != ""
430 encoding=$(git config i18n.commitencoding || echo UTF-8)
431 git show -s --pretty=raw --encoding="$encoding" "$use_commit" |
432 sed -e '1,/^$/d' -e 's/^ //'
433 elif test -f "$GIT_DIR/MERGE_MSG"
435 cat "$GIT_DIR/MERGE_MSG"
436 elif test -f "$GIT_DIR/SQUASH_MSG"
438 cat "$GIT_DIR/SQUASH_MSG"
439 elif test "$templatefile" != ""
442 fi | git stripspace >"$GIT_DIR"/COMMIT_EDITMSG
446 sign=$(git var GIT_COMMITTER_IDENT | sed -e '
450 blank_before_signoff=
451 tail -n 1 "$GIT_DIR"/COMMIT_EDITMSG |
452 grep 'Signed-off-by:' >/dev/null || blank_before_signoff='
454 tail -n 1 "$GIT_DIR"/COMMIT_EDITMSG |
455 grep "$sign"$ >/dev/null ||
456 printf '%s%s\n' "$blank_before_signoff" "$sign" \
457 >>"$GIT_DIR"/COMMIT_EDITMSG
461 if test -f "$GIT_DIR/MERGE_HEAD" && test -z "$no_edit"; then
463 echo "# It looks like you may be committing a MERGE."
464 echo "# If this is not correct, please remove the file"
465 printf '%s\n' "# $GIT_DIR/MERGE_HEAD"
466 echo "# and try again"
468 fi >>"$GIT_DIR"/COMMIT_EDITMSG
471 if test '' != "$use_commit"
473 eval "$(get_author_ident_from_commit "$use_commit")"
474 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
476 if test '' != "$force_author"
478 GIT_AUTHOR_NAME=$(expr "z$force_author" : 'z\(.*[^ ]\) *<.*') &&
479 GIT_AUTHOR_EMAIL=$(expr "z$force_author" : '.*\(<.*\)') &&
480 test '' != "$GIT_AUTHOR_NAME" &&
481 test '' != "$GIT_AUTHOR_EMAIL" ||
482 die "malformed --author parameter"
483 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
487 if test -z "$initial_commit"
490 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
491 rloga='commit (merge)'
492 PARENTS="-p HEAD "$(sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD")
493 elif test -n "$amend"; then
494 rloga='commit (amend)'
495 PARENTS=$(git cat-file commit HEAD |
496 sed -n -e '/^$/q' -e 's/^parent /-p /p')
498 current="$(git rev-parse --verify HEAD)"
500 if [ -z "$(git ls-files)" ]; then
501 echo >&2 'nothing to commit (use "git add file1 file2" to include for commit)'
505 rloga='commit (initial)'
508 set_reflog_action "$rloga"
510 if test -z "$no_edit"
514 echo "# Please enter the commit message for your changes."
515 echo "# (Comment lines starting with '#' will not be included)"
516 test -z "$only_include_assumed" || echo "$only_include_assumed"
518 } >>"$GIT_DIR"/COMMIT_EDITMSG
520 # we need to check if there is anything to commit
521 run_status >/dev/null
523 case "$allow_empty,$?,$PARENTS" in
524 t,* | ?,0,* | ?,*,-p' '?*-p' '?*)
525 # an explicit --allow-empty, or a merge commit can record the
526 # same tree as its parent. Otherwise having commitable paths
530 rm -f "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
538 git var GIT_AUTHOR_IDENT > /dev/null || die
539 git var GIT_COMMITTER_IDENT > /dev/null || die
540 git_editor "$GIT_DIR/COMMIT_EDITMSG"
546 if test -x "$GIT_DIR"/hooks/commit-msg
548 "$GIT_DIR"/hooks/commit-msg "$GIT_DIR"/COMMIT_EDITMSG || exit
552 if test -z "$no_edit"
560 ' "$GIT_DIR"/COMMIT_EDITMSG
562 cat "$GIT_DIR"/COMMIT_EDITMSG
564 git stripspace >"$GIT_DIR"/COMMIT_MSG
566 # Test whether the commit message has any content we didn't supply.
568 grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
569 git stripspace > "$GIT_DIR"/COMMIT_BAREMSG
571 # Is the commit message totally empty?
572 if test -s "$GIT_DIR"/COMMIT_BAREMSG
574 if test "$templatefile" != ""
576 # Test whether this is just the unaltered template.
577 if cnt=`sed -e '/^#/d' < "$templatefile" |
579 diff "$GIT_DIR"/COMMIT_BAREMSG - |
586 # No template, so the content in the commit message must
587 # have come from the user.
592 rm -f "$GIT_DIR"/COMMIT_BAREMSG
594 if test "$have_commitmsg" = "t"
596 if test -z "$TMP_INDEX"
598 tree=$(GIT_INDEX_FILE="$USE_INDEX" git write-tree)
600 tree=$(GIT_INDEX_FILE="$TMP_INDEX" git write-tree) &&
603 commit=$(git commit-tree $tree $PARENTS <"$GIT_DIR/COMMIT_MSG") &&
604 rlogm=$(sed -e 1q "$GIT_DIR"/COMMIT_MSG) &&
605 git update-ref -m "$GIT_REFLOG_ACTION: $rlogm" HEAD $commit "$current" &&
606 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" &&
607 if test -f "$NEXT_INDEX"
609 mv "$NEXT_INDEX" "$THIS_INDEX"
614 echo >&2 "* no commit message? aborting commit."
618 rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
627 if test -x "$GIT_DIR"/hooks/post-commit
629 "$GIT_DIR"/hooks/post-commit
633 commit=`git diff-tree --always --shortstat --pretty="format:%h: %s"\
634 --abbrev --summary --root HEAD --`
635 echo "Created${initial_commit:+ initial} commit $commit"