Do not use perl in git-commit.sh
[git/git.git] / git-commit.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2005 Linus Torvalds
4 # Copyright (c) 2006 Junio C Hamano
5
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
10 git-rev-parse --verify HEAD >/dev/null 2>&1 || initial_commit=t
11 branch=$(GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD)
12
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
21
22 refuse_partial () {
23 echo >&2 "$1"
24 echo >&2 "You might have meant to say 'git commit -i paths...', perhaps?"
25 exit 1
26 }
27
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"
33 }
34
35 report () {
36 header="#
37 # $1:
38 # ($2)
39 #
40 "
41 trailer=""
42 while read status name newname
43 do
44 printf '%s' "$header"
45 header=""
46 trailer="#
47 "
48 case "$status" in
49 M ) echo "# modified: $name";;
50 D*) echo "# deleted: $name";;
51 T ) echo "# typechange: $name";;
52 C*) echo "# copied: $name -> $newname";;
53 R*) echo "# renamed: $name -> $newname";;
54 A*) echo "# new file: $name";;
55 U ) echo "# unmerged: $name";;
56 esac
57 done
58 printf '%s' "$trailer"
59 [ "$header" ]
60 }
61
62 run_status () {
63 (
64 # We always show status for the whole tree.
65 cd "$TOP"
66
67 IS_INITIAL="$initial_commit"
68 REFERENCE=HEAD
69 case "$amend" in
70 t)
71 # If we are amending the initial commit, there
72 # is no HEAD^1.
73 if git-rev-parse --verify "HEAD^1" >/dev/null 2>&1
74 then
75 REFERENCE="HEAD^1"
76 IS_INITIAL=
77 else
78 IS_INITIAL=t
79 fi
80 ;;
81 esac
82
83 # If TMP_INDEX is defined, that means we are doing
84 # "--only" partial commit, and that index file is used
85 # to build the tree for the commit. Otherwise, if
86 # NEXT_INDEX exists, that is the index file used to
87 # make the commit. Otherwise we are using as-is commit
88 # so the regular index file is what we use to compare.
89 if test '' != "$TMP_INDEX"
90 then
91 GIT_INDEX_FILE="$TMP_INDEX"
92 export GIT_INDEX_FILE
93 elif test -f "$NEXT_INDEX"
94 then
95 GIT_INDEX_FILE="$NEXT_INDEX"
96 export GIT_INDEX_FILE
97 fi
98
99 case "$branch" in
100 refs/heads/master) ;;
101 *) echo "# On branch $branch" ;;
102 esac
103
104 if test -z "$IS_INITIAL"
105 then
106 git-diff-index -M --cached --name-status \
107 --diff-filter=MDTCRA $REFERENCE |
108 sed -e '
109 s/\\/\\\\/g
110 s/ /\\ /g
111 ' |
112 report "Updated but not checked in" "will commit"
113 committable="$?"
114 else
115 echo '#
116 # Initial commit
117 #'
118 git-ls-files |
119 sed -e '
120 s/\\/\\\\/g
121 s/ /\\ /g
122 s/^/A /
123 ' |
124 report "Updated but not checked in" "will commit"
125
126 committable="$?"
127 fi
128
129 git-diff-files --name-status |
130 sed -e '
131 s/\\/\\\\/g
132 s/ /\\ /g
133 ' |
134 report "Changed but not updated" \
135 "use git-update-index to mark for commit"
136
137 option=""
138 if test -z "$untracked_files"; then
139 option="--directory --no-empty-directory"
140 fi
141 hdr_shown=
142 if test -f "$GIT_DIR/info/exclude"
143 then
144 git-ls-files --others $option \
145 --exclude-from="$GIT_DIR/info/exclude" \
146 --exclude-per-directory=.gitignore
147 else
148 git-ls-files --others $option \
149 --exclude-per-directory=.gitignore
150 fi |
151 while read line; do
152 if [ -z "$hdr_shown" ]; then
153 echo '#'
154 echo '# Untracked files:'
155 echo '# (use "git add" to add to commit)'
156 echo '#'
157 hdr_shown=1
158 fi
159 echo "# $line"
160 done
161
162 if test -n "$verbose" -a -z "$IS_INITIAL"
163 then
164 git-diff-index --cached -M -p --diff-filter=MDTCRA $REFERENCE
165 fi
166 case "$committable" in
167 0)
168 case "$amend" in
169 t)
170 echo "# No changes" ;;
171 *)
172 echo "nothing to commit" ;;
173 esac
174 exit 1 ;;
175 esac
176 exit 0
177 )
178 }
179
180 trap '
181 test -z "$TMP_INDEX" || {
182 test -f "$TMP_INDEX" && rm -f "$TMP_INDEX"
183 }
184 rm -f "$NEXT_INDEX"
185 ' 0
186
187 ################################################################
188 # Command line argument parsing and sanity checking
189
190 all=
191 also=
192 only=
193 logfile=
194 use_commit=
195 amend=
196 edit_flag=
197 no_edit=
198 log_given=
199 log_message=
200 verify=t
201 verbose=
202 signoff=
203 force_author=
204 only_include_assumed=
205 untracked_files=
206 while case "$#" in 0) break;; esac
207 do
208 case "$1" in
209 -F|--F|-f|--f|--fi|--fil|--file)
210 case "$#" in 1) usage ;; esac
211 shift
212 no_edit=t
213 log_given=t$log_given
214 logfile="$1"
215 shift
216 ;;
217 -F*|-f*)
218 no_edit=t
219 log_given=t$log_given
220 logfile=`expr "z$1" : 'z-[Ff]\(.*\)'`
221 shift
222 ;;
223 --F=*|--f=*|--fi=*|--fil=*|--file=*)
224 no_edit=t
225 log_given=t$log_given
226 logfile=`expr "z$1" : 'z-[^=]*=\(.*\)'`
227 shift
228 ;;
229 -a|--a|--al|--all)
230 all=t
231 shift
232 ;;
233 --au=*|--aut=*|--auth=*|--autho=*|--author=*)
234 force_author=`expr "z$1" : 'z-[^=]*=\(.*\)'`
235 shift
236 ;;
237 --au|--aut|--auth|--autho|--author)
238 case "$#" in 1) usage ;; esac
239 shift
240 force_author="$1"
241 shift
242 ;;
243 -e|--e|--ed|--edi|--edit)
244 edit_flag=t
245 shift
246 ;;
247 -i|--i|--in|--inc|--incl|--inclu|--includ|--include)
248 also=t
249 shift
250 ;;
251 -o|--o|--on|--onl|--only)
252 only=t
253 shift
254 ;;
255 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
256 case "$#" in 1) usage ;; esac
257 shift
258 log_given=m$log_given
259 if test "$log_message" = ''
260 then
261 log_message="$1"
262 else
263 log_message="$log_message
264
265 $1"
266 fi
267 no_edit=t
268 shift
269 ;;
270 -m*)
271 log_given=m$log_given
272 if test "$log_message" = ''
273 then
274 log_message=`expr "z$1" : 'z-m\(.*\)'`
275 else
276 log_message="$log_message
277
278 `expr "z$1" : 'z-m\(.*\)'`"
279 fi
280 no_edit=t
281 shift
282 ;;
283 --m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
284 log_given=m$log_given
285 if test "$log_message" = ''
286 then
287 log_message=`expr "z$1" : 'z-[^=]*=\(.*\)'`
288 else
289 log_message="$log_message
290
291 `expr "z$1" : 'zq-[^=]*=\(.*\)'`"
292 fi
293 no_edit=t
294 shift
295 ;;
296 -n|--n|--no|--no-|--no-v|--no-ve|--no-ver|--no-veri|--no-verif|--no-verify)
297 verify=
298 shift
299 ;;
300 --a|--am|--ame|--amen|--amend)
301 amend=t
302 log_given=t$log_given
303 use_commit=HEAD
304 shift
305 ;;
306 -c)
307 case "$#" in 1) usage ;; esac
308 shift
309 log_given=t$log_given
310 use_commit="$1"
311 no_edit=
312 shift
313 ;;
314 --ree=*|--reed=*|--reedi=*|--reedit=*|--reedit-=*|--reedit-m=*|\
315 --reedit-me=*|--reedit-mes=*|--reedit-mess=*|--reedit-messa=*|\
316 --reedit-messag=*|--reedit-message=*)
317 log_given=t$log_given
318 use_commit=`expr "z$1" : 'z-[^=]*=\(.*\)'`
319 no_edit=
320 shift
321 ;;
322 --ree|--reed|--reedi|--reedit|--reedit-|--reedit-m|--reedit-me|\
323 --reedit-mes|--reedit-mess|--reedit-messa|--reedit-messag|--reedit-message)
324 case "$#" in 1) usage ;; esac
325 shift
326 log_given=t$log_given
327 use_commit="$1"
328 no_edit=
329 shift
330 ;;
331 -C)
332 case "$#" in 1) usage ;; esac
333 shift
334 log_given=t$log_given
335 use_commit="$1"
336 no_edit=t
337 shift
338 ;;
339 --reu=*|--reus=*|--reuse=*|--reuse-=*|--reuse-m=*|--reuse-me=*|\
340 --reuse-mes=*|--reuse-mess=*|--reuse-messa=*|--reuse-messag=*|\
341 --reuse-message=*)
342 log_given=t$log_given
343 use_commit=`expr "z$1" : 'z-[^=]*=\(.*\)'`
344 no_edit=t
345 shift
346 ;;
347 --reu|--reus|--reuse|--reuse-|--reuse-m|--reuse-me|--reuse-mes|\
348 --reuse-mess|--reuse-messa|--reuse-messag|--reuse-message)
349 case "$#" in 1) usage ;; esac
350 shift
351 log_given=t$log_given
352 use_commit="$1"
353 no_edit=t
354 shift
355 ;;
356 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
357 signoff=t
358 shift
359 ;;
360 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
361 verbose=t
362 shift
363 ;;
364 -u|--u|--un|--unt|--untr|--untra|--untrac|--untrack|--untracke|--untracked|\
365 --untracked-|--untracked-f|--untracked-fi|--untracked-fil|--untracked-file|\
366 --untracked-files)
367 untracked_files=t
368 shift
369 ;;
370 --)
371 shift
372 break
373 ;;
374 -*)
375 usage
376 ;;
377 *)
378 break
379 ;;
380 esac
381 done
382 case "$edit_flag" in t) no_edit= ;; esac
383
384 ################################################################
385 # Sanity check options
386
387 case "$amend,$initial_commit" in
388 t,t)
389 die "You do not have anything to amend." ;;
390 t,)
391 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
392 die "You are in the middle of a merge -- cannot amend."
393 fi ;;
394 esac
395
396 case "$log_given" in
397 tt*)
398 die "Only one of -c/-C/-F can be used." ;;
399 *tm*|*mt*)
400 die "Option -m cannot be combined with -c/-C/-F." ;;
401 esac
402
403 case "$#,$also,$only,$amend" in
404 *,t,t,*)
405 die "Only one of --include/--only can be used." ;;
406 0,t,,* | 0,,t,)
407 die "No paths with --include/--only does not make sense." ;;
408 0,,t,t)
409 only_include_assumed="# Clever... amending the last one with dirty index." ;;
410 0,,,*)
411 ;;
412 *,,,*)
413 only_include_assumed="# Explicit paths specified without -i nor -o; assuming --only paths..."
414 also=
415 ;;
416 esac
417 unset only
418 case "$all,$also,$#" in
419 t,t,*)
420 die "Cannot use -a and -i at the same time." ;;
421 t,,[1-9]*)
422 die "Paths with -a does not make sense." ;;
423 ,t,0)
424 die "No paths with -i does not make sense." ;;
425 esac
426
427 ################################################################
428 # Prepare index to have a tree to be committed
429
430 TOP=`git-rev-parse --show-cdup`
431 if test -z "$TOP"
432 then
433 TOP=./
434 fi
435
436 case "$all,$also" in
437 t,)
438 save_index &&
439 (
440 cd "$TOP"
441 GIT_INDEX_FILE="$NEXT_INDEX"
442 export GIT_INDEX_FILE
443 git-diff-files --name-only -z |
444 git-update-index --remove -z --stdin
445 )
446 ;;
447 ,t)
448 save_index &&
449 git-ls-files --error-unmatch -- "$@" >/dev/null || exit
450
451 git-diff-files --name-only -z -- "$@" |
452 (
453 cd "$TOP"
454 GIT_INDEX_FILE="$NEXT_INDEX"
455 export GIT_INDEX_FILE
456 git-update-index --remove -z --stdin
457 )
458 ;;
459 ,)
460 case "$#" in
461 0)
462 ;; # commit as-is
463 *)
464 if test -f "$GIT_DIR/MERGE_HEAD"
465 then
466 refuse_partial "Cannot do a partial commit during a merge."
467 fi
468 TMP_INDEX="$GIT_DIR/tmp-index$$"
469 if test -z "$initial_commit"
470 then
471 # make sure index is clean at the specified paths, or
472 # they are additions.
473 dirty_in_index=`git-diff-index --cached --name-status \
474 --diff-filter=DMTU HEAD -- "$@"`
475 test -z "$dirty_in_index" ||
476 refuse_partial "Different in index and the last commit:
477 $dirty_in_index"
478 fi
479 commit_only=`git-ls-files --error-unmatch -- "$@"` || exit
480
481 # Build the temporary index and update the real index
482 # the same way.
483 if test -z "$initial_commit"
484 then
485 cp "$THIS_INDEX" "$TMP_INDEX"
486 GIT_INDEX_FILE="$TMP_INDEX" git-read-tree -m HEAD
487 else
488 rm -f "$TMP_INDEX"
489 fi || exit
490
491 echo "$commit_only" |
492 GIT_INDEX_FILE="$TMP_INDEX" \
493 git-update-index --add --remove --stdin &&
494
495 save_index &&
496 echo "$commit_only" |
497 (
498 GIT_INDEX_FILE="$NEXT_INDEX"
499 export GIT_INDEX_FILE
500 git-update-index --remove --stdin
501 ) || exit
502 ;;
503 esac
504 ;;
505 esac
506
507 ################################################################
508 # If we do as-is commit, the index file will be THIS_INDEX,
509 # otherwise NEXT_INDEX after we make this commit. We leave
510 # the index as is if we abort.
511
512 if test -f "$NEXT_INDEX"
513 then
514 USE_INDEX="$NEXT_INDEX"
515 else
516 USE_INDEX="$THIS_INDEX"
517 fi
518
519 GIT_INDEX_FILE="$USE_INDEX" \
520 git-update-index -q $unmerged_ok_if_status --refresh || exit
521
522 ################################################################
523 # If the request is status, just show it and exit.
524
525 case "$0" in
526 *status)
527 run_status
528 exit $?
529 esac
530
531 ################################################################
532 # Grab commit message, write out tree and make commit.
533
534 if test t = "$verify" && test -x "$GIT_DIR"/hooks/pre-commit
535 then
536 if test "$TMP_INDEX"
537 then
538 GIT_INDEX_FILE="$TMP_INDEX" "$GIT_DIR"/hooks/pre-commit
539 else
540 GIT_INDEX_FILE="$USE_INDEX" "$GIT_DIR"/hooks/pre-commit
541 fi || exit
542 fi
543
544 if test "$log_message" != ''
545 then
546 echo "$log_message"
547 elif test "$logfile" != ""
548 then
549 if test "$logfile" = -
550 then
551 test -t 0 &&
552 echo >&2 "(reading log message from standard input)"
553 cat
554 else
555 cat <"$logfile"
556 fi
557 elif test "$use_commit" != ""
558 then
559 git-cat-file commit "$use_commit" | sed -e '1,/^$/d'
560 elif test -f "$GIT_DIR/MERGE_HEAD" && test -f "$GIT_DIR/MERGE_MSG"
561 then
562 cat "$GIT_DIR/MERGE_MSG"
563 elif test -f "$GIT_DIR/SQUASH_MSG"
564 then
565 cat "$GIT_DIR/SQUASH_MSG"
566 fi | git-stripspace >"$GIT_DIR"/COMMIT_EDITMSG
567
568 case "$signoff" in
569 t)
570 {
571 echo
572 git-var GIT_COMMITTER_IDENT | sed -e '
573 s/>.*/>/
574 s/^/Signed-off-by: /
575 '
576 } >>"$GIT_DIR"/COMMIT_EDITMSG
577 ;;
578 esac
579
580 if test -f "$GIT_DIR/MERGE_HEAD" && test -z "$no_edit"; then
581 echo "#"
582 echo "# It looks like you may be committing a MERGE."
583 echo "# If this is not correct, please remove the file"
584 echo "# $GIT_DIR/MERGE_HEAD"
585 echo "# and try again"
586 echo "#"
587 fi >>"$GIT_DIR"/COMMIT_EDITMSG
588
589 # Author
590 if test '' != "$force_author"
591 then
592 GIT_AUTHOR_NAME=`expr "z$force_author" : 'z\(.*[^ ]\) *<.*'` &&
593 GIT_AUTHOR_EMAIL=`expr "z$force_author" : '.*\(<.*\)'` &&
594 test '' != "$GIT_AUTHOR_NAME" &&
595 test '' != "$GIT_AUTHOR_EMAIL" ||
596 die "malformed --author parameter"
597 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
598 elif test '' != "$use_commit"
599 then
600 pick_author_script='
601 /^author /{
602 s/'\''/'\''\\'\'\''/g
603 h
604 s/^author \([^<]*\) <[^>]*> .*$/\1/
605 s/'\''/'\''\'\'\''/g
606 s/.*/GIT_AUTHOR_NAME='\''&'\''/p
607
608 g
609 s/^author [^<]* <\([^>]*\)> .*$/\1/
610 s/'\''/'\''\'\'\''/g
611 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
612
613 g
614 s/^author [^<]* <[^>]*> \(.*\)$/\1/
615 s/'\''/'\''\'\'\''/g
616 s/.*/GIT_AUTHOR_DATE='\''&'\''/p
617
618 q
619 }
620 '
621 set_author_env=`git-cat-file commit "$use_commit" |
622 LANG=C LC_ALL=C sed -ne "$pick_author_script"`
623 eval "$set_author_env"
624 export GIT_AUTHOR_NAME
625 export GIT_AUTHOR_EMAIL
626 export GIT_AUTHOR_DATE
627 fi
628
629 PARENTS="-p HEAD"
630 if test -z "$initial_commit"
631 then
632 rloga='commit'
633 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
634 rloga='commit (merge)'
635 PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
636 elif test -n "$amend"; then
637 rloga='commit (amend)'
638 PARENTS=$(git-cat-file commit HEAD |
639 sed -n -e '/^$/q' -e 's/^parent /-p /p')
640 fi
641 current=$(git-rev-parse --verify HEAD)
642 else
643 if [ -z "$(git-ls-files)" ]; then
644 echo >&2 Nothing to commit
645 exit 1
646 fi
647 PARENTS=""
648 current=
649 rloga='commit (initial)'
650 fi
651
652 if test -z "$no_edit"
653 then
654 {
655 echo ""
656 echo "# Please enter the commit message for your changes."
657 echo "# (Comment lines starting with '#' will not be included)"
658 test -z "$only_include_assumed" || echo "$only_include_assumed"
659 run_status
660 } >>"$GIT_DIR"/COMMIT_EDITMSG
661 else
662 # we need to check if there is anything to commit
663 run_status >/dev/null
664 fi
665 if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" -a -z "$amend" ]
666 then
667 rm -f "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
668 run_status
669 exit 1
670 fi
671
672 case "$no_edit" in
673 '')
674 case "${VISUAL:-$EDITOR},$TERM" in
675 ,dumb)
676 echo >&2 "Terminal is dumb but no VISUAL nor EDITOR defined."
677 echo >&2 "Please supply the commit log message using either"
678 echo >&2 "-m or -F option. A boilerplate log message has"
679 echo >&2 "been prepared in $GIT_DIR/COMMIT_EDITMSG"
680 exit 1
681 ;;
682 esac
683 git-var GIT_AUTHOR_IDENT > /dev/null || die
684 git-var GIT_COMMITTER_IDENT > /dev/null || die
685 ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR/COMMIT_EDITMSG"
686 ;;
687 esac
688
689 case "$verify" in
690 t)
691 if test -x "$GIT_DIR"/hooks/commit-msg
692 then
693 "$GIT_DIR"/hooks/commit-msg "$GIT_DIR"/COMMIT_EDITMSG || exit
694 fi
695 esac
696
697 if test -z "$no_edit"
698 then
699 sed -e '
700 /^diff --git a\/.*/{
701 s///
702 q
703 }
704 /^#/d
705 ' "$GIT_DIR"/COMMIT_EDITMSG
706 else
707 cat "$GIT_DIR"/COMMIT_EDITMSG
708 fi |
709 git-stripspace >"$GIT_DIR"/COMMIT_MSG
710
711 if cnt=`grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
712 git-stripspace |
713 wc -l` &&
714 test 0 -lt $cnt
715 then
716 if test -z "$TMP_INDEX"
717 then
718 tree=$(GIT_INDEX_FILE="$USE_INDEX" git-write-tree)
719 else
720 tree=$(GIT_INDEX_FILE="$TMP_INDEX" git-write-tree) &&
721 rm -f "$TMP_INDEX"
722 fi &&
723 commit=$(cat "$GIT_DIR"/COMMIT_MSG | git-commit-tree $tree $PARENTS) &&
724 rlogm=$(sed -e 1q "$GIT_DIR"/COMMIT_MSG) &&
725 git-update-ref -m "$rloga: $rlogm" HEAD $commit $current &&
726 rm -f -- "$GIT_DIR/MERGE_HEAD" &&
727 if test -f "$NEXT_INDEX"
728 then
729 mv "$NEXT_INDEX" "$THIS_INDEX"
730 else
731 : ;# happy
732 fi
733 else
734 echo >&2 "* no commit message? aborting commit."
735 false
736 fi
737 ret="$?"
738 rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
739 if test -d "$GIT_DIR/rr-cache"
740 then
741 git-rerere
742 fi
743
744 if test -x "$GIT_DIR"/hooks/post-commit && test "$ret" = 0
745 then
746 "$GIT_DIR"/hooks/post-commit
747 fi
748 exit "$ret"