]> git.ipfire.org Git - thirdparty/git.git/blame - git-commit.sh
Merge branch 'ml/cvsserver'
[thirdparty/git.git] / git-commit.sh
CommitLineData
a3e870f2 1#!/bin/sh
5fec3ef1
JH
2#
3# Copyright (c) 2005 Linus Torvalds
130fcca6
JH
4# Copyright (c) 2006 Junio C Hamano
5
4170a195 6USAGE='[-a] [-s] [-v] [--no-verify] [-m <message> | -F <logfile> | (-C|-c) <commit>] [-e] [--author <author>] [[-i | -o] <path>...]'
130fcca6 7SUBDIRECTORY_OK=Yes
ae2b0f15 8. git-sh-setup
b33e9666 9
cf7bb589
JH
10git-rev-parse --verify HEAD >/dev/null 2>&1 || initial_commit=t
11branch=$(GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD)
12
13case "$0" in
14*status)
15 status_only=t
16 unmerged_ok_if_status=--unmerged ;;
17*commit)
18 status_only=
19 unmerged_ok_if_status= ;;
20esac
130fcca6
JH
21
22refuse_partial () {
23 echo >&2 "$1"
130fcca6
JH
24 echo >&2 "You might have meant to say 'git commit -i paths...', perhaps?"
25 exit 1
26}
27
cf7bb589
JH
28THIS_INDEX="$GIT_DIR/index"
29NEXT_INDEX="$GIT_DIR/next-index$$"
30rm -f "$NEXT_INDEX"
5a798fb5 31save_index () {
cf7bb589
JH
32 cp "$THIS_INDEX" "$NEXT_INDEX"
33}
34
35report () {
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" ]
5a798fb5
JH
60}
61
62run_status () {
cf7bb589
JH
63 (
64 # We always show status for the whole tree.
65 cd "$TOP"
66
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
81 fi
82
83 case "$branch" in
84 refs/heads/master) ;;
85 *) echo "# On branch $branch" ;;
86 esac
87
88 if test -z "$initial_commit"
89 then
9ae6be80
JH
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"
cf7bb589
JH
97 committable="$?"
98 else
99 echo '#
100# Initial commit
101#'
102 git-ls-files |
103 sed -e '
104 s/\\/\\\\/g
105 s/ /\\ /g
106 s/^/A /
107 ' |
108 report "Updated but not checked in" "will commit"
109
110 committable="$?"
111 fi
112
113 git-diff-files --name-status |
114 sed -e '
115 s/\\/\\\\/g
116 s/ /\\ /g
117 ' |
118 report "Changed but not updated" \
119 "use git-update-index to mark for commit"
120
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;
143 }
144 print "$_\n";
145 }
146 '
9ae6be80
JH
147
148 if test -n "$verbose"
149 then
150 git-diff-index --cached -M -p --diff-filter=MDTCRA HEAD
151 fi
cf7bb589
JH
152 case "$committable" in
153 0)
154 echo "nothing to commit"
155 exit 1
156 esac
157 exit 0
158 )
5a798fb5
JH
159}
160
cf7bb589
JH
161trap '
162 test -z "$TMP_INDEX" || {
163 test -f "$TMP_INDEX" && rm -f "$TMP_INDEX"
164 }
165 rm -f "$NEXT_INDEX"
166' 0
167
168################################################################
169# Command line argument parsing and sanity checking
170
130fcca6
JH
171all=
172also=
5a798fb5 173only=
130fcca6
JH
174logfile=
175use_commit=
176no_edit=
177log_given=
178log_message=
179verify=t
cf7bb589 180verbose=
130fcca6
JH
181signoff=
182force_author=
bba319b5 183only_include_assumed=
0c091296 184while case "$#" in 0) break;; esac
5fec3ef1 185do
0c091296 186 case "$1" in
130fcca6
JH
187 -F|--F|-f|--f|--fi|--fil|--file)
188 case "$#" in 1) usage ;; esac
189 shift
190 no_edit=t
191 log_given=t$log_given
192 logfile="$1"
193 shift
194 ;;
195 -F*|-f*)
196 no_edit=t
197 log_given=t$log_given
198 logfile=`expr "$1" : '-[Ff]\(.*\)'`
199 shift
200 ;;
201 --F=*|--f=*|--fi=*|--fil=*|--file=*)
202 no_edit=t
203 log_given=t$log_given
204 logfile=`expr "$1" : '-[^=]*=\(.*\)'`
205 shift
206 ;;
0c091296 207 -a|--a|--al|--all)
130fcca6
JH
208 all=t
209 shift
210 ;;
211 --au=*|--aut=*|--auth=*|--autho=*|--author=*)
212 force_author=`expr "$1" : '-[^=]*=\(.*\)'`
213 shift
214 ;;
215 --au|--aut|--auth|--autho|--author)
216 case "$#" in 1) usage ;; esac
217 shift
218 force_author="$1"
219 shift
220 ;;
221 -e|--e|--ed|--edi|--edit)
222 no_edit=
223 shift
224 ;;
225 -i|--i|--in|--inc|--incl|--inclu|--includ|--include)
226 also=t
227 shift
228 ;;
5a798fb5
JH
229 -o|--o|--on|--onl|--only)
230 only=t
231 shift
232 ;;
0c091296 233 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
130fcca6
JH
234 case "$#" in 1) usage ;; esac
235 shift
236 log_given=t$log_given
237 log_message="$1"
238 no_edit=t
239 shift
240 ;;
241 -m*)
242 log_given=t$log_given
243 log_message=`expr "$1" : '-m\(.*\)'`
244 no_edit=t
245 shift
246 ;;
247 --m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
248 log_given=t$log_given
249 log_message=`expr "$1" : '-[^=]*=\(.*\)'`
250 no_edit=t
251 shift
252 ;;
253 -n|--n|--no|--no-|--no-v|--no-ve|--no-ver|--no-veri|--no-verif|--no-verify)
254 verify=
255 shift
256 ;;
257 -c)
258 case "$#" in 1) usage ;; esac
259 shift
260 log_given=t$log_given
261 use_commit="$1"
262 no_edit=
263 shift
264 ;;
265 --ree=*|--reed=*|--reedi=*|--reedit=*|--reedit-=*|--reedit-m=*|\
0c091296
JH
266 --reedit-me=*|--reedit-mes=*|--reedit-mess=*|--reedit-messa=*|\
267 --reedit-messag=*|--reedit-message=*)
130fcca6
JH
268 log_given=t$log_given
269 use_commit=`expr "$1" : '-[^=]*=\(.*\)'`
270 no_edit=
271 shift
272 ;;
273 --ree|--reed|--reedi|--reedit|--reedit-|--reedit-m|--reedit-me|\
0c091296 274 --reedit-mes|--reedit-mess|--reedit-messa|--reedit-messag|--reedit-message)
130fcca6
JH
275 case "$#" in 1) usage ;; esac
276 shift
277 log_given=t$log_given
278 use_commit="$1"
279 no_edit=
280 shift
281 ;;
282 -C)
283 case "$#" in 1) usage ;; esac
284 shift
285 log_given=t$log_given
286 use_commit="$1"
287 no_edit=t
288 shift
289 ;;
290 --reu=*|--reus=*|--reuse=*|--reuse-=*|--reuse-m=*|--reuse-me=*|\
0c091296
JH
291 --reuse-mes=*|--reuse-mess=*|--reuse-messa=*|--reuse-messag=*|\
292 --reuse-message=*)
130fcca6
JH
293 log_given=t$log_given
294 use_commit=`expr "$1" : '-[^=]*=\(.*\)'`
295 no_edit=t
296 shift
297 ;;
298 --reu|--reus|--reuse|--reuse-|--reuse-m|--reuse-me|--reuse-mes|\
0c091296 299 --reuse-mess|--reuse-messa|--reuse-messag|--reuse-message)
130fcca6
JH
300 case "$#" in 1) usage ;; esac
301 shift
302 log_given=t$log_given
303 use_commit="$1"
304 no_edit=t
305 shift
306 ;;
0cfe1d30 307 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
130fcca6
JH
308 signoff=t
309 shift
310 ;;
cf7bb589
JH
311 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
312 verbose=t
313 shift
130fcca6 314 ;;
0c091296 315 --)
130fcca6
JH
316 shift
317 break
318 ;;
0c091296 319 -*)
130fcca6
JH
320 usage
321 ;;
0c091296 322 *)
130fcca6
JH
323 break
324 ;;
0c091296 325 esac
5fec3ef1
JH
326done
327
cf7bb589
JH
328################################################################
329# Sanity check options
330
0c091296
JH
331case "$log_given" in
332tt*)
333 die "Only one of -c/-C/-F/-m can be used." ;;
334esac
335
5a798fb5
JH
336case "$#,$also$only" in
337*,tt)
338 die "Only one of --include/--only can be used." ;;
3390,t)
340 die "No paths with --include/--only does not make sense." ;;
3410,)
342 ;;
343*,)
756e3ee0 344 only_include_assumed="# Explicit paths specified without -i nor -o; assuming --only paths..."
4170a195 345 also=
5a798fb5
JH
346 ;;
347esac
348unset only
cf7bb589
JH
349case "$all,$also,$#" in
350t,t,*)
351 die "Cannot use -a and -i at the same time." ;;
352t,,[1-9]*)
353 die "Paths with -a does not make sense." ;;
354,t,0)
355 die "No paths with -i does not make sense." ;;
356esac
357
358################################################################
359# Prepare index to have a tree to be committed
5a798fb5 360
130fcca6 361TOP=`git-rev-parse --show-cdup`
5a798fb5
JH
362if test -z "$TOP"
363then
364 TOP=./
365fi
130fcca6
JH
366
367case "$all,$also" in
130fcca6 368t,)
5a798fb5 369 save_index &&
130fcca6 370 (
5a798fb5 371 cd "$TOP"
cf7bb589
JH
372 GIT_INDEX_FILE="$NEXT_INDEX"
373 export GIT_INDEX_FILE
130fcca6
JH
374 git-diff-files --name-only -z |
375 git-update-index --remove -z --stdin
376 )
6a0049c0 377 ;;
130fcca6 378,t)
5a798fb5 379 save_index &&
bba319b5
JH
380 git-ls-files --error-unmatch -- "$@" >/dev/null || exit
381
130fcca6 382 git-diff-files --name-only -z -- "$@" |
cf7bb589
JH
383 (
384 cd "$TOP"
385 GIT_INDEX_FILE="$NEXT_INDEX"
386 export GIT_INDEX_FILE
387 git-update-index --remove -z --stdin
388 )
22cff6a5 389 ;;
130fcca6
JH
390,)
391 case "$#" in
392 0)
393 ;; # commit as-is
394 *)
395 if test -f "$GIT_DIR/MERGE_HEAD"
396 then
397 refuse_partial "Cannot do a partial commit during a merge."
398 fi
399 TMP_INDEX="$GIT_DIR/tmp-index$$"
400 if test -z "$initial_commit"
401 then
402 # make sure index is clean at the specified paths, or
403 # they are additions.
404 dirty_in_index=`git-diff-index --cached --name-status \
5a798fb5 405 --diff-filter=DMTU HEAD -- "$@"`
130fcca6 406 test -z "$dirty_in_index" ||
5a798fb5 407 refuse_partial "Different in index and the last commit:
130fcca6
JH
408$dirty_in_index"
409 fi
bba319b5 410 commit_only=`git-ls-files --error-unmatch -- "$@"` || exit
cf7bb589
JH
411
412 # Build the temporary index and update the real index
413 # the same way.
414 if test -z "$initial_commit"
415 then
416 cp "$THIS_INDEX" "$TMP_INDEX"
417 GIT_INDEX_FILE="$TMP_INDEX" git-read-tree -m HEAD
418 else
419 rm -f "$TMP_INDEX"
420 fi || exit
421
422 echo "$commit_only" |
423 GIT_INDEX_FILE="$TMP_INDEX" \
424 git-update-index --add --remove --stdin &&
425
426 save_index &&
427 echo "$commit_only" |
428 (
429 GIT_INDEX_FILE="$NEXT_INDEX"
430 export GIT_INDEX_FILE
431 git-update-index --remove --stdin
432 ) || exit
433 ;;
130fcca6
JH
434 esac
435 ;;
436esac
437
cf7bb589
JH
438################################################################
439# If we do as-is commit, the index file will be THIS_INDEX,
440# otherwise NEXT_INDEX after we make this commit. We leave
441# the index as is if we abort.
130fcca6 442
cf7bb589 443if test -f "$NEXT_INDEX"
130fcca6 444then
cf7bb589
JH
445 USE_INDEX="$NEXT_INDEX"
446else
447 USE_INDEX="$THIS_INDEX"
130fcca6
JH
448fi
449
cf7bb589
JH
450GIT_INDEX_FILE="$USE_INDEX" \
451 git-update-index -q $unmerged_ok_if_status --refresh || exit
452
453################################################################
454# If the request is status, just show it and exit.
455
456case "$0" in
457*status)
458 run_status
459 exit $?
460esac
461
462################################################################
463# Grab commit message, write out tree and make commit.
464
130fcca6
JH
465if test t = "$verify" && test -x "$GIT_DIR"/hooks/pre-commit
466then
467 if test "$TMP_INDEX"
468 then
469 GIT_INDEX_FILE="$TMP_INDEX" "$GIT_DIR"/hooks/pre-commit
470 else
cf7bb589 471 GIT_INDEX_FILE="$USE_INDEX" "$GIT_DIR"/hooks/pre-commit
130fcca6
JH
472 fi || exit
473fi
0cfe1d30 474
aba2da13
JH
475if test "$log_message" != ''
476then
477 echo "$log_message"
478elif test "$logfile" != ""
479then
480 if test "$logfile" = -
481 then
482 test -t 0 &&
483 echo >&2 "(reading log message from standard input)"
484 cat
485 else
486 cat <"$logfile"
487 fi
488elif test "$use_commit" != ""
489then
490 git-cat-file commit "$use_commit" | sed -e '1,/^$/d'
e5215804
JH
491elif test -f "$GIT_DIR/MERGE_HEAD" && test -f "$GIT_DIR/MERGE_MSG"
492then
493 cat "$GIT_DIR/MERGE_MSG"
f8e2c54c 494fi | git-stripspace >"$GIT_DIR"/COMMIT_EDITMSG
2d569933
JH
495
496case "$signoff" in
497t)
f6413391
JH
498 {
499 echo
500 git-var GIT_COMMITTER_IDENT | sed -e '
501 s/>.*/>/
502 s/^/Signed-off-by: /
503 '
f8e2c54c 504 } >>"$GIT_DIR"/COMMIT_EDITMSG
2d569933
JH
505 ;;
506esac
507
508if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
509 echo "#"
b6ae5409 510 echo "# It looks like you may be committing a MERGE."
2d569933
JH
511 echo "# If this is not correct, please remove the file"
512 echo "# $GIT_DIR/MERGE_HEAD"
513 echo "# and try again"
514 echo "#"
f8e2c54c 515fi >>"$GIT_DIR"/COMMIT_EDITMSG
aba2da13 516
130fcca6 517# Author
5a798fb5
JH
518if test '' != "$force_author"
519then
520 GIT_AUTHOR_NAME=`expr "$force_author" : '\(.*[^ ]\) *<.*'` &&
521 GIT_AUTHOR_EMAIL=`expr "$force_author" : '.*\(<.*\)'` &&
522 test '' != "$GIT_AUTHOR_NAME" &&
523 test '' != "$GIT_AUTHOR_EMAIL" ||
524 die "malformatted --author parameter"
525 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
526elif test '' != "$use_commit"
130fcca6
JH
527then
528 pick_author_script='
529 /^author /{
530 s/'\''/'\''\\'\'\''/g
531 h
532 s/^author \([^<]*\) <[^>]*> .*$/\1/
533 s/'\''/'\''\'\'\''/g
534 s/.*/GIT_AUTHOR_NAME='\''&'\''/p
535
536 g
537 s/^author [^<]* <\([^>]*\)> .*$/\1/
538 s/'\''/'\''\'\'\''/g
539 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
540
541 g
542 s/^author [^<]* <[^>]*> \(.*\)$/\1/
543 s/'\''/'\''\'\'\''/g
544 s/.*/GIT_AUTHOR_DATE='\''&'\''/p
545
546 q
547 }
548 '
549 set_author_env=`git-cat-file commit "$use_commit" |
550 LANG=C LC_ALL=C sed -ne "$pick_author_script"`
551 eval "$set_author_env"
552 export GIT_AUTHOR_NAME
553 export GIT_AUTHOR_EMAIL
554 export GIT_AUTHOR_DATE
130fcca6
JH
555fi
556
96069cf0 557PARENTS="-p HEAD"
130fcca6 558if test -z "$initial_commit"
8098a178 559then
5fec3ef1 560 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
91063bbc 561 PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
aba2da13 562 fi
8098a178
JH
563else
564 if [ -z "$(git-ls-files)" ]; then
5a798fb5 565 echo >&2 Nothing to commit
8098a178
JH
566 exit 1
567 fi
568 PARENTS=""
96069cf0 569fi
130fcca6 570
bba319b5
JH
571{
572 test -z "$only_include_assumed" || echo "$only_include_assumed"
573 run_status
574} >>"$GIT_DIR"/COMMIT_EDITMSG
df34297a 575if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" ]
a3e870f2 576then
f8e2c54c 577 rm -f "$GIT_DIR/COMMIT_EDITMSG"
5a798fb5 578 run_status
a3e870f2
LT
579 exit 1
580fi
0c091296 581case "$no_edit" in
5fec3ef1 582'')
7334f06c
JH
583 case "${VISUAL:-$EDITOR},$TERM" in
584 ,dumb)
585 echo >&2 "Terminal is dumb but no VISUAL nor EDITOR defined."
586 echo >&2 "Please supply the commit log message using either"
587 echo >&2 "-m or -F option. A boilerplate log message has"
588 echo >&2 "been prepared in $GIT_DIR/COMMIT_EDITMSG"
130fcca6
JH
589 exit 1
590 ;;
7334f06c 591 esac
f8e2c54c 592 ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR/COMMIT_EDITMSG"
5fec3ef1
JH
593 ;;
594esac
89e2c5f1
JH
595
596case "$verify" in
597t)
598 if test -x "$GIT_DIR"/hooks/commit-msg
599 then
f8e2c54c 600 "$GIT_DIR"/hooks/commit-msg "$GIT_DIR"/COMMIT_EDITMSG || exit
89e2c5f1
JH
601 fi
602esac
603
cf7bb589 604sed -e '
9ae6be80
JH
605 /^diff --git a\/.*/{
606 s///
607 q
608 }
609 /^#/d
cf7bb589
JH
610' "$GIT_DIR"/COMMIT_EDITMSG |
611git-stripspace >"$GIT_DIR"/COMMIT_MSG
f8e2c54c
SB
612
613if cnt=`grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
614 git-stripspace |
615 wc -l` &&
616 test 0 -lt $cnt
0c091296 617then
130fcca6
JH
618 if test -z "$TMP_INDEX"
619 then
cf7bb589 620 tree=$(GIT_INDEX_FILE="$USE_INDEX" git-write-tree)
130fcca6
JH
621 else
622 tree=$(GIT_INDEX_FILE="$TMP_INDEX" git-write-tree) &&
623 rm -f "$TMP_INDEX"
624 fi &&
f8e2c54c 625 commit=$(cat "$GIT_DIR"/COMMIT_MSG | git-commit-tree $tree $PARENTS) &&
bf7960eb 626 git-update-ref HEAD $commit $current &&
cf7bb589
JH
627 rm -f -- "$GIT_DIR/MERGE_HEAD" &&
628 if test -f "$NEXT_INDEX"
629 then
630 mv "$NEXT_INDEX" "$THIS_INDEX"
631 else
632 : ;# happy
633 fi
0c091296
JH
634else
635 echo >&2 "* no commit message? aborting commit."
636 false
637fi
170241b7 638ret="$?"
f8e2c54c 639rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG"
1536dd9c
JH
640if test -d "$GIT_DIR/rr-cache"
641then
642 git-rerere
643fi
89e2c5f1
JH
644
645if test -x "$GIT_DIR"/hooks/post-commit && test "$ret" = 0
646then
647 "$GIT_DIR"/hooks/post-commit
648fi
170241b7 649exit "$ret"