Consistent message encoding while reusing log from an existing commit.
[git/gitweb.git] / git-commit.sh
blobb6387239dda2d9314b37eeabc2ff79bb4b013e01
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
9 require_work_tree
11 git-rev-parse --verify HEAD >/dev/null 2>&1 || initial_commit=t
13 case "$0" in
14 *status)
15 status_only=t
16 unmerged_ok_if_status=--unmerged ;;
17 *commit)
18 status_only=
19 unmerged_ok_if_status= ;;
20 esac
22 refuse_partial () {
23 echo >&2 "$1"
24 echo >&2 "You might have meant to say 'git commit -i paths...', perhaps?"
25 exit 1
28 THIS_INDEX="$GIT_DIR/index"
29 NEXT_INDEX="$GIT_DIR/next-index$$"
30 rm -f "$NEXT_INDEX"
31 save_index () {
32 cp -p "$THIS_INDEX" "$NEXT_INDEX"
35 run_status () {
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"
43 then
44 GIT_INDEX_FILE="$TMP_INDEX"
45 export GIT_INDEX_FILE
46 elif test -f "$NEXT_INDEX"
47 then
48 GIT_INDEX_FILE="$NEXT_INDEX"
49 export GIT_INDEX_FILE
52 case "$status_only" in
53 t) color= ;;
54 *) color=--nocolor ;;
55 esac
56 git-runstatus ${color} \
57 ${verbose:+--verbose} \
58 ${amend:+--amend} \
59 ${untracked_files:+--untracked}
62 trap '
63 test -z "$TMP_INDEX" || {
64 test -f "$TMP_INDEX" && rm -f "$TMP_INDEX"
66 rm -f "$NEXT_INDEX"
67 ' 0
69 ################################################################
70 # Command line argument parsing and sanity checking
72 all=
73 also=
74 only=
75 logfile=
76 use_commit=
77 amend=
78 edit_flag=
79 no_edit=
80 log_given=
81 log_message=
82 verify=t
83 quiet=
84 verbose=
85 signoff=
86 force_author=
87 only_include_assumed=
88 untracked_files=
89 while case "$#" in 0) break;; esac
91 case "$1" in
92 -F|--F|-f|--f|--fi|--fil|--file)
93 case "$#" in 1) usage ;; esac
94 shift
95 no_edit=t
96 log_given=t$log_given
97 logfile="$1"
98 shift
100 -F*|-f*)
101 no_edit=t
102 log_given=t$log_given
103 logfile=`expr "z$1" : 'z-[Ff]\(.*\)'`
104 shift
106 --F=*|--f=*|--fi=*|--fil=*|--file=*)
107 no_edit=t
108 log_given=t$log_given
109 logfile=`expr "z$1" : 'z-[^=]*=\(.*\)'`
110 shift
112 -a|--a|--al|--all)
113 all=t
114 shift
116 --au=*|--aut=*|--auth=*|--autho=*|--author=*)
117 force_author=`expr "z$1" : 'z-[^=]*=\(.*\)'`
118 shift
120 --au|--aut|--auth|--autho|--author)
121 case "$#" in 1) usage ;; esac
122 shift
123 force_author="$1"
124 shift
126 -e|--e|--ed|--edi|--edit)
127 edit_flag=t
128 shift
130 -i|--i|--in|--inc|--incl|--inclu|--includ|--include)
131 also=t
132 shift
134 -o|--o|--on|--onl|--only)
135 only=t
136 shift
138 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
139 case "$#" in 1) usage ;; esac
140 shift
141 log_given=m$log_given
142 if test "$log_message" = ''
143 then
144 log_message="$1"
145 else
146 log_message="$log_message
150 no_edit=t
151 shift
153 -m*)
154 log_given=m$log_given
155 if test "$log_message" = ''
156 then
157 log_message=`expr "z$1" : 'z-m\(.*\)'`
158 else
159 log_message="$log_message
161 `expr "z$1" : 'z-m\(.*\)'`"
163 no_edit=t
164 shift
166 --m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
167 log_given=m$log_given
168 if test "$log_message" = ''
169 then
170 log_message=`expr "z$1" : 'z-[^=]*=\(.*\)'`
171 else
172 log_message="$log_message
174 `expr "z$1" : 'zq-[^=]*=\(.*\)'`"
176 no_edit=t
177 shift
179 -n|--n|--no|--no-|--no-v|--no-ve|--no-ver|--no-veri|--no-verif|\
180 --no-verify)
181 verify=
182 shift
184 --a|--am|--ame|--amen|--amend)
185 amend=t
186 log_given=t$log_given
187 use_commit=HEAD
188 shift
191 case "$#" in 1) usage ;; esac
192 shift
193 log_given=t$log_given
194 use_commit="$1"
195 no_edit=
196 shift
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-[^=]*=\(.*\)'`
203 no_edit=
204 shift
206 --ree|--reed|--reedi|--reedit|--reedit-|--reedit-m|--reedit-me|\
207 --reedit-mes|--reedit-mess|--reedit-messa|--reedit-messag|\
208 --reedit-message)
209 case "$#" in 1) usage ;; esac
210 shift
211 log_given=t$log_given
212 use_commit="$1"
213 no_edit=
214 shift
217 case "$#" in 1) usage ;; esac
218 shift
219 log_given=t$log_given
220 use_commit="$1"
221 no_edit=t
222 shift
224 --reu=*|--reus=*|--reuse=*|--reuse-=*|--reuse-m=*|--reuse-me=*|\
225 --reuse-mes=*|--reuse-mess=*|--reuse-messa=*|--reuse-messag=*|\
226 --reuse-message=*)
227 log_given=t$log_given
228 use_commit=`expr "z$1" : 'z-[^=]*=\(.*\)'`
229 no_edit=t
230 shift
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
235 shift
236 log_given=t$log_given
237 use_commit="$1"
238 no_edit=t
239 shift
241 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
242 signoff=t
243 shift
245 -q|--q|--qu|--qui|--quie|--quiet)
246 quiet=t
247 shift
249 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
250 verbose=t
251 shift
253 -u|--u|--un|--unt|--untr|--untra|--untrac|--untrack|--untracke|\
254 --untracked|--untracked-|--untracked-f|--untracked-fi|--untracked-fil|\
255 --untracked-file|--untracked-files)
256 untracked_files=t
257 shift
260 shift
261 break
264 usage
267 break
269 esac
270 done
271 case "$edit_flag" in t) no_edit= ;; esac
273 ################################################################
274 # Sanity check options
276 case "$amend,$initial_commit" in
277 t,t)
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."
282 fi ;;
283 esac
285 case "$log_given" in
286 tt*)
287 die "Only one of -c/-C/-F can be used." ;;
288 *tm*|*mt*)
289 die "Option -m cannot be combined with -c/-C/-F." ;;
290 esac
292 case "$#,$also,$only,$amend" in
293 *,t,t,*)
294 die "Only one of --include/--only can be used." ;;
295 0,t,,* | 0,,t,)
296 die "No paths with --include/--only does not make sense." ;;
297 0,,t,t)
298 only_include_assumed="# Clever... amending the last one with dirty index." ;;
299 0,,,*)
301 *,,,*)
302 only_include_assumed="# Explicit paths specified without -i nor -o; assuming --only paths..."
303 also=
305 esac
306 unset only
307 case "$all,$also,$#" in
308 t,t,*)
309 die "Cannot use -a and -i at the same time." ;;
310 t,,[1-9]*)
311 die "Paths with -a does not make sense." ;;
312 ,t,0)
313 die "No paths with -i does not make sense." ;;
314 esac
316 ################################################################
317 # Prepare index to have a tree to be committed
319 TOP=`git-rev-parse --show-cdup`
320 if test -z "$TOP"
321 then
322 TOP=./
325 case "$all,$also" in
327 save_index &&
329 cd "$TOP"
330 GIT_INDEX_FILE="$NEXT_INDEX"
331 export GIT_INDEX_FILE
332 git-diff-files --name-only -z |
333 git-update-index --remove -z --stdin
337 save_index &&
338 git-ls-files --error-unmatch -- "$@" >/dev/null || exit
340 git-diff-files --name-only -z -- "$@" |
342 cd "$TOP"
343 GIT_INDEX_FILE="$NEXT_INDEX"
344 export GIT_INDEX_FILE
345 git-update-index --remove -z --stdin
349 case "$#" in
351 ;; # commit as-is
353 if test -f "$GIT_DIR/MERGE_HEAD"
354 then
355 refuse_partial "Cannot do a partial commit during a merge."
357 TMP_INDEX="$GIT_DIR/tmp-index$$"
358 commit_only=`git-ls-files --error-unmatch -- "$@"` || exit
360 # Build a temporary index and update the real index
361 # the same way.
362 if test -z "$initial_commit"
363 then
364 cp "$THIS_INDEX" "$TMP_INDEX"
365 GIT_INDEX_FILE="$TMP_INDEX" git-read-tree -m HEAD
366 else
367 rm -f "$TMP_INDEX"
368 fi || exit
370 echo "$commit_only" |
371 GIT_INDEX_FILE="$TMP_INDEX" \
372 git-update-index --add --remove --stdin &&
374 save_index &&
375 echo "$commit_only" |
377 GIT_INDEX_FILE="$NEXT_INDEX"
378 export GIT_INDEX_FILE
379 git-update-index --remove --stdin
380 ) || exit
382 esac
384 esac
386 ################################################################
387 # If we do as-is commit, the index file will be THIS_INDEX,
388 # otherwise NEXT_INDEX after we make this commit. We leave
389 # the index as is if we abort.
391 if test -f "$NEXT_INDEX"
392 then
393 USE_INDEX="$NEXT_INDEX"
394 else
395 USE_INDEX="$THIS_INDEX"
398 GIT_INDEX_FILE="$USE_INDEX" \
399 git-update-index -q $unmerged_ok_if_status --refresh || exit
401 ################################################################
402 # If the request is status, just show it and exit.
404 case "$0" in
405 *status)
406 run_status
407 exit $?
408 esac
410 ################################################################
411 # Grab commit message, write out tree and make commit.
413 if test t = "$verify" && test -x "$GIT_DIR"/hooks/pre-commit
414 then
415 if test "$TMP_INDEX"
416 then
417 GIT_INDEX_FILE="$TMP_INDEX" "$GIT_DIR"/hooks/pre-commit
418 else
419 GIT_INDEX_FILE="$USE_INDEX" "$GIT_DIR"/hooks/pre-commit
420 fi || exit
423 if test "$log_message" != ''
424 then
425 echo "$log_message"
426 elif test "$logfile" != ""
427 then
428 if test "$logfile" = -
429 then
430 test -t 0 &&
431 echo >&2 "(reading log message from standard input)"
433 else
434 cat <"$logfile"
436 elif test "$use_commit" != ""
437 then
438 encoding=$(git repo-config i18n.commitencoding || echo UTF-8)
439 git show -s --pretty=raw --encoding="$encoding" "$use_commit" |
440 sed -e '1,/^$/d' -e 's/^ //'
441 elif test -f "$GIT_DIR/MERGE_MSG"
442 then
443 cat "$GIT_DIR/MERGE_MSG"
444 elif test -f "$GIT_DIR/SQUASH_MSG"
445 then
446 cat "$GIT_DIR/SQUASH_MSG"
447 fi | git-stripspace >"$GIT_DIR"/COMMIT_EDITMSG
449 case "$signoff" in
452 echo
453 git-var GIT_COMMITTER_IDENT | sed -e '
454 s/>.*/>/
455 s/^/Signed-off-by: /
457 } >>"$GIT_DIR"/COMMIT_EDITMSG
459 esac
461 if test -f "$GIT_DIR/MERGE_HEAD" && test -z "$no_edit"; then
462 echo "#"
463 echo "# It looks like you may be committing a MERGE."
464 echo "# If this is not correct, please remove the file"
465 echo "# $GIT_DIR/MERGE_HEAD"
466 echo "# and try again"
467 echo "#"
468 fi >>"$GIT_DIR"/COMMIT_EDITMSG
470 # Author
471 if test '' != "$force_author"
472 then
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
479 elif test '' != "$use_commit"
480 then
481 pick_author_script='
482 /^author /{
483 s/'\''/'\''\\'\'\''/g
485 s/^author \([^<]*\) <[^>]*> .*$/\1/
486 s/'\''/'\''\'\'\''/g
487 s/.*/GIT_AUTHOR_NAME='\''&'\''/p
490 s/^author [^<]* <\([^>]*\)> .*$/\1/
491 s/'\''/'\''\'\'\''/g
492 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
495 s/^author [^<]* <[^>]*> \(.*\)$/\1/
496 s/'\''/'\''\'\'\''/g
497 s/.*/GIT_AUTHOR_DATE='\''&'\''/p
502 encoding=$(git repo-config i18n.commitencoding || echo UTF-8)
503 set_author_env=`git show -s --pretty=raw --encoding="$encoding" "$use_commit" |
504 LANG=C LC_ALL=C sed -ne "$pick_author_script"`
505 eval "$set_author_env"
506 export GIT_AUTHOR_NAME
507 export GIT_AUTHOR_EMAIL
508 export GIT_AUTHOR_DATE
511 PARENTS="-p HEAD"
512 if test -z "$initial_commit"
513 then
514 rloga='commit'
515 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
516 rloga='commit (merge)'
517 PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
518 elif test -n "$amend"; then
519 rloga='commit (amend)'
520 PARENTS=$(git-cat-file commit HEAD |
521 sed -n -e '/^$/q' -e 's/^parent /-p /p')
523 current="$(git-rev-parse --verify HEAD)"
524 else
525 if [ -z "$(git-ls-files)" ]; then
526 echo >&2 'nothing to commit (use "git add file1 file2" to include for commit)'
527 exit 1
529 PARENTS=""
530 rloga='commit (initial)'
531 current=''
534 if test -z "$no_edit"
535 then
537 echo ""
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"
541 run_status
542 } >>"$GIT_DIR"/COMMIT_EDITMSG
543 else
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" ]
548 then
549 rm -f "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
550 run_status
551 exit 1
554 case "$no_edit" in
556 case "${VISUAL:-$EDITOR},$TERM" in
557 ,dumb)
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"
562 exit 1
564 esac
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"
569 esac
571 case "$verify" in
573 if test -x "$GIT_DIR"/hooks/commit-msg
574 then
575 "$GIT_DIR"/hooks/commit-msg "$GIT_DIR"/COMMIT_EDITMSG || exit
577 esac
579 if test -z "$no_edit"
580 then
581 sed -e '
582 /^diff --git a\/.*/{
583 s///
586 /^#/d
587 ' "$GIT_DIR"/COMMIT_EDITMSG
588 else
589 cat "$GIT_DIR"/COMMIT_EDITMSG
590 fi |
591 git-stripspace >"$GIT_DIR"/COMMIT_MSG
593 if cnt=`grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
594 git-stripspace |
595 wc -l` &&
596 test 0 -lt $cnt
597 then
598 if test -z "$TMP_INDEX"
599 then
600 tree=$(GIT_INDEX_FILE="$USE_INDEX" git-write-tree)
601 else
602 tree=$(GIT_INDEX_FILE="$TMP_INDEX" git-write-tree) &&
603 rm -f "$TMP_INDEX"
604 fi &&
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 "$rloga: $rlogm" HEAD $commit "$current" &&
608 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" &&
609 if test -f "$NEXT_INDEX"
610 then
611 mv "$NEXT_INDEX" "$THIS_INDEX"
612 else
613 : ;# happy
615 else
616 echo >&2 "* no commit message? aborting commit."
617 false
619 ret="$?"
620 rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
621 if test -d "$GIT_DIR/rr-cache"
622 then
623 git-rerere
626 if test "$ret" = 0
627 then
628 if test -x "$GIT_DIR"/hooks/post-commit
629 then
630 "$GIT_DIR"/hooks/post-commit
632 if test -z "$quiet"
633 then
634 echo "Created${initial_commit:+ initial} commit $commit"
635 git-diff-tree --shortstat --summary --root --no-commit-id HEAD --
639 exit "$ret"