Use xmalloc instead of malloc
[git/platforms.git] / git-commit.sh
blob4cf3fab05cd3c3367173c86a65665ddfc1d89805
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
11 branch=$(GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD)
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 report () {
36 header="#
37 # $1:
38 # ($2)
41 trailer=""
42 while read status name newname
44 printf '%s' "$header"
45 header=""
46 trailer="#
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" ]
62 run_status () {
64 # We always show status for the whole tree.
65 cd "$TOP"
67 IS_INITIAL="$initial_commit"
68 REFERENCE=HEAD
69 case "$amend" in
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
81 esac
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
99 case "$branch" in
100 refs/heads/master) ;;
101 *) echo "# On branch $branch" ;;
102 esac
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
112 report "Updated but not checked in" "will commit"
113 committable="$?"
114 else
115 echo '#
116 # Initial commit
118 git-ls-files |
119 sed -e '
120 s/\\/\\\\/g
121 s/ /\\ /g
122 s/^/A /
124 report "Updated but not checked in" "will commit"
126 committable="$?"
129 git-diff-files --name-status |
130 sed -e '
131 s/\\/\\\\/g
132 s/ /\\ /g
134 report "Changed but not updated" \
135 "use git-update-index to mark for commit"
137 option=""
138 if test -z "$untracked_files"; then
139 option="--directory --no-empty-directory"
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
159 echo "# $line"
160 done
162 if test -n "$verbose" -a -z "$IS_INITIAL"
163 then
164 git-diff-index --cached -M -p --diff-filter=MDTCRA $REFERENCE
166 case "$committable" in
168 case "$amend" in
170 echo "# No changes" ;;
172 echo "nothing to commit" ;;
173 esac
174 exit 1 ;;
175 esac
176 exit 0
180 trap '
181 test -z "$TMP_INDEX" || {
182 test -f "$TMP_INDEX" && rm -f "$TMP_INDEX"
184 rm -f "$NEXT_INDEX"
187 ################################################################
188 # Command line argument parsing and sanity checking
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
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
217 -F*|-f*)
218 no_edit=t
219 log_given=t$log_given
220 logfile=`expr "z$1" : 'z-[Ff]\(.*\)'`
221 shift
223 --F=*|--f=*|--fi=*|--fil=*|--file=*)
224 no_edit=t
225 log_given=t$log_given
226 logfile=`expr "z$1" : 'z-[^=]*=\(.*\)'`
227 shift
229 -a|--a|--al|--all)
230 all=t
231 shift
233 --au=*|--aut=*|--auth=*|--autho=*|--author=*)
234 force_author=`expr "z$1" : 'z-[^=]*=\(.*\)'`
235 shift
237 --au|--aut|--auth|--autho|--author)
238 case "$#" in 1) usage ;; esac
239 shift
240 force_author="$1"
241 shift
243 -e|--e|--ed|--edi|--edit)
244 edit_flag=t
245 shift
247 -i|--i|--in|--inc|--incl|--inclu|--includ|--include)
248 also=t
249 shift
251 -o|--o|--on|--onl|--only)
252 only=t
253 shift
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
267 no_edit=t
268 shift
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
278 `expr "z$1" : 'z-m\(.*\)'`"
280 no_edit=t
281 shift
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
291 `expr "z$1" : 'zq-[^=]*=\(.*\)'`"
293 no_edit=t
294 shift
296 -n|--n|--no|--no-|--no-v|--no-ve|--no-ver|--no-veri|--no-verif|--no-verify)
297 verify=
298 shift
300 --a|--am|--ame|--amen|--amend)
301 amend=t
302 log_given=t$log_given
303 use_commit=HEAD
304 shift
307 case "$#" in 1) usage ;; esac
308 shift
309 log_given=t$log_given
310 use_commit="$1"
311 no_edit=
312 shift
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
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
332 case "$#" in 1) usage ;; esac
333 shift
334 log_given=t$log_given
335 use_commit="$1"
336 no_edit=t
337 shift
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
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
356 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
357 signoff=t
358 shift
360 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
361 verbose=t
362 shift
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
371 shift
372 break
375 usage
378 break
380 esac
381 done
382 case "$edit_flag" in t) no_edit= ;; esac
384 ################################################################
385 # Sanity check options
387 case "$amend,$initial_commit" in
388 t,t)
389 die "You do not have anything to amend." ;;
391 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
392 die "You are in the middle of a merge -- cannot amend."
393 fi ;;
394 esac
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
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,,,*)
412 *,,,*)
413 only_include_assumed="# Explicit paths specified without -i nor -o; assuming --only paths..."
414 also=
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
427 ################################################################
428 # Prepare index to have a tree to be committed
430 TOP=`git-rev-parse --show-cdup`
431 if test -z "$TOP"
432 then
433 TOP=./
436 case "$all,$also" in
438 save_index &&
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
448 save_index &&
449 git-ls-files --error-unmatch -- "$@" >/dev/null || exit
451 git-diff-files --name-only -z -- "$@" |
453 cd "$TOP"
454 GIT_INDEX_FILE="$NEXT_INDEX"
455 export GIT_INDEX_FILE
456 git-update-index --remove -z --stdin
460 case "$#" in
462 ;; # commit as-is
464 if test -f "$GIT_DIR/MERGE_HEAD"
465 then
466 refuse_partial "Cannot do a partial commit during a merge."
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"
479 commit_only=`git-ls-files --error-unmatch -- "$@"` || exit
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
491 echo "$commit_only" |
492 GIT_INDEX_FILE="$TMP_INDEX" \
493 git-update-index --add --remove --stdin &&
495 save_index &&
496 echo "$commit_only" |
498 GIT_INDEX_FILE="$NEXT_INDEX"
499 export GIT_INDEX_FILE
500 git-update-index --remove --stdin
501 ) || exit
503 esac
505 esac
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.
512 if test -f "$NEXT_INDEX"
513 then
514 USE_INDEX="$NEXT_INDEX"
515 else
516 USE_INDEX="$THIS_INDEX"
519 GIT_INDEX_FILE="$USE_INDEX" \
520 git-update-index -q $unmerged_ok_if_status --refresh || exit
522 ################################################################
523 # If the request is status, just show it and exit.
525 case "$0" in
526 *status)
527 run_status
528 exit $?
529 esac
531 ################################################################
532 # Grab commit message, write out tree and make commit.
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
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)"
554 else
555 cat <"$logfile"
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
568 case "$signoff" in
571 echo
572 git-var GIT_COMMITTER_IDENT | sed -e '
573 s/>.*/>/
574 s/^/Signed-off-by: /
576 } >>"$GIT_DIR"/COMMIT_EDITMSG
578 esac
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
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
604 s/^author \([^<]*\) <[^>]*> .*$/\1/
605 s/'\''/'\''\'\'\''/g
606 s/.*/GIT_AUTHOR_NAME='\''&'\''/p
609 s/^author [^<]* <\([^>]*\)> .*$/\1/
610 s/'\''/'\''\'\'\''/g
611 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
614 s/^author [^<]* <[^>]*> \(.*\)$/\1/
615 s/'\''/'\''\'\'\''/g
616 s/.*/GIT_AUTHOR_DATE='\''&'\''/p
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
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')
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
647 PARENTS=""
648 current=
649 rloga='commit (initial)'
652 if test -z "$no_edit"
653 then
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
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
672 case "$no_edit" in
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
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"
687 esac
689 case "$verify" in
691 if test -x "$GIT_DIR"/hooks/commit-msg
692 then
693 "$GIT_DIR"/hooks/commit-msg "$GIT_DIR"/COMMIT_EDITMSG || exit
695 esac
697 if test -z "$no_edit"
698 then
699 sed -e '
700 /^diff --git a\/.*/{
701 s///
704 /^#/d
705 ' "$GIT_DIR"/COMMIT_EDITMSG
706 else
707 cat "$GIT_DIR"/COMMIT_EDITMSG
708 fi |
709 git-stripspace >"$GIT_DIR"/COMMIT_MSG
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
733 else
734 echo >&2 "* no commit message? aborting commit."
735 false
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
744 if test -x "$GIT_DIR"/hooks/post-commit && test "$ret" = 0
745 then
746 "$GIT_DIR"/hooks/post-commit
748 exit "$ret"