blob: 23ffb028d1ece96d8c363ddeacca83d2b20b628f [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 Hamano8f321a32007-11-06 01:50:02 -08008OPTIONS_SPEC=
Junio C Hamanoae2b0f12005-11-24 00:12:11 -08009. git-sh-setup
Shawn O. Pearce7eff28a2006-12-30 23:32:38 -050010require_work_tree
Linus Torvaldsb33e9662005-07-08 10:57:21 -070011
Junio C Hamano5be60072007-07-02 22:52:14 -070012git rev-parse --verify HEAD >/dev/null 2>&1 || initial_commit=t
Junio C Hamanocf7bb582006-02-10 00:45:59 -080013
14case "$0" in
15*status)
16 status_only=t
Junio C Hamano2b5f9a82007-02-22 00:28:49 -080017 ;;
Junio C Hamanocf7bb582006-02-10 00:45:59 -080018*commit)
19 status_only=
Junio C Hamano2b5f9a82007-02-22 00:28:49 -080020 ;;
Junio C Hamanocf7bb582006-02-10 00:45:59 -080021esac
Junio C Hamano130fcca2006-02-05 00:07:44 -080022
23refuse_partial () {
24 echo >&2 "$1"
Junio C Hamano130fcca2006-02-05 00:07:44 -080025 echo >&2 "You might have meant to say 'git commit -i paths...', perhaps?"
26 exit 1
27}
28
Junio C Hamanoc8f20322007-10-02 11:47:58 -070029TMP_INDEX=
Rémi Vanicat859a4db2007-11-11 13:28:08 +010030THIS_INDEX="${GIT_INDEX_FILE:-$GIT_DIR/index}"
Junio C Hamanocf7bb582006-02-10 00:45:59 -080031NEXT_INDEX="$GIT_DIR/next-index$$"
32rm -f "$NEXT_INDEX"
Junio C Hamano5a798fb2006-02-05 16:08:01 -080033save_index () {
Junio C Hamanocc7d5bc2006-06-29 14:48:22 -070034 cp -p "$THIS_INDEX" "$NEXT_INDEX"
Junio C Hamanocf7bb582006-02-10 00:45:59 -080035}
36
Junio C Hamano5a798fb2006-02-05 16:08:01 -080037run_status () {
Junio C Hamanocf7bb582006-02-10 00:45:59 -080038 # If TMP_INDEX is defined, that means we are doing
39 # "--only" partial commit, and that index file is used
40 # to build the tree for the commit. Otherwise, if
41 # NEXT_INDEX exists, that is the index file used to
42 # make the commit. Otherwise we are using as-is commit
43 # so the regular index file is what we use to compare.
44 if test '' != "$TMP_INDEX"
45 then
Martin Waitz3de63c32006-10-07 21:07:40 +020046 GIT_INDEX_FILE="$TMP_INDEX"
47 export GIT_INDEX_FILE
Junio C Hamanocf7bb582006-02-10 00:45:59 -080048 elif test -f "$NEXT_INDEX"
49 then
Martin Waitz3de63c32006-10-07 21:07:40 +020050 GIT_INDEX_FILE="$NEXT_INDEX"
51 export GIT_INDEX_FILE
Junio C Hamanocf7bb582006-02-10 00:45:59 -080052 fi
53
Brian Hetro09b0d9d2007-08-26 14:35:26 -040054 if test "$status_only" = "t" -o "$use_status_color" = "t"; then
55 color=
56 else
57 color=--nocolor
58 fi
Junio C Hamano5be60072007-07-02 22:52:14 -070059 git runstatus ${color} \
Martin Waitz3de63c32006-10-07 21:07:40 +020060 ${verbose:+--verbose} \
61 ${amend:+--amend} \
Johannes Schindelin2074cb02006-09-12 22:45:12 +020062 ${untracked_files:+--untracked}
Junio C Hamano5a798fb2006-02-05 16:08:01 -080063}
64
Junio C Hamanocf7bb582006-02-10 00:45:59 -080065trap '
66 test -z "$TMP_INDEX" || {
67 test -f "$TMP_INDEX" && rm -f "$TMP_INDEX"
68 }
69 rm -f "$NEXT_INDEX"
70' 0
71
72################################################################
73# Command line argument parsing and sanity checking
74
Junio C Hamano130fcca2006-02-05 00:07:44 -080075all=
76also=
Junio C Hamano36863af2007-12-03 00:03:10 -080077allow_empty=f
Paolo Bonzini6cbf07e2007-03-05 08:57:53 +010078interactive=
Junio C Hamano5a798fb2006-02-05 16:08:01 -080079only=
Junio C Hamano130fcca2006-02-05 00:07:44 -080080logfile=
81use_commit=
Junio C Hamanob4019f02006-03-02 21:04:05 -080082amend=
Jeff Kingcda8ab52006-06-23 09:43:38 -040083edit_flag=
Junio C Hamano130fcca2006-02-05 00:07:44 -080084no_edit=
85log_given=
86log_message=
87verify=t
Nicolas Pitreebd124c2006-12-14 23:15:44 -050088quiet=
Junio C Hamanocf7bb582006-02-10 00:45:59 -080089verbose=
Junio C Hamano130fcca2006-02-05 00:07:44 -080090signoff=
91force_author=
Junio C Hamanobba319b2006-02-14 12:40:20 -080092only_include_assumed=
Matthias Lederhofer443f8332006-05-22 23:02:06 +020093untracked_files=
Steven Grimmd1cc1302007-07-22 21:17:42 -070094templatefile="`git config commit.template`"
David Kastrup822f7c72007-09-23 22:42:08 +020095while test $# != 0
Junio C Hamano5fec3ef2005-06-25 02:22:05 -070096do
Martin Waitz3de63c32006-10-07 21:07:40 +020097 case "$1" in
98 -F|--F|-f|--f|--fi|--fil|--file)
99 case "$#" in 1) usage ;; esac
100 shift
101 no_edit=t
102 log_given=t$log_given
103 logfile="$1"
Martin Waitz3de63c32006-10-07 21:07:40 +0200104 ;;
105 -F*|-f*)
106 no_edit=t
107 log_given=t$log_given
David Kastrupd2884042007-09-17 22:56:44 +0200108 logfile="${1#-[Ff]}"
Martin Waitz3de63c32006-10-07 21:07:40 +0200109 ;;
110 --F=*|--f=*|--fi=*|--fil=*|--file=*)
111 no_edit=t
112 log_given=t$log_given
David Kastrupd2884042007-09-17 22:56:44 +0200113 logfile="${1#*=}"
Martin Waitz3de63c32006-10-07 21:07:40 +0200114 ;;
115 -a|--a|--al|--all)
116 all=t
Martin Waitz3de63c32006-10-07 21:07:40 +0200117 ;;
Junio C Hamano36863af2007-12-03 00:03:10 -0800118 --allo|--allow|--allow-|--allow-e|--allow-em|--allow-emp|\
119 --allow-empt|--allow-empty)
120 allow_empty=t
121 ;;
Martin Waitz3de63c32006-10-07 21:07:40 +0200122 --au=*|--aut=*|--auth=*|--autho=*|--author=*)
David Kastrupd2884042007-09-17 22:56:44 +0200123 force_author="${1#*=}"
Martin Waitz3de63c32006-10-07 21:07:40 +0200124 ;;
125 --au|--aut|--auth|--autho|--author)
126 case "$#" in 1) usage ;; esac
127 shift
128 force_author="$1"
Martin Waitz3de63c32006-10-07 21:07:40 +0200129 ;;
130 -e|--e|--ed|--edi|--edit)
131 edit_flag=t
Martin Waitz3de63c32006-10-07 21:07:40 +0200132 ;;
133 -i|--i|--in|--inc|--incl|--inclu|--includ|--include)
134 also=t
Martin Waitz3de63c32006-10-07 21:07:40 +0200135 ;;
Paolo Bonzini6cbf07e2007-03-05 08:57:53 +0100136 --int|--inte|--inter|--intera|--interac|--interact|--interacti|\
137 --interactiv|--interactive)
138 interactive=t
Paolo Bonzini6cbf07e2007-03-05 08:57:53 +0100139 ;;
Martin Waitz3de63c32006-10-07 21:07:40 +0200140 -o|--o|--on|--onl|--only)
141 only=t
Martin Waitz3de63c32006-10-07 21:07:40 +0200142 ;;
143 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
144 case "$#" in 1) usage ;; esac
145 shift
146 log_given=m$log_given
David Kastrupd2884042007-09-17 22:56:44 +0200147 log_message="${log_message:+${log_message}
Shawn Pearce68912812006-05-29 04:45:49 -0400148
David Kastrupd2884042007-09-17 22:56:44 +0200149}$1"
Martin Waitz3de63c32006-10-07 21:07:40 +0200150 no_edit=t
Martin Waitz3de63c32006-10-07 21:07:40 +0200151 ;;
152 -m*)
153 log_given=m$log_given
David Kastrupd2884042007-09-17 22:56:44 +0200154 log_message="${log_message:+${log_message}
Shawn Pearce68912812006-05-29 04:45:49 -0400155
David Kastrupd2884042007-09-17 22:56:44 +0200156}${1#-m}"
Martin Waitz3de63c32006-10-07 21:07:40 +0200157 no_edit=t
Martin Waitz3de63c32006-10-07 21:07:40 +0200158 ;;
159 --m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
160 log_given=m$log_given
David Kastrupd2884042007-09-17 22:56:44 +0200161 log_message="${log_message:+${log_message}
Shawn Pearce68912812006-05-29 04:45:49 -0400162
David Kastrupd2884042007-09-17 22:56:44 +0200163}${1#*=}"
Martin Waitz3de63c32006-10-07 21:07:40 +0200164 no_edit=t
Martin Waitz3de63c32006-10-07 21:07:40 +0200165 ;;
166 -n|--n|--no|--no-|--no-v|--no-ve|--no-ver|--no-veri|--no-verif|\
167 --no-verify)
168 verify=
Martin Waitz3de63c32006-10-07 21:07:40 +0200169 ;;
170 --a|--am|--ame|--amen|--amend)
171 amend=t
Martin Waitz3de63c32006-10-07 21:07:40 +0200172 use_commit=HEAD
Martin Waitz3de63c32006-10-07 21:07:40 +0200173 ;;
174 -c)
175 case "$#" in 1) usage ;; esac
176 shift
177 log_given=t$log_given
178 use_commit="$1"
179 no_edit=
Martin Waitz3de63c32006-10-07 21:07:40 +0200180 ;;
181 --ree=*|--reed=*|--reedi=*|--reedit=*|--reedit-=*|--reedit-m=*|\
182 --reedit-me=*|--reedit-mes=*|--reedit-mess=*|--reedit-messa=*|\
183 --reedit-messag=*|--reedit-message=*)
184 log_given=t$log_given
David Kastrupd2884042007-09-17 22:56:44 +0200185 use_commit="${1#*=}"
Martin Waitz3de63c32006-10-07 21:07:40 +0200186 no_edit=
Martin Waitz3de63c32006-10-07 21:07:40 +0200187 ;;
188 --ree|--reed|--reedi|--reedit|--reedit-|--reedit-m|--reedit-me|\
189 --reedit-mes|--reedit-mess|--reedit-messa|--reedit-messag|\
190 --reedit-message)
191 case "$#" in 1) usage ;; esac
192 shift
193 log_given=t$log_given
194 use_commit="$1"
195 no_edit=
Martin Waitz3de63c32006-10-07 21:07:40 +0200196 ;;
197 -C)
198 case "$#" in 1) usage ;; esac
199 shift
200 log_given=t$log_given
201 use_commit="$1"
202 no_edit=t
Martin Waitz3de63c32006-10-07 21:07:40 +0200203 ;;
204 --reu=*|--reus=*|--reuse=*|--reuse-=*|--reuse-m=*|--reuse-me=*|\
205 --reuse-mes=*|--reuse-mess=*|--reuse-messa=*|--reuse-messag=*|\
206 --reuse-message=*)
207 log_given=t$log_given
David Kastrupd2884042007-09-17 22:56:44 +0200208 use_commit="${1#*=}"
Martin Waitz3de63c32006-10-07 21:07:40 +0200209 no_edit=t
Martin Waitz3de63c32006-10-07 21:07:40 +0200210 ;;
211 --reu|--reus|--reuse|--reuse-|--reuse-m|--reuse-me|--reuse-mes|\
212 --reuse-mess|--reuse-messa|--reuse-messag|--reuse-message)
213 case "$#" in 1) usage ;; esac
214 shift
215 log_given=t$log_given
216 use_commit="$1"
217 no_edit=t
Martin Waitz3de63c32006-10-07 21:07:40 +0200218 ;;
219 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
220 signoff=t
Martin Waitz3de63c32006-10-07 21:07:40 +0200221 ;;
Steven Grimmd1cc1302007-07-22 21:17:42 -0700222 -t|--t|--te|--tem|--temp|--templ|--templa|--templat|--template)
223 case "$#" in 1) usage ;; esac
224 shift
225 templatefile="$1"
226 no_edit=
Steven Grimmd1cc1302007-07-22 21:17:42 -0700227 ;;
Nicolas Pitreebd124c2006-12-14 23:15:44 -0500228 -q|--q|--qu|--qui|--quie|--quiet)
229 quiet=t
Nicolas Pitreebd124c2006-12-14 23:15:44 -0500230 ;;
Martin Waitz3de63c32006-10-07 21:07:40 +0200231 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
232 verbose=t
Martin Waitz3de63c32006-10-07 21:07:40 +0200233 ;;
234 -u|--u|--un|--unt|--untr|--untra|--untrac|--untrack|--untracke|\
235 --untracked|--untracked-|--untracked-f|--untracked-fi|--untracked-fil|\
236 --untracked-file|--untracked-files)
237 untracked_files=t
Martin Waitz3de63c32006-10-07 21:07:40 +0200238 ;;
239 --)
240 shift
241 break
242 ;;
243 -*)
244 usage
245 ;;
246 *)
247 break
248 ;;
249 esac
David Kastrupd2884042007-09-17 22:56:44 +0200250 shift
Junio C Hamano5fec3ef2005-06-25 02:22:05 -0700251done
Jeff Kingcda8ab52006-06-23 09:43:38 -0400252case "$edit_flag" in t) no_edit= ;; esac
Junio C Hamano5fec3ef2005-06-25 02:22:05 -0700253
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800254################################################################
255# Sanity check options
256
Junio C Hamanob4019f02006-03-02 21:04:05 -0800257case "$amend,$initial_commit" in
258t,t)
Martin Waitz3de63c32006-10-07 21:07:40 +0200259 die "You do not have anything to amend." ;;
Junio C Hamanob4019f02006-03-02 21:04:05 -0800260t,)
Martin Waitz3de63c32006-10-07 21:07:40 +0200261 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
262 die "You are in the middle of a merge -- cannot amend."
263 fi ;;
Junio C Hamanob4019f02006-03-02 21:04:05 -0800264esac
265
Junio C Hamano0c091292005-08-08 17:03:14 -0700266case "$log_given" in
267tt*)
Junio C Hamano6d4bbeb2007-08-01 18:14:41 -0700268 die "Only one of -c/-C/-F can be used." ;;
Shawn Pearce68912812006-05-29 04:45:49 -0400269*tm*|*mt*)
Junio C Hamano6d4bbeb2007-08-01 18:14:41 -0700270 die "Option -m cannot be combined with -c/-C/-F." ;;
Junio C Hamano0c091292005-08-08 17:03:14 -0700271esac
272
Junio C Hamano6a746422006-04-20 01:20:56 -0700273case "$#,$also,$only,$amend" in
274*,t,t,*)
Martin Waitz3de63c32006-10-07 21:07:40 +0200275 die "Only one of --include/--only can be used." ;;
Junio C Hamano6a746422006-04-20 01:20:56 -07002760,t,,* | 0,,t,)
Martin Waitz3de63c32006-10-07 21:07:40 +0200277 die "No paths with --include/--only does not make sense." ;;
Junio C Hamano6a746422006-04-20 01:20:56 -07002780,,t,t)
Martin Waitz3de63c32006-10-07 21:07:40 +0200279 only_include_assumed="# Clever... amending the last one with dirty index." ;;
Junio C Hamano6a746422006-04-20 01:20:56 -07002800,,,*)
Martin Waitz3de63c32006-10-07 21:07:40 +0200281 ;;
Junio C Hamano6a746422006-04-20 01:20:56 -0700282*,,,*)
Martin Waitz3de63c32006-10-07 21:07:40 +0200283 only_include_assumed="# Explicit paths specified without -i nor -o; assuming --only paths..."
284 also=
285 ;;
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800286esac
287unset only
Paolo Bonzini6cbf07e2007-03-05 08:57:53 +0100288case "$all,$interactive,$also,$#" in
289*t,*t,*)
290 die "Cannot use -a, --interactive or -i at the same time." ;;
Björn Steinbrink34cb7042007-11-05 20:36:33 +0100291t,,,[1-9]*)
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800292 die "Paths with -a does not make sense." ;;
Björn Steinbrink34cb7042007-11-05 20:36:33 +0100293,t,,[1-9]*)
Paolo Bonzini6cbf07e2007-03-05 08:57:53 +0100294 die "Paths with --interactive does not make sense." ;;
295,,t,0)
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800296 die "No paths with -i does not make sense." ;;
297esac
298
Steven Grimmd1cc1302007-07-22 21:17:42 -0700299if test ! -z "$templatefile" -a -z "$log_given"
300then
301 if test ! -f "$templatefile"
302 then
303 die "Commit template file does not exist."
304 fi
305fi
306
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800307################################################################
308# Prepare index to have a tree to be committed
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800309
Junio C Hamano130fcca2006-02-05 00:07:44 -0800310case "$all,$also" in
Junio C Hamano130fcca2006-02-05 00:07:44 -0800311t,)
Fredrik Kuivinen755b99d2007-02-22 21:28:12 +0100312 if test ! -f "$THIS_INDEX"
313 then
314 die 'nothing to commit (use "git add file1 file2" to include for commit)'
315 fi
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800316 save_index &&
Junio C Hamano130fcca2006-02-05 00:07:44 -0800317 (
Junio C Hamano514c09f2007-01-12 12:49:05 -0800318 cd_to_toplevel &&
319 GIT_INDEX_FILE="$NEXT_INDEX" &&
320 export GIT_INDEX_FILE &&
Junio C Hamano5be60072007-07-02 22:52:14 -0700321 git diff-files --name-only -z |
322 git update-index --remove -z --stdin
Junio C Hamano514c09f2007-01-12 12:49:05 -0800323 ) || exit
Junio C Hamano130fcca2006-02-05 00:07:44 -0800324 ;;
325,t)
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800326 save_index &&
Junio C Hamano5be60072007-07-02 22:52:14 -0700327 git ls-files --error-unmatch -- "$@" >/dev/null || exit
Junio C Hamanobba319b2006-02-14 12:40:20 -0800328
Junio C Hamano5be60072007-07-02 22:52:14 -0700329 git diff-files --name-only -z -- "$@" |
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800330 (
Junio C Hamano514c09f2007-01-12 12:49:05 -0800331 cd_to_toplevel &&
332 GIT_INDEX_FILE="$NEXT_INDEX" &&
333 export GIT_INDEX_FILE &&
Junio C Hamano5be60072007-07-02 22:52:14 -0700334 git update-index --remove -z --stdin
Junio C Hamano514c09f2007-01-12 12:49:05 -0800335 ) || exit
Junio C Hamano22cff6a2005-08-16 18:08:19 -0700336 ;;
Junio C Hamano130fcca2006-02-05 00:07:44 -0800337,)
Paolo Bonzini6cbf07e2007-03-05 08:57:53 +0100338 if test "$interactive" = t; then
339 git add --interactive || exit
340 fi
Junio C Hamano130fcca2006-02-05 00:07:44 -0800341 case "$#" in
342 0)
Martin Waitz3de63c32006-10-07 21:07:40 +0200343 ;; # commit as-is
Junio C Hamano130fcca2006-02-05 00:07:44 -0800344 *)
Martin Waitz3de63c32006-10-07 21:07:40 +0200345 if test -f "$GIT_DIR/MERGE_HEAD"
346 then
347 refuse_partial "Cannot do a partial commit during a merge."
348 fi
Junio C Hamano80bffaf2007-09-12 16:04:22 -0700349
Martin Waitz3de63c32006-10-07 21:07:40 +0200350 TMP_INDEX="$GIT_DIR/tmp-index$$"
Junio C Hamano80bffaf2007-09-12 16:04:22 -0700351 W=
352 test -z "$initial_commit" && W=--with-tree=HEAD
353 commit_only=`git ls-files --error-unmatch $W -- "$@"` || exit
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800354
Junio C Hamano158d0572006-12-09 22:32:43 -0800355 # Build a temporary index and update the real index
Martin Waitz3de63c32006-10-07 21:07:40 +0200356 # the same way.
357 if test -z "$initial_commit"
358 then
Junio C Hamano5e7f56a2007-03-31 23:27:41 -0700359 GIT_INDEX_FILE="$THIS_INDEX" \
Junio C Hamano5be60072007-07-02 22:52:14 -0700360 git read-tree --index-output="$TMP_INDEX" -i -m HEAD
Martin Waitz3de63c32006-10-07 21:07:40 +0200361 else
362 rm -f "$TMP_INDEX"
363 fi || exit
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800364
Junio C Hamano293623e2007-05-25 22:00:54 -0700365 printf '%s\n' "$commit_only" |
Martin Waitz3de63c32006-10-07 21:07:40 +0200366 GIT_INDEX_FILE="$TMP_INDEX" \
Junio C Hamano5be60072007-07-02 22:52:14 -0700367 git update-index --add --remove --stdin &&
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800368
Martin Waitz3de63c32006-10-07 21:07:40 +0200369 save_index &&
Junio C Hamano293623e2007-05-25 22:00:54 -0700370 printf '%s\n' "$commit_only" |
Martin Waitz3de63c32006-10-07 21:07:40 +0200371 (
372 GIT_INDEX_FILE="$NEXT_INDEX"
373 export GIT_INDEX_FILE
Junio C Hamanodb33af02007-09-14 16:53:58 -0700374 git update-index --add --remove --stdin
Martin Waitz3de63c32006-10-07 21:07:40 +0200375 ) || exit
376 ;;
Junio C Hamano130fcca2006-02-05 00:07:44 -0800377 esac
Junio C Hamanof678dd12005-11-25 13:33:14 -0800378 ;;
Junio C Hamano130fcca2006-02-05 00:07:44 -0800379esac
380
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800381################################################################
382# If we do as-is commit, the index file will be THIS_INDEX,
383# otherwise NEXT_INDEX after we make this commit. We leave
384# the index as is if we abort.
Junio C Hamano0c091292005-08-08 17:03:14 -0700385
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800386if test -f "$NEXT_INDEX"
Junio C Hamano130fcca2006-02-05 00:07:44 -0800387then
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800388 USE_INDEX="$NEXT_INDEX"
389else
390 USE_INDEX="$THIS_INDEX"
Junio C Hamano130fcca2006-02-05 00:07:44 -0800391fi
392
Junio C Hamano2b5f9a82007-02-22 00:28:49 -0800393case "$status_only" in
394t)
395 # This will silently fail in a read-only repository, which is
396 # what we want.
Junio C Hamano5be60072007-07-02 22:52:14 -0700397 GIT_INDEX_FILE="$USE_INDEX" git update-index -q --unmerged --refresh
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800398 run_status
399 exit $?
Junio C Hamano2b5f9a82007-02-22 00:28:49 -0800400 ;;
401'')
Junio C Hamano5be60072007-07-02 22:52:14 -0700402 GIT_INDEX_FILE="$USE_INDEX" git update-index -q --refresh || exit
Junio C Hamano2b5f9a82007-02-22 00:28:49 -0800403 ;;
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800404esac
405
406################################################################
407# Grab commit message, write out tree and make commit.
408
Junio C Hamano130fcca2006-02-05 00:07:44 -0800409if test t = "$verify" && test -x "$GIT_DIR"/hooks/pre-commit
410then
David Kastrupd2884042007-09-17 22:56:44 +0200411 GIT_INDEX_FILE="${TMP_INDEX:-${USE_INDEX}}" "$GIT_DIR"/hooks/pre-commit \
412 || exit
Junio C Hamano130fcca2006-02-05 00:07:44 -0800413fi
Junio C Hamano0cfe1d32005-08-12 23:39:15 -0700414
Junio C Hamanoaba2da12005-08-25 18:57:35 -0700415if test "$log_message" != ''
416then
Junio C Hamano293623e2007-05-25 22:00:54 -0700417 printf '%s\n' "$log_message"
Junio C Hamanoaba2da12005-08-25 18:57:35 -0700418elif test "$logfile" != ""
419then
420 if test "$logfile" = -
421 then
422 test -t 0 &&
423 echo >&2 "(reading log message from standard input)"
424 cat
425 else
426 cat <"$logfile"
427 fi
428elif test "$use_commit" != ""
429then
Tom Princee0d10e12007-01-28 16:16:53 -0800430 encoding=$(git config i18n.commitencoding || echo UTF-8)
Junio C Hamano5ac27152007-01-13 13:33:07 -0800431 git show -s --pretty=raw --encoding="$encoding" "$use_commit" |
432 sed -e '1,/^$/d' -e 's/^ //'
Luben Tuikova9cb3c62006-10-12 14:52:42 -0700433elif test -f "$GIT_DIR/MERGE_MSG"
Junio C Hamanoe5215802005-11-01 22:01:28 -0800434then
435 cat "$GIT_DIR/MERGE_MSG"
Junio C Hamano7d0c6882006-06-23 01:37:02 -0700436elif test -f "$GIT_DIR/SQUASH_MSG"
437then
438 cat "$GIT_DIR/SQUASH_MSG"
Steven Grimmd1cc1302007-07-22 21:17:42 -0700439elif test "$templatefile" != ""
440then
441 cat "$templatefile"
Junio C Hamano5be60072007-07-02 22:52:14 -0700442fi | git stripspace >"$GIT_DIR"/COMMIT_EDITMSG
Junio C Hamano2d569932005-08-31 17:15:25 -0700443
444case "$signoff" in
445t)
Todd Zullinger5354a562008-07-30 13:48:33 -0400446 sign=$(git var GIT_COMMITTER_IDENT | sed -e '
Gerrit Papea7342912007-07-06 14:42:27 +0000447 s/>.*/>/
448 s/^/Signed-off-by: /
449 ')
450 blank_before_signoff=
Junio C Hamanodbaa06a2007-01-29 01:06:27 -0800451 tail -n 1 "$GIT_DIR"/COMMIT_EDITMSG |
Gerrit Papea7342912007-07-06 14:42:27 +0000452 grep 'Signed-off-by:' >/dev/null || blank_before_signoff='
453'
454 tail -n 1 "$GIT_DIR"/COMMIT_EDITMSG |
455 grep "$sign"$ >/dev/null ||
456 printf '%s%s\n' "$blank_before_signoff" "$sign" \
457 >>"$GIT_DIR"/COMMIT_EDITMSG
Junio C Hamano2d569932005-08-31 17:15:25 -0700458 ;;
459esac
460
Junio C Hamano475443c2006-04-12 11:45:18 -0700461if test -f "$GIT_DIR/MERGE_HEAD" && test -z "$no_edit"; then
Junio C Hamano2d569932005-08-31 17:15:25 -0700462 echo "#"
Alex Riesenb6ae5402006-01-05 12:44:59 +0100463 echo "# It looks like you may be committing a MERGE."
Junio C Hamano2d569932005-08-31 17:15:25 -0700464 echo "# If this is not correct, please remove the file"
Junio C Hamano293623e2007-05-25 22:00:54 -0700465 printf '%s\n' "# $GIT_DIR/MERGE_HEAD"
Junio C Hamano2d569932005-08-31 17:15:25 -0700466 echo "# and try again"
467 echo "#"
Santi_Béjarf8e2c542005-10-09 17:30:19 -0700468fi >>"$GIT_DIR"/COMMIT_EDITMSG
Junio C Hamanoaba2da12005-08-25 18:57:35 -0700469
Junio C Hamano130fcca2006-02-05 00:07:44 -0800470# Author
Junio C Hamano83e24dc2007-01-22 13:03:31 -0800471if test '' != "$use_commit"
Junio C Hamano130fcca2006-02-05 00:07:44 -0800472then
Johannes Schindelin0cae2342007-06-25 01:04:11 +0100473 eval "$(get_author_ident_from_commit "$use_commit")"
474 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
Junio C Hamano130fcca2006-02-05 00:07:44 -0800475fi
Junio C Hamano83e24dc2007-01-22 13:03:31 -0800476if test '' != "$force_author"
477then
478 GIT_AUTHOR_NAME=`expr "z$force_author" : 'z\(.*[^ ]\) *<.*'` &&
479 GIT_AUTHOR_EMAIL=`expr "z$force_author" : '.*\(<.*\)'` &&
480 test '' != "$GIT_AUTHOR_NAME" &&
481 test '' != "$GIT_AUTHOR_EMAIL" ||
482 die "malformed --author parameter"
483 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
484fi
Junio C Hamano130fcca2006-02-05 00:07:44 -0800485
Linus Torvalds96069cf2005-06-14 10:20:14 -0700486PARENTS="-p HEAD"
Junio C Hamano130fcca2006-02-05 00:07:44 -0800487if test -z "$initial_commit"
Junio C Hamano8098a172005-09-30 14:26:57 -0700488then
Shawn Pearcea3a733e2006-07-10 22:48:47 -0400489 rloga='commit'
Junio C Hamano5fec3ef2005-06-25 02:22:05 -0700490 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
Shawn Pearcea3a733e2006-07-10 22:48:47 -0400491 rloga='commit (merge)'
Junio C Hamano91063bb2005-09-08 13:47:12 -0700492 PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
Junio C Hamanob4019f02006-03-02 21:04:05 -0800493 elif test -n "$amend"; then
Shawn Pearcea3a733e2006-07-10 22:48:47 -0400494 rloga='commit (amend)'
Junio C Hamano5be60072007-07-02 22:52:14 -0700495 PARENTS=$(git cat-file commit HEAD |
Junio C Hamanob4019f02006-03-02 21:04:05 -0800496 sed -n -e '/^$/q' -e 's/^parent /-p /p')
Junio C Hamanoaba2da12005-08-25 18:57:35 -0700497 fi
Junio C Hamano5be60072007-07-02 22:52:14 -0700498 current="$(git rev-parse --verify HEAD)"
Junio C Hamano8098a172005-09-30 14:26:57 -0700499else
Junio C Hamano5be60072007-07-02 22:52:14 -0700500 if [ -z "$(git ls-files)" ]; then
Shawn O. Pearceaeb80c72006-12-15 21:53:09 -0500501 echo >&2 'nothing to commit (use "git add file1 file2" to include for commit)'
Junio C Hamano8098a172005-09-30 14:26:57 -0700502 exit 1
503 fi
504 PARENTS=""
Shawn Pearcea3a733e2006-07-10 22:48:47 -0400505 rloga='commit (initial)'
Junio C Hamanocede7522006-09-27 02:06:31 -0700506 current=''
Linus Torvalds96069cf2005-06-14 10:20:14 -0700507fi
Junio C Hamano23913dc2007-01-19 17:12:11 -0800508set_reflog_action "$rloga"
Junio C Hamano130fcca2006-02-05 00:07:44 -0800509
Junio C Hamano475443c2006-04-12 11:45:18 -0700510if test -z "$no_edit"
511then
512 {
Martin Waitz88a15312006-05-26 01:42:18 +0200513 echo ""
514 echo "# Please enter the commit message for your changes."
515 echo "# (Comment lines starting with '#' will not be included)"
Junio C Hamano475443c2006-04-12 11:45:18 -0700516 test -z "$only_include_assumed" || echo "$only_include_assumed"
517 run_status
518 } >>"$GIT_DIR"/COMMIT_EDITMSG
519else
520 # we need to check if there is anything to commit
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700521 run_status >/dev/null
Junio C Hamano475443c2006-04-12 11:45:18 -0700522fi
Junio C Hamano36863af2007-12-03 00:03:10 -0800523case "$allow_empty,$?,$PARENTS" in
524t,* | ?,0,* | ?,*,-p' '?*-p' '?*)
525 # an explicit --allow-empty, or a merge commit can record the
526 # same tree as its parent. Otherwise having commitable paths
527 # is required.
Johannes Sixt13aba1e2007-12-03 08:24:50 +0100528 ;;
529*)
Junio C Hamano7d0c6882006-06-23 01:37:02 -0700530 rm -f "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
Brian Hetro09b0d9d2007-08-26 14:35:26 -0400531 use_status_color=t
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800532 run_status
Linus Torvaldsa3e870f2005-05-30 12:51:00 -0700533 exit 1
Johannes Sixt13aba1e2007-12-03 08:24:50 +0100534esac
Junio C Hamano475443c2006-04-12 11:45:18 -0700535
Junio C Hamano0c091292005-08-08 17:03:14 -0700536case "$no_edit" in
Junio C Hamano5fec3ef2005-06-25 02:22:05 -0700537'')
Todd Zullinger5354a562008-07-30 13:48:33 -0400538 git var GIT_AUTHOR_IDENT > /dev/null || die
539 git var GIT_COMMITTER_IDENT > /dev/null || die
Adam Robenef0c2ab2007-07-19 22:09:35 -0700540 git_editor "$GIT_DIR/COMMIT_EDITMSG"
Junio C Hamano5fec3ef2005-06-25 02:22:05 -0700541 ;;
542esac
Junio C Hamano89e2c5f2005-08-18 17:20:08 -0700543
544case "$verify" in
545t)
546 if test -x "$GIT_DIR"/hooks/commit-msg
547 then
Santi_Béjarf8e2c542005-10-09 17:30:19 -0700548 "$GIT_DIR"/hooks/commit-msg "$GIT_DIR"/COMMIT_EDITMSG || exit
Junio C Hamano89e2c5f2005-08-18 17:20:08 -0700549 fi
550esac
551
Yann Dirson29f4ad82006-06-24 00:04:05 +0200552if test -z "$no_edit"
553then
554 sed -e '
555 /^diff --git a\/.*/{
556 s///
557 q
558 }
559 /^#/d
560 ' "$GIT_DIR"/COMMIT_EDITMSG
561else
562 cat "$GIT_DIR"/COMMIT_EDITMSG
563fi |
Junio C Hamano5be60072007-07-02 22:52:14 -0700564git stripspace >"$GIT_DIR"/COMMIT_MSG
Santi_Béjarf8e2c542005-10-09 17:30:19 -0700565
Steven Grimmd1cc1302007-07-22 21:17:42 -0700566# Test whether the commit message has any content we didn't supply.
567have_commitmsg=
568grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
569 git stripspace > "$GIT_DIR"/COMMIT_BAREMSG
570
571# Is the commit message totally empty?
572if test -s "$GIT_DIR"/COMMIT_BAREMSG
573then
574 if test "$templatefile" != ""
575 then
576 # Test whether this is just the unaltered template.
577 if cnt=`sed -e '/^#/d' < "$templatefile" |
578 git stripspace |
579 diff "$GIT_DIR"/COMMIT_BAREMSG - |
580 wc -l` &&
581 test 0 -lt $cnt
582 then
583 have_commitmsg=t
584 fi
585 else
586 # No template, so the content in the commit message must
587 # have come from the user.
588 have_commitmsg=t
589 fi
590fi
591
592rm -f "$GIT_DIR"/COMMIT_BAREMSG
593
594if test "$have_commitmsg" = "t"
Junio C Hamano0c091292005-08-08 17:03:14 -0700595then
Junio C Hamano130fcca2006-02-05 00:07:44 -0800596 if test -z "$TMP_INDEX"
597 then
Junio C Hamano5be60072007-07-02 22:52:14 -0700598 tree=$(GIT_INDEX_FILE="$USE_INDEX" git write-tree)
Junio C Hamano130fcca2006-02-05 00:07:44 -0800599 else
Junio C Hamano5be60072007-07-02 22:52:14 -0700600 tree=$(GIT_INDEX_FILE="$TMP_INDEX" git write-tree) &&
Junio C Hamano130fcca2006-02-05 00:07:44 -0800601 rm -f "$TMP_INDEX"
602 fi &&
Josh Triplett9d6f2202007-07-14 01:05:43 -0700603 commit=$(git commit-tree $tree $PARENTS <"$GIT_DIR/COMMIT_MSG") &&
Shawn Pearce67644a42006-05-19 05:16:18 -0400604 rlogm=$(sed -e 1q "$GIT_DIR"/COMMIT_MSG) &&
Junio C Hamano5be60072007-07-02 22:52:14 -0700605 git update-ref -m "$GIT_REFLOG_ACTION: $rlogm" HEAD $commit "$current" &&
Luben Tuikova9cb3c62006-10-12 14:52:42 -0700606 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" &&
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800607 if test -f "$NEXT_INDEX"
608 then
609 mv "$NEXT_INDEX" "$THIS_INDEX"
610 else
611 : ;# happy
612 fi
Junio C Hamano0c091292005-08-08 17:03:14 -0700613else
614 echo >&2 "* no commit message? aborting commit."
615 false
616fi
Linus Torvalds170241b2005-06-19 19:57:01 -0700617ret="$?"
Junio C Hamano7d0c6882006-06-23 01:37:02 -0700618rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
Junio C Hamanoc93d88a2007-03-05 12:35:41 -0800619
620cd_to_toplevel
621
Johannes Schindelinb4372ef2007-07-06 13:05:59 +0100622git rerere
Junio C Hamano89e2c5f2005-08-18 17:20:08 -0700623
Nicolas Pitreebd124c2006-12-14 23:15:44 -0500624if test "$ret" = 0
Junio C Hamano89e2c5f2005-08-18 17:20:08 -0700625then
Junio C Hamanod4bb43e2007-09-05 14:59:59 -0700626 git gc --auto
Nicolas Pitreebd124c2006-12-14 23:15:44 -0500627 if test -x "$GIT_DIR"/hooks/post-commit
628 then
629 "$GIT_DIR"/hooks/post-commit
630 fi
631 if test -z "$quiet"
632 then
Junio C Hamano5be60072007-07-02 22:52:14 -0700633 commit=`git diff-tree --always --shortstat --pretty="format:%h: %s"\
Jonathan Nieder7f1592d2010-07-27 13:44:51 -0500634 --abbrev --summary --root HEAD --`
Nicolas Pitreebd124c2006-12-14 23:15:44 -0500635 echo "Created${initial_commit:+ initial} commit $commit"
Nicolas Pitreebd124c2006-12-14 23:15:44 -0500636 fi
Junio C Hamano89e2c5f2005-08-18 17:20:08 -0700637fi
Junio C Hamano61f5cb72006-10-24 21:48:55 -0700638
Linus Torvalds170241b2005-06-19 19:57:01 -0700639exit "$ret"