Merge branch 'jc/empty-commit'
[git/jnareb-git/bp-gitweb.git] / git-commit.sh
blobe6793bd67e42375f33b900e27f04b9af14d6f27f
1 #!/bin/sh
3 # Copyright (c) 2005 Linus Torvalds
4 # Copyright (c) 2006 Junio C Hamano
6 USAGE='[-a] [-i] [-s] [-v] [--no-verify] [-m <message> | -F <logfile> | (-C|-c) <commit>] [-e] [--author <author>] [<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 "$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 # If TMP_INDEX is defined, that means we are doing
68 # "--only" partial commit, and that index file is used
69 # to build the tree for the commit. Otherwise, if
70 # NEXT_INDEX exists, that is the index file used to
71 # make the commit. Otherwise we are using as-is commit
72 # so the regular index file is what we use to compare.
73 if test '' != "$TMP_INDEX"
74 then
75 GIT_INDEX_FILE="$TMP_INDEX"
76 export GIT_INDEX_FILE
77 elif test -f "$NEXT_INDEX"
78 then
79 GIT_INDEX_FILE="$NEXT_INDEX"
80 export GIT_INDEX_FILE
83 case "$branch" in
84 refs/heads/master) ;;
85 *) echo "# On branch $branch" ;;
86 esac
88 if test -z "$initial_commit"
89 then
90 if test -z "$verbose"
91 then
92 git-diff-index -M --cached --name-status \
93 --diff-filter=MDTCRA HEAD |
94 sed -e '
95 s/\\/\\\\/g
96 s/ /\\ /g
97 ' |
98 report "Updated but not checked in" "will commit"
99 else
100 if git-diff-index --cached -M -p --diff-filter=MDTCRA HEAD |
101 grep .
102 then
103 false
104 else
105 true
108 committable="$?"
109 else
110 echo '#
111 # Initial commit
113 git-ls-files |
114 sed -e '
115 s/\\/\\\\/g
116 s/ /\\ /g
117 s/^/A /
119 report "Updated but not checked in" "will commit"
121 committable="$?"
124 git-diff-files --name-status |
125 sed -e '
126 s/\\/\\\\/g
127 s/ /\\ /g
129 report "Changed but not updated" \
130 "use git-update-index to mark for commit"
132 if test -f "$GIT_DIR/info/exclude"
133 then
134 git-ls-files -z --others --directory \
135 --exclude-from="$GIT_DIR/info/exclude" \
136 --exclude-per-directory=.gitignore
137 else
138 git-ls-files -z --others --directory \
139 --exclude-per-directory=.gitignore
140 fi |
141 perl -e '$/ = "\0";
142 my $shown = 0;
143 while (<>) {
144 chomp;
145 s|\\|\\\\|g;
146 s|\t|\\t|g;
147 s|\n|\\n|g;
148 s/^/# /;
149 if (!$shown) {
150 print "#\n# Untracked files:\n";
151 print "# (use \"git add\" to add to commit)\n";
152 print "#\n";
153 $shown = 1;
155 print "$_\n";
158 case "$committable" in
160 echo "nothing to commit"
161 exit 1
162 esac
163 exit 0
167 trap '
168 test -z "$TMP_INDEX" || {
169 test -f "$TMP_INDEX" && rm -f "$TMP_INDEX"
171 rm -f "$NEXT_INDEX"
174 ################################################################
175 # Command line argument parsing and sanity checking
177 all=
178 also=
179 only=
180 logfile=
181 use_commit=
182 no_edit=
183 log_given=
184 log_message=
185 verify=t
186 verbose=
187 signoff=
188 force_author=
189 while case "$#" in 0) break;; esac
191 case "$1" in
192 -F|--F|-f|--f|--fi|--fil|--file)
193 case "$#" in 1) usage ;; esac
194 shift
195 no_edit=t
196 log_given=t$log_given
197 logfile="$1"
198 shift
200 -F*|-f*)
201 no_edit=t
202 log_given=t$log_given
203 logfile=`expr "$1" : '-[Ff]\(.*\)'`
204 shift
206 --F=*|--f=*|--fi=*|--fil=*|--file=*)
207 no_edit=t
208 log_given=t$log_given
209 logfile=`expr "$1" : '-[^=]*=\(.*\)'`
210 shift
212 -a|--a|--al|--all)
213 all=t
214 shift
216 --au=*|--aut=*|--auth=*|--autho=*|--author=*)
217 force_author=`expr "$1" : '-[^=]*=\(.*\)'`
218 shift
220 --au|--aut|--auth|--autho|--author)
221 case "$#" in 1) usage ;; esac
222 shift
223 force_author="$1"
224 shift
226 -e|--e|--ed|--edi|--edit)
227 no_edit=
228 shift
230 -i|--i|--in|--inc|--incl|--inclu|--includ|--include)
231 also=t
232 shift
234 -o|--o|--on|--onl|--only)
235 only=t
236 shift
238 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
239 case "$#" in 1) usage ;; esac
240 shift
241 log_given=t$log_given
242 log_message="$1"
243 no_edit=t
244 shift
246 -m*)
247 log_given=t$log_given
248 log_message=`expr "$1" : '-m\(.*\)'`
249 no_edit=t
250 shift
252 --m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
253 log_given=t$log_given
254 log_message=`expr "$1" : '-[^=]*=\(.*\)'`
255 no_edit=t
256 shift
258 -n|--n|--no|--no-|--no-v|--no-ve|--no-ver|--no-veri|--no-verif|--no-verify)
259 verify=
260 shift
263 case "$#" in 1) usage ;; esac
264 shift
265 log_given=t$log_given
266 use_commit="$1"
267 no_edit=
268 shift
270 --ree=*|--reed=*|--reedi=*|--reedit=*|--reedit-=*|--reedit-m=*|\
271 --reedit-me=*|--reedit-mes=*|--reedit-mess=*|--reedit-messa=*|\
272 --reedit-messag=*|--reedit-message=*)
273 log_given=t$log_given
274 use_commit=`expr "$1" : '-[^=]*=\(.*\)'`
275 no_edit=
276 shift
278 --ree|--reed|--reedi|--reedit|--reedit-|--reedit-m|--reedit-me|\
279 --reedit-mes|--reedit-mess|--reedit-messa|--reedit-messag|--reedit-message)
280 case "$#" in 1) usage ;; esac
281 shift
282 log_given=t$log_given
283 use_commit="$1"
284 no_edit=
285 shift
288 case "$#" in 1) usage ;; esac
289 shift
290 log_given=t$log_given
291 use_commit="$1"
292 no_edit=t
293 shift
295 --reu=*|--reus=*|--reuse=*|--reuse-=*|--reuse-m=*|--reuse-me=*|\
296 --reuse-mes=*|--reuse-mess=*|--reuse-messa=*|--reuse-messag=*|\
297 --reuse-message=*)
298 log_given=t$log_given
299 use_commit=`expr "$1" : '-[^=]*=\(.*\)'`
300 no_edit=t
301 shift
303 --reu|--reus|--reuse|--reuse-|--reuse-m|--reuse-me|--reuse-mes|\
304 --reuse-mess|--reuse-messa|--reuse-messag|--reuse-message)
305 case "$#" in 1) usage ;; esac
306 shift
307 log_given=t$log_given
308 use_commit="$1"
309 no_edit=t
310 shift
312 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
313 signoff=t
314 shift
316 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
317 verbose=t
318 shift
321 shift
322 break
325 usage
328 break
330 esac
331 done
333 ################################################################
334 # Sanity check options
336 case "$log_given" in
337 tt*)
338 die "Only one of -c/-C/-F/-m can be used." ;;
339 esac
341 case "$#,$also$only" in
342 *,tt)
343 die "Only one of --include/--only can be used." ;;
344 0,t)
345 die "No paths with --include/--only does not make sense." ;;
349 echo >&2 "assuming --include paths..."
350 also=t
351 # Later when switch the defaults, we will replace them with these:
352 # echo >&2 "assuming --only paths..."
353 # also=
355 # If we are going to launch an editor, the message won't be
356 # shown without this...
357 test -z "$log_given$status_only" && sleep 1
359 esac
360 unset only
361 case "$all,$also,$#" in
362 t,t,*)
363 die "Cannot use -a and -i at the same time." ;;
364 t,,[1-9]*)
365 die "Paths with -a does not make sense." ;;
366 ,t,0)
367 die "No paths with -i does not make sense." ;;
368 esac
370 ################################################################
371 # Prepare index to have a tree to be committed
373 TOP=`git-rev-parse --show-cdup`
374 if test -z "$TOP"
375 then
376 TOP=./
379 case "$all,$also" in
381 save_index &&
383 cd "$TOP"
384 GIT_INDEX_FILE="$NEXT_INDEX"
385 export GIT_INDEX_FILE
386 git-diff-files --name-only -z |
387 git-update-index --remove -z --stdin
391 save_index &&
392 git-diff-files --name-only -z -- "$@" |
394 cd "$TOP"
395 GIT_INDEX_FILE="$NEXT_INDEX"
396 export GIT_INDEX_FILE
397 git-update-index --remove -z --stdin
401 case "$#" in
403 ;; # commit as-is
405 if test -f "$GIT_DIR/MERGE_HEAD"
406 then
407 refuse_partial "Cannot do a partial commit during a merge."
409 TMP_INDEX="$GIT_DIR/tmp-index$$"
410 if test -z "$initial_commit"
411 then
412 # make sure index is clean at the specified paths, or
413 # they are additions.
414 dirty_in_index=`git-diff-index --cached --name-status \
415 --diff-filter=DMTU HEAD -- "$@"`
416 test -z "$dirty_in_index" ||
417 refuse_partial "Different in index and the last commit:
418 $dirty_in_index"
420 commit_only=`git-ls-files -- "$@"`
422 # Build the temporary index and update the real index
423 # the same way.
424 if test -z "$initial_commit"
425 then
426 cp "$THIS_INDEX" "$TMP_INDEX"
427 GIT_INDEX_FILE="$TMP_INDEX" git-read-tree -m HEAD
428 else
429 rm -f "$TMP_INDEX"
430 fi || exit
432 echo "$commit_only" |
433 GIT_INDEX_FILE="$TMP_INDEX" \
434 git-update-index --add --remove --stdin &&
436 save_index &&
437 echo "$commit_only" |
439 GIT_INDEX_FILE="$NEXT_INDEX"
440 export GIT_INDEX_FILE
441 git-update-index --remove --stdin
442 ) || exit
444 esac
446 esac
448 ################################################################
449 # If we do as-is commit, the index file will be THIS_INDEX,
450 # otherwise NEXT_INDEX after we make this commit. We leave
451 # the index as is if we abort.
453 if test -f "$NEXT_INDEX"
454 then
455 USE_INDEX="$NEXT_INDEX"
456 else
457 USE_INDEX="$THIS_INDEX"
460 GIT_INDEX_FILE="$USE_INDEX" \
461 git-update-index -q $unmerged_ok_if_status --refresh || exit
463 ################################################################
464 # If the request is status, just show it and exit.
466 case "$0" in
467 *status)
468 run_status
469 exit $?
470 esac
472 ################################################################
473 # Grab commit message, write out tree and make commit.
475 if test t = "$verify" && test -x "$GIT_DIR"/hooks/pre-commit
476 then
477 if test "$TMP_INDEX"
478 then
479 GIT_INDEX_FILE="$TMP_INDEX" "$GIT_DIR"/hooks/pre-commit
480 else
481 GIT_INDEX_FILE="$USE_INDEX" "$GIT_DIR"/hooks/pre-commit
482 fi || exit
485 if test "$log_message" != ''
486 then
487 echo "$log_message"
488 elif test "$logfile" != ""
489 then
490 if test "$logfile" = -
491 then
492 test -t 0 &&
493 echo >&2 "(reading log message from standard input)"
495 else
496 cat <"$logfile"
498 elif test "$use_commit" != ""
499 then
500 git-cat-file commit "$use_commit" | sed -e '1,/^$/d'
501 elif test -f "$GIT_DIR/MERGE_HEAD" && test -f "$GIT_DIR/MERGE_MSG"
502 then
503 cat "$GIT_DIR/MERGE_MSG"
504 fi | git-stripspace >"$GIT_DIR"/COMMIT_EDITMSG
506 case "$signoff" in
509 echo
510 git-var GIT_COMMITTER_IDENT | sed -e '
511 s/>.*/>/
512 s/^/Signed-off-by: /
514 } >>"$GIT_DIR"/COMMIT_EDITMSG
516 esac
518 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
519 echo "#"
520 echo "# It looks like you may be committing a MERGE."
521 echo "# If this is not correct, please remove the file"
522 echo "# $GIT_DIR/MERGE_HEAD"
523 echo "# and try again"
524 echo "#"
525 fi >>"$GIT_DIR"/COMMIT_EDITMSG
527 # Author
528 if test '' != "$force_author"
529 then
530 GIT_AUTHOR_NAME=`expr "$force_author" : '\(.*[^ ]\) *<.*'` &&
531 GIT_AUTHOR_EMAIL=`expr "$force_author" : '.*\(<.*\)'` &&
532 test '' != "$GIT_AUTHOR_NAME" &&
533 test '' != "$GIT_AUTHOR_EMAIL" ||
534 die "malformatted --author parameter"
535 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
536 elif test '' != "$use_commit"
537 then
538 pick_author_script='
539 /^author /{
540 s/'\''/'\''\\'\'\''/g
542 s/^author \([^<]*\) <[^>]*> .*$/\1/
543 s/'\''/'\''\'\'\''/g
544 s/.*/GIT_AUTHOR_NAME='\''&'\''/p
547 s/^author [^<]* <\([^>]*\)> .*$/\1/
548 s/'\''/'\''\'\'\''/g
549 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
552 s/^author [^<]* <[^>]*> \(.*\)$/\1/
553 s/'\''/'\''\'\'\''/g
554 s/.*/GIT_AUTHOR_DATE='\''&'\''/p
559 set_author_env=`git-cat-file commit "$use_commit" |
560 LANG=C LC_ALL=C sed -ne "$pick_author_script"`
561 eval "$set_author_env"
562 export GIT_AUTHOR_NAME
563 export GIT_AUTHOR_EMAIL
564 export GIT_AUTHOR_DATE
567 PARENTS="-p HEAD"
568 if test -z "$initial_commit"
569 then
570 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
571 PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
573 else
574 if [ -z "$(git-ls-files)" ]; then
575 echo >&2 Nothing to commit
576 exit 1
578 PARENTS=""
582 test -z "$verbose" || echo '---'
583 run_status
584 } >>"$GIT_DIR"/COMMIT_EDITMSG
585 if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" ]
586 then
587 rm -f "$GIT_DIR/COMMIT_EDITMSG"
588 run_status
589 exit 1
591 case "$no_edit" in
593 case "${VISUAL:-$EDITOR},$TERM" in
594 ,dumb)
595 echo >&2 "Terminal is dumb but no VISUAL nor EDITOR defined."
596 echo >&2 "Please supply the commit log message using either"
597 echo >&2 "-m or -F option. A boilerplate log message has"
598 echo >&2 "been prepared in $GIT_DIR/COMMIT_EDITMSG"
599 exit 1
601 esac
602 ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR/COMMIT_EDITMSG"
604 esac
606 case "$verify" in
608 if test -x "$GIT_DIR"/hooks/commit-msg
609 then
610 "$GIT_DIR"/hooks/commit-msg "$GIT_DIR"/COMMIT_EDITMSG || exit
612 esac
614 sed -e '
615 /^---$/{
616 s///
619 /^#/d
620 ' "$GIT_DIR"/COMMIT_EDITMSG |
621 git-stripspace >"$GIT_DIR"/COMMIT_MSG
623 if cnt=`grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
624 git-stripspace |
625 wc -l` &&
626 test 0 -lt $cnt
627 then
628 if test -z "$TMP_INDEX"
629 then
630 tree=$(GIT_INDEX_FILE="$USE_INDEX" git-write-tree)
631 else
632 tree=$(GIT_INDEX_FILE="$TMP_INDEX" git-write-tree) &&
633 rm -f "$TMP_INDEX"
634 fi &&
635 commit=$(cat "$GIT_DIR"/COMMIT_MSG | git-commit-tree $tree $PARENTS) &&
636 git-update-ref HEAD $commit $current &&
637 rm -f -- "$GIT_DIR/MERGE_HEAD" &&
638 if test -f "$NEXT_INDEX"
639 then
640 mv "$NEXT_INDEX" "$THIS_INDEX"
641 else
642 : ;# happy
644 else
645 echo >&2 "* no commit message? aborting commit."
646 false
648 ret="$?"
649 rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG"
650 git-rerere
652 if test -x "$GIT_DIR"/hooks/post-commit && test "$ret" = 0
653 then
654 "$GIT_DIR"/hooks/post-commit
656 exit "$ret"