Install man3 manpages to $PREFIX/share/man/man3 even for site installs
[git/dscho.git] / git-commit.sh
blobd7e7028c15aeceaf2ba64a82ee9fc15f54b20eaa
1 #!/bin/sh
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>...]'
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
17 *commit)
18 status_only=
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 interactive=
75 only=
76 logfile=
77 use_commit=
78 amend=
79 edit_flag=
80 no_edit=
81 log_given=
82 log_message=
83 verify=t
84 quiet=
85 verbose=
86 signoff=
87 force_author=
88 only_include_assumed=
89 untracked_files=
90 templatefile="`git config commit.template`"
91 while case "$#" in 0) break;; esac
93 case "$1" in
94 -F|--F|-f|--f|--fi|--fil|--file)
95 case "$#" in 1) usage ;; esac
96 shift
97 no_edit=t
98 log_given=t$log_given
99 logfile="$1"
100 shift
102 -F*|-f*)
103 no_edit=t
104 log_given=t$log_given
105 logfile=`expr "z$1" : 'z-[Ff]\(.*\)'`
106 shift
108 --F=*|--f=*|--fi=*|--fil=*|--file=*)
109 no_edit=t
110 log_given=t$log_given
111 logfile=`expr "z$1" : 'z-[^=]*=\(.*\)'`
112 shift
114 -a|--a|--al|--all)
115 all=t
116 shift
118 --au=*|--aut=*|--auth=*|--autho=*|--author=*)
119 force_author=`expr "z$1" : 'z-[^=]*=\(.*\)'`
120 shift
122 --au|--aut|--auth|--autho|--author)
123 case "$#" in 1) usage ;; esac
124 shift
125 force_author="$1"
126 shift
128 -e|--e|--ed|--edi|--edit)
129 edit_flag=t
130 shift
132 -i|--i|--in|--inc|--incl|--inclu|--includ|--include)
133 also=t
134 shift
136 --int|--inte|--inter|--intera|--interac|--interact|--interacti|\
137 --interactiv|--interactive)
138 interactive=t
139 shift
141 -o|--o|--on|--onl|--only)
142 only=t
143 shift
145 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
146 case "$#" in 1) usage ;; esac
147 shift
148 log_given=m$log_given
149 if test "$log_message" = ''
150 then
151 log_message="$1"
152 else
153 log_message="$log_message
157 no_edit=t
158 shift
160 -m*)
161 log_given=m$log_given
162 if test "$log_message" = ''
163 then
164 log_message=`expr "z$1" : 'z-m\(.*\)'`
165 else
166 log_message="$log_message
168 `expr "z$1" : 'z-m\(.*\)'`"
170 no_edit=t
171 shift
173 --m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
174 log_given=m$log_given
175 if test "$log_message" = ''
176 then
177 log_message=`expr "z$1" : 'z-[^=]*=\(.*\)'`
178 else
179 log_message="$log_message
181 `expr "z$1" : 'zq-[^=]*=\(.*\)'`"
183 no_edit=t
184 shift
186 -n|--n|--no|--no-|--no-v|--no-ve|--no-ver|--no-veri|--no-verif|\
187 --no-verify)
188 verify=
189 shift
191 --a|--am|--ame|--amen|--amend)
192 amend=t
193 use_commit=HEAD
194 shift
197 case "$#" in 1) usage ;; esac
198 shift
199 log_given=t$log_given
200 use_commit="$1"
201 no_edit=
202 shift
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-[^=]*=\(.*\)'`
209 no_edit=
210 shift
212 --ree|--reed|--reedi|--reedit|--reedit-|--reedit-m|--reedit-me|\
213 --reedit-mes|--reedit-mess|--reedit-messa|--reedit-messag|\
214 --reedit-message)
215 case "$#" in 1) usage ;; esac
216 shift
217 log_given=t$log_given
218 use_commit="$1"
219 no_edit=
220 shift
223 case "$#" in 1) usage ;; esac
224 shift
225 log_given=t$log_given
226 use_commit="$1"
227 no_edit=t
228 shift
230 --reu=*|--reus=*|--reuse=*|--reuse-=*|--reuse-m=*|--reuse-me=*|\
231 --reuse-mes=*|--reuse-mess=*|--reuse-messa=*|--reuse-messag=*|\
232 --reuse-message=*)
233 log_given=t$log_given
234 use_commit=`expr "z$1" : 'z-[^=]*=\(.*\)'`
235 no_edit=t
236 shift
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
241 shift
242 log_given=t$log_given
243 use_commit="$1"
244 no_edit=t
245 shift
247 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
248 signoff=t
249 shift
251 -t|--t|--te|--tem|--temp|--templ|--templa|--templat|--template)
252 case "$#" in 1) usage ;; esac
253 shift
254 templatefile="$1"
255 no_edit=
256 shift
258 -q|--q|--qu|--qui|--quie|--quiet)
259 quiet=t
260 shift
262 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
263 verbose=t
264 shift
266 -u|--u|--un|--unt|--untr|--untra|--untrac|--untrack|--untracke|\
267 --untracked|--untracked-|--untracked-f|--untracked-fi|--untracked-fil|\
268 --untracked-file|--untracked-files)
269 untracked_files=t
270 shift
273 shift
274 break
277 usage
280 break
282 esac
283 done
284 case "$edit_flag" in t) no_edit= ;; esac
286 ################################################################
287 # Sanity check options
289 case "$amend,$initial_commit" in
290 t,t)
291 die "You do not have anything to amend." ;;
293 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
294 die "You are in the middle of a merge -- cannot amend."
295 fi ;;
296 esac
298 case "$log_given" in
299 tt*)
300 die "Only one of -c/-C/-F can be used." ;;
301 *tm*|*mt*)
302 die "Option -m cannot be combined with -c/-C/-F." ;;
303 esac
305 case "$#,$also,$only,$amend" in
306 *,t,t,*)
307 die "Only one of --include/--only can be used." ;;
308 0,t,,* | 0,,t,)
309 die "No paths with --include/--only does not make sense." ;;
310 0,,t,t)
311 only_include_assumed="# Clever... amending the last one with dirty index." ;;
312 0,,,*)
314 *,,,*)
315 only_include_assumed="# Explicit paths specified without -i nor -o; assuming --only paths..."
316 also=
318 esac
319 unset only
320 case "$all,$interactive,$also,$#" in
321 *t,*t,*)
322 die "Cannot use -a, --interactive or -i at the same time." ;;
323 t,,[1-9]*)
324 die "Paths with -a does not make sense." ;;
325 ,t,[1-9]*)
326 die "Paths with --interactive does not make sense." ;;
327 ,,t,0)
328 die "No paths with -i does not make sense." ;;
329 esac
331 if test ! -z "$templatefile" -a -z "$log_given"
332 then
333 if test ! -f "$templatefile"
334 then
335 die "Commit template file does not exist."
339 ################################################################
340 # Prepare index to have a tree to be committed
342 case "$all,$also" in
344 if test ! -f "$THIS_INDEX"
345 then
346 die 'nothing to commit (use "git add file1 file2" to include for commit)'
348 save_index &&
350 cd_to_toplevel &&
351 GIT_INDEX_FILE="$NEXT_INDEX" &&
352 export GIT_INDEX_FILE &&
353 git diff-files --name-only -z |
354 git update-index --remove -z --stdin
355 ) || exit
358 save_index &&
359 git ls-files --error-unmatch -- "$@" >/dev/null || exit
361 git diff-files --name-only -z -- "$@" |
363 cd_to_toplevel &&
364 GIT_INDEX_FILE="$NEXT_INDEX" &&
365 export GIT_INDEX_FILE &&
366 git update-index --remove -z --stdin
367 ) || exit
370 if test "$interactive" = t; then
371 git add --interactive || exit
373 case "$#" in
375 ;; # commit as-is
377 if test -f "$GIT_DIR/MERGE_HEAD"
378 then
379 refuse_partial "Cannot do a partial commit during a merge."
381 TMP_INDEX="$GIT_DIR/tmp-index$$"
382 commit_only=`git ls-files --error-unmatch -- "$@"` || exit
384 # Build a temporary index and update the real index
385 # the same way.
386 if test -z "$initial_commit"
387 then
388 GIT_INDEX_FILE="$THIS_INDEX" \
389 git read-tree --index-output="$TMP_INDEX" -i -m HEAD
390 else
391 rm -f "$TMP_INDEX"
392 fi || exit
394 printf '%s\n' "$commit_only" |
395 GIT_INDEX_FILE="$TMP_INDEX" \
396 git update-index --add --remove --stdin &&
398 save_index &&
399 printf '%s\n' "$commit_only" |
401 GIT_INDEX_FILE="$NEXT_INDEX"
402 export GIT_INDEX_FILE
403 git update-index --remove --stdin
404 ) || exit
406 esac
408 esac
410 ################################################################
411 # If we do as-is commit, the index file will be THIS_INDEX,
412 # otherwise NEXT_INDEX after we make this commit. We leave
413 # the index as is if we abort.
415 if test -f "$NEXT_INDEX"
416 then
417 USE_INDEX="$NEXT_INDEX"
418 else
419 USE_INDEX="$THIS_INDEX"
422 case "$status_only" in
424 # This will silently fail in a read-only repository, which is
425 # what we want.
426 GIT_INDEX_FILE="$USE_INDEX" git update-index -q --unmerged --refresh
427 run_status
428 exit $?
431 GIT_INDEX_FILE="$USE_INDEX" git update-index -q --refresh || exit
433 esac
435 ################################################################
436 # Grab commit message, write out tree and make commit.
438 if test t = "$verify" && test -x "$GIT_DIR"/hooks/pre-commit
439 then
440 if test "$TMP_INDEX"
441 then
442 GIT_INDEX_FILE="$TMP_INDEX" "$GIT_DIR"/hooks/pre-commit
443 else
444 GIT_INDEX_FILE="$USE_INDEX" "$GIT_DIR"/hooks/pre-commit
445 fi || exit
448 if test "$log_message" != ''
449 then
450 printf '%s\n' "$log_message"
451 elif test "$logfile" != ""
452 then
453 if test "$logfile" = -
454 then
455 test -t 0 &&
456 echo >&2 "(reading log message from standard input)"
458 else
459 cat <"$logfile"
461 elif test "$use_commit" != ""
462 then
463 encoding=$(git config i18n.commitencoding || echo UTF-8)
464 git show -s --pretty=raw --encoding="$encoding" "$use_commit" |
465 sed -e '1,/^$/d' -e 's/^ //'
466 elif test -f "$GIT_DIR/MERGE_MSG"
467 then
468 cat "$GIT_DIR/MERGE_MSG"
469 elif test -f "$GIT_DIR/SQUASH_MSG"
470 then
471 cat "$GIT_DIR/SQUASH_MSG"
472 elif test "$templatefile" != ""
473 then
474 cat "$templatefile"
475 fi | git stripspace >"$GIT_DIR"/COMMIT_EDITMSG
477 case "$signoff" in
479 sign=$(git-var GIT_COMMITTER_IDENT | sed -e '
480 s/>.*/>/
481 s/^/Signed-off-by: /
483 blank_before_signoff=
484 tail -n 1 "$GIT_DIR"/COMMIT_EDITMSG |
485 grep 'Signed-off-by:' >/dev/null || blank_before_signoff='
487 tail -n 1 "$GIT_DIR"/COMMIT_EDITMSG |
488 grep "$sign"$ >/dev/null ||
489 printf '%s%s\n' "$blank_before_signoff" "$sign" \
490 >>"$GIT_DIR"/COMMIT_EDITMSG
492 esac
494 if test -f "$GIT_DIR/MERGE_HEAD" && test -z "$no_edit"; then
495 echo "#"
496 echo "# It looks like you may be committing a MERGE."
497 echo "# If this is not correct, please remove the file"
498 printf '%s\n' "# $GIT_DIR/MERGE_HEAD"
499 echo "# and try again"
500 echo "#"
501 fi >>"$GIT_DIR"/COMMIT_EDITMSG
503 # Author
504 if test '' != "$use_commit"
505 then
506 eval "$(get_author_ident_from_commit "$use_commit")"
507 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
509 if test '' != "$force_author"
510 then
511 GIT_AUTHOR_NAME=`expr "z$force_author" : 'z\(.*[^ ]\) *<.*'` &&
512 GIT_AUTHOR_EMAIL=`expr "z$force_author" : '.*\(<.*\)'` &&
513 test '' != "$GIT_AUTHOR_NAME" &&
514 test '' != "$GIT_AUTHOR_EMAIL" ||
515 die "malformed --author parameter"
516 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
519 PARENTS="-p HEAD"
520 if test -z "$initial_commit"
521 then
522 rloga='commit'
523 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
524 rloga='commit (merge)'
525 PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
526 elif test -n "$amend"; then
527 rloga='commit (amend)'
528 PARENTS=$(git cat-file commit HEAD |
529 sed -n -e '/^$/q' -e 's/^parent /-p /p')
531 current="$(git rev-parse --verify HEAD)"
532 else
533 if [ -z "$(git ls-files)" ]; then
534 echo >&2 'nothing to commit (use "git add file1 file2" to include for commit)'
535 exit 1
537 PARENTS=""
538 rloga='commit (initial)'
539 current=''
541 set_reflog_action "$rloga"
543 if test -z "$no_edit"
544 then
546 echo ""
547 echo "# Please enter the commit message for your changes."
548 echo "# (Comment lines starting with '#' will not be included)"
549 test -z "$only_include_assumed" || echo "$only_include_assumed"
550 run_status
551 } >>"$GIT_DIR"/COMMIT_EDITMSG
552 else
553 # we need to check if there is anything to commit
554 run_status >/dev/null
556 if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" -a -z "$amend" ]
557 then
558 rm -f "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
559 run_status
560 exit 1
563 case "$no_edit" in
565 git-var GIT_AUTHOR_IDENT > /dev/null || die
566 git-var GIT_COMMITTER_IDENT > /dev/null || die
567 git_editor "$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 # Test whether the commit message has any content we didn't supply.
594 have_commitmsg=
595 grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
596 git stripspace > "$GIT_DIR"/COMMIT_BAREMSG
598 # Is the commit message totally empty?
599 if test -s "$GIT_DIR"/COMMIT_BAREMSG
600 then
601 if test "$templatefile" != ""
602 then
603 # Test whether this is just the unaltered template.
604 if cnt=`sed -e '/^#/d' < "$templatefile" |
605 git stripspace |
606 diff "$GIT_DIR"/COMMIT_BAREMSG - |
607 wc -l` &&
608 test 0 -lt $cnt
609 then
610 have_commitmsg=t
612 else
613 # No template, so the content in the commit message must
614 # have come from the user.
615 have_commitmsg=t
619 rm -f "$GIT_DIR"/COMMIT_BAREMSG
621 if test "$have_commitmsg" = "t"
622 then
623 if test -z "$TMP_INDEX"
624 then
625 tree=$(GIT_INDEX_FILE="$USE_INDEX" git write-tree)
626 else
627 tree=$(GIT_INDEX_FILE="$TMP_INDEX" git write-tree) &&
628 rm -f "$TMP_INDEX"
629 fi &&
630 commit=$(git commit-tree $tree $PARENTS <"$GIT_DIR/COMMIT_MSG") &&
631 rlogm=$(sed -e 1q "$GIT_DIR"/COMMIT_MSG) &&
632 git update-ref -m "$GIT_REFLOG_ACTION: $rlogm" HEAD $commit "$current" &&
633 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" &&
634 if test -f "$NEXT_INDEX"
635 then
636 mv "$NEXT_INDEX" "$THIS_INDEX"
637 else
638 : ;# happy
640 else
641 echo >&2 "* no commit message? aborting commit."
642 false
644 ret="$?"
645 rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
647 cd_to_toplevel
649 git rerere
651 if test "$ret" = 0
652 then
653 if test -x "$GIT_DIR"/hooks/post-commit
654 then
655 "$GIT_DIR"/hooks/post-commit
657 if test -z "$quiet"
658 then
659 commit=`git diff-tree --always --shortstat --pretty="format:%h: %s"\
660 --summary --root HEAD --`
661 echo "Created${initial_commit:+ initial} commit $commit"
665 exit "$ret"