1#!/bin/sh2#3# Copyright (c) 2005 Linus Torvalds4#56. git-sh-setup-script || die "Not a git archive"78usage () {9die 'git commit [-a] [-m <message>] [-F <logfile>] [(-C|-c) <commit>] [<path>...]'10}1112all= logfile= use_commit= no_edit= log_given= log_message= verify= signoff=13while case "$#" in 0) break;; esac14do15case "$1" in16-a|--a|--al|--all)17all=t18shift ;;19-F=*|--f=*|--fi=*|--fil=*|--file=*)20log_given=t$log_given21logfile=`expr "$1" : '-[^=]*=\(.*\)'`22no_edit=t23shift ;;24-F|--f|--fi|--fil|--file)25case "$#" in 1) usage ;; esac; shift26log_given=t$log_given27logfile="$1"28no_edit=t29shift ;;30-m=*|--m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)31log_given=t$log_given32log_message=`expr "$1" : '-[^=]*=\(.*\)'`33no_edit=t34shift ;;35-m|--m|--me|--mes|--mess|--messa|--messag|--message)36case "$#" in 1) usage ;; esac; shift37log_given=t$log_given38log_message="$1"39no_edit=t40shift ;;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_given45use_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; shift50log_given=t$log_given51use_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_given57use_commit=`expr "$1" : '-[^=]*=\(.*\)'`58no_edit=t59shift ;;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; shift63log_given=t$log_given64use_commit="$1"65no_edit=t66shift ;;67-s|--s|--si|--sig|--sign|--signo|--signof|--signoff)68signoff=t69shift ;;70-v|--v|--ve|--ver|--veri|--verif|--verify)71verify=t72shift ;;73--)74shift75break ;;76-*)77usage ;;78*)79break ;;80esac81done8283case "$log_given" in84tt*)85die "Only one of -c/-C/-F/-m can be used." ;;86esac8788case "$all" in89t)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 198git-update-cache -q --refresh || exit 199100case "$verify" in101t)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 ;;152esac153154PARENTS="-p HEAD"155if [ ! -r "$GIT_DIR/HEAD" ]; then156if [ -z "$(git-ls-files)" ]; then157echo Nothing to commit 1>&2158exit 1159fi160{161echo "#"162echo "# Initial commit"163case "$no_edit" in164t) echo "# (ignoring your commit message for initial commit)"165no_edit=166esac167echo "#"168git-ls-files | sed 's/^/# New file: /'169echo "#"170} >.editmsg171PARENTS=""172no_edit=173else174if [ -f "$GIT_DIR/MERGE_HEAD" ]; then175{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" in182t) echo "# (ignoring your commit message for merge commit)"183no_edit=184esac185echo "#"186} |187git-stripspace >.editmsg188PARENTS="-p HEAD -p MERGE_HEAD"189elif test "$log_message" != ''190then191echo "$log_message" |192git-stripspace >.editmsg193elif test "$logfile" != ""194then195if test "$logfile" = -196then197test -t 0 &&198echo >&2 "(reading log message from standard input)"199cat200else201cat <"$logfile"202fi |203git-stripspace >.editmsg204elif test "$use_commit" != ""205then206pick_author_script='207/^author /{208h209s/^author \([^<]*\) <[^>]*> .*$/\1/210s/'\''/'\''\'\'\''/g211s/.*/GIT_AUTHOR_NAME='\''&'\''/p212213g214s/^author [^<]* <\([^>]*\)> .*$/\1/215s/'\''/'\''\'\'\''/g216s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p217218g219s/^author [^<]* <[^>]*> \(.*\)$/\1/220s/'\''/'\''\'\'\''/g221s/.*/GIT_AUTHOR_DATE='\''&'\''/p222223q224}225'226set_author_env=`git-cat-file commit "$use_commit" |227sed -ne "$pick_author_script"`228eval "$set_author_env"229export GIT_AUTHOR_NAME230export GIT_AUTHOR_EMAIL231export GIT_AUTHOR_DATE232git-cat-file commit "$use_commit" |233sed -e '1,/^$/d' |234git-stripspace >.editmsg235fi236237case "$signoff" in238t)239git-var GIT_COMMITTER_IDENT | sed -e '240s/>.*/>/241s/^/Signed-off-by: /' >>.editmsg ;;242esac243git-status-script >>.editmsg244fi245if [ "$?" != "0" -a ! -f $GIT_DIR/MERGE_HEAD ]246then247cat .editmsg248rm .editmsg249exit 1250fi251case "$no_edit" in252'')253${VISUAL:-${EDITOR:-vi}} .editmsg254;;255esac256grep -v '^#' < .editmsg | git-stripspace > .cmitmsg257grep -v -i '^Signed-off-by' .cmitmsg >.cmitchk258if test -s .cmitchk259then260tree=$(git-write-tree) &&261commit=$(cat .cmitmsg | git-commit-tree $tree $PARENTS) &&262echo $commit > "$GIT_DIR/HEAD" &&263rm -f -- "$GIT_DIR/MERGE_HEAD"264else265echo >&2 "* no commit message? aborting commit."266false267fi268ret="$?"269rm -f .cmitmsg .editmsg .cmitchk270exit "$ret"