Merge branch 'jc/fixdiff'
[git.git] / git-commit.sh
blob59551d99f9672233da2d8d847f9f4d535dba26de
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 git-diff-index -M --cached --name-status \
91 --diff-filter=MDTCRA HEAD |
92 sed -e '
93 s/\\/\\\\/g
94 s/ /\\ /g
95 ' |
96 report "Updated but not checked in" "will commit"
97 committable="$?"
98 else
99 echo '#
100 # Initial commit
102 git-ls-files |
103 sed -e '
104 s/\\/\\\\/g
105 s/ /\\ /g
106 s/^/A /
108 report "Updated but not checked in" "will commit"
110 committable="$?"
113 git-diff-files --name-status |
114 sed -e '
115 s/\\/\\\\/g
116 s/ /\\ /g
118 report "Changed but not updated" \
119 "use git-update-index to mark for commit"
121 if test -f "$GIT_DIR/info/exclude"
122 then
123 git-ls-files -z --others --directory \
124 --exclude-from="$GIT_DIR/info/exclude" \
125 --exclude-per-directory=.gitignore
126 else
127 git-ls-files -z --others --directory \
128 --exclude-per-directory=.gitignore
129 fi |
130 perl -e '$/ = "\0";
131 my $shown = 0;
132 while (<>) {
133 chomp;
134 s|\\|\\\\|g;
135 s|\t|\\t|g;
136 s|\n|\\n|g;
137 s/^/# /;
138 if (!$shown) {
139 print "#\n# Untracked files:\n";
140 print "# (use \"git add\" to add to commit)\n";
141 print "#\n";
142 $shown = 1;
144 print "$_\n";
148 if test -n "$verbose"
149 then
150 git-diff-index --cached -M -p --diff-filter=MDTCRA HEAD
152 case "$committable" in
154 echo "nothing to commit"
155 exit 1
156 esac
157 exit 0
161 trap '
162 test -z "$TMP_INDEX" || {
163 test -f "$TMP_INDEX" && rm -f "$TMP_INDEX"
165 rm -f "$NEXT_INDEX"
168 ################################################################
169 # Command line argument parsing and sanity checking
171 all=
172 also=
173 only=
174 logfile=
175 use_commit=
176 no_edit=
177 log_given=
178 log_message=
179 verify=t
180 verbose=
181 signoff=
182 force_author=
183 while case "$#" in 0) break;; esac
185 case "$1" in
186 -F|--F|-f|--f|--fi|--fil|--file)
187 case "$#" in 1) usage ;; esac
188 shift
189 no_edit=t
190 log_given=t$log_given
191 logfile="$1"
192 shift
194 -F*|-f*)
195 no_edit=t
196 log_given=t$log_given
197 logfile=`expr "$1" : '-[Ff]\(.*\)'`
198 shift
200 --F=*|--f=*|--fi=*|--fil=*|--file=*)
201 no_edit=t
202 log_given=t$log_given
203 logfile=`expr "$1" : '-[^=]*=\(.*\)'`
204 shift
206 -a|--a|--al|--all)
207 all=t
208 shift
210 --au=*|--aut=*|--auth=*|--autho=*|--author=*)
211 force_author=`expr "$1" : '-[^=]*=\(.*\)'`
212 shift
214 --au|--aut|--auth|--autho|--author)
215 case "$#" in 1) usage ;; esac
216 shift
217 force_author="$1"
218 shift
220 -e|--e|--ed|--edi|--edit)
221 no_edit=
222 shift
224 -i|--i|--in|--inc|--incl|--inclu|--includ|--include)
225 also=t
226 shift
228 -o|--o|--on|--onl|--only)
229 only=t
230 shift
232 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
233 case "$#" in 1) usage ;; esac
234 shift
235 log_given=t$log_given
236 log_message="$1"
237 no_edit=t
238 shift
240 -m*)
241 log_given=t$log_given
242 log_message=`expr "$1" : '-m\(.*\)'`
243 no_edit=t
244 shift
246 --m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
247 log_given=t$log_given
248 log_message=`expr "$1" : '-[^=]*=\(.*\)'`
249 no_edit=t
250 shift
252 -n|--n|--no|--no-|--no-v|--no-ve|--no-ver|--no-veri|--no-verif|--no-verify)
253 verify=
254 shift
257 case "$#" in 1) usage ;; esac
258 shift
259 log_given=t$log_given
260 use_commit="$1"
261 no_edit=
262 shift
264 --ree=*|--reed=*|--reedi=*|--reedit=*|--reedit-=*|--reedit-m=*|\
265 --reedit-me=*|--reedit-mes=*|--reedit-mess=*|--reedit-messa=*|\
266 --reedit-messag=*|--reedit-message=*)
267 log_given=t$log_given
268 use_commit=`expr "$1" : '-[^=]*=\(.*\)'`
269 no_edit=
270 shift
272 --ree|--reed|--reedi|--reedit|--reedit-|--reedit-m|--reedit-me|\
273 --reedit-mes|--reedit-mess|--reedit-messa|--reedit-messag|--reedit-message)
274 case "$#" in 1) usage ;; esac
275 shift
276 log_given=t$log_given
277 use_commit="$1"
278 no_edit=
279 shift
282 case "$#" in 1) usage ;; esac
283 shift
284 log_given=t$log_given
285 use_commit="$1"
286 no_edit=t
287 shift
289 --reu=*|--reus=*|--reuse=*|--reuse-=*|--reuse-m=*|--reuse-me=*|\
290 --reuse-mes=*|--reuse-mess=*|--reuse-messa=*|--reuse-messag=*|\
291 --reuse-message=*)
292 log_given=t$log_given
293 use_commit=`expr "$1" : '-[^=]*=\(.*\)'`
294 no_edit=t
295 shift
297 --reu|--reus|--reuse|--reuse-|--reuse-m|--reuse-me|--reuse-mes|\
298 --reuse-mess|--reuse-messa|--reuse-messag|--reuse-message)
299 case "$#" in 1) usage ;; esac
300 shift
301 log_given=t$log_given
302 use_commit="$1"
303 no_edit=t
304 shift
306 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
307 signoff=t
308 shift
310 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
311 verbose=t
312 shift
315 shift
316 break
319 usage
322 break
324 esac
325 done
327 ################################################################
328 # Sanity check options
330 case "$log_given" in
331 tt*)
332 die "Only one of -c/-C/-F/-m can be used." ;;
333 esac
335 case "$#,$also$only" in
336 *,tt)
337 die "Only one of --include/--only can be used." ;;
338 0,t)
339 die "No paths with --include/--only does not make sense." ;;
343 echo >&2 "assuming --include paths..."
344 also=t
345 # Later when switch the defaults, we will replace them with these:
346 # echo >&2 "assuming --only paths..."
347 # also=
349 # If we are going to launch an editor, the message won't be
350 # shown without this...
351 test -z "$log_given$status_only" && sleep 1
353 esac
354 unset only
355 case "$all,$also,$#" in
356 t,t,*)
357 die "Cannot use -a and -i at the same time." ;;
358 t,,[1-9]*)
359 die "Paths with -a does not make sense." ;;
360 ,t,0)
361 die "No paths with -i does not make sense." ;;
362 esac
364 ################################################################
365 # Prepare index to have a tree to be committed
367 TOP=`git-rev-parse --show-cdup`
368 if test -z "$TOP"
369 then
370 TOP=./
373 case "$all,$also" in
375 save_index &&
377 cd "$TOP"
378 GIT_INDEX_FILE="$NEXT_INDEX"
379 export GIT_INDEX_FILE
380 git-diff-files --name-only -z |
381 git-update-index --remove -z --stdin
385 save_index &&
386 git-diff-files --name-only -z -- "$@" |
388 cd "$TOP"
389 GIT_INDEX_FILE="$NEXT_INDEX"
390 export GIT_INDEX_FILE
391 git-update-index --remove -z --stdin
395 case "$#" in
397 ;; # commit as-is
399 if test -f "$GIT_DIR/MERGE_HEAD"
400 then
401 refuse_partial "Cannot do a partial commit during a merge."
403 TMP_INDEX="$GIT_DIR/tmp-index$$"
404 if test -z "$initial_commit"
405 then
406 # make sure index is clean at the specified paths, or
407 # they are additions.
408 dirty_in_index=`git-diff-index --cached --name-status \
409 --diff-filter=DMTU HEAD -- "$@"`
410 test -z "$dirty_in_index" ||
411 refuse_partial "Different in index and the last commit:
412 $dirty_in_index"
414 commit_only=`git-ls-files -- "$@"`
416 # Build the temporary index and update the real index
417 # the same way.
418 if test -z "$initial_commit"
419 then
420 cp "$THIS_INDEX" "$TMP_INDEX"
421 GIT_INDEX_FILE="$TMP_INDEX" git-read-tree -m HEAD
422 else
423 rm -f "$TMP_INDEX"
424 fi || exit
426 echo "$commit_only" |
427 GIT_INDEX_FILE="$TMP_INDEX" \
428 git-update-index --add --remove --stdin &&
430 save_index &&
431 echo "$commit_only" |
433 GIT_INDEX_FILE="$NEXT_INDEX"
434 export GIT_INDEX_FILE
435 git-update-index --remove --stdin
436 ) || exit
438 esac
440 esac
442 ################################################################
443 # If we do as-is commit, the index file will be THIS_INDEX,
444 # otherwise NEXT_INDEX after we make this commit. We leave
445 # the index as is if we abort.
447 if test -f "$NEXT_INDEX"
448 then
449 USE_INDEX="$NEXT_INDEX"
450 else
451 USE_INDEX="$THIS_INDEX"
454 GIT_INDEX_FILE="$USE_INDEX" \
455 git-update-index -q $unmerged_ok_if_status --refresh || exit
457 ################################################################
458 # If the request is status, just show it and exit.
460 case "$0" in
461 *status)
462 run_status
463 exit $?
464 esac
466 ################################################################
467 # Grab commit message, write out tree and make commit.
469 if test t = "$verify" && test -x "$GIT_DIR"/hooks/pre-commit
470 then
471 if test "$TMP_INDEX"
472 then
473 GIT_INDEX_FILE="$TMP_INDEX" "$GIT_DIR"/hooks/pre-commit
474 else
475 GIT_INDEX_FILE="$USE_INDEX" "$GIT_DIR"/hooks/pre-commit
476 fi || exit
479 if test "$log_message" != ''
480 then
481 echo "$log_message"
482 elif test "$logfile" != ""
483 then
484 if test "$logfile" = -
485 then
486 test -t 0 &&
487 echo >&2 "(reading log message from standard input)"
489 else
490 cat <"$logfile"
492 elif test "$use_commit" != ""
493 then
494 git-cat-file commit "$use_commit" | sed -e '1,/^$/d'
495 elif test -f "$GIT_DIR/MERGE_HEAD" && test -f "$GIT_DIR/MERGE_MSG"
496 then
497 cat "$GIT_DIR/MERGE_MSG"
498 fi | git-stripspace >"$GIT_DIR"/COMMIT_EDITMSG
500 case "$signoff" in
503 echo
504 git-var GIT_COMMITTER_IDENT | sed -e '
505 s/>.*/>/
506 s/^/Signed-off-by: /
508 } >>"$GIT_DIR"/COMMIT_EDITMSG
510 esac
512 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
513 echo "#"
514 echo "# It looks like you may be committing a MERGE."
515 echo "# If this is not correct, please remove the file"
516 echo "# $GIT_DIR/MERGE_HEAD"
517 echo "# and try again"
518 echo "#"
519 fi >>"$GIT_DIR"/COMMIT_EDITMSG
521 # Author
522 if test '' != "$force_author"
523 then
524 GIT_AUTHOR_NAME=`expr "$force_author" : '\(.*[^ ]\) *<.*'` &&
525 GIT_AUTHOR_EMAIL=`expr "$force_author" : '.*\(<.*\)'` &&
526 test '' != "$GIT_AUTHOR_NAME" &&
527 test '' != "$GIT_AUTHOR_EMAIL" ||
528 die "malformatted --author parameter"
529 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
530 elif test '' != "$use_commit"
531 then
532 pick_author_script='
533 /^author /{
534 s/'\''/'\''\\'\'\''/g
536 s/^author \([^<]*\) <[^>]*> .*$/\1/
537 s/'\''/'\''\'\'\''/g
538 s/.*/GIT_AUTHOR_NAME='\''&'\''/p
541 s/^author [^<]* <\([^>]*\)> .*$/\1/
542 s/'\''/'\''\'\'\''/g
543 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
546 s/^author [^<]* <[^>]*> \(.*\)$/\1/
547 s/'\''/'\''\'\'\''/g
548 s/.*/GIT_AUTHOR_DATE='\''&'\''/p
553 set_author_env=`git-cat-file commit "$use_commit" |
554 LANG=C LC_ALL=C sed -ne "$pick_author_script"`
555 eval "$set_author_env"
556 export GIT_AUTHOR_NAME
557 export GIT_AUTHOR_EMAIL
558 export GIT_AUTHOR_DATE
561 PARENTS="-p HEAD"
562 if test -z "$initial_commit"
563 then
564 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
565 PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
567 else
568 if [ -z "$(git-ls-files)" ]; then
569 echo >&2 Nothing to commit
570 exit 1
572 PARENTS=""
575 run_status >>"$GIT_DIR"/COMMIT_EDITMSG
576 if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" ]
577 then
578 rm -f "$GIT_DIR/COMMIT_EDITMSG"
579 run_status
580 exit 1
582 case "$no_edit" in
584 case "${VISUAL:-$EDITOR},$TERM" in
585 ,dumb)
586 echo >&2 "Terminal is dumb but no VISUAL nor EDITOR defined."
587 echo >&2 "Please supply the commit log message using either"
588 echo >&2 "-m or -F option. A boilerplate log message has"
589 echo >&2 "been prepared in $GIT_DIR/COMMIT_EDITMSG"
590 exit 1
592 esac
593 ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR/COMMIT_EDITMSG"
595 esac
597 case "$verify" in
599 if test -x "$GIT_DIR"/hooks/commit-msg
600 then
601 "$GIT_DIR"/hooks/commit-msg "$GIT_DIR"/COMMIT_EDITMSG || exit
603 esac
605 sed -e '
606 /^diff --git a\/.*/{
607 s///
610 /^#/d
611 ' "$GIT_DIR"/COMMIT_EDITMSG |
612 git-stripspace >"$GIT_DIR"/COMMIT_MSG
614 if cnt=`grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
615 git-stripspace |
616 wc -l` &&
617 test 0 -lt $cnt
618 then
619 if test -z "$TMP_INDEX"
620 then
621 tree=$(GIT_INDEX_FILE="$USE_INDEX" git-write-tree)
622 else
623 tree=$(GIT_INDEX_FILE="$TMP_INDEX" git-write-tree) &&
624 rm -f "$TMP_INDEX"
625 fi &&
626 commit=$(cat "$GIT_DIR"/COMMIT_MSG | git-commit-tree $tree $PARENTS) &&
627 git-update-ref HEAD $commit $current &&
628 rm -f -- "$GIT_DIR/MERGE_HEAD" &&
629 if test -f "$NEXT_INDEX"
630 then
631 mv "$NEXT_INDEX" "$THIS_INDEX"
632 else
633 : ;# happy
635 else
636 echo >&2 "* no commit message? aborting commit."
637 false
639 ret="$?"
640 rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG"
641 if test -d "$GIT_DIR/rr-cache"
642 then
643 git-rerere
646 if test -x "$GIT_DIR"/hooks/post-commit && test "$ret" = 0
647 then
648 "$GIT_DIR"/hooks/post-commit
650 exit "$ret"