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_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" -o "$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
93 templatefile="`git config commit.template`"
97 -F|--F|-f|--f|--fi|--fil|--file)
98 case "$#" in 1) usage ;; esac
101 log_given=t$log_given
106 log_given=t$log_given
109 --F=*|--f=*|--fi=*|--fil=*|--file=*)
111 log_given=t$log_given
117 --au=*|--aut=*|--auth=*|--autho=*|--author=*)
118 force_author="${1#*=}"
120 --au|--aut|--auth|--autho|--author)
121 case "$#" in 1) usage ;; esac
125 -e|--e|--ed|--edi|--edit)
128 -i|--i|--in|--inc|--incl|--inclu|--includ|--include)
131 --int|--inte|--inter|--intera|--interac|--interact|--interacti|\
132 --interactiv|--interactive)
135 -o|--o|--on|--onl|--only)
138 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
139 case "$#" in 1) usage ;; esac
141 log_given=m$log_given
142 log_message="${log_message:+${log_message}
148 log_given=m$log_given
149 log_message="${log_message:+${log_message}
154 --m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
155 log_given=m$log_given
156 log_message="${log_message:+${log_message}
161 -n|--n|--no|--no-|--no-v|--no-ve|--no-ver|--no-veri|--no-verif|\
165 --a|--am|--ame|--amen|--amend)
170 case "$#" in 1) usage ;; esac
172 log_given=t$log_given
176 --ree=*|--reed=*|--reedi=*|--reedit=*|--reedit-=*|--reedit-m=*|\
177 --reedit-me=*|--reedit-mes=*|--reedit-mess=*|--reedit-messa=*|\
178 --reedit-messag=*|--reedit-message=*)
179 log_given=t$log_given
183 --ree|--reed|--reedi|--reedit|--reedit-|--reedit-m|--reedit-me|\
184 --reedit-mes|--reedit-mess|--reedit-messa|--reedit-messag|\
186 case "$#" in 1) usage ;; esac
188 log_given=t$log_given
193 case "$#" in 1) usage ;; esac
195 log_given=t$log_given
199 --reu=*|--reus=*|--reuse=*|--reuse-=*|--reuse-m=*|--reuse-me=*|\
200 --reuse-mes=*|--reuse-mess=*|--reuse-messa=*|--reuse-messag=*|\
202 log_given=t$log_given
206 --reu|--reus|--reuse|--reuse-|--reuse-m|--reuse-me|--reuse-mes|\
207 --reuse-mess|--reuse-messa|--reuse-messag|--reuse-message)
208 case "$#" in 1) usage ;; esac
210 log_given=t$log_given
214 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
217 -t|--t|--te|--tem|--temp|--templ|--templa|--templat|--template)
218 case "$#" in 1) usage ;; esac
223 -q|--q|--qu|--qui|--quie|--quiet)
226 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
229 -u|--u|--un|--unt|--untr|--untra|--untrac|--untrack|--untracke|\
230 --untracked|--untracked-|--untracked-f|--untracked-fi|--untracked-fil|\
231 --untracked-file|--untracked-files)
247 case "$edit_flag" in t) no_edit= ;; esac
249 ################################################################
250 # Sanity check options
252 case "$amend,$initial_commit" in
254 die "You do not have anything to amend." ;;
256 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
257 die "You are in the middle of a merge -- cannot amend."
263 die "Only one of -c/-C/-F can be used." ;;
265 die "Option -m cannot be combined with -c/-C/-F." ;;
268 case "$#,$also,$only,$amend" in
270 die "Only one of --include/--only can be used." ;;
272 die "No paths with --include/--only does not make sense." ;;
274 only_include_assumed="# Clever... amending the last one with dirty index." ;;
278 only_include_assumed="# Explicit paths specified without -i nor -o; assuming --only paths..."
283 case "$all,$interactive,$also,$#" in
285 die "Cannot use -a, --interactive or -i at the same time." ;;
287 die "Paths with -a does not make sense." ;;
289 die "Paths with --interactive does not make sense." ;;
291 die "No paths with -i does not make sense." ;;
294 if test ! -z "$templatefile" -a -z "$log_given"
296 if test ! -f "$templatefile"
298 die "Commit template file does not exist."
302 ################################################################
303 # Prepare index to have a tree to be committed
307 if test ! -f "$THIS_INDEX"
309 die 'nothing to commit (use "git add file1 file2" to include for commit)'
314 GIT_INDEX_FILE="$NEXT_INDEX" &&
315 export GIT_INDEX_FILE &&
316 git diff-files --name-only -z |
317 git update-index --remove -z --stdin
322 git ls-files --error-unmatch -- "$@" >/dev/null || exit
324 git diff-files --name-only -z -- "$@" |
327 GIT_INDEX_FILE="$NEXT_INDEX" &&
328 export GIT_INDEX_FILE &&
329 git update-index --remove -z --stdin
333 if test "$interactive" = t; then
334 git add --interactive || exit
340 if test -f "$GIT_DIR/MERGE_HEAD"
342 refuse_partial "Cannot do a partial commit during a merge."
345 TMP_INDEX="$GIT_DIR/tmp-index$$"
347 test -z "$initial_commit" && W=--with-tree=HEAD
348 commit_only=`git ls-files --error-unmatch $W -- "$@"` || exit
350 # Build a temporary index and update the real index
352 if test -z "$initial_commit"
354 GIT_INDEX_FILE="$THIS_INDEX" \
355 git read-tree --index-output="$TMP_INDEX" -i -m HEAD
360 printf '%s\n' "$commit_only" |
361 GIT_INDEX_FILE="$TMP_INDEX" \
362 git update-index --add --remove --stdin &&
365 printf '%s\n' "$commit_only" |
367 GIT_INDEX_FILE="$NEXT_INDEX"
368 export GIT_INDEX_FILE
369 git update-index --add --remove --stdin
376 ################################################################
377 # If we do as-is commit, the index file will be THIS_INDEX,
378 # otherwise NEXT_INDEX after we make this commit. We leave
379 # the index as is if we abort.
381 if test -f "$NEXT_INDEX"
383 USE_INDEX="$NEXT_INDEX"
385 USE_INDEX="$THIS_INDEX"
388 case "$status_only" in
390 # This will silently fail in a read-only repository, which is
392 GIT_INDEX_FILE="$USE_INDEX" git update-index -q --unmerged --refresh
397 GIT_INDEX_FILE="$USE_INDEX" git update-index -q --refresh || exit
401 ################################################################
402 # Grab commit message, write out tree and make commit.
404 if test t = "$verify" && test -x "$GIT_DIR"/hooks/pre-commit
406 GIT_INDEX_FILE="${TMP_INDEX:-${USE_INDEX}}" "$GIT_DIR"/hooks/pre-commit \
410 if test "$log_message" != ''
412 printf '%s\n' "$log_message"
413 elif test "$logfile" != ""
415 if test "$logfile" = -
418 echo >&2 "(reading log message from standard input)"
423 elif test "$use_commit" != ""
425 encoding=$(git config i18n.commitencoding || echo UTF-8)
426 git show -s --pretty=raw --encoding="$encoding" "$use_commit" |
427 sed -e '1,/^$/d' -e 's/^ //'
428 elif test -f "$GIT_DIR/MERGE_MSG"
430 cat "$GIT_DIR/MERGE_MSG"
431 elif test -f "$GIT_DIR/SQUASH_MSG"
433 cat "$GIT_DIR/SQUASH_MSG"
434 elif test "$templatefile" != ""
437 fi | git stripspace >"$GIT_DIR"/COMMIT_EDITMSG
441 sign=$(git-var GIT_COMMITTER_IDENT | sed -e '
445 blank_before_signoff=
446 tail -n 1 "$GIT_DIR"/COMMIT_EDITMSG |
447 grep 'Signed-off-by:' >/dev/null || blank_before_signoff='
449 tail -n 1 "$GIT_DIR"/COMMIT_EDITMSG |
450 grep "$sign"$ >/dev/null ||
451 printf '%s%s\n' "$blank_before_signoff" "$sign" \
452 >>"$GIT_DIR"/COMMIT_EDITMSG
456 if test -f "$GIT_DIR/MERGE_HEAD" && test -z "$no_edit"; then
458 echo "# It looks like you may be committing a MERGE."
459 echo "# If this is not correct, please remove the file"
460 printf '%s\n' "# $GIT_DIR/MERGE_HEAD"
461 echo "# and try again"
463 fi >>"$GIT_DIR"/COMMIT_EDITMSG
466 if test '' != "$use_commit"
468 eval "$(get_author_ident_from_commit "$use_commit")"
469 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
471 if test '' != "$force_author"
473 GIT_AUTHOR_NAME=`expr "z$force_author" : 'z\(.*[^ ]\) *<.*'` &&
474 GIT_AUTHOR_EMAIL=`expr "z$force_author" : '.*\(<.*\)'` &&
475 test '' != "$GIT_AUTHOR_NAME" &&
476 test '' != "$GIT_AUTHOR_EMAIL" ||
477 die "malformed --author parameter"
478 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
482 if test -z "$initial_commit"
485 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
486 rloga='commit (merge)'
487 PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
488 elif test -n "$amend"; then
489 rloga='commit (amend)'
490 PARENTS=$(git cat-file commit HEAD |
491 sed -n -e '/^$/q' -e 's/^parent /-p /p')
493 current="$(git rev-parse --verify HEAD)"
495 if [ -z "$(git ls-files)" ]; then
496 echo >&2 'nothing to commit (use "git add file1 file2" to include for commit)'
500 rloga='commit (initial)'
503 set_reflog_action "$rloga"
505 if test -z "$no_edit"
509 echo "# Please enter the commit message for your changes."
510 echo "# (Comment lines starting with '#' will not be included)"
511 test -z "$only_include_assumed" || echo "$only_include_assumed"
513 } >>"$GIT_DIR"/COMMIT_EDITMSG
515 # we need to check if there is anything to commit
516 run_status >/dev/null
518 if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" ]
520 rm -f "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
528 git-var GIT_AUTHOR_IDENT > /dev/null || die
529 git-var GIT_COMMITTER_IDENT > /dev/null || die
530 git_editor "$GIT_DIR/COMMIT_EDITMSG"
536 if test -x "$GIT_DIR"/hooks/commit-msg
538 "$GIT_DIR"/hooks/commit-msg "$GIT_DIR"/COMMIT_EDITMSG || exit
542 if test -z "$no_edit"
550 ' "$GIT_DIR"/COMMIT_EDITMSG
552 cat "$GIT_DIR"/COMMIT_EDITMSG
554 git stripspace >"$GIT_DIR"/COMMIT_MSG
556 # Test whether the commit message has any content we didn't supply.
558 grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
559 git stripspace > "$GIT_DIR"/COMMIT_BAREMSG
561 # Is the commit message totally empty?
562 if test -s "$GIT_DIR"/COMMIT_BAREMSG
564 if test "$templatefile" != ""
566 # Test whether this is just the unaltered template.
567 if cnt=`sed -e '/^#/d' < "$templatefile" |
569 diff "$GIT_DIR"/COMMIT_BAREMSG - |
576 # No template, so the content in the commit message must
577 # have come from the user.
582 rm -f "$GIT_DIR"/COMMIT_BAREMSG
584 if test "$have_commitmsg" = "t"
586 if test -z "$TMP_INDEX"
588 tree=$(GIT_INDEX_FILE="$USE_INDEX" git write-tree)
590 tree=$(GIT_INDEX_FILE="$TMP_INDEX" git write-tree) &&
593 commit=$(git commit-tree $tree $PARENTS <"$GIT_DIR/COMMIT_MSG") &&
594 rlogm=$(sed -e 1q "$GIT_DIR"/COMMIT_MSG) &&
595 git update-ref -m "$GIT_REFLOG_ACTION: $rlogm" HEAD $commit "$current" &&
596 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" &&
597 if test -f "$NEXT_INDEX"
599 mv "$NEXT_INDEX" "$THIS_INDEX"
604 echo >&2 "* no commit message? aborting commit."
608 rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
617 if test -x "$GIT_DIR"/hooks/post-commit
619 "$GIT_DIR"/hooks/post-commit
623 commit=`git diff-tree --always --shortstat --pretty="format:%h: %s"\
624 --summary --root HEAD --`
625 echo "Created${initial_commit:+ initial} commit $commit"