git-commit.shon commit Documentation(cvs-migration): minor cleanups. (6bad190)
   1#!/bin/sh
   2#
   3# Copyright (c) 2005 Linus Torvalds
   4#
   5
   6. git-sh-setup
   7
   8usage () {
   9        die 'git commit [-a] [-s] [-v | --no-verify]  [-m <message> | -F <logfile> | (-C|-c) <commit>] [-e] [<path>...]'
  10}
  11
  12all= logfile= use_commit= no_edit= log_given= log_message= verify=t signoff=
  13while case "$#" in 0) break;; esac
  14do
  15  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 ;;
  67  -e|--e|--ed|--edi|--edit)
  68    no_edit=
  69    shift ;;
  70  -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
  71    signoff=t
  72    shift ;;
  73  -n|--n|--no|--no-|--no-v|--no-ve|--no-ver|--no-veri|--no-verif|--no-verify)
  74    verify=
  75    shift ;;
  76  -v|--v|--ve|--ver|--veri|--verif|--verify)
  77    verify=t
  78    shift ;;
  79  --)
  80    shift
  81    break ;;
  82  -*)
  83     usage ;;
  84  *)
  85    break ;;
  86  esac
  87done
  88
  89case "$log_given" in
  90tt*)
  91  die "Only one of -c/-C/-F/-m can be used." ;;
  92esac
  93
  94case "$all,$#" in
  95t,0)
  96        git-diff-files --name-only -z |
  97        git-update-index --remove -z --stdin
  98        ;;
  99t,*)
 100        die "Cannot use -a and explicit files at the same time."
 101        ;;
 102,0)
 103        ;;
 104*)
 105        git-diff-files --name-only -z -- "$@" |
 106        git-update-index --remove -z --stdin
 107        ;;
 108esac || exit 1
 109git-update-index -q --refresh || exit 1
 110
 111case "$verify" in
 112t)
 113        if test -x "$GIT_DIR"/hooks/pre-commit
 114        then
 115                "$GIT_DIR"/hooks/pre-commit || exit
 116        fi
 117esac
 118
 119if test "$log_message" != ''
 120then
 121        echo "$log_message"
 122elif test "$logfile" != ""
 123then
 124        if test "$logfile" = -
 125        then
 126                test -t 0 &&
 127                echo >&2 "(reading log message from standard input)"
 128                cat
 129        else
 130                cat <"$logfile"
 131        fi
 132elif test "$use_commit" != ""
 133then
 134        git-cat-file commit "$use_commit" | sed -e '1,/^$/d'
 135elif test -f "$GIT_DIR/MERGE_HEAD" && test -f "$GIT_DIR/MERGE_MSG"
 136then
 137        cat "$GIT_DIR/MERGE_MSG"
 138fi | git-stripspace >"$GIT_DIR"/COMMIT_EDITMSG
 139
 140case "$signoff" in
 141t)
 142        {
 143                echo
 144                git-var GIT_COMMITTER_IDENT | sed -e '
 145                        s/>.*/>/
 146                        s/^/Signed-off-by: /
 147                '
 148        } >>"$GIT_DIR"/COMMIT_EDITMSG
 149        ;;
 150esac
 151
 152if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
 153        echo "#"
 154        echo "# It looks like your may be committing a MERGE."
 155        echo "# If this is not correct, please remove the file"
 156        echo "# $GIT_DIR/MERGE_HEAD"
 157        echo "# and try again"
 158        echo "#"
 159fi >>"$GIT_DIR"/COMMIT_EDITMSG
 160
 161PARENTS="-p HEAD"
 162if GIT_DIR="$GIT_DIR" git-rev-parse --verify HEAD >/dev/null 2>&1
 163then
 164        if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
 165                PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
 166        fi
 167        if test "$use_commit" != ""
 168        then
 169                pick_author_script='
 170                /^author /{
 171                        h
 172                        s/^author \([^<]*\) <[^>]*> .*$/\1/
 173                        s/'\''/'\''\'\'\''/g
 174                        s/.*/GIT_AUTHOR_NAME='\''&'\''/p
 175
 176                        g
 177                        s/^author [^<]* <\([^>]*\)> .*$/\1/
 178                        s/'\''/'\''\'\'\''/g
 179                        s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
 180
 181                        g
 182                        s/^author [^<]* <[^>]*> \(.*\)$/\1/
 183                        s/'\''/'\''\'\'\''/g
 184                        s/.*/GIT_AUTHOR_DATE='\''&'\''/p
 185
 186                        q
 187                }
 188                '
 189                set_author_env=`git-cat-file commit "$use_commit" |
 190                LANG=C LC_ALL=C sed -ne "$pick_author_script"`
 191                eval "$set_author_env"
 192                export GIT_AUTHOR_NAME
 193                export GIT_AUTHOR_EMAIL
 194                export GIT_AUTHOR_DATE
 195        fi
 196else
 197        if [ -z "$(git-ls-files)" ]; then
 198                echo Nothing to commit 1>&2
 199                exit 1
 200        fi
 201        PARENTS=""
 202fi
 203git-status >>"$GIT_DIR"/COMMIT_EDITMSG
 204if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" ]
 205then
 206        rm -f "$GIT_DIR/COMMIT_EDITMSG"
 207        git-status
 208        exit 1
 209fi
 210case "$no_edit" in
 211'')
 212        ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR/COMMIT_EDITMSG"
 213        ;;
 214esac
 215
 216case "$verify" in
 217t)
 218        if test -x "$GIT_DIR"/hooks/commit-msg
 219        then
 220                "$GIT_DIR"/hooks/commit-msg "$GIT_DIR"/COMMIT_EDITMSG || exit
 221        fi
 222esac
 223
 224grep -v '^#' < "$GIT_DIR"/COMMIT_EDITMSG |
 225git-stripspace > "$GIT_DIR"/COMMIT_MSG
 226
 227if cnt=`grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
 228        git-stripspace |
 229        wc -l` &&
 230   test 0 -lt $cnt
 231then
 232        tree=$(git-write-tree) &&
 233        commit=$(cat "$GIT_DIR"/COMMIT_MSG | git-commit-tree $tree $PARENTS) &&
 234        git-update-ref HEAD $commit $current &&
 235        rm -f -- "$GIT_DIR/MERGE_HEAD"
 236else
 237        echo >&2 "* no commit message?  aborting commit."
 238        false
 239fi
 240ret="$?"
 241rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG"
 242
 243if test -x "$GIT_DIR"/hooks/post-commit && test "$ret" = 0
 244then
 245        "$GIT_DIR"/hooks/post-commit
 246fi
 247exit "$ret"