#!/bin/sh # # Copyright (c) 2005 Linus Torvalds # Copyright (c) 2006 Junio C Hamano setglobal USAGE = ''[-a | --interactive] [-s] [-v] [--no-verify] [-m | -F | (-C|-c) | --amend] [-u] [-e] [--author ] [--template ] [[-i | -o] ...]'' setglobal SUBDIRECTORY_OK = 'Yes' setglobal OPTIONS_SPEC = '' source git-sh-setup require_work_tree git rev-parse --verify HEAD >/dev/null !2 > !1 || setglobal initial_commit = 't' match $0 { with *status setglobal status_only = 't' with *commit setglobal status_only = '' } proc refuse_partial { echo >&2 $1> !2 "$1" echo >&2 "You might have meant to say 'git commit -i paths...', perhaps?> !2 "You might have meant to say 'git commit -i paths...', perhaps?" exit 1 } setglobal TMP_INDEX = '' setglobal THIS_INDEX = $(GIT_INDEX_FILE:-$GIT_DIR/index) setglobal NEXT_INDEX = ""$GIT_DIR/next-index$Pid"" rm -f $NEXT_INDEX proc save_index { cp -p $THIS_INDEX $NEXT_INDEX } proc run_status { # If TMP_INDEX is defined, that means we are doing # "--only" partial commit, and that index file is used # to build the tree for the commit. Otherwise, if # NEXT_INDEX exists, that is the index file used to # make the commit. Otherwise we are using as-is commit # so the regular index file is what we use to compare. if test '' != $TMP_INDEX { setglobal GIT_INDEX_FILE = $TMP_INDEX export GIT_INDEX_FILE } elif test -f $NEXT_INDEX { setglobal GIT_INDEX_FILE = $NEXT_INDEX export GIT_INDEX_FILE } if test $status_only = "t" || test $use_status_color = "t" { setglobal color = '' } else { setglobal color = '--nocolor' } git runstatus $(color) \ $(verbose:+--verbose) \ $(amend:+--amend) \ $(untracked_files:+--untracked) } trap ' test -z "$TMP_INDEX" || { test -f "$TMP_INDEX" && rm -f "$TMP_INDEX" } rm -f "$NEXT_INDEX" ' 0 ################################################################ # Command line argument parsing and sanity checking setglobal all = '' setglobal also = '' setglobal allow_empty = 'f' setglobal interactive = '' setglobal only = '' setglobal logfile = '' setglobal use_commit = '' setglobal amend = '' setglobal edit_flag = '' setglobal no_edit = '' setglobal log_given = '' setglobal log_message = '' setglobal verify = 't' setglobal quiet = '' setglobal verbose = '' setglobal signoff = '' setglobal force_author = '' setglobal only_include_assumed = '' setglobal untracked_files = '' setglobal templatefile = $[git config commit.template] while test $# != 0 { match $1 { with -F|--F|-f|--f|--fi|--fil|--file match "$Argc" { with 1 usage } shift setglobal no_edit = 't' setglobal log_given = "t$log_given" setglobal logfile = $1 with -F*|-f* setglobal no_edit = 't' setglobal log_given = "t$log_given" setglobal logfile = $(1#-[Ff]) with --F=*|--f=*|--fi=*|--fil=*|--file=* setglobal no_edit = 't' setglobal log_given = "t$log_given" setglobal logfile = $(1#*=) with -a|--a|--al|--all setglobal all = 't' with --allo|--allow|--allow-|--allow-e|--allow-em|--allow-emp|\ --allow-empt|--allow-empty setglobal allow_empty = 't' with --au=*|--aut=*|--auth=*|--autho=*|--author=* setglobal force_author = $(1#*=) with --au|--aut|--auth|--autho|--author match "$Argc" { with 1 usage } shift setglobal force_author = $1 with -e|--e|--ed|--edi|--edit setglobal edit_flag = 't' with -i|--i|--in|--inc|--incl|--inclu|--includ|--include setglobal also = 't' with --int|--inte|--inter|--intera|--interac|--interact|--interacti|\ --interactiv|--interactive setglobal interactive = 't' with -o|--o|--on|--onl|--only setglobal only = 't' with -m|--m|--me|--mes|--mess|--messa|--messag|--message match "$Argc" { with 1 usage } shift setglobal log_given = "m$log_given" setglobal log_message = ""$(log_message:+${log_message} )$1"" setglobal no_edit = 't' with -m* setglobal log_given = "m$log_given" setglobal log_message = ""$(log_message:+${log_message} )$(1#-m)"" setglobal no_edit = 't' with --m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=* setglobal log_given = "m$log_given" setglobal log_message = ""$(log_message:+${log_message} )$(1#*=)"" setglobal no_edit = 't' with -n|--n|--no|--no-|--no-v|--no-ve|--no-ver|--no-veri|--no-verif|\ --no-verify setglobal verify = '' with --a|--am|--ame|--amen|--amend setglobal amend = 't' setglobal use_commit = 'HEAD' with -c match "$Argc" { with 1 usage } shift setglobal log_given = "t$log_given" setglobal use_commit = $1 setglobal no_edit = '' with --ree=*|--reed=*|--reedi=*|--reedit=*|--reedit-=*|--reedit-m=*|\ --reedit-me=*|--reedit-mes=*|--reedit-mess=*|--reedit-messa=*|\ --reedit-messag=*|--reedit-message=* setglobal log_given = "t$log_given" setglobal use_commit = $(1#*=) setglobal no_edit = '' with --ree|--reed|--reedi|--reedit|--reedit-|--reedit-m|--reedit-me|\ --reedit-mes|--reedit-mess|--reedit-messa|--reedit-messag|\ --reedit-message match "$Argc" { with 1 usage } shift setglobal log_given = "t$log_given" setglobal use_commit = $1 setglobal no_edit = '' with -C match "$Argc" { with 1 usage } shift setglobal log_given = "t$log_given" setglobal use_commit = $1 setglobal no_edit = 't' with --reu=*|--reus=*|--reuse=*|--reuse-=*|--reuse-m=*|--reuse-me=*|\ --reuse-mes=*|--reuse-mess=*|--reuse-messa=*|--reuse-messag=*|\ --reuse-message=* setglobal log_given = "t$log_given" setglobal use_commit = $(1#*=) setglobal no_edit = 't' with --reu|--reus|--reuse|--reuse-|--reuse-m|--reuse-me|--reuse-mes|\ --reuse-mess|--reuse-messa|--reuse-messag|--reuse-message match "$Argc" { with 1 usage } shift setglobal log_given = "t$log_given" setglobal use_commit = $1 setglobal no_edit = 't' with -s|--s|--si|--sig|--sign|--signo|--signof|--signoff setglobal signoff = 't' with -t|--t|--te|--tem|--temp|--templ|--templa|--templat|--template match "$Argc" { with 1 usage } shift setglobal templatefile = $1 setglobal no_edit = '' with -q|--q|--qu|--qui|--quie|--quiet setglobal quiet = 't' with -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose setglobal verbose = 't' with -u|--u|--un|--unt|--untr|--untra|--untrac|--untrack|--untracke|\ --untracked|--untracked-|--untracked-f|--untracked-fi|--untracked-fil|\ --untracked-file|--untracked-files setglobal untracked_files = 't' with -- shift break with -* usage with * break } shift } match $edit_flag { with t setglobal no_edit = '' } ################################################################ # Sanity check options match "$amend,$initial_commit" { with t,t die "You do not have anything to amend." with t, if test -f "$GIT_DIR/MERGE_HEAD" { die "You are in the middle of a merge -- cannot amend." } } match $log_given { with tt* die "Only one of -c/-C/-F can be used." with *tm*|*mt* die "Option -m cannot be combined with -c/-C/-F." } match "$Argc,$also,$only,$amend" { with *,t,t,* die "Only one of --include/--only can be used." with 0,t,,* | 0,,t, die "No paths with --include/--only does not make sense." with 0,,t,t setglobal only_include_assumed = '"# Clever... amending the last one with dirty index.'" with 0,,,* with *,,,* setglobal only_include_assumed = '"# Explicit paths specified without -i or -o; assuming --only paths...'" setglobal also = '' } unset only match "$all,$interactive,$also,$Argc" { with *t,*t,* die "Cannot use -a, --interactive or -i at the same time." with t,,,[1-9]* die "Paths with -a does not make sense." with ,t,,[1-9]* die "Paths with --interactive does not make sense." with ,,t,0 die "No paths with -i does not make sense." } if test ! -z $templatefile && test -z $log_given { if test ! -f $templatefile { die "Commit template file does not exist." } } ################################################################ # Prepare index to have a tree to be committed match "$all,$also" { with t, if test ! -f $THIS_INDEX { die 'nothing to commit (use "git add file1 file2" to include for commit)' } save_index && shell { cd_to_toplevel && setglobal GIT_INDEX_FILE = $NEXT_INDEX && export GIT_INDEX_FILE && git diff-files --name-only -z | git update-index --remove -z --stdin } || exit with ,t save_index && git ls-files --error-unmatch -- @Argv >/dev/null || exit git diff-files --name-only -z -- @Argv | shell { cd_to_toplevel && setglobal GIT_INDEX_FILE = $NEXT_INDEX && export GIT_INDEX_FILE && git update-index --remove -z --stdin } || exit with , if test $interactive = t { git add --interactive || exit } match "$Argc" { with 0 # commit as-is with * if test -f "$GIT_DIR/MERGE_HEAD" { refuse_partial "Cannot do a partial commit during a merge." } setglobal TMP_INDEX = ""$GIT_DIR/tmp-index$Pid"" setglobal W = '' test -z $initial_commit && setglobal W = '--with-tree=HEAD' setglobal commit_only = $[git ls-files --error-unmatch $W -- @Argv] || exit # Build a temporary index and update the real index # the same way. if test -z $initial_commit { env GIT_INDEX_FILE=$THIS_INDEX \ git read-tree --index-output="$TMP_INDEX" -i -m HEAD } else { rm -f $TMP_INDEX } || exit printf '%s\n' $commit_only | env GIT_INDEX_FILE=$TMP_INDEX \ git update-index --add --remove --stdin && save_index && printf '%s\n' $commit_only | shell { setglobal GIT_INDEX_FILE = $NEXT_INDEX export GIT_INDEX_FILE git update-index --add --remove --stdin } || exit } } ################################################################ # If we do as-is commit, the index file will be THIS_INDEX, # otherwise NEXT_INDEX after we make this commit. We leave # the index as is if we abort. if test -f $NEXT_INDEX { setglobal USE_INDEX = $NEXT_INDEX } else { setglobal USE_INDEX = $THIS_INDEX } match $status_only { with t # This will silently fail in a read-only repository, which is # what we want. env GIT_INDEX_FILE=$USE_INDEX git update-index -q --unmerged --refresh run_status exit $? with '' env GIT_INDEX_FILE=$USE_INDEX git update-index -q --refresh || exit } ################################################################ # Grab commit message, write out tree and make commit. if test t = $verify && test -x "$GIT_DIR"/hooks/pre-commit { env GIT_INDEX_FILE=$(TMP_INDEX:-${USE_INDEX}) "$GIT_DIR"/hooks/pre-commit \ || exit } if test $log_message != '' { printf '%s\n' $log_message } elif test $logfile != "" { if test $logfile = - { test -t 0 && echo >&2 "(reading log message from standard input)> !2 "(reading log message from standard input)" cat } else { cat <$logfile } } elif test $use_commit != "" { setglobal encoding = $[git config i18n.commitencoding || echo UTF-8] git show -s --pretty=raw --encoding="$encoding" $use_commit | sed -e '1,/^$/d' -e 's/^ //' } elif test -f "$GIT_DIR/MERGE_MSG" { cat "$GIT_DIR/MERGE_MSG" } elif test -f "$GIT_DIR/SQUASH_MSG" { cat "$GIT_DIR/SQUASH_MSG" } elif test $templatefile != "" { cat $templatefile } | git stripspace >"$GIT_DIR"/COMMIT_EDITMSG match $signoff { with t setglobal sign = $[git var GIT_COMMITTER_IDENT | sed -e ' s/>.*/>/ s/^/Signed-off-by: / ] setglobal blank_before_signoff = '' tail -n 1 "$GIT_DIR"/COMMIT_EDITMSG | grep 'Signed-off-by:' >/dev/null || setglobal blank_before_signoff = '' '' tail -n 1 "$GIT_DIR"/COMMIT_EDITMSG | grep "$sign"$ >/dev/null || printf '%s%s\n' $blank_before_signoff $sign \ >>"$GIT_DIR"/COMMIT_EDITMSG } if test -f "$GIT_DIR/MERGE_HEAD" && test -z $no_edit { echo "#" echo "# It looks like you may be committing a MERGE." echo "# If this is not correct, please remove the file" printf '%s\n' "# $GIT_DIR/MERGE_HEAD" echo "# and try again" echo "#" } >>"$GIT_DIR"/COMMIT_EDITMSG # Author if test '' != $use_commit { eval $[get_author_ident_from_commit $use_commit] export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE } if test '' != $force_author { setglobal GIT_AUTHOR_NAME = $[expr "z$force_author" : 'z\(.*[^ ]\) *<.*] && setglobal GIT_AUTHOR_EMAIL = $[expr "z$force_author" : '.*\(<.*\)] && test '' != $GIT_AUTHOR_NAME && test '' != $GIT_AUTHOR_EMAIL || die "malformed --author parameter" export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL } setglobal PARENTS = '"-p HEAD'" if test -z $initial_commit { setglobal rloga = ''commit'' if test -f "$GIT_DIR/MERGE_HEAD" { setglobal rloga = ''commit (merge)'' setglobal PARENTS = ""-p HEAD "$[sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD]" } elif test -n $amend { setglobal rloga = ''commit (amend)'' setglobal PARENTS = $[git cat-file commit HEAD | sed -n -e '/^$/q' -e 's/^parent /-p /p] } setglobal current = $[git rev-parse --verify HEAD] } else { if test -z $[git ls-files] { echo >&2 'nothing to commit (use "git add file1 file2" to include for commit)> !2 'nothing to commit (use "git add file1 file2" to include for commit)' exit 1 } setglobal PARENTS = ''"" setglobal rloga = ''commit (initial)'' setglobal current = '''' } set_reflog_action $rloga if test -z $no_edit { do { echo "" echo "# Please enter the commit message for your changes." echo "# (Comment lines starting with '#' will not be included)" test -z $only_include_assumed || echo $only_include_assumed run_status } >>"$GIT_DIR"/COMMIT_EDITMSG } else { # we need to check if there is anything to commit run_status >/dev/null } match "$allow_empty,$Status,$PARENTS" { with t,* | ?,0,* | ?,*,-p' '?*-p' '?* # an explicit --allow-empty, or a merge commit can record the # same tree as its parent. Otherwise having commitable paths # is required. with * rm -f "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG" setglobal use_status_color = 't' run_status exit 1 } match $no_edit { with '' git var GIT_AUTHOR_IDENT > /dev/null || die git var GIT_COMMITTER_IDENT > /dev/null || die git_editor "$GIT_DIR/COMMIT_EDITMSG" } match $verify { with t if test -x "$GIT_DIR"/hooks/commit-msg { "$GIT_DIR"/hooks/commit-msg "$GIT_DIR"/COMMIT_EDITMSG || exit } } if test -z $no_edit { sed -e ' /^diff --git a\/.*/{ s/// q } /^#/d ' "$GIT_DIR"/COMMIT_EDITMSG } else { cat "$GIT_DIR"/COMMIT_EDITMSG } | git stripspace >"$GIT_DIR"/COMMIT_MSG # Test whether the commit message has any content we didn't supply. setglobal have_commitmsg = '' grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG | git stripspace > "$GIT_DIR"/COMMIT_BAREMSG # Is the commit message totally empty? if test -s "$GIT_DIR"/COMMIT_BAREMSG { if test $templatefile != "" { # Test whether this is just the unaltered template. if setglobal cnt = $[sed -e '/^#/d' < $templatefile | git stripspace | diff "$GIT_DIR"/COMMIT_BAREMSG - | wc -l] && test 0 -lt $cnt { setglobal have_commitmsg = 't' } } else { # No template, so the content in the commit message must # have come from the user. setglobal have_commitmsg = 't' } } rm -f "$GIT_DIR"/COMMIT_BAREMSG if test $have_commitmsg = "t" { if test -z $TMP_INDEX { setglobal tree = $[env GIT_INDEX_FILE=$USE_INDEX git write-tree] } else { setglobal tree = $[env GIT_INDEX_FILE=$TMP_INDEX git write-tree] && rm -f $TMP_INDEX } && setglobal commit = $[git commit-tree $tree $PARENTS <"$GIT_DIR/COMMIT_MSG] && setglobal rlogm = $[sed -e 1q "$GIT_DIR"/COMMIT_MSG] && git update-ref -m "$GIT_REFLOG_ACTION: $rlogm" HEAD $commit $current && rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" && if test -f $NEXT_INDEX { mv $NEXT_INDEX $THIS_INDEX } else { : ;# happy } } else { echo >&2 "* no commit message? aborting commit.> !2 "* no commit message? aborting commit." false } setglobal ret = "$Status" rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG" cd_to_toplevel git rerere if test $ret = 0 { git gc --auto if test -x "$GIT_DIR"/hooks/post-commit { "$GIT_DIR"/hooks/post-commit } if test -z $quiet { setglobal commit = $[git diff-tree --always --shortstat --pretty="format:%h: %s"\ --abbrev --summary --root HEAD --] echo "Created$(initial_commit:+ initial) commit $commit" } } exit "$ret"