1#!/bin/sh
2#
3# Copyright (c) 2005 Linus Torvalds
4#
56
. git-sh-setup-script || die "Not a git archive"
78
usage () {
9die 'git commit [-a] [-m <message>] [-F <logfile>] [(-C|-c) <commit>] [<path>...]'
10}
1112
all= logfile= use_commit= no_edit= log_given= log_message= verify= signoff=
13while case "$#" in 0) break;; esac
14do
15case "$1" in
16-a|--a|--al|--all)
17all=t
18shift ;;
19-F=*|--f=*|--fi=*|--fil=*|--file=*)
20log_given=t$log_given
21logfile=`expr "$1" : '-[^=]*=\(.*\)'`
22no_edit=t
23shift ;;
24-F|--f|--fi|--fil|--file)
25case "$#" in 1) usage ;; esac; shift
26log_given=t$log_given
27logfile="$1"
28no_edit=t
29shift ;;
30-m=*|--m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
31log_given=t$log_given
32log_message=`expr "$1" : '-[^=]*=\(.*\)'`
33no_edit=t
34shift ;;
35-m|--m|--me|--mes|--mess|--messa|--messag|--message)
36case "$#" in 1) usage ;; esac; shift
37log_given=t$log_given
38log_message="$1"
39no_edit=t
40shift ;;
41-c=*|--ree=*|--reed=*|--reedi=*|--reedit=*|--reedit-=*|--reedit-m=*|\
42--reedit-me=*|--reedit-mes=*|--reedit-mess=*|--reedit-messa=*|\
43--reedit-messag=*|--reedit-message=*)
44log_given=t$log_given
45use_commit=`expr "$1" : '-[^=]*=\(.*\)'`
46shift ;;
47-c|--ree|--reed|--reedi|--reedit|--reedit-|--reedit-m|--reedit-me|\
48--reedit-mes|--reedit-mess|--reedit-messa|--reedit-messag|--reedit-message)
49case "$#" in 1) usage ;; esac; shift
50log_given=t$log_given
51use_commit="$1"
52shift ;;
53-C=*|--reu=*|--reus=*|--reuse=*|--reuse-=*|--reuse-m=*|--reuse-me=*|\
54--reuse-mes=*|--reuse-mess=*|--reuse-messa=*|--reuse-messag=*|\
55--reuse-message=*)
56log_given=t$log_given
57use_commit=`expr "$1" : '-[^=]*=\(.*\)'`
58no_edit=t
59shift ;;
60-C|--reu|--reus|--reuse|--reuse-|--reuse-m|--reuse-me|--reuse-mes|\
61--reuse-mess|--reuse-messa|--reuse-messag|--reuse-message)
62case "$#" in 1) usage ;; esac; shift
63log_given=t$log_given
64use_commit="$1"
65no_edit=t
66shift ;;
67-s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
68signoff=t
69shift ;;
70-v|--v|--ve|--ver|--veri|--verif|--verify)
71verify=t
72shift ;;
73--)
74shift
75break ;;
76-*)
77usage ;;
78*)
79break ;;
80esac
81done
8283
case "$log_given" in
84tt*)
85die "Only one of -c/-C/-F/-m can be used." ;;
86esac
8788
case "$all" in
89t)
90git-diff-files --name-only -z |
91xargs -0 git-update-cache -q --
92;;
93*)
94git-diff-files --name-only -z "$@" |
95xargs -0 git-update-cache -q --
96;;
97esac || exit 1
98git-update-cache -q --refresh || exit 1
99100
case "$verify" in
101t)
102# This is slightly modified from Andrew Morton's Perfect Patch.
103# Lines you introduce should not have trailing whitespace.
104# Also check for an indentation that has SP before a TAB.
105perl -e '
106my $fh;
107my $found_bad = 0;
108my $filename;
109my $reported_filename = "";
110my $lineno;
111sub bad_line {
112my ($why, $line) = @_;
113if (!$found_bad) {
114print "*\n";
115print "* You have some suspicious patch lines:\n";
116print "*\n";
117$found_bad = 1;
118}
119if ($reported_filename ne $filename) {
120print "* In $filename\n";
121$reported_filename = $filename;
122}
123print "* $why (line $lineno)\n$line\n";
124}
125open $fh, "-|", qw(git-diff-cache -p -M --cached HEAD);
126while (<$fh>) {
127if (m|^diff --git a/(.*) b/\1$|) {
128$filename = $1;
129next;
130}
131if (/^@@ -\S+ \+(\d+)/) {
132$lineno = $1 - 1;
133next;
134}
135if (/^ /) {
136$lineno++;
137next;
138}
139if (s/^\+//) {
140$lineno++;
141chomp;
142if (/\s$/) {
143bad_line("trailing whitespace", $_);
144}
145if (/^\s* /) {
146bad_line("indent SP followed by a TAB", $_);
147}
148}
149}
150exit($found_bad);
151' || exit ;;
152esac
153154
PARENTS="-p HEAD"
155if [ ! -r "$GIT_DIR/HEAD" ]; then
156if [ -z "$(git-ls-files)" ]; then
157echo Nothing to commit 1>&2
158exit 1
159fi
160{
161echo "#"
162echo "# Initial commit"
163case "$no_edit" in
164t) echo "# (ignoring your commit message for initial commit)"
165no_edit=
166esac
167echo "#"
168git-ls-files | sed 's/^/# New file: /'
169echo "#"
170} >.editmsg
171PARENTS=""
172no_edit=
173else
174if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
175{
176echo "#"
177echo "# It looks like your may be committing a MERGE."
178echo "# If this is not correct, please remove the file"
179echo "# $GIT_DIR/MERGE_HEAD"
180echo "# and try again"
181case "$no_edit" in
182t) echo "# (ignoring your commit message for merge commit)"
183no_edit=
184esac
185echo "#"
186} |
187git-stripspace >.editmsg
188PARENTS="-p HEAD -p MERGE_HEAD"
189elif test "$log_message" != ''
190then
191echo "$log_message" |
192git-stripspace >.editmsg
193elif test "$logfile" != ""
194then
195if test "$logfile" = -
196then
197test -t 0 &&
198echo >&2 "(reading log message from standard input)"
199cat
200else
201cat <"$logfile"
202fi |
203git-stripspace >.editmsg
204elif test "$use_commit" != ""
205then
206pick_author_script='
207/^author /{
208h
209s/^author \([^<]*\) <[^>]*> .*$/\1/
210s/'\''/'\''\'\'\''/g
211s/.*/GIT_AUTHOR_NAME='\''&'\''/p
212213
g
214s/^author [^<]* <\([^>]*\)> .*$/\1/
215s/'\''/'\''\'\'\''/g
216s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
217218
g
219s/^author [^<]* <[^>]*> \(.*\)$/\1/
220s/'\''/'\''\'\'\''/g
221s/.*/GIT_AUTHOR_DATE='\''&'\''/p
222223
q
224}
225'
226set_author_env=`git-cat-file commit "$use_commit" |
227sed -ne "$pick_author_script"`
228eval "$set_author_env"
229export GIT_AUTHOR_NAME
230export GIT_AUTHOR_EMAIL
231export GIT_AUTHOR_DATE
232git-cat-file commit "$use_commit" |
233sed -e '1,/^$/d' |
234git-stripspace >.editmsg
235fi
236237
case "$signoff" in
238t)
239git-var GIT_COMMITTER_IDENT | sed -e '
240s/>.*/>/
241s/^/Signed-off-by: /' >>.editmsg ;;
242esac
243git-status-script >>.editmsg
244fi
245if [ "$?" != "0" -a ! -f $GIT_DIR/MERGE_HEAD ]
246then
247cat .editmsg
248rm .editmsg
249exit 1
250fi
251case "$no_edit" in
252'')
253${VISUAL:-${EDITOR:-vi}} .editmsg
254;;
255esac
256grep -v '^#' < .editmsg | git-stripspace > .cmitmsg
257grep -v -i '^Signed-off-by' .cmitmsg >.cmitchk
258if test -s .cmitchk
259then
260tree=$(git-write-tree) &&
261commit=$(cat .cmitmsg | git-commit-tree $tree $PARENTS) &&
262echo $commit > "$GIT_DIR/HEAD" &&
263rm -f -- "$GIT_DIR/MERGE_HEAD"
264else
265echo >&2 "* no commit message? aborting commit."
266false
267fi
268ret="$?"
269rm -f .cmitmsg .editmsg .cmitchk
270exit "$ret"