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