-u is now default for 'git-applymbox'
[git/gitweb.git] / git-commit.sh
blobc2beb76fe436279422ff5212aed573fb821bad7b
1 #!/bin/sh
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>] [-u] [--amend] [-e] [--author <author>] [[-i | -o] <path>...]'
7 SUBDIRECTORY_OK=Yes
8 . git-sh-setup
10 git-rev-parse --verify HEAD >/dev/null 2>&1 || initial_commit=t
12 case "$0" in
13 *status)
14 status_only=t
15 unmerged_ok_if_status=--unmerged ;;
16 *commit)
17 status_only=
18 unmerged_ok_if_status= ;;
19 esac
21 refuse_partial () {
22 echo >&2 "$1"
23 echo >&2 "You might have meant to say 'git commit -i paths...', perhaps?"
24 exit 1
27 THIS_INDEX="$GIT_DIR/index"
28 NEXT_INDEX="$GIT_DIR/next-index$$"
29 rm -f "$NEXT_INDEX"
30 save_index () {
31 cp -p "$THIS_INDEX" "$NEXT_INDEX"
34 run_status () {
35 # If TMP_INDEX is defined, that means we are doing
36 # "--only" partial commit, and that index file is used
37 # to build the tree for the commit. Otherwise, if
38 # NEXT_INDEX exists, that is the index file used to
39 # make the commit. Otherwise we are using as-is commit
40 # so the regular index file is what we use to compare.
41 if test '' != "$TMP_INDEX"
42 then
43 GIT_INDEX_FILE="$TMP_INDEX"
44 export GIT_INDEX_FILE
45 elif test -f "$NEXT_INDEX"
46 then
47 GIT_INDEX_FILE="$NEXT_INDEX"
48 export GIT_INDEX_FILE
51 case "$status_only" in
52 t) color= ;;
53 *) color=--nocolor ;;
54 esac
55 git-runstatus ${color} \
56 ${verbose:+--verbose} \
57 ${amend:+--amend} \
58 ${untracked_files:+--untracked}
61 trap '
62 test -z "$TMP_INDEX" || {
63 test -f "$TMP_INDEX" && rm -f "$TMP_INDEX"
65 rm -f "$NEXT_INDEX"
66 ' 0
68 ################################################################
69 # Command line argument parsing and sanity checking
71 all=
72 also=
73 only=
74 logfile=
75 use_commit=
76 amend=
77 edit_flag=
78 no_edit=
79 log_given=
80 log_message=
81 verify=t
82 quiet=
83 verbose=
84 signoff=
85 force_author=
86 only_include_assumed=
87 untracked_files=
88 while case "$#" in 0) break;; esac
90 case "$1" in
91 -F|--F|-f|--f|--fi|--fil|--file)
92 case "$#" in 1) usage ;; esac
93 shift
94 no_edit=t
95 log_given=t$log_given
96 logfile="$1"
97 shift
99 -F*|-f*)
100 no_edit=t
101 log_given=t$log_given
102 logfile=`expr "z$1" : 'z-[Ff]\(.*\)'`
103 shift
105 --F=*|--f=*|--fi=*|--fil=*|--file=*)
106 no_edit=t
107 log_given=t$log_given
108 logfile=`expr "z$1" : 'z-[^=]*=\(.*\)'`
109 shift
111 -a|--a|--al|--all)
112 all=t
113 shift
115 --au=*|--aut=*|--auth=*|--autho=*|--author=*)
116 force_author=`expr "z$1" : 'z-[^=]*=\(.*\)'`
117 shift
119 --au|--aut|--auth|--autho|--author)
120 case "$#" in 1) usage ;; esac
121 shift
122 force_author="$1"
123 shift
125 -e|--e|--ed|--edi|--edit)
126 edit_flag=t
127 shift
129 -i|--i|--in|--inc|--incl|--inclu|--includ|--include)
130 also=t
131 shift
133 -o|--o|--on|--onl|--only)
134 only=t
135 shift
137 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
138 case "$#" in 1) usage ;; esac
139 shift
140 log_given=m$log_given
141 if test "$log_message" = ''
142 then
143 log_message="$1"
144 else
145 log_message="$log_message
149 no_edit=t
150 shift
152 -m*)
153 log_given=m$log_given
154 if test "$log_message" = ''
155 then
156 log_message=`expr "z$1" : 'z-m\(.*\)'`
157 else
158 log_message="$log_message
160 `expr "z$1" : 'z-m\(.*\)'`"
162 no_edit=t
163 shift
165 --m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
166 log_given=m$log_given
167 if test "$log_message" = ''
168 then
169 log_message=`expr "z$1" : 'z-[^=]*=\(.*\)'`
170 else
171 log_message="$log_message
173 `expr "z$1" : 'zq-[^=]*=\(.*\)'`"
175 no_edit=t
176 shift
178 -n|--n|--no|--no-|--no-v|--no-ve|--no-ver|--no-veri|--no-verif|\
179 --no-verify)
180 verify=
181 shift
183 --a|--am|--ame|--amen|--amend)
184 amend=t
185 log_given=t$log_given
186 use_commit=HEAD
187 shift
190 case "$#" in 1) usage ;; esac
191 shift
192 log_given=t$log_given
193 use_commit="$1"
194 no_edit=
195 shift
197 --ree=*|--reed=*|--reedi=*|--reedit=*|--reedit-=*|--reedit-m=*|\
198 --reedit-me=*|--reedit-mes=*|--reedit-mess=*|--reedit-messa=*|\
199 --reedit-messag=*|--reedit-message=*)
200 log_given=t$log_given
201 use_commit=`expr "z$1" : 'z-[^=]*=\(.*\)'`
202 no_edit=
203 shift
205 --ree|--reed|--reedi|--reedit|--reedit-|--reedit-m|--reedit-me|\
206 --reedit-mes|--reedit-mess|--reedit-messa|--reedit-messag|\
207 --reedit-message)
208 case "$#" in 1) usage ;; esac
209 shift
210 log_given=t$log_given
211 use_commit="$1"
212 no_edit=
213 shift
216 case "$#" in 1) usage ;; esac
217 shift
218 log_given=t$log_given
219 use_commit="$1"
220 no_edit=t
221 shift
223 --reu=*|--reus=*|--reuse=*|--reuse-=*|--reuse-m=*|--reuse-me=*|\
224 --reuse-mes=*|--reuse-mess=*|--reuse-messa=*|--reuse-messag=*|\
225 --reuse-message=*)
226 log_given=t$log_given
227 use_commit=`expr "z$1" : 'z-[^=]*=\(.*\)'`
228 no_edit=t
229 shift
231 --reu|--reus|--reuse|--reuse-|--reuse-m|--reuse-me|--reuse-mes|\
232 --reuse-mess|--reuse-messa|--reuse-messag|--reuse-message)
233 case "$#" in 1) usage ;; esac
234 shift
235 log_given=t$log_given
236 use_commit="$1"
237 no_edit=t
238 shift
240 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
241 signoff=t
242 shift
244 -q|--q|--qu|--qui|--quie|--quiet)
245 quiet=t
246 shift
248 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
249 verbose=t
250 shift
252 -u|--u|--un|--unt|--untr|--untra|--untrac|--untrack|--untracke|\
253 --untracked|--untracked-|--untracked-f|--untracked-fi|--untracked-fil|\
254 --untracked-file|--untracked-files)
255 untracked_files=t
256 shift
259 shift
260 break
263 usage
266 break
268 esac
269 done
270 case "$edit_flag" in t) no_edit= ;; esac
272 ################################################################
273 # Sanity check options
275 case "$amend,$initial_commit" in
276 t,t)
277 die "You do not have anything to amend." ;;
279 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
280 die "You are in the middle of a merge -- cannot amend."
281 fi ;;
282 esac
284 case "$log_given" in
285 tt*)
286 die "Only one of -c/-C/-F can be used." ;;
287 *tm*|*mt*)
288 die "Option -m cannot be combined with -c/-C/-F." ;;
289 esac
291 case "$#,$also,$only,$amend" in
292 *,t,t,*)
293 die "Only one of --include/--only can be used." ;;
294 0,t,,* | 0,,t,)
295 die "No paths with --include/--only does not make sense." ;;
296 0,,t,t)
297 only_include_assumed="# Clever... amending the last one with dirty index." ;;
298 0,,,*)
300 *,,,*)
301 only_include_assumed="# Explicit paths specified without -i nor -o; assuming --only paths..."
302 also=
304 esac
305 unset only
306 case "$all,$also,$#" in
307 t,t,*)
308 die "Cannot use -a and -i at the same time." ;;
309 t,,[1-9]*)
310 die "Paths with -a does not make sense." ;;
311 ,t,0)
312 die "No paths with -i does not make sense." ;;
313 esac
315 ################################################################
316 # Prepare index to have a tree to be committed
318 TOP=`git-rev-parse --show-cdup`
319 if test -z "$TOP"
320 then
321 TOP=./
324 case "$all,$also" in
326 save_index &&
328 cd "$TOP"
329 GIT_INDEX_FILE="$NEXT_INDEX"
330 export GIT_INDEX_FILE
331 git-diff-files --name-only -z |
332 git-update-index --remove -z --stdin
336 save_index &&
337 git-ls-files --error-unmatch -- "$@" >/dev/null || exit
339 git-diff-files --name-only -z -- "$@" |
341 cd "$TOP"
342 GIT_INDEX_FILE="$NEXT_INDEX"
343 export GIT_INDEX_FILE
344 git-update-index --remove -z --stdin
348 case "$#" in
350 ;; # commit as-is
352 if test -f "$GIT_DIR/MERGE_HEAD"
353 then
354 refuse_partial "Cannot do a partial commit during a merge."
356 TMP_INDEX="$GIT_DIR/tmp-index$$"
357 commit_only=`git-ls-files --error-unmatch -- "$@"` || exit
359 # Build a temporary index and update the real index
360 # the same way.
361 if test -z "$initial_commit"
362 then
363 cp "$THIS_INDEX" "$TMP_INDEX"
364 GIT_INDEX_FILE="$TMP_INDEX" git-read-tree -m HEAD
365 else
366 rm -f "$TMP_INDEX"
367 fi || exit
369 echo "$commit_only" |
370 GIT_INDEX_FILE="$TMP_INDEX" \
371 git-update-index --add --remove --stdin &&
373 save_index &&
374 echo "$commit_only" |
376 GIT_INDEX_FILE="$NEXT_INDEX"
377 export GIT_INDEX_FILE
378 git-update-index --remove --stdin
379 ) || exit
381 esac
383 esac
385 ################################################################
386 # If we do as-is commit, the index file will be THIS_INDEX,
387 # otherwise NEXT_INDEX after we make this commit. We leave
388 # the index as is if we abort.
390 if test -f "$NEXT_INDEX"
391 then
392 USE_INDEX="$NEXT_INDEX"
393 else
394 USE_INDEX="$THIS_INDEX"
397 GIT_INDEX_FILE="$USE_INDEX" \
398 git-update-index -q $unmerged_ok_if_status --refresh || exit
400 ################################################################
401 # If the request is status, just show it and exit.
403 case "$0" in
404 *status)
405 run_status
406 exit $?
407 esac
409 ################################################################
410 # Grab commit message, write out tree and make commit.
412 if test t = "$verify" && test -x "$GIT_DIR"/hooks/pre-commit
413 then
414 if test "$TMP_INDEX"
415 then
416 GIT_INDEX_FILE="$TMP_INDEX" "$GIT_DIR"/hooks/pre-commit
417 else
418 GIT_INDEX_FILE="$USE_INDEX" "$GIT_DIR"/hooks/pre-commit
419 fi || exit
422 if test "$log_message" != ''
423 then
424 echo "$log_message"
425 elif test "$logfile" != ""
426 then
427 if test "$logfile" = -
428 then
429 test -t 0 &&
430 echo >&2 "(reading log message from standard input)"
432 else
433 cat <"$logfile"
435 elif test "$use_commit" != ""
436 then
437 git-cat-file commit "$use_commit" | sed -e '1,/^$/d'
438 elif test -f "$GIT_DIR/MERGE_MSG"
439 then
440 cat "$GIT_DIR/MERGE_MSG"
441 elif test -f "$GIT_DIR/SQUASH_MSG"
442 then
443 cat "$GIT_DIR/SQUASH_MSG"
444 fi | git-stripspace >"$GIT_DIR"/COMMIT_EDITMSG
446 case "$signoff" in
449 echo
450 git-var GIT_COMMITTER_IDENT | sed -e '
451 s/>.*/>/
452 s/^/Signed-off-by: /
454 } >>"$GIT_DIR"/COMMIT_EDITMSG
456 esac
458 if test -f "$GIT_DIR/MERGE_HEAD" && test -z "$no_edit"; then
459 echo "#"
460 echo "# It looks like you may be committing a MERGE."
461 echo "# If this is not correct, please remove the file"
462 echo "# $GIT_DIR/MERGE_HEAD"
463 echo "# and try again"
464 echo "#"
465 fi >>"$GIT_DIR"/COMMIT_EDITMSG
467 # Author
468 if test '' != "$force_author"
469 then
470 GIT_AUTHOR_NAME=`expr "z$force_author" : 'z\(.*[^ ]\) *<.*'` &&
471 GIT_AUTHOR_EMAIL=`expr "z$force_author" : '.*\(<.*\)'` &&
472 test '' != "$GIT_AUTHOR_NAME" &&
473 test '' != "$GIT_AUTHOR_EMAIL" ||
474 die "malformed --author parameter"
475 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
476 elif test '' != "$use_commit"
477 then
478 pick_author_script='
479 /^author /{
480 s/'\''/'\''\\'\'\''/g
482 s/^author \([^<]*\) <[^>]*> .*$/\1/
483 s/'\''/'\''\'\'\''/g
484 s/.*/GIT_AUTHOR_NAME='\''&'\''/p
487 s/^author [^<]* <\([^>]*\)> .*$/\1/
488 s/'\''/'\''\'\'\''/g
489 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
492 s/^author [^<]* <[^>]*> \(.*\)$/\1/
493 s/'\''/'\''\'\'\''/g
494 s/.*/GIT_AUTHOR_DATE='\''&'\''/p
499 set_author_env=`git-cat-file commit "$use_commit" |
500 LANG=C LC_ALL=C sed -ne "$pick_author_script"`
501 eval "$set_author_env"
502 export GIT_AUTHOR_NAME
503 export GIT_AUTHOR_EMAIL
504 export GIT_AUTHOR_DATE
507 PARENTS="-p HEAD"
508 if test -z "$initial_commit"
509 then
510 rloga='commit'
511 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
512 rloga='commit (merge)'
513 PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
514 elif test -n "$amend"; then
515 rloga='commit (amend)'
516 PARENTS=$(git-cat-file commit HEAD |
517 sed -n -e '/^$/q' -e 's/^parent /-p /p')
519 current="$(git-rev-parse --verify HEAD)"
520 else
521 if [ -z "$(git-ls-files)" ]; then
522 echo >&2 'nothing to commit (use "git add file1 file2" to include for commit)'
523 exit 1
525 PARENTS=""
526 rloga='commit (initial)'
527 current=''
530 if test -z "$no_edit"
531 then
533 echo ""
534 echo "# Please enter the commit message for your changes."
535 echo "# (Comment lines starting with '#' will not be included)"
536 test -z "$only_include_assumed" || echo "$only_include_assumed"
537 run_status
538 } >>"$GIT_DIR"/COMMIT_EDITMSG
539 else
540 # we need to check if there is anything to commit
541 run_status >/dev/null
543 if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" -a -z "$amend" ]
544 then
545 rm -f "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
546 run_status
547 exit 1
550 case "$no_edit" in
552 case "${VISUAL:-$EDITOR},$TERM" in
553 ,dumb)
554 echo >&2 "Terminal is dumb but no VISUAL nor EDITOR defined."
555 echo >&2 "Please supply the commit log message using either"
556 echo >&2 "-m or -F option. A boilerplate log message has"
557 echo >&2 "been prepared in $GIT_DIR/COMMIT_EDITMSG"
558 exit 1
560 esac
561 git-var GIT_AUTHOR_IDENT > /dev/null || die
562 git-var GIT_COMMITTER_IDENT > /dev/null || die
563 ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR/COMMIT_EDITMSG"
565 esac
567 case "$verify" in
569 if test -x "$GIT_DIR"/hooks/commit-msg
570 then
571 "$GIT_DIR"/hooks/commit-msg "$GIT_DIR"/COMMIT_EDITMSG || exit
573 esac
575 if test -z "$no_edit"
576 then
577 sed -e '
578 /^diff --git a\/.*/{
579 s///
582 /^#/d
583 ' "$GIT_DIR"/COMMIT_EDITMSG
584 else
585 cat "$GIT_DIR"/COMMIT_EDITMSG
586 fi |
587 git-stripspace >"$GIT_DIR"/COMMIT_MSG
589 if cnt=`grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
590 git-stripspace |
591 wc -l` &&
592 test 0 -lt $cnt
593 then
594 if test -z "$TMP_INDEX"
595 then
596 tree=$(GIT_INDEX_FILE="$USE_INDEX" git-write-tree)
597 else
598 tree=$(GIT_INDEX_FILE="$TMP_INDEX" git-write-tree) &&
599 rm -f "$TMP_INDEX"
600 fi &&
601 commit=$(cat "$GIT_DIR"/COMMIT_MSG | git-commit-tree $tree $PARENTS) &&
602 rlogm=$(sed -e 1q "$GIT_DIR"/COMMIT_MSG) &&
603 git-update-ref -m "$rloga: $rlogm" HEAD $commit "$current" &&
604 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" &&
605 if test -f "$NEXT_INDEX"
606 then
607 mv "$NEXT_INDEX" "$THIS_INDEX"
608 else
609 : ;# happy
611 else
612 echo >&2 "* no commit message? aborting commit."
613 false
615 ret="$?"
616 rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
617 if test -d "$GIT_DIR/rr-cache"
618 then
619 git-rerere
622 if test "$ret" = 0
623 then
624 if test -x "$GIT_DIR"/hooks/post-commit
625 then
626 "$GIT_DIR"/hooks/post-commit
628 if test -z "$quiet"
629 then
630 echo "Created${initial_commit:+ initial} commit $commit"
631 git-diff-tree --shortstat --summary --root --no-commit-id HEAD --
635 exit "$ret"