3 # Copyright (c) 2005 Linus Torvalds
4 # Copyright (c) 2006 Junio C Hamano
6 USAGE
='[-a] [-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
89 while case "$#" in 0) break;; esac
92 -F|
--F|
-f|
--f|
--fi|
--fil|
--file)
93 case "$#" in 1) usage
;; esac
102 log_given
=t
$log_given
103 logfile
=`expr "z$1" : 'z-[Ff]\(.*\)'`
106 --F=*|
--f=*|
--fi=*|
--fil=*|
--file=*)
108 log_given
=t
$log_given
109 logfile
=`expr "z$1" : 'z-[^=]*=\(.*\)'`
116 --au=*|
--aut=*|
--auth=*|
--autho=*|
--author=*)
117 force_author
=`expr "z$1" : 'z-[^=]*=\(.*\)'`
120 --au|
--aut|
--auth|
--autho|
--author)
121 case "$#" in 1) usage
;; esac
126 -e|
--e|
--ed|
--edi|
--edit)
130 -i|
--i|
--in|
--inc|
--incl|
--inclu|
--includ|
--include)
134 -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 if test "$log_message" = ''
146 log_message
="$log_message
154 log_given
=m
$log_given
155 if test "$log_message" = ''
157 log_message
=`expr "z$1" : 'z-m\(.*\)'`
159 log_message
="$log_message
161 `expr "z
$1" : 'z-m\(.*\)'`"
166 --m=*|
--me=*|
--mes=*|
--mess=*|
--messa=*|
--messag=*|
--message=*)
167 log_given
=m
$log_given
168 if test "$log_message" = ''
170 log_message
=`expr "z$1" : 'z-[^=]*=\(.*\)'`
172 log_message
="$log_message
174 `expr "z
$1" : 'zq-[^=]*=\(.*\)'`"
179 -n|
--n|
--no|
--no-|
--no-v|
--no-ve|
--no-ver|
--no-veri|
--no-verif|\
184 --a|
--am|
--ame|
--amen|
--amend)
186 log_given
=t
$log_given
191 case "$#" in 1) usage
;; esac
193 log_given
=t
$log_given
198 --ree=*|
--reed=*|
--reedi=*|
--reedit=*|
--reedit-=*|
--reedit-m=*|\
199 --reedit-me=*|
--reedit-mes=*|
--reedit-mess=*|
--reedit-messa=*|\
200 --reedit-messag=*|
--reedit-message=*)
201 log_given
=t
$log_given
202 use_commit
=`expr "z$1" : 'z-[^=]*=\(.*\)'`
206 --ree|
--reed|
--reedi|
--reedit|
--reedit-|
--reedit-m|
--reedit-me|\
207 --reedit-mes|
--reedit-mess|
--reedit-messa|
--reedit-messag|\
209 case "$#" in 1) usage
;; esac
211 log_given
=t
$log_given
217 case "$#" in 1) usage
;; esac
219 log_given
=t
$log_given
224 --reu=*|
--reus=*|
--reuse=*|
--reuse-=*|
--reuse-m=*|
--reuse-me=*|\
225 --reuse-mes=*|
--reuse-mess=*|
--reuse-messa=*|
--reuse-messag=*|\
227 log_given
=t
$log_given
228 use_commit
=`expr "z$1" : 'z-[^=]*=\(.*\)'`
232 --reu|
--reus|
--reuse|
--reuse-|
--reuse-m|
--reuse-me|
--reuse-mes|\
233 --reuse-mess|
--reuse-messa|
--reuse-messag|
--reuse-message)
234 case "$#" in 1) usage
;; esac
236 log_given
=t
$log_given
241 -s|
--s|
--si|
--sig|
--sign|
--signo|
--signof|
--signoff)
245 -q|
--q|
--qu|
--qui|
--quie|
--quiet)
249 -v|
--v|
--ve|
--ver|
--verb|
--verbo|
--verbos|
--verbose)
253 -u|
--u|
--un|
--unt|
--untr|
--untra|
--untrac|
--untrack|
--untracke|\
254 --untracked|
--untracked-|
--untracked-f|
--untracked-fi|
--untracked-fil|\
255 --untracked-file|
--untracked-files)
271 case "$edit_flag" in t
) no_edit
= ;; esac
273 ################################################################
274 # Sanity check options
276 case "$amend,$initial_commit" in
278 die
"You do not have anything to amend." ;;
280 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
281 die
"You are in the middle of a merge -- cannot amend."
287 die
"Only one of -c/-C/-F/--amend can be used." ;;
289 die
"Option -m cannot be combined with -c/-C/-F/--amend." ;;
292 case "$#,$also,$only,$amend" in
294 die
"Only one of --include/--only can be used." ;;
296 die
"No paths with --include/--only does not make sense." ;;
298 only_include_assumed
="# Clever... amending the last one with dirty index." ;;
302 only_include_assumed
="# Explicit paths specified without -i nor -o; assuming --only paths..."
307 case "$all,$also,$#" in
309 die
"Cannot use -a and -i at the same time." ;;
311 die
"Paths with -a does not make sense." ;;
313 die
"No paths with -i does not make sense." ;;
316 ################################################################
317 # Prepare index to have a tree to be committed
324 GIT_INDEX_FILE
="$NEXT_INDEX" &&
325 export GIT_INDEX_FILE
&&
326 git-diff-files
--name-only -z |
327 git-update-index
--remove -z --stdin
332 git-ls-files
--error-unmatch -- "$@" >/dev
/null ||
exit
334 git-diff-files
--name-only -z -- "$@" |
337 GIT_INDEX_FILE
="$NEXT_INDEX" &&
338 export GIT_INDEX_FILE
&&
339 git-update-index
--remove -z --stdin
347 if test -f "$GIT_DIR/MERGE_HEAD"
349 refuse_partial
"Cannot do a partial commit during a merge."
351 TMP_INDEX
="$GIT_DIR/tmp-index$$"
352 commit_only
=`git-ls-files --error-unmatch -- "$@"` ||
exit
354 # Build a temporary index and update the real index
356 if test -z "$initial_commit"
358 cp "$THIS_INDEX" "$TMP_INDEX"
359 GIT_INDEX_FILE
="$TMP_INDEX" git-read-tree
-m HEAD
364 echo "$commit_only" |
365 GIT_INDEX_FILE
="$TMP_INDEX" \
366 git-update-index
--add --remove --stdin &&
369 echo "$commit_only" |
371 GIT_INDEX_FILE
="$NEXT_INDEX"
372 export GIT_INDEX_FILE
373 git-update-index
--remove --stdin
380 ################################################################
381 # If we do as-is commit, the index file will be THIS_INDEX,
382 # otherwise NEXT_INDEX after we make this commit. We leave
383 # the index as is if we abort.
385 if test -f "$NEXT_INDEX"
387 USE_INDEX
="$NEXT_INDEX"
389 USE_INDEX
="$THIS_INDEX"
392 case "$status_only" in
394 # This will silently fail in a read-only repository, which is
396 GIT_INDEX_FILE
="$USE_INDEX" git-update-index
-q --unmerged --refresh
401 GIT_INDEX_FILE
="$USE_INDEX" git-update-index
-q --refresh ||
exit
405 ################################################################
406 # Grab commit message, write out tree and make commit.
408 if test t
= "$verify" && test -x "$GIT_DIR"/hooks
/pre-commit
412 GIT_INDEX_FILE
="$TMP_INDEX" "$GIT_DIR"/hooks
/pre-commit
414 GIT_INDEX_FILE
="$USE_INDEX" "$GIT_DIR"/hooks
/pre-commit
418 if test "$log_message" != ''
421 elif test "$logfile" != ""
423 if test "$logfile" = -
426 echo >&2 "(reading log message from standard input)"
431 elif test "$use_commit" != ""
433 encoding
=$
(git config i18n.commitencoding ||
echo UTF-8
)
434 git show
-s --pretty=raw
--encoding="$encoding" "$use_commit" |
435 sed -e '1,/^$/d' -e 's/^ //'
436 elif test -f "$GIT_DIR/MERGE_MSG"
438 cat "$GIT_DIR/MERGE_MSG"
439 elif test -f "$GIT_DIR/SQUASH_MSG"
441 cat "$GIT_DIR/SQUASH_MSG"
442 fi | git-stripspace
>"$GIT_DIR"/COMMIT_EDITMSG
446 need_blank_before_signoff
=
447 tail -n 1 "$GIT_DIR"/COMMIT_EDITMSG |
448 grep 'Signed-off-by:' >/dev
/null || need_blank_before_signoff
=yes
450 test -z "$need_blank_before_signoff" ||
echo
451 git-var GIT_COMMITTER_IDENT |
sed -e '
455 } >>"$GIT_DIR"/COMMIT_EDITMSG
459 if test -f "$GIT_DIR/MERGE_HEAD" && test -z "$no_edit"; then
461 echo "# It looks like you may be committing a MERGE."
462 echo "# If this is not correct, please remove the file"
463 echo "# $GIT_DIR/MERGE_HEAD"
464 echo "# and try again"
466 fi >>"$GIT_DIR"/COMMIT_EDITMSG
469 if test '' != "$use_commit"
473 s/'\''/'\''\\'\'\''/g
475 s/^author \([^<]*\) <[^>]*> .*$/\1/
477 s
/.
*/GIT_AUTHOR_NAME
='\''&'\''/p
480 s
/^author
[^
<]* <\
([^
>]*\
)> .
*$
/\
1/
482 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
485 s/^author [^<]* <[^>]*> \(.*\)$/\1/
487 s
/.
*/GIT_AUTHOR_DATE
='\''&'\''/p
492 encoding=$(git config i18n.commitencoding || echo UTF-8)
493 set_author_env=`git show -s --pretty=raw --encoding="$encoding" "$use_commit" |
494 LANG=C LC_ALL=C sed -ne "$pick_author_script"`
495 eval "$set_author_env"
496 export GIT_AUTHOR_NAME
497 export GIT_AUTHOR_EMAIL
498 export GIT_AUTHOR_DATE
500 if test '' != "$force_author"
502 GIT_AUTHOR_NAME=`expr "z$force_author" : 'z\
(.
*[^
]\
) *<.
*'` &&
503 GIT_AUTHOR_EMAIL=`expr "z$force_author" : '.
*\
(<.
*\
)'` &&
504 test '' != "$GIT_AUTHOR_NAME" &&
505 test '' != "$GIT_AUTHOR_EMAIL" ||
506 die "malformed --author parameter"
507 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
511 if test -z "$initial_commit"
514 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
515 rloga='commit
(merge
)'
516 PARENTS="-p HEAD "`sed -e 's
/^
/-p /' "$GIT_DIR/MERGE_HEAD"`
517 elif test -n "$amend"; then
518 rloga='commit
(amend
)'
519 PARENTS=$(git-cat-file commit HEAD |
520 sed -n -e '/^$
/q
' -e 's
/^parent
/-p /p
')
522 current="$(git-rev-parse --verify HEAD)"
524 if [ -z "$(git-ls-files)" ]; then
525 echo >&2 'nothing to commit
(use
"git add file1 file2" to include
for commit
)'
529 rloga='commit
(initial
)'
532 set_reflog_action "$rloga"
534 if test -z "$no_edit"
538 echo "# Please enter the commit message for your changes."
539 echo "# (Comment lines starting with '#' will not be included)"
540 test -z "$only_include_assumed" ||
echo "$only_include_assumed"
542 } >>"$GIT_DIR"/COMMIT_EDITMSG
544 # we need to check if there is anything to commit
545 run_status
>/dev
/null
547 if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" -a -z "$amend" ]
549 rm -f "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
556 case "${VISUAL:-$EDITOR},$TERM" in
558 echo >&2 "Terminal is dumb but no VISUAL nor EDITOR defined."
559 echo >&2 "Please supply the commit log message using either"
560 echo >&2 "-m or -F option. A boilerplate log message has"
561 echo >&2 "been prepared in $GIT_DIR/COMMIT_EDITMSG"
565 git-var GIT_AUTHOR_IDENT
> /dev
/null || die
566 git-var GIT_COMMITTER_IDENT
> /dev
/null || die
567 ${VISUAL:-${EDITOR:-vi}} "$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 if cnt
=`grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
598 if test -z "$TMP_INDEX"
600 tree
=$
(GIT_INDEX_FILE
="$USE_INDEX" git-write-tree
)
602 tree
=$
(GIT_INDEX_FILE
="$TMP_INDEX" git-write-tree
) &&
605 commit
=$
(cat "$GIT_DIR"/COMMIT_MSG | git-commit-tree
$tree $PARENTS) &&
606 rlogm
=$
(sed -e 1q
"$GIT_DIR"/COMMIT_MSG
) &&
607 git-update-ref
-m "$GIT_REFLOG_ACTION: $rlogm" HEAD
$commit "$current" &&
608 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" &&
609 if test -f "$NEXT_INDEX"
611 mv "$NEXT_INDEX" "$THIS_INDEX"
616 echo >&2 "* no commit message? aborting commit."
620 rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
621 if test -d "$GIT_DIR/rr-cache"
628 if test -x "$GIT_DIR"/hooks
/post-commit
630 "$GIT_DIR"/hooks
/post-commit
634 echo "Created${initial_commit:+ initial} commit $commit"
635 git-diff-tree
--shortstat --summary --root --no-commit-id HEAD
--