blob: e8b60f70497c71bd66b96115b9bba78cee523a8c [file] [log] [blame]
Linus Torvaldsa3e870f2005-05-30 12:51:00 -07001#!/bin/sh
Junio C Hamano5fec3ef2005-06-25 02:22:05 -07002#
3# Copyright (c) 2005 Linus Torvalds
Junio C Hamano130fcca2006-02-05 00:07:44 -08004# Copyright (c) 2006 Junio C Hamano
Junio C Hamano5fec3ef2005-06-25 02:22:05 -07005
Paolo Bonzini6cbf07e2007-03-05 08:57:53 +01006USAGE='[-a | --interactive] [-s] [-v] [--no-verify] [-m <message> | -F <logfile> | (-C|-c) <commit> | --amend] [-u] [-e] [--author <author>] [[-i | -o] <path>...]'
Junio C Hamano130fcca2006-02-05 00:07:44 -08007SUBDIRECTORY_OK=Yes
Junio C Hamanoae2b0f12005-11-24 00:12:11 -08008. git-sh-setup
Shawn O. Pearce7eff28a2006-12-30 23:32:38 -05009require_work_tree
Linus Torvaldsb33e9662005-07-08 10:57:21 -070010
Junio C Hamanocf7bb582006-02-10 00:45:59 -080011git-rev-parse --verify HEAD >/dev/null 2>&1 || initial_commit=t
Junio C Hamanocf7bb582006-02-10 00:45:59 -080012
13case "$0" in
14*status)
15 status_only=t
Junio C Hamano2b5f9a82007-02-22 00:28:49 -080016 ;;
Junio C Hamanocf7bb582006-02-10 00:45:59 -080017*commit)
18 status_only=
Junio C Hamano2b5f9a82007-02-22 00:28:49 -080019 ;;
Junio C Hamanocf7bb582006-02-10 00:45:59 -080020esac
Junio C Hamano130fcca2006-02-05 00:07:44 -080021
22refuse_partial () {
23 echo >&2 "$1"
Junio C Hamano130fcca2006-02-05 00:07:44 -080024 echo >&2 "You might have meant to say 'git commit -i paths...', perhaps?"
25 exit 1
26}
27
Junio C Hamanocf7bb582006-02-10 00:45:59 -080028THIS_INDEX="$GIT_DIR/index"
29NEXT_INDEX="$GIT_DIR/next-index$$"
30rm -f "$NEXT_INDEX"
Junio C Hamano5a798fb2006-02-05 16:08:01 -080031save_index () {
Junio C Hamanocc7d5bc2006-06-29 14:48:22 -070032 cp -p "$THIS_INDEX" "$NEXT_INDEX"
Junio C Hamanocf7bb582006-02-10 00:45:59 -080033}
34
Junio C Hamano5a798fb2006-02-05 16:08:01 -080035run_status () {
Junio C Hamanocf7bb582006-02-10 00:45:59 -080036 # If TMP_INDEX is defined, that means we are doing
37 # "--only" partial commit, and that index file is used
38 # to build the tree for the commit. Otherwise, if
39 # NEXT_INDEX exists, that is the index file used to
40 # make the commit. Otherwise we are using as-is commit
41 # so the regular index file is what we use to compare.
42 if test '' != "$TMP_INDEX"
43 then
Martin Waitz3de63c32006-10-07 21:07:40 +020044 GIT_INDEX_FILE="$TMP_INDEX"
45 export GIT_INDEX_FILE
Junio C Hamanocf7bb582006-02-10 00:45:59 -080046 elif test -f "$NEXT_INDEX"
47 then
Martin Waitz3de63c32006-10-07 21:07:40 +020048 GIT_INDEX_FILE="$NEXT_INDEX"
49 export GIT_INDEX_FILE
Junio C Hamanocf7bb582006-02-10 00:45:59 -080050 fi
51
Martin Waitz3de63c32006-10-07 21:07:40 +020052 case "$status_only" in
53 t) color= ;;
54 *) color=--nocolor ;;
55 esac
56 git-runstatus ${color} \
57 ${verbose:+--verbose} \
58 ${amend:+--amend} \
Johannes Schindelin2074cb02006-09-12 22:45:12 +020059 ${untracked_files:+--untracked}
Junio C Hamano5a798fb2006-02-05 16:08:01 -080060}
61
Junio C Hamanocf7bb582006-02-10 00:45:59 -080062trap '
63 test -z "$TMP_INDEX" || {
64 test -f "$TMP_INDEX" && rm -f "$TMP_INDEX"
65 }
66 rm -f "$NEXT_INDEX"
67' 0
68
69################################################################
70# Command line argument parsing and sanity checking
71
Junio C Hamano130fcca2006-02-05 00:07:44 -080072all=
73also=
Paolo Bonzini6cbf07e2007-03-05 08:57:53 +010074interactive=
Junio C Hamano5a798fb2006-02-05 16:08:01 -080075only=
Junio C Hamano130fcca2006-02-05 00:07:44 -080076logfile=
77use_commit=
Junio C Hamanob4019f02006-03-02 21:04:05 -080078amend=
Jeff Kingcda8ab52006-06-23 09:43:38 -040079edit_flag=
Junio C Hamano130fcca2006-02-05 00:07:44 -080080no_edit=
81log_given=
82log_message=
83verify=t
Nicolas Pitreebd124c2006-12-14 23:15:44 -050084quiet=
Junio C Hamanocf7bb582006-02-10 00:45:59 -080085verbose=
Junio C Hamano130fcca2006-02-05 00:07:44 -080086signoff=
87force_author=
Junio C Hamanobba319b2006-02-14 12:40:20 -080088only_include_assumed=
Matthias Lederhofer443f8332006-05-22 23:02:06 +020089untracked_files=
Junio C Hamano0c091292005-08-08 17:03:14 -070090while case "$#" in 0) break;; esac
Junio C Hamano5fec3ef2005-06-25 02:22:05 -070091do
Martin Waitz3de63c32006-10-07 21:07:40 +020092 case "$1" in
93 -F|--F|-f|--f|--fi|--fil|--file)
94 case "$#" in 1) usage ;; esac
95 shift
96 no_edit=t
97 log_given=t$log_given
98 logfile="$1"
99 shift
100 ;;
101 -F*|-f*)
102 no_edit=t
103 log_given=t$log_given
104 logfile=`expr "z$1" : 'z-[Ff]\(.*\)'`
105 shift
106 ;;
107 --F=*|--f=*|--fi=*|--fil=*|--file=*)
108 no_edit=t
109 log_given=t$log_given
110 logfile=`expr "z$1" : 'z-[^=]*=\(.*\)'`
111 shift
112 ;;
113 -a|--a|--al|--all)
114 all=t
115 shift
116 ;;
117 --au=*|--aut=*|--auth=*|--autho=*|--author=*)
118 force_author=`expr "z$1" : 'z-[^=]*=\(.*\)'`
119 shift
120 ;;
121 --au|--aut|--auth|--autho|--author)
122 case "$#" in 1) usage ;; esac
123 shift
124 force_author="$1"
125 shift
126 ;;
127 -e|--e|--ed|--edi|--edit)
128 edit_flag=t
129 shift
130 ;;
131 -i|--i|--in|--inc|--incl|--inclu|--includ|--include)
132 also=t
133 shift
134 ;;
Paolo Bonzini6cbf07e2007-03-05 08:57:53 +0100135 --int|--inte|--inter|--intera|--interac|--interact|--interacti|\
136 --interactiv|--interactive)
137 interactive=t
138 shift
139 ;;
Martin Waitz3de63c32006-10-07 21:07:40 +0200140 -o|--o|--on|--onl|--only)
141 only=t
142 shift
143 ;;
144 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
145 case "$#" in 1) usage ;; esac
146 shift
147 log_given=m$log_given
148 if test "$log_message" = ''
149 then
150 log_message="$1"
151 else
152 log_message="$log_message
Shawn Pearce68912812006-05-29 04:45:49 -0400153
154$1"
Martin Waitz3de63c32006-10-07 21:07:40 +0200155 fi
156 no_edit=t
157 shift
158 ;;
159 -m*)
160 log_given=m$log_given
161 if test "$log_message" = ''
162 then
163 log_message=`expr "z$1" : 'z-m\(.*\)'`
164 else
165 log_message="$log_message
Shawn Pearce68912812006-05-29 04:45:49 -0400166
Dennis Stosberg8096fae2006-06-27 18:54:26 +0200167`expr "z$1" : 'z-m\(.*\)'`"
Martin Waitz3de63c32006-10-07 21:07:40 +0200168 fi
169 no_edit=t
170 shift
171 ;;
172 --m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
173 log_given=m$log_given
174 if test "$log_message" = ''
175 then
176 log_message=`expr "z$1" : 'z-[^=]*=\(.*\)'`
177 else
178 log_message="$log_message
Shawn Pearce68912812006-05-29 04:45:49 -0400179
Dennis Stosberg8096fae2006-06-27 18:54:26 +0200180`expr "z$1" : 'zq-[^=]*=\(.*\)'`"
Martin Waitz3de63c32006-10-07 21:07:40 +0200181 fi
182 no_edit=t
183 shift
184 ;;
185 -n|--n|--no|--no-|--no-v|--no-ve|--no-ver|--no-veri|--no-verif|\
186 --no-verify)
187 verify=
188 shift
189 ;;
190 --a|--am|--ame|--amen|--amend)
191 amend=t
192 log_given=t$log_given
193 use_commit=HEAD
194 shift
195 ;;
196 -c)
197 case "$#" in 1) usage ;; esac
198 shift
199 log_given=t$log_given
200 use_commit="$1"
201 no_edit=
202 shift
203 ;;
204 --ree=*|--reed=*|--reedi=*|--reedit=*|--reedit-=*|--reedit-m=*|\
205 --reedit-me=*|--reedit-mes=*|--reedit-mess=*|--reedit-messa=*|\
206 --reedit-messag=*|--reedit-message=*)
207 log_given=t$log_given
208 use_commit=`expr "z$1" : 'z-[^=]*=\(.*\)'`
209 no_edit=
210 shift
211 ;;
212 --ree|--reed|--reedi|--reedit|--reedit-|--reedit-m|--reedit-me|\
213 --reedit-mes|--reedit-mess|--reedit-messa|--reedit-messag|\
214 --reedit-message)
215 case "$#" in 1) usage ;; esac
216 shift
217 log_given=t$log_given
218 use_commit="$1"
219 no_edit=
220 shift
221 ;;
222 -C)
223 case "$#" in 1) usage ;; esac
224 shift
225 log_given=t$log_given
226 use_commit="$1"
227 no_edit=t
228 shift
229 ;;
230 --reu=*|--reus=*|--reuse=*|--reuse-=*|--reuse-m=*|--reuse-me=*|\
231 --reuse-mes=*|--reuse-mess=*|--reuse-messa=*|--reuse-messag=*|\
232 --reuse-message=*)
233 log_given=t$log_given
234 use_commit=`expr "z$1" : 'z-[^=]*=\(.*\)'`
235 no_edit=t
236 shift
237 ;;
238 --reu|--reus|--reuse|--reuse-|--reuse-m|--reuse-me|--reuse-mes|\
239 --reuse-mess|--reuse-messa|--reuse-messag|--reuse-message)
240 case "$#" in 1) usage ;; esac
241 shift
242 log_given=t$log_given
243 use_commit="$1"
244 no_edit=t
245 shift
246 ;;
247 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
248 signoff=t
249 shift
250 ;;
Nicolas Pitreebd124c2006-12-14 23:15:44 -0500251 -q|--q|--qu|--qui|--quie|--quiet)
252 quiet=t
253 shift
254 ;;
Martin Waitz3de63c32006-10-07 21:07:40 +0200255 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
256 verbose=t
257 shift
258 ;;
259 -u|--u|--un|--unt|--untr|--untra|--untrac|--untrack|--untracke|\
260 --untracked|--untracked-|--untracked-f|--untracked-fi|--untracked-fil|\
261 --untracked-file|--untracked-files)
262 untracked_files=t
263 shift
264 ;;
265 --)
266 shift
267 break
268 ;;
269 -*)
270 usage
271 ;;
272 *)
273 break
274 ;;
275 esac
Junio C Hamano5fec3ef2005-06-25 02:22:05 -0700276done
Jeff Kingcda8ab52006-06-23 09:43:38 -0400277case "$edit_flag" in t) no_edit= ;; esac
Junio C Hamano5fec3ef2005-06-25 02:22:05 -0700278
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800279################################################################
280# Sanity check options
281
Junio C Hamanob4019f02006-03-02 21:04:05 -0800282case "$amend,$initial_commit" in
283t,t)
Martin Waitz3de63c32006-10-07 21:07:40 +0200284 die "You do not have anything to amend." ;;
Junio C Hamanob4019f02006-03-02 21:04:05 -0800285t,)
Martin Waitz3de63c32006-10-07 21:07:40 +0200286 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
287 die "You are in the middle of a merge -- cannot amend."
288 fi ;;
Junio C Hamanob4019f02006-03-02 21:04:05 -0800289esac
290
Junio C Hamano0c091292005-08-08 17:03:14 -0700291case "$log_given" in
292tt*)
Peter Eriksen497171e2007-01-24 20:54:46 +0100293 die "Only one of -c/-C/-F/--amend can be used." ;;
Shawn Pearce68912812006-05-29 04:45:49 -0400294*tm*|*mt*)
Peter Eriksen497171e2007-01-24 20:54:46 +0100295 die "Option -m cannot be combined with -c/-C/-F/--amend." ;;
Junio C Hamano0c091292005-08-08 17:03:14 -0700296esac
297
Junio C Hamano6a746422006-04-20 01:20:56 -0700298case "$#,$also,$only,$amend" in
299*,t,t,*)
Martin Waitz3de63c32006-10-07 21:07:40 +0200300 die "Only one of --include/--only can be used." ;;
Junio C Hamano6a746422006-04-20 01:20:56 -07003010,t,,* | 0,,t,)
Martin Waitz3de63c32006-10-07 21:07:40 +0200302 die "No paths with --include/--only does not make sense." ;;
Junio C Hamano6a746422006-04-20 01:20:56 -07003030,,t,t)
Martin Waitz3de63c32006-10-07 21:07:40 +0200304 only_include_assumed="# Clever... amending the last one with dirty index." ;;
Junio C Hamano6a746422006-04-20 01:20:56 -07003050,,,*)
Martin Waitz3de63c32006-10-07 21:07:40 +0200306 ;;
Junio C Hamano6a746422006-04-20 01:20:56 -0700307*,,,*)
Martin Waitz3de63c32006-10-07 21:07:40 +0200308 only_include_assumed="# Explicit paths specified without -i nor -o; assuming --only paths..."
309 also=
310 ;;
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800311esac
312unset only
Paolo Bonzini6cbf07e2007-03-05 08:57:53 +0100313case "$all,$interactive,$also,$#" in
314*t,*t,*)
315 die "Cannot use -a, --interactive or -i at the same time." ;;
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800316t,,[1-9]*)
317 die "Paths with -a does not make sense." ;;
Paolo Bonzini6cbf07e2007-03-05 08:57:53 +0100318,t,[1-9]*)
319 die "Paths with --interactive does not make sense." ;;
320,,t,0)
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800321 die "No paths with -i does not make sense." ;;
322esac
323
324################################################################
325# Prepare index to have a tree to be committed
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800326
Junio C Hamano130fcca2006-02-05 00:07:44 -0800327case "$all,$also" in
Junio C Hamano130fcca2006-02-05 00:07:44 -0800328t,)
Fredrik Kuivinen755b99d2007-02-22 21:28:12 +0100329 if test ! -f "$THIS_INDEX"
330 then
331 die 'nothing to commit (use "git add file1 file2" to include for commit)'
332 fi
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800333 save_index &&
Junio C Hamano130fcca2006-02-05 00:07:44 -0800334 (
Junio C Hamano514c09f2007-01-12 12:49:05 -0800335 cd_to_toplevel &&
336 GIT_INDEX_FILE="$NEXT_INDEX" &&
337 export GIT_INDEX_FILE &&
Junio C Hamano130fcca2006-02-05 00:07:44 -0800338 git-diff-files --name-only -z |
339 git-update-index --remove -z --stdin
Junio C Hamano514c09f2007-01-12 12:49:05 -0800340 ) || exit
Junio C Hamano130fcca2006-02-05 00:07:44 -0800341 ;;
342,t)
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800343 save_index &&
Junio C Hamanobba319b2006-02-14 12:40:20 -0800344 git-ls-files --error-unmatch -- "$@" >/dev/null || exit
345
Junio C Hamano130fcca2006-02-05 00:07:44 -0800346 git-diff-files --name-only -z -- "$@" |
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800347 (
Junio C Hamano514c09f2007-01-12 12:49:05 -0800348 cd_to_toplevel &&
349 GIT_INDEX_FILE="$NEXT_INDEX" &&
350 export GIT_INDEX_FILE &&
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800351 git-update-index --remove -z --stdin
Junio C Hamano514c09f2007-01-12 12:49:05 -0800352 ) || exit
Junio C Hamano22cff6a2005-08-16 18:08:19 -0700353 ;;
Junio C Hamano130fcca2006-02-05 00:07:44 -0800354,)
Paolo Bonzini6cbf07e2007-03-05 08:57:53 +0100355 if test "$interactive" = t; then
356 git add --interactive || exit
357 fi
Junio C Hamano130fcca2006-02-05 00:07:44 -0800358 case "$#" in
359 0)
Martin Waitz3de63c32006-10-07 21:07:40 +0200360 ;; # commit as-is
Junio C Hamano130fcca2006-02-05 00:07:44 -0800361 *)
Martin Waitz3de63c32006-10-07 21:07:40 +0200362 if test -f "$GIT_DIR/MERGE_HEAD"
363 then
364 refuse_partial "Cannot do a partial commit during a merge."
365 fi
366 TMP_INDEX="$GIT_DIR/tmp-index$$"
Martin Waitz3de63c32006-10-07 21:07:40 +0200367 commit_only=`git-ls-files --error-unmatch -- "$@"` || exit
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800368
Junio C Hamano158d0572006-12-09 22:32:43 -0800369 # Build a temporary index and update the real index
Martin Waitz3de63c32006-10-07 21:07:40 +0200370 # the same way.
371 if test -z "$initial_commit"
372 then
Junio C Hamano5e7f56a2007-03-31 23:27:41 -0700373 GIT_INDEX_FILE="$THIS_INDEX" \
374 git-read-tree --index-output="$TMP_INDEX" -i -m HEAD
Martin Waitz3de63c32006-10-07 21:07:40 +0200375 else
376 rm -f "$TMP_INDEX"
377 fi || exit
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800378
Junio C Hamano293623e2007-05-25 22:00:54 -0700379 printf '%s\n' "$commit_only" |
Martin Waitz3de63c32006-10-07 21:07:40 +0200380 GIT_INDEX_FILE="$TMP_INDEX" \
381 git-update-index --add --remove --stdin &&
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800382
Martin Waitz3de63c32006-10-07 21:07:40 +0200383 save_index &&
Junio C Hamano293623e2007-05-25 22:00:54 -0700384 printf '%s\n' "$commit_only" |
Martin Waitz3de63c32006-10-07 21:07:40 +0200385 (
386 GIT_INDEX_FILE="$NEXT_INDEX"
387 export GIT_INDEX_FILE
388 git-update-index --remove --stdin
389 ) || exit
390 ;;
Junio C Hamano130fcca2006-02-05 00:07:44 -0800391 esac
Junio C Hamanof678dd12005-11-25 13:33:14 -0800392 ;;
Junio C Hamano130fcca2006-02-05 00:07:44 -0800393esac
394
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800395################################################################
396# If we do as-is commit, the index file will be THIS_INDEX,
397# otherwise NEXT_INDEX after we make this commit. We leave
398# the index as is if we abort.
Junio C Hamano0c091292005-08-08 17:03:14 -0700399
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800400if test -f "$NEXT_INDEX"
Junio C Hamano130fcca2006-02-05 00:07:44 -0800401then
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800402 USE_INDEX="$NEXT_INDEX"
403else
404 USE_INDEX="$THIS_INDEX"
Junio C Hamano130fcca2006-02-05 00:07:44 -0800405fi
406
Junio C Hamano2b5f9a82007-02-22 00:28:49 -0800407case "$status_only" in
408t)
409 # This will silently fail in a read-only repository, which is
410 # what we want.
411 GIT_INDEX_FILE="$USE_INDEX" git-update-index -q --unmerged --refresh
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800412 run_status
413 exit $?
Junio C Hamano2b5f9a82007-02-22 00:28:49 -0800414 ;;
415'')
416 GIT_INDEX_FILE="$USE_INDEX" git-update-index -q --refresh || exit
417 ;;
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800418esac
419
420################################################################
421# Grab commit message, write out tree and make commit.
422
Junio C Hamano130fcca2006-02-05 00:07:44 -0800423if test t = "$verify" && test -x "$GIT_DIR"/hooks/pre-commit
424then
425 if test "$TMP_INDEX"
426 then
427 GIT_INDEX_FILE="$TMP_INDEX" "$GIT_DIR"/hooks/pre-commit
428 else
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800429 GIT_INDEX_FILE="$USE_INDEX" "$GIT_DIR"/hooks/pre-commit
Junio C Hamano130fcca2006-02-05 00:07:44 -0800430 fi || exit
431fi
Junio C Hamano0cfe1d32005-08-12 23:39:15 -0700432
Junio C Hamanoaba2da12005-08-25 18:57:35 -0700433if test "$log_message" != ''
434then
Junio C Hamano293623e2007-05-25 22:00:54 -0700435 printf '%s\n' "$log_message"
Junio C Hamanoaba2da12005-08-25 18:57:35 -0700436elif test "$logfile" != ""
437then
438 if test "$logfile" = -
439 then
440 test -t 0 &&
441 echo >&2 "(reading log message from standard input)"
442 cat
443 else
444 cat <"$logfile"
445 fi
446elif test "$use_commit" != ""
447then
Tom Princee0d10e12007-01-28 16:16:53 -0800448 encoding=$(git config i18n.commitencoding || echo UTF-8)
Junio C Hamano5ac27152007-01-13 13:33:07 -0800449 git show -s --pretty=raw --encoding="$encoding" "$use_commit" |
450 sed -e '1,/^$/d' -e 's/^ //'
Luben Tuikova9cb3c62006-10-12 14:52:42 -0700451elif test -f "$GIT_DIR/MERGE_MSG"
Junio C Hamanoe5215802005-11-01 22:01:28 -0800452then
453 cat "$GIT_DIR/MERGE_MSG"
Junio C Hamano7d0c6882006-06-23 01:37:02 -0700454elif test -f "$GIT_DIR/SQUASH_MSG"
455then
456 cat "$GIT_DIR/SQUASH_MSG"
Santi_Béjarf8e2c542005-10-09 17:30:19 -0700457fi | git-stripspace >"$GIT_DIR"/COMMIT_EDITMSG
Junio C Hamano2d569932005-08-31 17:15:25 -0700458
459case "$signoff" in
460t)
Junio C Hamanodbaa06a2007-01-29 01:06:27 -0800461 need_blank_before_signoff=
462 tail -n 1 "$GIT_DIR"/COMMIT_EDITMSG |
463 grep 'Signed-off-by:' >/dev/null || need_blank_before_signoff=yes
Junio C Hamanof6413392005-10-03 23:49:46 -0700464 {
Junio C Hamanodbaa06a2007-01-29 01:06:27 -0800465 test -z "$need_blank_before_signoff" || echo
Junio C Hamanof6413392005-10-03 23:49:46 -0700466 git-var GIT_COMMITTER_IDENT | sed -e '
467 s/>.*/>/
468 s/^/Signed-off-by: /
469 '
Santi_Béjarf8e2c542005-10-09 17:30:19 -0700470 } >>"$GIT_DIR"/COMMIT_EDITMSG
Junio C Hamano2d569932005-08-31 17:15:25 -0700471 ;;
472esac
473
Junio C Hamano475443c2006-04-12 11:45:18 -0700474if test -f "$GIT_DIR/MERGE_HEAD" && test -z "$no_edit"; then
Junio C Hamano2d569932005-08-31 17:15:25 -0700475 echo "#"
Alex Riesenb6ae5402006-01-05 12:44:59 +0100476 echo "# It looks like you may be committing a MERGE."
Junio C Hamano2d569932005-08-31 17:15:25 -0700477 echo "# If this is not correct, please remove the file"
Junio C Hamano293623e2007-05-25 22:00:54 -0700478 printf '%s\n' "# $GIT_DIR/MERGE_HEAD"
Junio C Hamano2d569932005-08-31 17:15:25 -0700479 echo "# and try again"
480 echo "#"
Santi_Béjarf8e2c542005-10-09 17:30:19 -0700481fi >>"$GIT_DIR"/COMMIT_EDITMSG
Junio C Hamanoaba2da12005-08-25 18:57:35 -0700482
Junio C Hamano130fcca2006-02-05 00:07:44 -0800483# Author
Junio C Hamano83e24dc2007-01-22 13:03:31 -0800484if test '' != "$use_commit"
Junio C Hamano130fcca2006-02-05 00:07:44 -0800485then
486 pick_author_script='
487 /^author /{
488 s/'\''/'\''\\'\'\''/g
489 h
490 s/^author \([^<]*\) <[^>]*> .*$/\1/
491 s/'\''/'\''\'\'\''/g
492 s/.*/GIT_AUTHOR_NAME='\''&'\''/p
493
494 g
495 s/^author [^<]* <\([^>]*\)> .*$/\1/
496 s/'\''/'\''\'\'\''/g
497 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
498
499 g
500 s/^author [^<]* <[^>]*> \(.*\)$/\1/
501 s/'\''/'\''\'\'\''/g
502 s/.*/GIT_AUTHOR_DATE='\''&'\''/p
503
504 q
505 }
506 '
Tom Princee0d10e12007-01-28 16:16:53 -0800507 encoding=$(git config i18n.commitencoding || echo UTF-8)
Junio C Hamano5ac27152007-01-13 13:33:07 -0800508 set_author_env=`git show -s --pretty=raw --encoding="$encoding" "$use_commit" |
Junio C Hamano130fcca2006-02-05 00:07:44 -0800509 LANG=C LC_ALL=C sed -ne "$pick_author_script"`
510 eval "$set_author_env"
511 export GIT_AUTHOR_NAME
512 export GIT_AUTHOR_EMAIL
513 export GIT_AUTHOR_DATE
Junio C Hamano130fcca2006-02-05 00:07:44 -0800514fi
Junio C Hamano83e24dc2007-01-22 13:03:31 -0800515if test '' != "$force_author"
516then
517 GIT_AUTHOR_NAME=`expr "z$force_author" : 'z\(.*[^ ]\) *<.*'` &&
518 GIT_AUTHOR_EMAIL=`expr "z$force_author" : '.*\(<.*\)'` &&
519 test '' != "$GIT_AUTHOR_NAME" &&
520 test '' != "$GIT_AUTHOR_EMAIL" ||
521 die "malformed --author parameter"
522 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
523fi
Junio C Hamano130fcca2006-02-05 00:07:44 -0800524
Linus Torvalds96069cf2005-06-14 10:20:14 -0700525PARENTS="-p HEAD"
Junio C Hamano130fcca2006-02-05 00:07:44 -0800526if test -z "$initial_commit"
Junio C Hamano8098a172005-09-30 14:26:57 -0700527then
Shawn Pearcea3a733e2006-07-10 22:48:47 -0400528 rloga='commit'
Junio C Hamano5fec3ef2005-06-25 02:22:05 -0700529 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
Shawn Pearcea3a733e2006-07-10 22:48:47 -0400530 rloga='commit (merge)'
Junio C Hamano91063bb2005-09-08 13:47:12 -0700531 PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
Junio C Hamanob4019f02006-03-02 21:04:05 -0800532 elif test -n "$amend"; then
Shawn Pearcea3a733e2006-07-10 22:48:47 -0400533 rloga='commit (amend)'
Junio C Hamanob4019f02006-03-02 21:04:05 -0800534 PARENTS=$(git-cat-file commit HEAD |
535 sed -n -e '/^$/q' -e 's/^parent /-p /p')
Junio C Hamanoaba2da12005-08-25 18:57:35 -0700536 fi
Junio C Hamanocede7522006-09-27 02:06:31 -0700537 current="$(git-rev-parse --verify HEAD)"
Junio C Hamano8098a172005-09-30 14:26:57 -0700538else
539 if [ -z "$(git-ls-files)" ]; then
Shawn O. Pearceaeb80c72006-12-15 21:53:09 -0500540 echo >&2 'nothing to commit (use "git add file1 file2" to include for commit)'
Junio C Hamano8098a172005-09-30 14:26:57 -0700541 exit 1
542 fi
543 PARENTS=""
Shawn Pearcea3a733e2006-07-10 22:48:47 -0400544 rloga='commit (initial)'
Junio C Hamanocede7522006-09-27 02:06:31 -0700545 current=''
Linus Torvalds96069cf2005-06-14 10:20:14 -0700546fi
Junio C Hamano23913dc2007-01-19 17:12:11 -0800547set_reflog_action "$rloga"
Junio C Hamano130fcca2006-02-05 00:07:44 -0800548
Junio C Hamano475443c2006-04-12 11:45:18 -0700549if test -z "$no_edit"
550then
551 {
Martin Waitz88a15312006-05-26 01:42:18 +0200552 echo ""
553 echo "# Please enter the commit message for your changes."
554 echo "# (Comment lines starting with '#' will not be included)"
Junio C Hamano475443c2006-04-12 11:45:18 -0700555 test -z "$only_include_assumed" || echo "$only_include_assumed"
556 run_status
557 } >>"$GIT_DIR"/COMMIT_EDITMSG
558else
559 # we need to check if there is anything to commit
560 run_status >/dev/null
561fi
Junio C Hamano85884522006-03-04 20:36:28 -0800562if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" -a -z "$amend" ]
Linus Torvaldsa3e870f2005-05-30 12:51:00 -0700563then
Junio C Hamano7d0c6882006-06-23 01:37:02 -0700564 rm -f "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800565 run_status
Linus Torvaldsa3e870f2005-05-30 12:51:00 -0700566 exit 1
567fi
Junio C Hamano475443c2006-04-12 11:45:18 -0700568
Junio C Hamano0c091292005-08-08 17:03:14 -0700569case "$no_edit" in
Junio C Hamano5fec3ef2005-06-25 02:22:05 -0700570'')
Junio C Hamano7334f062006-02-04 22:10:32 -0800571 case "${VISUAL:-$EDITOR},$TERM" in
572 ,dumb)
573 echo >&2 "Terminal is dumb but no VISUAL nor EDITOR defined."
574 echo >&2 "Please supply the commit log message using either"
575 echo >&2 "-m or -F option. A boilerplate log message has"
576 echo >&2 "been prepared in $GIT_DIR/COMMIT_EDITMSG"
Junio C Hamano130fcca2006-02-05 00:07:44 -0800577 exit 1
578 ;;
Junio C Hamano7334f062006-02-04 22:10:32 -0800579 esac
Seanec4e69c2006-05-13 23:09:32 -0400580 git-var GIT_AUTHOR_IDENT > /dev/null || die
581 git-var GIT_COMMITTER_IDENT > /dev/null || die
Santi_Béjarf8e2c542005-10-09 17:30:19 -0700582 ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR/COMMIT_EDITMSG"
Junio C Hamano5fec3ef2005-06-25 02:22:05 -0700583 ;;
584esac
Junio C Hamano89e2c5f2005-08-18 17:20:08 -0700585
586case "$verify" in
587t)
588 if test -x "$GIT_DIR"/hooks/commit-msg
589 then
Santi_Béjarf8e2c542005-10-09 17:30:19 -0700590 "$GIT_DIR"/hooks/commit-msg "$GIT_DIR"/COMMIT_EDITMSG || exit
Junio C Hamano89e2c5f2005-08-18 17:20:08 -0700591 fi
592esac
593
Yann Dirson29f4ad82006-06-24 00:04:05 +0200594if test -z "$no_edit"
595then
596 sed -e '
597 /^diff --git a\/.*/{
598 s///
599 q
600 }
601 /^#/d
602 ' "$GIT_DIR"/COMMIT_EDITMSG
603else
604 cat "$GIT_DIR"/COMMIT_EDITMSG
605fi |
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800606git-stripspace >"$GIT_DIR"/COMMIT_MSG
Santi_Béjarf8e2c542005-10-09 17:30:19 -0700607
608if cnt=`grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
609 git-stripspace |
610 wc -l` &&
611 test 0 -lt $cnt
Junio C Hamano0c091292005-08-08 17:03:14 -0700612then
Junio C Hamano130fcca2006-02-05 00:07:44 -0800613 if test -z "$TMP_INDEX"
614 then
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800615 tree=$(GIT_INDEX_FILE="$USE_INDEX" git-write-tree)
Junio C Hamano130fcca2006-02-05 00:07:44 -0800616 else
617 tree=$(GIT_INDEX_FILE="$TMP_INDEX" git-write-tree) &&
618 rm -f "$TMP_INDEX"
619 fi &&
Santi_Béjarf8e2c542005-10-09 17:30:19 -0700620 commit=$(cat "$GIT_DIR"/COMMIT_MSG | git-commit-tree $tree $PARENTS) &&
Shawn Pearce67644a42006-05-19 05:16:18 -0400621 rlogm=$(sed -e 1q "$GIT_DIR"/COMMIT_MSG) &&
Junio C Hamano23913dc2007-01-19 17:12:11 -0800622 git-update-ref -m "$GIT_REFLOG_ACTION: $rlogm" HEAD $commit "$current" &&
Luben Tuikova9cb3c62006-10-12 14:52:42 -0700623 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" &&
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800624 if test -f "$NEXT_INDEX"
625 then
626 mv "$NEXT_INDEX" "$THIS_INDEX"
627 else
628 : ;# happy
629 fi
Junio C Hamano0c091292005-08-08 17:03:14 -0700630else
631 echo >&2 "* no commit message? aborting commit."
632 false
633fi
Linus Torvalds170241b2005-06-19 19:57:01 -0700634ret="$?"
Junio C Hamano7d0c6882006-06-23 01:37:02 -0700635rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
Junio C Hamanoc93d88a2007-03-05 12:35:41 -0800636
637cd_to_toplevel
638
Junio C Hamano1536dd9c62006-02-11 18:55:43 -0800639if test -d "$GIT_DIR/rr-cache"
640then
641 git-rerere
642fi
Junio C Hamano89e2c5f2005-08-18 17:20:08 -0700643
Nicolas Pitreebd124c2006-12-14 23:15:44 -0500644if test "$ret" = 0
Junio C Hamano89e2c5f2005-08-18 17:20:08 -0700645then
Nicolas Pitreebd124c2006-12-14 23:15:44 -0500646 if test -x "$GIT_DIR"/hooks/post-commit
647 then
648 "$GIT_DIR"/hooks/post-commit
649 fi
650 if test -z "$quiet"
651 then
Michael S. Tsirkinc7263d42007-04-16 08:51:11 +0300652 commit=`git-diff-tree --always --shortstat --pretty="format:%h: %s"\
653 --summary --root HEAD --`
Nicolas Pitreebd124c2006-12-14 23:15:44 -0500654 echo "Created${initial_commit:+ initial} commit $commit"
Nicolas Pitreebd124c2006-12-14 23:15:44 -0500655 fi
Junio C Hamano89e2c5f2005-08-18 17:20:08 -0700656fi
Junio C Hamano61f5cb72006-10-24 21:48:55 -0700657
Linus Torvalds170241b2005-06-19 19:57:01 -0700658exit "$ret"