git-commit: revamp the git-commit semantics.
[alt-git.git] / git-commit.sh
blob7b9f11b6e6cf3e6807c4d190c2bab37539c09b15
1 #!/bin/sh
3 # Copyright (c) 2005 Linus Torvalds
4 # Copyright (c) 2006 Junio C Hamano
6 USAGE='[-a] [-i] [-s] [-v | --no-verify] [-m <message> | -F <logfile> | (-C|-c) <commit>] [-e] [--author <author>] [<path>...]'
8 SUBDIRECTORY_OK=Yes
9 . git-sh-setup
11 git-rev-parse --verify HEAD >/dev/null 2>&1 ||
12 initial_commit=t
14 refuse_partial () {
15 echo >&2 "$1"
16 echo >&2 "Experienced git users:"
17 echo >&2 "You might have meant to say 'git commit -i paths...', perhaps?"
18 exit 1
21 all=
22 also=
23 logfile=
24 use_commit=
25 no_edit=
26 log_given=
27 log_message=
28 verify=t
29 signoff=
30 force_author=
31 while case "$#" in 0) break;; esac
33 case "$1" in
34 -F|--F|-f|--f|--fi|--fil|--file)
35 case "$#" in 1) usage ;; esac
36 shift
37 no_edit=t
38 log_given=t$log_given
39 logfile="$1"
40 shift
42 -F*|-f*)
43 no_edit=t
44 log_given=t$log_given
45 logfile=`expr "$1" : '-[Ff]\(.*\)'`
46 shift
48 --F=*|--f=*|--fi=*|--fil=*|--file=*)
49 no_edit=t
50 log_given=t$log_given
51 logfile=`expr "$1" : '-[^=]*=\(.*\)'`
52 shift
54 -a|--a|--al|--all)
55 all=t
56 shift
58 --au=*|--aut=*|--auth=*|--autho=*|--author=*)
59 force_author=`expr "$1" : '-[^=]*=\(.*\)'`
60 shift
62 --au|--aut|--auth|--autho|--author)
63 case "$#" in 1) usage ;; esac
64 shift
65 force_author="$1"
66 shift
68 -e|--e|--ed|--edi|--edit)
69 no_edit=
70 shift
72 -i|--i|--in|--inc|--incl|--inclu|--includ|--include)
73 also=t
74 shift
76 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
77 case "$#" in 1) usage ;; esac
78 shift
79 log_given=t$log_given
80 log_message="$1"
81 no_edit=t
82 shift
84 -m*)
85 log_given=t$log_given
86 log_message=`expr "$1" : '-m\(.*\)'`
87 no_edit=t
88 shift
90 --m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
91 log_given=t$log_given
92 log_message=`expr "$1" : '-[^=]*=\(.*\)'`
93 no_edit=t
94 shift
96 -n|--n|--no|--no-|--no-v|--no-ve|--no-ver|--no-veri|--no-verif|--no-verify)
97 verify=
98 shift
101 case "$#" in 1) usage ;; esac
102 shift
103 log_given=t$log_given
104 use_commit="$1"
105 no_edit=
106 shift
108 --ree=*|--reed=*|--reedi=*|--reedit=*|--reedit-=*|--reedit-m=*|\
109 --reedit-me=*|--reedit-mes=*|--reedit-mess=*|--reedit-messa=*|\
110 --reedit-messag=*|--reedit-message=*)
111 log_given=t$log_given
112 use_commit=`expr "$1" : '-[^=]*=\(.*\)'`
113 no_edit=
114 shift
116 --ree|--reed|--reedi|--reedit|--reedit-|--reedit-m|--reedit-me|\
117 --reedit-mes|--reedit-mess|--reedit-messa|--reedit-messag|--reedit-message)
118 case "$#" in 1) usage ;; esac
119 shift
120 log_given=t$log_given
121 use_commit="$1"
122 no_edit=
123 shift
126 case "$#" in 1) usage ;; esac
127 shift
128 log_given=t$log_given
129 use_commit="$1"
130 no_edit=t
131 shift
133 --reu=*|--reus=*|--reuse=*|--reuse-=*|--reuse-m=*|--reuse-me=*|\
134 --reuse-mes=*|--reuse-mess=*|--reuse-messa=*|--reuse-messag=*|\
135 --reuse-message=*)
136 log_given=t$log_given
137 use_commit=`expr "$1" : '-[^=]*=\(.*\)'`
138 no_edit=t
139 shift
141 --reu|--reus|--reuse|--reuse-|--reuse-m|--reuse-me|--reuse-mes|\
142 --reuse-mess|--reuse-messa|--reuse-messag|--reuse-message)
143 case "$#" in 1) usage ;; esac
144 shift
145 log_given=t$log_given
146 use_commit="$1"
147 no_edit=t
148 shift
150 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
151 signoff=t
152 shift
154 -v|--v|--ve|--ver|--veri|--verif|--verify)
155 verify=t
156 shift
159 shift
160 break
163 usage
166 break
168 esac
169 done
171 case "$log_given" in
172 tt*)
173 die "Only one of -c/-C/-F/-m can be used." ;;
174 esac
176 TOP=`git-rev-parse --show-cdup`
178 case "$all,$also" in
179 t,t)
180 die "Cannot use -a and -i at the same time." ;;
182 SAVE_INDEX="$GIT_DIR/save-index$$" &&
183 cp "$GIT_DIR/index" "$SAVE_INDEX" &&
185 if test '' != "$TOP"
186 then
187 cd "$TOP"
188 fi &&
189 git-diff-files --name-only -z |
190 git-update-index --remove -z --stdin
194 case "$#" in
195 0) die "No paths with -i does not make sense." ;;
196 esac
197 SAVE_INDEX="$GIT_DIR/save-index$$" &&
198 cp "$GIT_DIR/index" "$SAVE_INDEX" &&
199 git-diff-files --name-only -z -- "$@" |
200 git-update-index --remove -z --stdin
203 case "$#" in
205 ;; # commit as-is
207 if test -f "$GIT_DIR/MERGE_HEAD"
208 then
209 refuse_partial "Cannot do a partial commit during a merge."
211 TMP_INDEX="$GIT_DIR/tmp-index$$"
212 if test -z "$initial_commit"
213 then
214 # make sure index is clean at the specified paths, or
215 # they are additions.
216 dirty_in_index=`git-diff-index --cached --name-status \
217 --diff-filter=DMTXU HEAD -- "$@"`
218 test -z "$dirty_in_index" ||
219 refuse_partial "Cannot do a partial commit of paths dirty in index:
221 $dirty_in_index"
223 commit_only=`git-ls-files -- "$@"` ;;
224 esac
226 esac
228 git-update-index -q --refresh || exit 1
230 trap '
231 test -f "$TMP_INDEX" && rm -f "$TMP_INDEX"
232 test -f "$SAVE_INDEX" && mv -f "$SAVE_INDEX" "$GIT_DIR/index"
235 if test "$TMP_INDEX"
236 then
237 if test -z "$initial_commit"
238 then
239 GIT_INDEX_FILE="$TMP_INDEX" git-read-tree HEAD
240 else
241 rm -f "$TMP_INDEX"
242 fi || exit
243 echo "$commit_only" |
244 GIT_INDEX_FILE="$TMP_INDEX" git-update-index --add --remove --stdin &&
245 echo "$commit_only" |
246 git-update-index --remove --stdin ||
247 exit
248 else
253 if test t = "$verify" && test -x "$GIT_DIR"/hooks/pre-commit
254 then
255 if test "$TMP_INDEX"
256 then
257 GIT_INDEX_FILE="$TMP_INDEX" "$GIT_DIR"/hooks/pre-commit
258 else
259 "$GIT_DIR"/hooks/pre-commit
260 fi || exit
263 if test "$log_message" != ''
264 then
265 echo "$log_message"
266 elif test "$logfile" != ""
267 then
268 if test "$logfile" = -
269 then
270 test -t 0 &&
271 echo >&2 "(reading log message from standard input)"
273 else
274 cat <"$logfile"
276 elif test "$use_commit" != ""
277 then
278 git-cat-file commit "$use_commit" | sed -e '1,/^$/d'
279 elif test -f "$GIT_DIR/MERGE_HEAD" && test -f "$GIT_DIR/MERGE_MSG"
280 then
281 cat "$GIT_DIR/MERGE_MSG"
282 fi | git-stripspace >"$GIT_DIR"/COMMIT_EDITMSG
284 case "$signoff" in
287 echo
288 git-var GIT_COMMITTER_IDENT | sed -e '
289 s/>.*/>/
290 s/^/Signed-off-by: /
292 } >>"$GIT_DIR"/COMMIT_EDITMSG
294 esac
296 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
297 echo "#"
298 echo "# It looks like you may be committing a MERGE."
299 echo "# If this is not correct, please remove the file"
300 echo "# $GIT_DIR/MERGE_HEAD"
301 echo "# and try again"
302 echo "#"
303 fi >>"$GIT_DIR"/COMMIT_EDITMSG
305 # Author
306 if test '' != "$use_commit"
307 then
308 pick_author_script='
309 /^author /{
310 s/'\''/'\''\\'\'\''/g
312 s/^author \([^<]*\) <[^>]*> .*$/\1/
313 s/'\''/'\''\'\'\''/g
314 s/.*/GIT_AUTHOR_NAME='\''&'\''/p
317 s/^author [^<]* <\([^>]*\)> .*$/\1/
318 s/'\''/'\''\'\'\''/g
319 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
322 s/^author [^<]* <[^>]*> \(.*\)$/\1/
323 s/'\''/'\''\'\'\''/g
324 s/.*/GIT_AUTHOR_DATE='\''&'\''/p
329 set_author_env=`git-cat-file commit "$use_commit" |
330 LANG=C LC_ALL=C sed -ne "$pick_author_script"`
331 eval "$set_author_env"
332 export GIT_AUTHOR_NAME
333 export GIT_AUTHOR_EMAIL
334 export GIT_AUTHOR_DATE
335 elif test '' != "$force_author"
336 then
337 GIT_AUTHOR_NAME=`expr "$force_author" : '\(.*[^ ]\) *<.*'` &&
338 GIT_AUTHOR_EMAIL=`expr "$force_author" : '.*\(<.*\)'` &&
339 test '' != "$GIT_AUTHOR_NAME" &&
340 test '' != "$GIT_AUTHOR_EMAIL" ||
341 die "malformatted --author parameter"
342 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
345 PARENTS="-p HEAD"
346 if test -z "$initial_commit"
347 then
348 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
349 PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
351 else
352 if [ -z "$(git-ls-files)" ]; then
353 echo Nothing to commit 1>&2
354 exit 1
356 PARENTS=""
360 if test '' != "$TOP"
361 then
362 cd "$TOP"
363 fi &&
364 git-status >>"$GIT_DIR"/COMMIT_EDITMSG
366 if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" ]
367 then
368 rm -f "$GIT_DIR/COMMIT_EDITMSG"
369 if test '' != "$TOP"
370 then
371 cd "$TOP"
372 fi &&
373 git-status
374 exit 1
376 case "$no_edit" in
378 case "${VISUAL:-$EDITOR},$TERM" in
379 ,dumb)
380 echo >&2 "Terminal is dumb but no VISUAL nor EDITOR defined."
381 echo >&2 "Please supply the commit log message using either"
382 echo >&2 "-m or -F option. A boilerplate log message has"
383 echo >&2 "been prepared in $GIT_DIR/COMMIT_EDITMSG"
384 exit 1
386 esac
387 ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR/COMMIT_EDITMSG"
389 esac
391 case "$verify" in
393 if test -x "$GIT_DIR"/hooks/commit-msg
394 then
395 "$GIT_DIR"/hooks/commit-msg "$GIT_DIR"/COMMIT_EDITMSG || exit
397 esac
399 grep -v '^#' < "$GIT_DIR"/COMMIT_EDITMSG |
400 git-stripspace > "$GIT_DIR"/COMMIT_MSG
402 if cnt=`grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
403 git-stripspace |
404 wc -l` &&
405 test 0 -lt $cnt
406 then
407 if test -z "$TMP_INDEX"
408 then
409 tree=$(git-write-tree)
410 else
411 tree=$(GIT_INDEX_FILE="$TMP_INDEX" git-write-tree) &&
412 rm -f "$TMP_INDEX"
413 fi &&
414 commit=$(cat "$GIT_DIR"/COMMIT_MSG | git-commit-tree $tree $PARENTS) &&
415 git-update-ref HEAD $commit $current &&
416 rm -f -- "$GIT_DIR/MERGE_HEAD"
417 else
418 echo >&2 "* no commit message? aborting commit."
419 false
421 ret="$?"
422 rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG"
423 git-rerere
425 if test -x "$GIT_DIR"/hooks/post-commit && test "$ret" = 0
426 then
427 "$GIT_DIR"/hooks/post-commit
429 if test 0 -eq "$ret"
430 then
431 rm -f "$SAVE_INDEX"
433 exit "$ret"