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>] [[-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 while case "$#" in 0) break;; esac
93 -F|
--F|
-f|
--f|
--fi|
--fil|
--file)
94 case "$#" in 1) usage
;; esac
103 log_given
=t
$log_given
104 logfile
=`expr "z$1" : 'z-[Ff]\(.*\)'`
107 --F=*|
--f=*|
--fi=*|
--fil=*|
--file=*)
109 log_given
=t
$log_given
110 logfile
=`expr "z$1" : 'z-[^=]*=\(.*\)'`
117 --au=*|
--aut=*|
--auth=*|
--autho=*|
--author=*)
118 force_author
=`expr "z$1" : 'z-[^=]*=\(.*\)'`
121 --au|
--aut|
--auth|
--autho|
--author)
122 case "$#" in 1) usage
;; esac
127 -e|
--e|
--ed|
--edi|
--edit)
131 -i|
--i|
--in|
--inc|
--incl|
--inclu|
--includ|
--include)
135 --int|
--inte|
--inter|
--intera|
--interac|
--interact|
--interacti|\
136 --interactiv|
--interactive)
140 -o|
--o|
--on|
--onl|
--only)
144 -m|
--m|
--me|
--mes|
--mess|
--messa|
--messag|
--message)
145 case "$#" in 1) usage
;; esac
147 log_given
=m
$log_given
148 if test "$log_message" = ''
152 log_message
="$log_message
160 log_given
=m
$log_given
161 if test "$log_message" = ''
163 log_message
=`expr "z$1" : 'z-m\(.*\)'`
165 log_message
="$log_message
167 `expr "z
$1" : 'z-m\(.*\)'`"
172 --m=*|
--me=*|
--mes=*|
--mess=*|
--messa=*|
--messag=*|
--message=*)
173 log_given
=m
$log_given
174 if test "$log_message" = ''
176 log_message
=`expr "z$1" : 'z-[^=]*=\(.*\)'`
178 log_message
="$log_message
180 `expr "z
$1" : 'zq-[^=]*=\(.*\)'`"
185 -n|
--n|
--no|
--no-|
--no-v|
--no-ve|
--no-ver|
--no-veri|
--no-verif|\
190 --a|
--am|
--ame|
--amen|
--amend)
192 log_given
=t
$log_given
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 -q|
--q|
--qu|
--qui|
--quie|
--quiet)
255 -v|
--v|
--ve|
--ver|
--verb|
--verbo|
--verbos|
--verbose)
259 -u|
--u|
--un|
--unt|
--untr|
--untra|
--untrac|
--untrack|
--untracke|\
260 --untracked|
--untracked-|
--untracked-f|
--untracked-fi|
--untracked-fil|\
261 --untracked-file|
--untracked-files)
277 case "$edit_flag" in t
) no_edit
= ;; esac
279 ################################################################
280 # Sanity check options
282 case "$amend,$initial_commit" in
284 die
"You do not have anything to amend." ;;
286 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
287 die
"You are in the middle of a merge -- cannot amend."
293 die
"Only one of -c/-C/-F/--amend can be used." ;;
295 die
"Option -m cannot be combined with -c/-C/-F/--amend." ;;
298 case "$#,$also,$only,$amend" in
300 die
"Only one of --include/--only can be used." ;;
302 die
"No paths with --include/--only does not make sense." ;;
304 only_include_assumed
="# Clever... amending the last one with dirty index." ;;
308 only_include_assumed
="# Explicit paths specified without -i nor -o; assuming --only paths..."
313 case "$all,$interactive,$also,$#" in
315 die
"Cannot use -a, --interactive or -i at the same time." ;;
317 die
"Paths with -a does not make sense." ;;
319 die
"Paths with --interactive does not make sense." ;;
321 die
"No paths with -i does not make sense." ;;
324 ################################################################
325 # Prepare index to have a tree to be committed
329 if test ! -f "$THIS_INDEX"
331 die
'nothing to commit (use "git add file1 file2" to include for commit)'
336 GIT_INDEX_FILE
="$NEXT_INDEX" &&
337 export GIT_INDEX_FILE
&&
338 git diff-files
--name-only -z |
339 git update-index
--remove -z --stdin
344 git ls-files
--error-unmatch -- "$@" >/dev
/null ||
exit
346 git diff-files
--name-only -z -- "$@" |
349 GIT_INDEX_FILE
="$NEXT_INDEX" &&
350 export GIT_INDEX_FILE
&&
351 git update-index
--remove -z --stdin
355 if test "$interactive" = t
; then
356 git add
--interactive ||
exit
362 if test -f "$GIT_DIR/MERGE_HEAD"
364 refuse_partial
"Cannot do a partial commit during a merge."
366 TMP_INDEX
="$GIT_DIR/tmp-index$$"
367 commit_only
=`git ls-files --error-unmatch -- "$@"` ||
exit
369 # Build a temporary index and update the real index
371 if test -z "$initial_commit"
373 GIT_INDEX_FILE
="$THIS_INDEX" \
374 git read-tree
--index-output="$TMP_INDEX" -i -m HEAD
379 printf '%s\n' "$commit_only" |
380 GIT_INDEX_FILE
="$TMP_INDEX" \
381 git update-index
--add --remove --stdin &&
384 printf '%s\n' "$commit_only" |
386 GIT_INDEX_FILE
="$NEXT_INDEX"
387 export GIT_INDEX_FILE
388 git update-index
--remove --stdin
395 ################################################################
396 # If we do as-is commit, the index file will be THIS_INDEX,
397 # otherwise NEXT_INDEX after we make this commit. We leave
398 # the index as is if we abort.
400 if test -f "$NEXT_INDEX"
402 USE_INDEX
="$NEXT_INDEX"
404 USE_INDEX
="$THIS_INDEX"
407 case "$status_only" in
409 # This will silently fail in a read-only repository, which is
411 GIT_INDEX_FILE
="$USE_INDEX" git update-index
-q --unmerged --refresh
416 GIT_INDEX_FILE
="$USE_INDEX" git update-index
-q --refresh ||
exit
420 ################################################################
421 # Grab commit message, write out tree and make commit.
423 if test t
= "$verify" && test -x "$GIT_DIR"/hooks
/pre-commit
427 GIT_INDEX_FILE
="$TMP_INDEX" "$GIT_DIR"/hooks
/pre-commit
429 GIT_INDEX_FILE
="$USE_INDEX" "$GIT_DIR"/hooks
/pre-commit
433 if test "$log_message" != ''
435 printf '%s\n' "$log_message"
436 elif test "$logfile" != ""
438 if test "$logfile" = -
441 echo >&2 "(reading log message from standard input)"
446 elif test "$use_commit" != ""
448 encoding
=$
(git config i18n.commitencoding ||
echo UTF-8
)
449 git show
-s --pretty=raw
--encoding="$encoding" "$use_commit" |
450 sed -e '1,/^$/d' -e 's/^ //'
451 elif test -f "$GIT_DIR/MERGE_MSG"
453 cat "$GIT_DIR/MERGE_MSG"
454 elif test -f "$GIT_DIR/SQUASH_MSG"
456 cat "$GIT_DIR/SQUASH_MSG"
457 fi | git stripspace
>"$GIT_DIR"/COMMIT_EDITMSG
461 sign
=$
(git-var GIT_COMMITTER_IDENT |
sed -e '
465 blank_before_signoff
=
466 tail -n 1 "$GIT_DIR"/COMMIT_EDITMSG |
467 grep 'Signed-off-by:' >/dev
/null || blank_before_signoff
='
469 tail -n 1 "$GIT_DIR"/COMMIT_EDITMSG |
470 grep "$sign"$
>/dev
/null ||
471 printf '%s%s\n' "$blank_before_signoff" "$sign" \
472 >>"$GIT_DIR"/COMMIT_EDITMSG
476 if test -f "$GIT_DIR/MERGE_HEAD" && test -z "$no_edit"; then
478 echo "# It looks like you may be committing a MERGE."
479 echo "# If this is not correct, please remove the file"
480 printf '%s\n' "# $GIT_DIR/MERGE_HEAD"
481 echo "# and try again"
483 fi >>"$GIT_DIR"/COMMIT_EDITMSG
486 if test '' != "$use_commit"
488 eval "$(get_author_ident_from_commit "$use_commit")"
489 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
491 if test '' != "$force_author"
493 GIT_AUTHOR_NAME
=`expr "z$force_author" : 'z\(.*[^ ]\) *<.*'` &&
494 GIT_AUTHOR_EMAIL
=`expr "z$force_author" : '.*\(<.*\)'` &&
495 test '' != "$GIT_AUTHOR_NAME" &&
496 test '' != "$GIT_AUTHOR_EMAIL" ||
497 die
"malformed --author parameter"
498 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
502 if test -z "$initial_commit"
505 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
506 rloga
='commit (merge)'
507 PARENTS
="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
508 elif test -n "$amend"; then
509 rloga
='commit (amend)'
510 PARENTS
=$
(git cat-file commit HEAD |
511 sed -n -e '/^$/q' -e 's/^parent /-p /p')
513 current
="$(git rev-parse --verify HEAD)"
515 if [ -z "$(git ls-files)" ]; then
516 echo >&2 'nothing to commit (use "git add file1 file2" to include for commit)'
520 rloga
='commit (initial)'
523 set_reflog_action
"$rloga"
525 if test -z "$no_edit"
529 echo "# Please enter the commit message for your changes."
530 echo "# (Comment lines starting with '#' will not be included)"
531 test -z "$only_include_assumed" ||
echo "$only_include_assumed"
533 } >>"$GIT_DIR"/COMMIT_EDITMSG
535 # we need to check if there is anything to commit
536 run_status
>/dev
/null
538 if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" -a -z "$amend" ]
540 rm -f "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
547 case "${VISUAL:-$EDITOR},$TERM" in
549 echo >&2 "Terminal is dumb but no VISUAL nor EDITOR defined."
550 echo >&2 "Please supply the commit log message using either"
551 echo >&2 "-m or -F option. A boilerplate log message has"
552 echo >&2 "been prepared in $GIT_DIR/COMMIT_EDITMSG"
556 git-var GIT_AUTHOR_IDENT
> /dev
/null || die
557 git-var GIT_COMMITTER_IDENT
> /dev
/null || die
558 ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR/COMMIT_EDITMSG"
564 if test -x "$GIT_DIR"/hooks
/commit-msg
566 "$GIT_DIR"/hooks
/commit-msg
"$GIT_DIR"/COMMIT_EDITMSG ||
exit
570 if test -z "$no_edit"
578 ' "$GIT_DIR"/COMMIT_EDITMSG
580 cat "$GIT_DIR"/COMMIT_EDITMSG
582 git stripspace
>"$GIT_DIR"/COMMIT_MSG
584 if cnt
=`grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
589 if test -z "$TMP_INDEX"
591 tree
=$
(GIT_INDEX_FILE
="$USE_INDEX" git write-tree
)
593 tree
=$
(GIT_INDEX_FILE
="$TMP_INDEX" git write-tree
) &&
596 commit
=$
(git commit-tree
$tree $PARENTS <"$GIT_DIR/COMMIT_MSG") &&
597 rlogm
=$
(sed -e 1q
"$GIT_DIR"/COMMIT_MSG
) &&
598 git update-ref
-m "$GIT_REFLOG_ACTION: $rlogm" HEAD
$commit "$current" &&
599 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" &&
600 if test -f "$NEXT_INDEX"
602 mv "$NEXT_INDEX" "$THIS_INDEX"
607 echo >&2 "* no commit message? aborting commit."
611 rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
619 if test -x "$GIT_DIR"/hooks
/post-commit
621 "$GIT_DIR"/hooks
/post-commit
625 commit
=`git diff-tree --always --shortstat --pretty="format:%h: %s"\
626 --summary --root HEAD --`
627 echo "Created${initial_commit:+ initial} commit $commit"