get_sha1_basic(): corner case ambiguity fix
[gitweb.git] / git-commit.sh
index 18b259c7086224f70cfd69798dc6332df040a61d..7e39c107cff93f601c51fec6544a0defe9814db1 100755 (executable)
@@ -3,11 +3,8 @@
 # Copyright (c) 2005 Linus Torvalds
 #
 
-. git-sh-setup || die "Not a git archive"
-
-usage () {
-       die 'git commit [-a] [-v | --no-verify]  [-m <message>] [-F <logfile>] [(-C|-c) <commit>] [<path>...]'
-}
+USAGE='[-a] [-s] [-v | --no-verify]  [-m <message> | -F <logfile> | (-C|-c) <commit>] [-e] [<path>...]'
+. git-sh-setup
 
 all= logfile= use_commit= no_edit= log_given= log_message= verify=t signoff=
 while case "$#" in 0) break;; esac
@@ -92,14 +89,17 @@ tt*)
 esac
 
 case "$all,$#" in
-t,*)
+t,0)
        git-diff-files --name-only -z |
        git-update-index --remove -z --stdin
        ;;
+t,*)
+       die "Cannot use -a and explicit files at the same time."
+       ;;
 ,0)
        ;;
 *)
-       git-diff-files --name-only -z "$@" |
+       git-diff-files --name-only -z -- "$@" |
        git-update-index --remove -z --stdin
        ;;
 esac || exit 1
@@ -129,39 +129,35 @@ then
 elif test "$use_commit" != ""
 then
        git-cat-file commit "$use_commit" | sed -e '1,/^$/d'
-fi | git-stripspace >.editmsg
+elif test -f "$GIT_DIR/MERGE_HEAD" && test -f "$GIT_DIR/MERGE_MSG"
+then
+       cat "$GIT_DIR/MERGE_MSG"
+fi | git-stripspace >"$GIT_DIR"/COMMIT_EDITMSG
 
 case "$signoff" in
 t)
-       git-var GIT_COMMITTER_IDENT | sed -e '
-               s/>.*/>/
-               s/^/Signed-off-by: /
-       ' >>.editmsg
+       {
+               echo
+               git-var GIT_COMMITTER_IDENT | sed -e '
+                       s/>.*/>/
+                       s/^/Signed-off-by: /
+               '
+       } >>"$GIT_DIR"/COMMIT_EDITMSG
        ;;
 esac
 
 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
-
-       test -f "$GIT_DIR/MERGE_MSG" && cat "$GIT_DIR/MERGE_MSG"
-
        echo "#"
        echo "# It looks like your may be committing a MERGE."
        echo "# If this is not correct, please remove the file"
        echo "# $GIT_DIR/MERGE_HEAD"
        echo "# and try again"
        echo "#"
-fi >>.editmsg
+fi >>"$GIT_DIR"/COMMIT_EDITMSG
 
 PARENTS="-p HEAD"
-if [ ! -r "$GIT_DIR/HEAD" ]; then
-       if [ -z "$(git-ls-files)" ]; then
-               echo Nothing to commit 1>&2
-               exit 1
-       fi
-       PARENTS=""
-       current=
-else
-       current=$(git-rev-parse --verify HEAD)
+if GIT_DIR="$GIT_DIR" git-rev-parse --verify HEAD >/dev/null 2>&1
+then
        if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
                PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
        fi
@@ -188,23 +184,29 @@ else
                }
                '
                set_author_env=`git-cat-file commit "$use_commit" |
-               sed -ne "$pick_author_script"`
+               LANG=C LC_ALL=C sed -ne "$pick_author_script"`
                eval "$set_author_env"
                export GIT_AUTHOR_NAME
                export GIT_AUTHOR_EMAIL
                export GIT_AUTHOR_DATE
        fi
+else
+       if [ -z "$(git-ls-files)" ]; then
+               echo Nothing to commit 1>&2
+               exit 1
+       fi
+       PARENTS=""
 fi
-git-status >>.editmsg
-if [ "$?" != "0" -a ! -f $GIT_DIR/MERGE_HEAD ]
+git-status >>"$GIT_DIR"/COMMIT_EDITMSG
+if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" ]
 then
-       rm -f .editmsg
+       rm -f "$GIT_DIR/COMMIT_EDITMSG"
        git-status
        exit 1
 fi
 case "$no_edit" in
 '')
-       ${VISUAL:-${EDITOR:-vi}} .editmsg
+       ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR/COMMIT_EDITMSG"
        ;;
 esac
 
@@ -212,16 +214,20 @@ case "$verify" in
 t)
        if test -x "$GIT_DIR"/hooks/commit-msg
        then
-               "$GIT_DIR"/hooks/commit-msg .editmsg || exit
+               "$GIT_DIR"/hooks/commit-msg "$GIT_DIR"/COMMIT_EDITMSG || exit
        fi
 esac
 
-grep -v '^#' < .editmsg | git-stripspace > .cmitmsg
-grep -v -i '^Signed-off-by' .cmitmsg >.cmitchk
-if test -s .cmitchk
+grep -v '^#' < "$GIT_DIR"/COMMIT_EDITMSG |
+git-stripspace > "$GIT_DIR"/COMMIT_MSG
+
+if cnt=`grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
+       git-stripspace |
+       wc -l` &&
+   test 0 -lt $cnt
 then
        tree=$(git-write-tree) &&
-       commit=$(cat .cmitmsg | git-commit-tree $tree $PARENTS) &&
+       commit=$(cat "$GIT_DIR"/COMMIT_MSG | git-commit-tree $tree $PARENTS) &&
        git-update-ref HEAD $commit $current &&
        rm -f -- "$GIT_DIR/MERGE_HEAD"
 else
@@ -229,7 +235,7 @@ else
        false
 fi
 ret="$?"
-rm -f .cmitmsg .editmsg .cmitchk
+rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG"
 
 if test -x "$GIT_DIR"/hooks/post-commit && test "$ret" = 0
 then