blob: ab43217be4b49ce71ffee461569e0e4b395dfb5d [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
Steven Grimmd1cc1302007-07-22 21:17:42 -07006USAGE='[-a | --interactive] [-s] [-v] [--no-verify] [-m <message> | -F <logfile> | (-C|-c) <commit> | --amend] [-u] [-e] [--author <author>] [--template <file>] [[-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 Hamano5be60072007-07-02 22:52:14 -070011git 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 Hamanoc8f20322007-10-02 11:47:58 -070028TMP_INDEX=
Junio C Hamanocf7bb582006-02-10 00:45:59 -080029THIS_INDEX="$GIT_DIR/index"
30NEXT_INDEX="$GIT_DIR/next-index$$"
31rm -f "$NEXT_INDEX"
Junio C Hamano5a798fb2006-02-05 16:08:01 -080032save_index () {
Junio C Hamanocc7d5bc2006-06-29 14:48:22 -070033 cp -p "$THIS_INDEX" "$NEXT_INDEX"
Junio C Hamanocf7bb582006-02-10 00:45:59 -080034}
35
Junio C Hamano5a798fb2006-02-05 16:08:01 -080036run_status () {
Junio C Hamanocf7bb582006-02-10 00:45:59 -080037 # If TMP_INDEX is defined, that means we are doing
38 # "--only" partial commit, and that index file is used
39 # to build the tree for the commit. Otherwise, if
40 # NEXT_INDEX exists, that is the index file used to
41 # make the commit. Otherwise we are using as-is commit
42 # so the regular index file is what we use to compare.
43 if test '' != "$TMP_INDEX"
44 then
Martin Waitz3de63c32006-10-07 21:07:40 +020045 GIT_INDEX_FILE="$TMP_INDEX"
46 export GIT_INDEX_FILE
Junio C Hamanocf7bb582006-02-10 00:45:59 -080047 elif test -f "$NEXT_INDEX"
48 then
Martin Waitz3de63c32006-10-07 21:07:40 +020049 GIT_INDEX_FILE="$NEXT_INDEX"
50 export GIT_INDEX_FILE
Junio C Hamanocf7bb582006-02-10 00:45:59 -080051 fi
52
Brian Hetro09b0d9d2007-08-26 14:35:26 -040053 if test "$status_only" = "t" -o "$use_status_color" = "t"; then
54 color=
55 else
56 color=--nocolor
57 fi
Junio C Hamano5be60072007-07-02 22:52:14 -070058 git runstatus ${color} \
Martin Waitz3de63c32006-10-07 21:07:40 +020059 ${verbose:+--verbose} \
60 ${amend:+--amend} \
Johannes Schindelin2074cb02006-09-12 22:45:12 +020061 ${untracked_files:+--untracked}
Junio C Hamano5a798fb2006-02-05 16:08:01 -080062}
63
Junio C Hamanocf7bb582006-02-10 00:45:59 -080064trap '
65 test -z "$TMP_INDEX" || {
66 test -f "$TMP_INDEX" && rm -f "$TMP_INDEX"
67 }
68 rm -f "$NEXT_INDEX"
69' 0
70
71################################################################
72# Command line argument parsing and sanity checking
73
Junio C Hamano130fcca2006-02-05 00:07:44 -080074all=
75also=
Paolo Bonzini6cbf07e2007-03-05 08:57:53 +010076interactive=
Junio C Hamano5a798fb2006-02-05 16:08:01 -080077only=
Junio C Hamano130fcca2006-02-05 00:07:44 -080078logfile=
79use_commit=
Junio C Hamanob4019f02006-03-02 21:04:05 -080080amend=
Jeff Kingcda8ab52006-06-23 09:43:38 -040081edit_flag=
Junio C Hamano130fcca2006-02-05 00:07:44 -080082no_edit=
83log_given=
84log_message=
85verify=t
Nicolas Pitreebd124c2006-12-14 23:15:44 -050086quiet=
Junio C Hamanocf7bb582006-02-10 00:45:59 -080087verbose=
Junio C Hamano130fcca2006-02-05 00:07:44 -080088signoff=
89force_author=
Junio C Hamanobba319b2006-02-14 12:40:20 -080090only_include_assumed=
Matthias Lederhofer443f8332006-05-22 23:02:06 +020091untracked_files=
Steven Grimmd1cc1302007-07-22 21:17:42 -070092templatefile="`git config commit.template`"
David Kastrup822f7c72007-09-23 22:42:08 +020093while test $# != 0
Junio C Hamano5fec3ef2005-06-25 02:22:05 -070094do
Martin Waitz3de63c32006-10-07 21:07:40 +020095 case "$1" in
96 -F|--F|-f|--f|--fi|--fil|--file)
97 case "$#" in 1) usage ;; esac
98 shift
99 no_edit=t
100 log_given=t$log_given
101 logfile="$1"
102 shift
103 ;;
104 -F*|-f*)
105 no_edit=t
106 log_given=t$log_given
107 logfile=`expr "z$1" : 'z-[Ff]\(.*\)'`
108 shift
109 ;;
110 --F=*|--f=*|--fi=*|--fil=*|--file=*)
111 no_edit=t
112 log_given=t$log_given
113 logfile=`expr "z$1" : 'z-[^=]*=\(.*\)'`
114 shift
115 ;;
116 -a|--a|--al|--all)
117 all=t
118 shift
119 ;;
120 --au=*|--aut=*|--auth=*|--autho=*|--author=*)
121 force_author=`expr "z$1" : 'z-[^=]*=\(.*\)'`
122 shift
123 ;;
124 --au|--aut|--auth|--autho|--author)
125 case "$#" in 1) usage ;; esac
126 shift
127 force_author="$1"
128 shift
129 ;;
130 -e|--e|--ed|--edi|--edit)
131 edit_flag=t
132 shift
133 ;;
134 -i|--i|--in|--inc|--incl|--inclu|--includ|--include)
135 also=t
136 shift
137 ;;
Paolo Bonzini6cbf07e2007-03-05 08:57:53 +0100138 --int|--inte|--inter|--intera|--interac|--interact|--interacti|\
139 --interactiv|--interactive)
140 interactive=t
141 shift
142 ;;
Martin Waitz3de63c32006-10-07 21:07:40 +0200143 -o|--o|--on|--onl|--only)
144 only=t
145 shift
146 ;;
147 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
148 case "$#" in 1) usage ;; esac
149 shift
150 log_given=m$log_given
151 if test "$log_message" = ''
152 then
153 log_message="$1"
154 else
155 log_message="$log_message
Shawn Pearce68912812006-05-29 04:45:49 -0400156
157$1"
Martin Waitz3de63c32006-10-07 21:07:40 +0200158 fi
159 no_edit=t
160 shift
161 ;;
162 -m*)
163 log_given=m$log_given
164 if test "$log_message" = ''
165 then
166 log_message=`expr "z$1" : 'z-m\(.*\)'`
167 else
168 log_message="$log_message
Shawn Pearce68912812006-05-29 04:45:49 -0400169
Dennis Stosberg8096fae2006-06-27 18:54:26 +0200170`expr "z$1" : 'z-m\(.*\)'`"
Martin Waitz3de63c32006-10-07 21:07:40 +0200171 fi
172 no_edit=t
173 shift
174 ;;
175 --m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
176 log_given=m$log_given
177 if test "$log_message" = ''
178 then
179 log_message=`expr "z$1" : 'z-[^=]*=\(.*\)'`
180 else
181 log_message="$log_message
Shawn Pearce68912812006-05-29 04:45:49 -0400182
Dennis Stosberg8096fae2006-06-27 18:54:26 +0200183`expr "z$1" : 'zq-[^=]*=\(.*\)'`"
Martin Waitz3de63c32006-10-07 21:07:40 +0200184 fi
185 no_edit=t
186 shift
187 ;;
188 -n|--n|--no|--no-|--no-v|--no-ve|--no-ver|--no-veri|--no-verif|\
189 --no-verify)
190 verify=
191 shift
192 ;;
193 --a|--am|--ame|--amen|--amend)
194 amend=t
Martin Waitz3de63c32006-10-07 21:07:40 +0200195 use_commit=HEAD
196 shift
197 ;;
198 -c)
199 case "$#" in 1) usage ;; esac
200 shift
201 log_given=t$log_given
202 use_commit="$1"
203 no_edit=
204 shift
205 ;;
206 --ree=*|--reed=*|--reedi=*|--reedit=*|--reedit-=*|--reedit-m=*|\
207 --reedit-me=*|--reedit-mes=*|--reedit-mess=*|--reedit-messa=*|\
208 --reedit-messag=*|--reedit-message=*)
209 log_given=t$log_given
210 use_commit=`expr "z$1" : 'z-[^=]*=\(.*\)'`
211 no_edit=
212 shift
213 ;;
214 --ree|--reed|--reedi|--reedit|--reedit-|--reedit-m|--reedit-me|\
215 --reedit-mes|--reedit-mess|--reedit-messa|--reedit-messag|\
216 --reedit-message)
217 case "$#" in 1) usage ;; esac
218 shift
219 log_given=t$log_given
220 use_commit="$1"
221 no_edit=
222 shift
223 ;;
224 -C)
225 case "$#" in 1) usage ;; esac
226 shift
227 log_given=t$log_given
228 use_commit="$1"
229 no_edit=t
230 shift
231 ;;
232 --reu=*|--reus=*|--reuse=*|--reuse-=*|--reuse-m=*|--reuse-me=*|\
233 --reuse-mes=*|--reuse-mess=*|--reuse-messa=*|--reuse-messag=*|\
234 --reuse-message=*)
235 log_given=t$log_given
236 use_commit=`expr "z$1" : 'z-[^=]*=\(.*\)'`
237 no_edit=t
238 shift
239 ;;
240 --reu|--reus|--reuse|--reuse-|--reuse-m|--reuse-me|--reuse-mes|\
241 --reuse-mess|--reuse-messa|--reuse-messag|--reuse-message)
242 case "$#" in 1) usage ;; esac
243 shift
244 log_given=t$log_given
245 use_commit="$1"
246 no_edit=t
247 shift
248 ;;
249 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
250 signoff=t
251 shift
252 ;;
Steven Grimmd1cc1302007-07-22 21:17:42 -0700253 -t|--t|--te|--tem|--temp|--templ|--templa|--templat|--template)
254 case "$#" in 1) usage ;; esac
255 shift
256 templatefile="$1"
257 no_edit=
258 shift
259 ;;
Nicolas Pitreebd124c2006-12-14 23:15:44 -0500260 -q|--q|--qu|--qui|--quie|--quiet)
261 quiet=t
262 shift
263 ;;
Martin Waitz3de63c32006-10-07 21:07:40 +0200264 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
265 verbose=t
266 shift
267 ;;
268 -u|--u|--un|--unt|--untr|--untra|--untrac|--untrack|--untracke|\
269 --untracked|--untracked-|--untracked-f|--untracked-fi|--untracked-fil|\
270 --untracked-file|--untracked-files)
271 untracked_files=t
272 shift
273 ;;
274 --)
275 shift
276 break
277 ;;
278 -*)
279 usage
280 ;;
281 *)
282 break
283 ;;
284 esac
Junio C Hamano5fec3ef2005-06-25 02:22:05 -0700285done
Jeff Kingcda8ab52006-06-23 09:43:38 -0400286case "$edit_flag" in t) no_edit= ;; esac
Junio C Hamano5fec3ef2005-06-25 02:22:05 -0700287
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800288################################################################
289# Sanity check options
290
Junio C Hamanob4019f02006-03-02 21:04:05 -0800291case "$amend,$initial_commit" in
292t,t)
Martin Waitz3de63c32006-10-07 21:07:40 +0200293 die "You do not have anything to amend." ;;
Junio C Hamanob4019f02006-03-02 21:04:05 -0800294t,)
Martin Waitz3de63c32006-10-07 21:07:40 +0200295 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
296 die "You are in the middle of a merge -- cannot amend."
297 fi ;;
Junio C Hamanob4019f02006-03-02 21:04:05 -0800298esac
299
Junio C Hamano0c091292005-08-08 17:03:14 -0700300case "$log_given" in
301tt*)
Junio C Hamano6d4bbeb2007-08-01 18:14:41 -0700302 die "Only one of -c/-C/-F can be used." ;;
Shawn Pearce68912812006-05-29 04:45:49 -0400303*tm*|*mt*)
Junio C Hamano6d4bbeb2007-08-01 18:14:41 -0700304 die "Option -m cannot be combined with -c/-C/-F." ;;
Junio C Hamano0c091292005-08-08 17:03:14 -0700305esac
306
Junio C Hamano6a746422006-04-20 01:20:56 -0700307case "$#,$also,$only,$amend" in
308*,t,t,*)
Martin Waitz3de63c32006-10-07 21:07:40 +0200309 die "Only one of --include/--only can be used." ;;
Junio C Hamano6a746422006-04-20 01:20:56 -07003100,t,,* | 0,,t,)
Martin Waitz3de63c32006-10-07 21:07:40 +0200311 die "No paths with --include/--only does not make sense." ;;
Junio C Hamano6a746422006-04-20 01:20:56 -07003120,,t,t)
Martin Waitz3de63c32006-10-07 21:07:40 +0200313 only_include_assumed="# Clever... amending the last one with dirty index." ;;
Junio C Hamano6a746422006-04-20 01:20:56 -07003140,,,*)
Martin Waitz3de63c32006-10-07 21:07:40 +0200315 ;;
Junio C Hamano6a746422006-04-20 01:20:56 -0700316*,,,*)
Martin Waitz3de63c32006-10-07 21:07:40 +0200317 only_include_assumed="# Explicit paths specified without -i nor -o; assuming --only paths..."
318 also=
319 ;;
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800320esac
321unset only
Paolo Bonzini6cbf07e2007-03-05 08:57:53 +0100322case "$all,$interactive,$also,$#" in
323*t,*t,*)
324 die "Cannot use -a, --interactive or -i at the same time." ;;
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800325t,,[1-9]*)
326 die "Paths with -a does not make sense." ;;
Paolo Bonzini6cbf07e2007-03-05 08:57:53 +0100327,t,[1-9]*)
328 die "Paths with --interactive does not make sense." ;;
329,,t,0)
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800330 die "No paths with -i does not make sense." ;;
331esac
332
Steven Grimmd1cc1302007-07-22 21:17:42 -0700333if test ! -z "$templatefile" -a -z "$log_given"
334then
335 if test ! -f "$templatefile"
336 then
337 die "Commit template file does not exist."
338 fi
339fi
340
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800341################################################################
342# Prepare index to have a tree to be committed
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800343
Junio C Hamano130fcca2006-02-05 00:07:44 -0800344case "$all,$also" in
Junio C Hamano130fcca2006-02-05 00:07:44 -0800345t,)
Fredrik Kuivinen755b99d2007-02-22 21:28:12 +0100346 if test ! -f "$THIS_INDEX"
347 then
348 die 'nothing to commit (use "git add file1 file2" to include for commit)'
349 fi
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800350 save_index &&
Junio C Hamano130fcca2006-02-05 00:07:44 -0800351 (
Junio C Hamano514c09f2007-01-12 12:49:05 -0800352 cd_to_toplevel &&
353 GIT_INDEX_FILE="$NEXT_INDEX" &&
354 export GIT_INDEX_FILE &&
Junio C Hamano5be60072007-07-02 22:52:14 -0700355 git diff-files --name-only -z |
356 git update-index --remove -z --stdin
Junio C Hamano514c09f2007-01-12 12:49:05 -0800357 ) || exit
Junio C Hamano130fcca2006-02-05 00:07:44 -0800358 ;;
359,t)
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800360 save_index &&
Junio C Hamano5be60072007-07-02 22:52:14 -0700361 git ls-files --error-unmatch -- "$@" >/dev/null || exit
Junio C Hamanobba319b2006-02-14 12:40:20 -0800362
Junio C Hamano5be60072007-07-02 22:52:14 -0700363 git diff-files --name-only -z -- "$@" |
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800364 (
Junio C Hamano514c09f2007-01-12 12:49:05 -0800365 cd_to_toplevel &&
366 GIT_INDEX_FILE="$NEXT_INDEX" &&
367 export GIT_INDEX_FILE &&
Junio C Hamano5be60072007-07-02 22:52:14 -0700368 git update-index --remove -z --stdin
Junio C Hamano514c09f2007-01-12 12:49:05 -0800369 ) || exit
Junio C Hamano22cff6a2005-08-16 18:08:19 -0700370 ;;
Junio C Hamano130fcca2006-02-05 00:07:44 -0800371,)
Paolo Bonzini6cbf07e2007-03-05 08:57:53 +0100372 if test "$interactive" = t; then
373 git add --interactive || exit
374 fi
Junio C Hamano130fcca2006-02-05 00:07:44 -0800375 case "$#" in
376 0)
Martin Waitz3de63c32006-10-07 21:07:40 +0200377 ;; # commit as-is
Junio C Hamano130fcca2006-02-05 00:07:44 -0800378 *)
Martin Waitz3de63c32006-10-07 21:07:40 +0200379 if test -f "$GIT_DIR/MERGE_HEAD"
380 then
381 refuse_partial "Cannot do a partial commit during a merge."
382 fi
Junio C Hamano64586e72007-09-12 16:04:22 -0700383
Martin Waitz3de63c32006-10-07 21:07:40 +0200384 TMP_INDEX="$GIT_DIR/tmp-index$$"
Junio C Hamano64586e72007-09-12 16:04:22 -0700385 W=
386 test -z "$initial_commit" && W=--with-tree=HEAD
387 commit_only=`git ls-files --error-unmatch $W -- "$@"` || exit
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800388
Junio C Hamano158d0572006-12-09 22:32:43 -0800389 # Build a temporary index and update the real index
Martin Waitz3de63c32006-10-07 21:07:40 +0200390 # the same way.
391 if test -z "$initial_commit"
392 then
Junio C Hamano5e7f56a2007-03-31 23:27:41 -0700393 GIT_INDEX_FILE="$THIS_INDEX" \
Junio C Hamano5be60072007-07-02 22:52:14 -0700394 git read-tree --index-output="$TMP_INDEX" -i -m HEAD
Martin Waitz3de63c32006-10-07 21:07:40 +0200395 else
396 rm -f "$TMP_INDEX"
397 fi || exit
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800398
Junio C Hamano293623e2007-05-25 22:00:54 -0700399 printf '%s\n' "$commit_only" |
Martin Waitz3de63c32006-10-07 21:07:40 +0200400 GIT_INDEX_FILE="$TMP_INDEX" \
Junio C Hamano5be60072007-07-02 22:52:14 -0700401 git update-index --add --remove --stdin &&
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800402
Martin Waitz3de63c32006-10-07 21:07:40 +0200403 save_index &&
Junio C Hamano293623e2007-05-25 22:00:54 -0700404 printf '%s\n' "$commit_only" |
Martin Waitz3de63c32006-10-07 21:07:40 +0200405 (
406 GIT_INDEX_FILE="$NEXT_INDEX"
407 export GIT_INDEX_FILE
Junio C Hamanocba8d482007-09-14 16:53:58 -0700408 git update-index --add --remove --stdin
Martin Waitz3de63c32006-10-07 21:07:40 +0200409 ) || exit
410 ;;
Junio C Hamano130fcca2006-02-05 00:07:44 -0800411 esac
Junio C Hamanof678dd12005-11-25 13:33:14 -0800412 ;;
Junio C Hamano130fcca2006-02-05 00:07:44 -0800413esac
414
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800415################################################################
416# If we do as-is commit, the index file will be THIS_INDEX,
417# otherwise NEXT_INDEX after we make this commit. We leave
418# the index as is if we abort.
Junio C Hamano0c091292005-08-08 17:03:14 -0700419
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800420if test -f "$NEXT_INDEX"
Junio C Hamano130fcca2006-02-05 00:07:44 -0800421then
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800422 USE_INDEX="$NEXT_INDEX"
423else
424 USE_INDEX="$THIS_INDEX"
Junio C Hamano130fcca2006-02-05 00:07:44 -0800425fi
426
Junio C Hamano2b5f9a82007-02-22 00:28:49 -0800427case "$status_only" in
428t)
429 # This will silently fail in a read-only repository, which is
430 # what we want.
Junio C Hamano5be60072007-07-02 22:52:14 -0700431 GIT_INDEX_FILE="$USE_INDEX" git update-index -q --unmerged --refresh
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800432 run_status
433 exit $?
Junio C Hamano2b5f9a82007-02-22 00:28:49 -0800434 ;;
435'')
Junio C Hamano5be60072007-07-02 22:52:14 -0700436 GIT_INDEX_FILE="$USE_INDEX" git update-index -q --refresh || exit
Junio C Hamano2b5f9a82007-02-22 00:28:49 -0800437 ;;
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800438esac
439
440################################################################
441# Grab commit message, write out tree and make commit.
442
Junio C Hamano130fcca2006-02-05 00:07:44 -0800443if test t = "$verify" && test -x "$GIT_DIR"/hooks/pre-commit
444then
445 if test "$TMP_INDEX"
446 then
447 GIT_INDEX_FILE="$TMP_INDEX" "$GIT_DIR"/hooks/pre-commit
448 else
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800449 GIT_INDEX_FILE="$USE_INDEX" "$GIT_DIR"/hooks/pre-commit
Junio C Hamano130fcca2006-02-05 00:07:44 -0800450 fi || exit
451fi
Junio C Hamano0cfe1d32005-08-12 23:39:15 -0700452
Junio C Hamanoaba2da12005-08-25 18:57:35 -0700453if test "$log_message" != ''
454then
Junio C Hamano293623e2007-05-25 22:00:54 -0700455 printf '%s\n' "$log_message"
Junio C Hamanoaba2da12005-08-25 18:57:35 -0700456elif test "$logfile" != ""
457then
458 if test "$logfile" = -
459 then
460 test -t 0 &&
461 echo >&2 "(reading log message from standard input)"
462 cat
463 else
464 cat <"$logfile"
465 fi
466elif test "$use_commit" != ""
467then
Tom Princee0d10e12007-01-28 16:16:53 -0800468 encoding=$(git config i18n.commitencoding || echo UTF-8)
Junio C Hamano5ac27152007-01-13 13:33:07 -0800469 git show -s --pretty=raw --encoding="$encoding" "$use_commit" |
470 sed -e '1,/^$/d' -e 's/^ //'
Luben Tuikova9cb3c62006-10-12 14:52:42 -0700471elif test -f "$GIT_DIR/MERGE_MSG"
Junio C Hamanoe5215802005-11-01 22:01:28 -0800472then
473 cat "$GIT_DIR/MERGE_MSG"
Junio C Hamano7d0c6882006-06-23 01:37:02 -0700474elif test -f "$GIT_DIR/SQUASH_MSG"
475then
476 cat "$GIT_DIR/SQUASH_MSG"
Steven Grimmd1cc1302007-07-22 21:17:42 -0700477elif test "$templatefile" != ""
478then
479 cat "$templatefile"
Junio C Hamano5be60072007-07-02 22:52:14 -0700480fi | git stripspace >"$GIT_DIR"/COMMIT_EDITMSG
Junio C Hamano2d569932005-08-31 17:15:25 -0700481
482case "$signoff" in
483t)
Gerrit Papea7342912007-07-06 14:42:27 +0000484 sign=$(git-var GIT_COMMITTER_IDENT | sed -e '
485 s/>.*/>/
486 s/^/Signed-off-by: /
487 ')
488 blank_before_signoff=
Junio C Hamanodbaa06a2007-01-29 01:06:27 -0800489 tail -n 1 "$GIT_DIR"/COMMIT_EDITMSG |
Gerrit Papea7342912007-07-06 14:42:27 +0000490 grep 'Signed-off-by:' >/dev/null || blank_before_signoff='
491'
492 tail -n 1 "$GIT_DIR"/COMMIT_EDITMSG |
493 grep "$sign"$ >/dev/null ||
494 printf '%s%s\n' "$blank_before_signoff" "$sign" \
495 >>"$GIT_DIR"/COMMIT_EDITMSG
Junio C Hamano2d569932005-08-31 17:15:25 -0700496 ;;
497esac
498
Junio C Hamano475443c2006-04-12 11:45:18 -0700499if test -f "$GIT_DIR/MERGE_HEAD" && test -z "$no_edit"; then
Junio C Hamano2d569932005-08-31 17:15:25 -0700500 echo "#"
Alex Riesenb6ae5402006-01-05 12:44:59 +0100501 echo "# It looks like you may be committing a MERGE."
Junio C Hamano2d569932005-08-31 17:15:25 -0700502 echo "# If this is not correct, please remove the file"
Junio C Hamano293623e2007-05-25 22:00:54 -0700503 printf '%s\n' "# $GIT_DIR/MERGE_HEAD"
Junio C Hamano2d569932005-08-31 17:15:25 -0700504 echo "# and try again"
505 echo "#"
Santi_Béjarf8e2c542005-10-09 17:30:19 -0700506fi >>"$GIT_DIR"/COMMIT_EDITMSG
Junio C Hamanoaba2da12005-08-25 18:57:35 -0700507
Junio C Hamano130fcca2006-02-05 00:07:44 -0800508# Author
Junio C Hamano83e24dc2007-01-22 13:03:31 -0800509if test '' != "$use_commit"
Junio C Hamano130fcca2006-02-05 00:07:44 -0800510then
Johannes Schindelin0cae2342007-06-25 01:04:11 +0100511 eval "$(get_author_ident_from_commit "$use_commit")"
512 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
Junio C Hamano130fcca2006-02-05 00:07:44 -0800513fi
Junio C Hamano83e24dc2007-01-22 13:03:31 -0800514if test '' != "$force_author"
515then
516 GIT_AUTHOR_NAME=`expr "z$force_author" : 'z\(.*[^ ]\) *<.*'` &&
517 GIT_AUTHOR_EMAIL=`expr "z$force_author" : '.*\(<.*\)'` &&
518 test '' != "$GIT_AUTHOR_NAME" &&
519 test '' != "$GIT_AUTHOR_EMAIL" ||
520 die "malformed --author parameter"
521 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
522fi
Junio C Hamano130fcca2006-02-05 00:07:44 -0800523
Linus Torvalds96069cf2005-06-14 10:20:14 -0700524PARENTS="-p HEAD"
Junio C Hamano130fcca2006-02-05 00:07:44 -0800525if test -z "$initial_commit"
Junio C Hamano8098a172005-09-30 14:26:57 -0700526then
Shawn Pearcea3a733e2006-07-10 22:48:47 -0400527 rloga='commit'
Junio C Hamano5fec3ef2005-06-25 02:22:05 -0700528 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
Shawn Pearcea3a733e2006-07-10 22:48:47 -0400529 rloga='commit (merge)'
Junio C Hamano91063bb2005-09-08 13:47:12 -0700530 PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
Junio C Hamanob4019f02006-03-02 21:04:05 -0800531 elif test -n "$amend"; then
Shawn Pearcea3a733e2006-07-10 22:48:47 -0400532 rloga='commit (amend)'
Junio C Hamano5be60072007-07-02 22:52:14 -0700533 PARENTS=$(git cat-file commit HEAD |
Junio C Hamanob4019f02006-03-02 21:04:05 -0800534 sed -n -e '/^$/q' -e 's/^parent /-p /p')
Junio C Hamanoaba2da12005-08-25 18:57:35 -0700535 fi
Junio C Hamano5be60072007-07-02 22:52:14 -0700536 current="$(git rev-parse --verify HEAD)"
Junio C Hamano8098a172005-09-30 14:26:57 -0700537else
Junio C Hamano5be60072007-07-02 22:52:14 -0700538 if [ -z "$(git ls-files)" ]; then
Shawn O. Pearceaeb80c72006-12-15 21:53:09 -0500539 echo >&2 'nothing to commit (use "git add file1 file2" to include for commit)'
Junio C Hamano8098a172005-09-30 14:26:57 -0700540 exit 1
541 fi
542 PARENTS=""
Shawn Pearcea3a733e2006-07-10 22:48:47 -0400543 rloga='commit (initial)'
Junio C Hamanocede7522006-09-27 02:06:31 -0700544 current=''
Linus Torvalds96069cf2005-06-14 10:20:14 -0700545fi
Junio C Hamano23913dc2007-01-19 17:12:11 -0800546set_reflog_action "$rloga"
Junio C Hamano130fcca2006-02-05 00:07:44 -0800547
Junio C Hamano475443c2006-04-12 11:45:18 -0700548if test -z "$no_edit"
549then
550 {
Martin Waitz88a15312006-05-26 01:42:18 +0200551 echo ""
552 echo "# Please enter the commit message for your changes."
553 echo "# (Comment lines starting with '#' will not be included)"
Junio C Hamano475443c2006-04-12 11:45:18 -0700554 test -z "$only_include_assumed" || echo "$only_include_assumed"
555 run_status
556 } >>"$GIT_DIR"/COMMIT_EDITMSG
557else
558 # we need to check if there is anything to commit
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700559 run_status >/dev/null
Junio C Hamano475443c2006-04-12 11:45:18 -0700560fi
Dmitry V. Levin4fb5fd52007-09-12 20:14:22 +0400561if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" ]
Linus Torvaldsa3e870f2005-05-30 12:51:00 -0700562then
Junio C Hamano7d0c6882006-06-23 01:37:02 -0700563 rm -f "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
Brian Hetro09b0d9d2007-08-26 14:35:26 -0400564 use_status_color=t
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'')
Seanec4e69c2006-05-13 23:09:32 -0400571 git-var GIT_AUTHOR_IDENT > /dev/null || die
572 git-var GIT_COMMITTER_IDENT > /dev/null || die
Adam Robenef0c2ab2007-07-19 22:09:35 -0700573 git_editor "$GIT_DIR/COMMIT_EDITMSG"
Junio C Hamano5fec3ef2005-06-25 02:22:05 -0700574 ;;
575esac
Junio C Hamano89e2c5f2005-08-18 17:20:08 -0700576
577case "$verify" in
578t)
579 if test -x "$GIT_DIR"/hooks/commit-msg
580 then
Santi_Béjarf8e2c542005-10-09 17:30:19 -0700581 "$GIT_DIR"/hooks/commit-msg "$GIT_DIR"/COMMIT_EDITMSG || exit
Junio C Hamano89e2c5f2005-08-18 17:20:08 -0700582 fi
583esac
584
Yann Dirson29f4ad82006-06-24 00:04:05 +0200585if test -z "$no_edit"
586then
587 sed -e '
588 /^diff --git a\/.*/{
589 s///
590 q
591 }
592 /^#/d
593 ' "$GIT_DIR"/COMMIT_EDITMSG
594else
595 cat "$GIT_DIR"/COMMIT_EDITMSG
596fi |
Junio C Hamano5be60072007-07-02 22:52:14 -0700597git stripspace >"$GIT_DIR"/COMMIT_MSG
Santi_Béjarf8e2c542005-10-09 17:30:19 -0700598
Steven Grimmd1cc1302007-07-22 21:17:42 -0700599# Test whether the commit message has any content we didn't supply.
600have_commitmsg=
601grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
602 git stripspace > "$GIT_DIR"/COMMIT_BAREMSG
603
604# Is the commit message totally empty?
605if test -s "$GIT_DIR"/COMMIT_BAREMSG
606then
607 if test "$templatefile" != ""
608 then
609 # Test whether this is just the unaltered template.
610 if cnt=`sed -e '/^#/d' < "$templatefile" |
611 git stripspace |
612 diff "$GIT_DIR"/COMMIT_BAREMSG - |
613 wc -l` &&
614 test 0 -lt $cnt
615 then
616 have_commitmsg=t
617 fi
618 else
619 # No template, so the content in the commit message must
620 # have come from the user.
621 have_commitmsg=t
622 fi
623fi
624
625rm -f "$GIT_DIR"/COMMIT_BAREMSG
626
627if test "$have_commitmsg" = "t"
Junio C Hamano0c091292005-08-08 17:03:14 -0700628then
Junio C Hamano130fcca2006-02-05 00:07:44 -0800629 if test -z "$TMP_INDEX"
630 then
Junio C Hamano5be60072007-07-02 22:52:14 -0700631 tree=$(GIT_INDEX_FILE="$USE_INDEX" git write-tree)
Junio C Hamano130fcca2006-02-05 00:07:44 -0800632 else
Junio C Hamano5be60072007-07-02 22:52:14 -0700633 tree=$(GIT_INDEX_FILE="$TMP_INDEX" git write-tree) &&
Junio C Hamano130fcca2006-02-05 00:07:44 -0800634 rm -f "$TMP_INDEX"
635 fi &&
Josh Triplett9d6f2202007-07-14 01:05:43 -0700636 commit=$(git commit-tree $tree $PARENTS <"$GIT_DIR/COMMIT_MSG") &&
Shawn Pearce67644a42006-05-19 05:16:18 -0400637 rlogm=$(sed -e 1q "$GIT_DIR"/COMMIT_MSG) &&
Junio C Hamano5be60072007-07-02 22:52:14 -0700638 git update-ref -m "$GIT_REFLOG_ACTION: $rlogm" HEAD $commit "$current" &&
Luben Tuikova9cb3c62006-10-12 14:52:42 -0700639 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" &&
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800640 if test -f "$NEXT_INDEX"
641 then
642 mv "$NEXT_INDEX" "$THIS_INDEX"
643 else
644 : ;# happy
645 fi
Junio C Hamano0c091292005-08-08 17:03:14 -0700646else
647 echo >&2 "* no commit message? aborting commit."
648 false
649fi
Linus Torvalds170241b2005-06-19 19:57:01 -0700650ret="$?"
Junio C Hamano7d0c6882006-06-23 01:37:02 -0700651rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
Junio C Hamanoc93d88a2007-03-05 12:35:41 -0800652
653cd_to_toplevel
654
Johannes Schindelinb4372ef2007-07-06 13:05:59 +0100655git rerere
Junio C Hamano89e2c5f2005-08-18 17:20:08 -0700656
Nicolas Pitreebd124c2006-12-14 23:15:44 -0500657if test "$ret" = 0
Junio C Hamano89e2c5f2005-08-18 17:20:08 -0700658then
Nicolas Pitreebd124c2006-12-14 23:15:44 -0500659 if test -x "$GIT_DIR"/hooks/post-commit
660 then
661 "$GIT_DIR"/hooks/post-commit
662 fi
663 if test -z "$quiet"
664 then
Junio C Hamano5be60072007-07-02 22:52:14 -0700665 commit=`git diff-tree --always --shortstat --pretty="format:%h: %s"\
Michael S. Tsirkinc7263d42007-04-16 08:51:11 +0300666 --summary --root HEAD --`
Nicolas Pitreebd124c2006-12-14 23:15:44 -0500667 echo "Created${initial_commit:+ initial} commit $commit"
Nicolas Pitreebd124c2006-12-14 23:15:44 -0500668 fi
Junio C Hamano89e2c5f2005-08-18 17:20:08 -0700669fi
Junio C Hamano61f5cb72006-10-24 21:48:55 -0700670
Linus Torvalds170241b2005-06-19 19:57:01 -0700671exit "$ret"