blob: a2455b0f8ff1582248b0678b9c85b2f064d972c4 [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
4#
5
Linus Torvaldsb33e9662005-07-08 10:57:21 -07006. git-sh-setup-script || die "Not a git archive"
7
Junio C Hamano5fec3ef2005-06-25 02:22:05 -07008usage () {
Junio C Hamano89e2c5f2005-08-18 17:20:08 -07009 die 'git commit [-a] [-v | --no-verify] [-m <message>] [-F <logfile>] [(-C|-c) <commit>] [<path>...]'
Junio C Hamano5fec3ef2005-06-25 02:22:05 -070010}
11
Junio C Hamano89e2c5f2005-08-18 17:20:08 -070012all= logfile= use_commit= no_edit= log_given= log_message= verify=t signoff=
Junio C Hamano0c091292005-08-08 17:03:14 -070013while case "$#" in 0) break;; esac
Junio C Hamano5fec3ef2005-06-25 02:22:05 -070014do
Junio C Hamano0c091292005-08-08 17:03:14 -070015 case "$1" in
16 -a|--a|--al|--all)
17 all=t
18 shift ;;
19 -F=*|--f=*|--fi=*|--fil=*|--file=*)
20 log_given=t$log_given
21 logfile=`expr "$1" : '-[^=]*=\(.*\)'`
22 no_edit=t
23 shift ;;
24 -F|--f|--fi|--fil|--file)
25 case "$#" in 1) usage ;; esac; shift
26 log_given=t$log_given
27 logfile="$1"
28 no_edit=t
29 shift ;;
30 -m=*|--m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
31 log_given=t$log_given
32 log_message=`expr "$1" : '-[^=]*=\(.*\)'`
33 no_edit=t
34 shift ;;
35 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
36 case "$#" in 1) usage ;; esac; shift
37 log_given=t$log_given
38 log_message="$1"
39 no_edit=t
40 shift ;;
41 -c=*|--ree=*|--reed=*|--reedi=*|--reedit=*|--reedit-=*|--reedit-m=*|\
42 --reedit-me=*|--reedit-mes=*|--reedit-mess=*|--reedit-messa=*|\
43 --reedit-messag=*|--reedit-message=*)
44 log_given=t$log_given
45 use_commit=`expr "$1" : '-[^=]*=\(.*\)'`
46 shift ;;
47 -c|--ree|--reed|--reedi|--reedit|--reedit-|--reedit-m|--reedit-me|\
48 --reedit-mes|--reedit-mess|--reedit-messa|--reedit-messag|--reedit-message)
49 case "$#" in 1) usage ;; esac; shift
50 log_given=t$log_given
51 use_commit="$1"
52 shift ;;
53 -C=*|--reu=*|--reus=*|--reuse=*|--reuse-=*|--reuse-m=*|--reuse-me=*|\
54 --reuse-mes=*|--reuse-mess=*|--reuse-messa=*|--reuse-messag=*|\
55 --reuse-message=*)
56 log_given=t$log_given
57 use_commit=`expr "$1" : '-[^=]*=\(.*\)'`
58 no_edit=t
59 shift ;;
60 -C|--reu|--reus|--reuse|--reuse-|--reuse-m|--reuse-me|--reuse-mes|\
61 --reuse-mess|--reuse-messa|--reuse-messag|--reuse-message)
62 case "$#" in 1) usage ;; esac; shift
63 log_given=t$log_given
64 use_commit="$1"
65 no_edit=t
66 shift ;;
Junio C Hamanobc4af682005-08-29 21:25:19 -070067 -e|--e|--ed|--edi|--edit)
68 no_edit=
69 shift ;;
Junio C Hamano0cfe1d32005-08-12 23:39:15 -070070 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
71 signoff=t
72 shift ;;
Junio C Hamano89e2c5f2005-08-18 17:20:08 -070073 -n|--n|--no|--no-|--no-v|--no-ve|--no-ver|--no-veri|--no-verif|--no-verify)
74 verify=
75 shift ;;
Junio C Hamano0cfe1d32005-08-12 23:39:15 -070076 -v|--v|--ve|--ver|--veri|--verif|--verify)
77 verify=t
78 shift ;;
Junio C Hamano0c091292005-08-08 17:03:14 -070079 --)
Junio C Hamano5fec3ef2005-06-25 02:22:05 -070080 shift
Junio C Hamano0c091292005-08-08 17:03:14 -070081 break ;;
82 -*)
83 usage ;;
84 *)
85 break ;;
86 esac
Junio C Hamano5fec3ef2005-06-25 02:22:05 -070087done
88
Junio C Hamano0c091292005-08-08 17:03:14 -070089case "$log_given" in
90tt*)
91 die "Only one of -c/-C/-F/-m can be used." ;;
92esac
93
Junio C Hamano6a0049c2005-08-17 15:17:03 -070094case "$all,$#" in
95t,*)
Junio C Hamano0c091292005-08-08 17:03:14 -070096 git-diff-files --name-only -z |
Junio C Hamanoe20ce6a2005-08-18 00:59:37 -070097 xargs -0 git-update-cache -q --remove --
Junio C Hamano22cff6a2005-08-16 18:08:19 -070098 ;;
Junio C Hamano6a0049c2005-08-17 15:17:03 -070099,0)
100 ;;
Junio C Hamano22cff6a2005-08-16 18:08:19 -0700101*)
102 git-diff-files --name-only -z "$@" |
Junio C Hamanoe20ce6a2005-08-18 00:59:37 -0700103 xargs -0 git-update-cache -q --remove --
Junio C Hamano22cff6a2005-08-16 18:08:19 -0700104 ;;
105esac || exit 1
106git-update-cache -q --refresh || exit 1
Junio C Hamano0c091292005-08-08 17:03:14 -0700107
Junio C Hamano0cfe1d32005-08-12 23:39:15 -0700108case "$verify" in
109t)
Junio C Hamano89e2c5f2005-08-18 17:20:08 -0700110 if test -x "$GIT_DIR"/hooks/pre-commit
111 then
112 "$GIT_DIR"/hooks/pre-commit || exit
113 fi
Junio C Hamano0cfe1d32005-08-12 23:39:15 -0700114esac
115
Junio C Hamanoaba2da12005-08-25 18:57:35 -0700116if test "$log_message" != ''
117then
118 echo "$log_message"
119elif test "$logfile" != ""
120then
121 if test "$logfile" = -
122 then
123 test -t 0 &&
124 echo >&2 "(reading log message from standard input)"
125 cat
126 else
127 cat <"$logfile"
128 fi
129elif test "$use_commit" != ""
130then
131 git-cat-file commit "$use_commit" | sed -e '1,/^$/d'
Junio C Hamano2d569932005-08-31 17:15:25 -0700132fi | git-stripspace >.editmsg
133
134case "$signoff" in
135t)
136 git-var GIT_COMMITTER_IDENT | sed -e '
137 s/>.*/>/
138 s/^/Signed-off-by: /
139 ' >>.editmsg
140 ;;
141esac
142
143if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
144 echo "#"
145 echo "# It looks like your may be committing a MERGE."
146 echo "# If this is not correct, please remove the file"
147 echo "# $GIT_DIR/MERGE_HEAD"
148 echo "# and try again"
149 echo "#"
150fi >>.editmsg
Junio C Hamanoaba2da12005-08-25 18:57:35 -0700151
Linus Torvalds96069cf2005-06-14 10:20:14 -0700152PARENTS="-p HEAD"
Junio C Hamano5fec3ef2005-06-25 02:22:05 -0700153if [ ! -r "$GIT_DIR/HEAD" ]; then
Linus Torvalds96069cf2005-06-14 10:20:14 -0700154 if [ -z "$(git-ls-files)" ]; then
155 echo Nothing to commit 1>&2
156 exit 1
157 fi
Linus Torvalds96069cf2005-06-14 10:20:14 -0700158 PARENTS=""
159else
Junio C Hamano5fec3ef2005-06-25 02:22:05 -0700160 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
Linus Torvalds96069cf2005-06-14 10:20:14 -0700161 PARENTS="-p HEAD -p MERGE_HEAD"
Junio C Hamanoaba2da12005-08-25 18:57:35 -0700162 fi
163 if test "$use_commit" != ""
Junio C Hamano5fec3ef2005-06-25 02:22:05 -0700164 then
165 pick_author_script='
166 /^author /{
167 h
168 s/^author \([^<]*\) <[^>]*> .*$/\1/
169 s/'\''/'\''\'\'\''/g
170 s/.*/GIT_AUTHOR_NAME='\''&'\''/p
171
172 g
173 s/^author [^<]* <\([^>]*\)> .*$/\1/
174 s/'\''/'\''\'\'\''/g
175 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
176
177 g
178 s/^author [^<]* <[^>]*> \(.*\)$/\1/
179 s/'\''/'\''\'\'\''/g
180 s/.*/GIT_AUTHOR_DATE='\''&'\''/p
181
182 q
183 }
184 '
185 set_author_env=`git-cat-file commit "$use_commit" |
186 sed -ne "$pick_author_script"`
187 eval "$set_author_env"
188 export GIT_AUTHOR_NAME
189 export GIT_AUTHOR_EMAIL
190 export GIT_AUTHOR_DATE
Junio C Hamano2150cc92005-08-15 15:37:37 -0700191 fi
Linus Torvalds96069cf2005-06-14 10:20:14 -0700192fi
Junio C Hamano2d569932005-08-31 17:15:25 -0700193git-status-script >>.editmsg
Johannes Schindelineca35ec2005-08-05 17:34:38 +0200194if [ "$?" != "0" -a ! -f $GIT_DIR/MERGE_HEAD ]
Linus Torvaldsa3e870f2005-05-30 12:51:00 -0700195then
Junio C Hamano9804b7d2005-08-26 16:53:26 -0700196 rm -f .editmsg
197 git-status-script
Linus Torvaldsa3e870f2005-05-30 12:51:00 -0700198 exit 1
199fi
Junio C Hamano0c091292005-08-08 17:03:14 -0700200case "$no_edit" in
Junio C Hamano5fec3ef2005-06-25 02:22:05 -0700201'')
202 ${VISUAL:-${EDITOR:-vi}} .editmsg
203 ;;
204esac
Junio C Hamano89e2c5f2005-08-18 17:20:08 -0700205
206case "$verify" in
207t)
208 if test -x "$GIT_DIR"/hooks/commit-msg
209 then
210 "$GIT_DIR"/hooks/commit-msg .editmsg || exit
211 fi
212esac
213
Linus Torvaldsa3e870f2005-05-30 12:51:00 -0700214grep -v '^#' < .editmsg | git-stripspace > .cmitmsg
Junio C Hamano0cfe1d32005-08-12 23:39:15 -0700215grep -v -i '^Signed-off-by' .cmitmsg >.cmitchk
216if test -s .cmitchk
Junio C Hamano0c091292005-08-08 17:03:14 -0700217then
Linus Torvalds170241b2005-06-19 19:57:01 -0700218 tree=$(git-write-tree) &&
219 commit=$(cat .cmitmsg | git-commit-tree $tree $PARENTS) &&
Junio C Hamano5fec3ef2005-06-25 02:22:05 -0700220 echo $commit > "$GIT_DIR/HEAD" &&
221 rm -f -- "$GIT_DIR/MERGE_HEAD"
Junio C Hamano0c091292005-08-08 17:03:14 -0700222else
223 echo >&2 "* no commit message? aborting commit."
224 false
225fi
Linus Torvalds170241b2005-06-19 19:57:01 -0700226ret="$?"
Junio C Hamano0cfe1d32005-08-12 23:39:15 -0700227rm -f .cmitmsg .editmsg .cmitchk
Junio C Hamano89e2c5f2005-08-18 17:20:08 -0700228
229if test -x "$GIT_DIR"/hooks/post-commit && test "$ret" = 0
230then
231 "$GIT_DIR"/hooks/post-commit
232fi
Linus Torvalds170241b2005-06-19 19:57:01 -0700233exit "$ret"