git-commit.shon commit Merge branch 'jc/diff--cached' (67bed72)
   1#!/bin/sh
   2#
   3# Copyright (c) 2005 Linus Torvalds
   4# Copyright (c) 2006 Junio C Hamano
   5
   6USAGE='[-a] [-s] [-v] [--no-verify] [-m <message> | -F <logfile> | (-C|-c) <commit>] [-u] [--amend] [-e] [--author <author>] [[-i | -o] <path>...]'
   7SUBDIRECTORY_OK=Yes
   8. git-sh-setup
   9
  10git-rev-parse --verify HEAD >/dev/null 2>&1 || initial_commit=t
  11branch=$(GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD)
  12
  13case "$0" in
  14*status)
  15        status_only=t
  16        unmerged_ok_if_status=--unmerged ;;
  17*commit)
  18        status_only=
  19        unmerged_ok_if_status= ;;
  20esac
  21
  22refuse_partial () {
  23        echo >&2 "$1"
  24        echo >&2 "You might have meant to say 'git commit -i paths...', perhaps?"
  25        exit 1
  26}
  27
  28THIS_INDEX="$GIT_DIR/index"
  29NEXT_INDEX="$GIT_DIR/next-index$$"
  30rm -f "$NEXT_INDEX"
  31save_index () {
  32        cp -p "$THIS_INDEX" "$NEXT_INDEX"
  33}
  34
  35run_status () {
  36        # If TMP_INDEX is defined, that means we are doing
  37        # "--only" partial commit, and that index file is used
  38        # to build the tree for the commit.  Otherwise, if
  39        # NEXT_INDEX exists, that is the index file used to
  40        # make the commit.  Otherwise we are using as-is commit
  41        # so the regular index file is what we use to compare.
  42        if test '' != "$TMP_INDEX"
  43        then
  44                GIT_INDEX_FILE="$TMP_INDEX"
  45                export GIT_INDEX_FILE
  46        elif test -f "$NEXT_INDEX"
  47        then
  48                GIT_INDEX_FILE="$NEXT_INDEX"
  49                export GIT_INDEX_FILE
  50        fi
  51
  52        case "$status_only" in
  53        t) color= ;;
  54        *) color=--nocolor ;;
  55        esac
  56        git-runstatus ${color} \
  57                ${verbose:+--verbose} \
  58                ${amend:+--amend} \
  59                ${untracked_files:+--untracked}
  60}
  61
  62trap '
  63        test -z "$TMP_INDEX" || {
  64                test -f "$TMP_INDEX" && rm -f "$TMP_INDEX"
  65        }
  66        rm -f "$NEXT_INDEX"
  67' 0
  68
  69################################################################
  70# Command line argument parsing and sanity checking
  71
  72all=
  73also=
  74only=
  75logfile=
  76use_commit=
  77amend=
  78edit_flag=
  79no_edit=
  80log_given=
  81log_message=
  82verify=t
  83verbose=
  84signoff=
  85force_author=
  86only_include_assumed=
  87untracked_files=
  88while case "$#" in 0) break;; esac
  89do
  90        case "$1" in
  91        -F|--F|-f|--f|--fi|--fil|--file)
  92                case "$#" in 1) usage ;; esac
  93                shift
  94                no_edit=t
  95                log_given=t$log_given
  96                logfile="$1"
  97                shift
  98                ;;
  99        -F*|-f*)
 100                no_edit=t
 101                log_given=t$log_given
 102                logfile=`expr "z$1" : 'z-[Ff]\(.*\)'`
 103                shift
 104                ;;
 105        --F=*|--f=*|--fi=*|--fil=*|--file=*)
 106                no_edit=t
 107                log_given=t$log_given
 108                logfile=`expr "z$1" : 'z-[^=]*=\(.*\)'`
 109                shift
 110                ;;
 111        -a|--a|--al|--all)
 112                all=t
 113                shift
 114                ;;
 115        --au=*|--aut=*|--auth=*|--autho=*|--author=*)
 116                force_author=`expr "z$1" : 'z-[^=]*=\(.*\)'`
 117                shift
 118                ;;
 119        --au|--aut|--auth|--autho|--author)
 120                case "$#" in 1) usage ;; esac
 121                shift
 122                force_author="$1"
 123                shift
 124                ;;
 125        -e|--e|--ed|--edi|--edit)
 126                edit_flag=t
 127                shift
 128                ;;
 129        -i|--i|--in|--inc|--incl|--inclu|--includ|--include)
 130                also=t
 131                shift
 132                ;;
 133        -o|--o|--on|--onl|--only)
 134                only=t
 135                shift
 136                ;;
 137        -m|--m|--me|--mes|--mess|--messa|--messag|--message)
 138                case "$#" in 1) usage ;; esac
 139                shift
 140                log_given=m$log_given
 141                if test "$log_message" = ''
 142                then
 143                    log_message="$1"
 144                else
 145                    log_message="$log_message
 146
 147$1"
 148                fi
 149                no_edit=t
 150                shift
 151                ;;
 152        -m*)
 153                log_given=m$log_given
 154                if test "$log_message" = ''
 155                then
 156                    log_message=`expr "z$1" : 'z-m\(.*\)'`
 157                else
 158                    log_message="$log_message
 159
 160`expr "z$1" : 'z-m\(.*\)'`"
 161                fi
 162                no_edit=t
 163                shift
 164                ;;
 165        --m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
 166                log_given=m$log_given
 167                if test "$log_message" = ''
 168                then
 169                    log_message=`expr "z$1" : 'z-[^=]*=\(.*\)'`
 170                else
 171                    log_message="$log_message
 172
 173`expr "z$1" : 'zq-[^=]*=\(.*\)'`"
 174                fi
 175                no_edit=t
 176                shift
 177                ;;
 178        -n|--n|--no|--no-|--no-v|--no-ve|--no-ver|--no-veri|--no-verif|\
 179        --no-verify)
 180                verify=
 181                shift
 182                ;;
 183        --a|--am|--ame|--amen|--amend)
 184                amend=t
 185                log_given=t$log_given
 186                use_commit=HEAD
 187                shift
 188                ;;
 189        -c)
 190                case "$#" in 1) usage ;; esac
 191                shift
 192                log_given=t$log_given
 193                use_commit="$1"
 194                no_edit=
 195                shift
 196                ;;
 197        --ree=*|--reed=*|--reedi=*|--reedit=*|--reedit-=*|--reedit-m=*|\
 198        --reedit-me=*|--reedit-mes=*|--reedit-mess=*|--reedit-messa=*|\
 199        --reedit-messag=*|--reedit-message=*)
 200                log_given=t$log_given
 201                use_commit=`expr "z$1" : 'z-[^=]*=\(.*\)'`
 202                no_edit=
 203                shift
 204                ;;
 205        --ree|--reed|--reedi|--reedit|--reedit-|--reedit-m|--reedit-me|\
 206        --reedit-mes|--reedit-mess|--reedit-messa|--reedit-messag|\
 207        --reedit-message)
 208                case "$#" in 1) usage ;; esac
 209                shift
 210                log_given=t$log_given
 211                use_commit="$1"
 212                no_edit=
 213                shift
 214                ;;
 215        -C)
 216                case "$#" in 1) usage ;; esac
 217                shift
 218                log_given=t$log_given
 219                use_commit="$1"
 220                no_edit=t
 221                shift
 222                ;;
 223        --reu=*|--reus=*|--reuse=*|--reuse-=*|--reuse-m=*|--reuse-me=*|\
 224        --reuse-mes=*|--reuse-mess=*|--reuse-messa=*|--reuse-messag=*|\
 225        --reuse-message=*)
 226                log_given=t$log_given
 227                use_commit=`expr "z$1" : 'z-[^=]*=\(.*\)'`
 228                no_edit=t
 229                shift
 230                ;;
 231        --reu|--reus|--reuse|--reuse-|--reuse-m|--reuse-me|--reuse-mes|\
 232        --reuse-mess|--reuse-messa|--reuse-messag|--reuse-message)
 233                case "$#" in 1) usage ;; esac
 234                shift
 235                log_given=t$log_given
 236                use_commit="$1"
 237                no_edit=t
 238                shift
 239                ;;
 240        -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
 241                signoff=t
 242                shift
 243                ;;
 244        -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
 245                verbose=t
 246                shift
 247                ;;
 248        -u|--u|--un|--unt|--untr|--untra|--untrac|--untrack|--untracke|\
 249        --untracked|--untracked-|--untracked-f|--untracked-fi|--untracked-fil|\
 250        --untracked-file|--untracked-files)
 251                untracked_files=t
 252                shift
 253                ;;
 254        --)
 255                shift
 256                break
 257                ;;
 258        -*)
 259                usage
 260                ;;
 261        *)
 262                break
 263                ;;
 264        esac
 265done
 266case "$edit_flag" in t) no_edit= ;; esac
 267
 268################################################################
 269# Sanity check options
 270
 271case "$amend,$initial_commit" in
 272t,t)
 273        die "You do not have anything to amend." ;;
 274t,)
 275        if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
 276                die "You are in the middle of a merge -- cannot amend."
 277        fi ;;
 278esac
 279
 280case "$log_given" in
 281tt*)
 282        die "Only one of -c/-C/-F can be used." ;;
 283*tm*|*mt*)
 284        die "Option -m cannot be combined with -c/-C/-F." ;;
 285esac
 286
 287case "$#,$also,$only,$amend" in
 288*,t,t,*)
 289        die "Only one of --include/--only can be used." ;;
 2900,t,,* | 0,,t,)
 291        die "No paths with --include/--only does not make sense." ;;
 2920,,t,t)
 293        only_include_assumed="# Clever... amending the last one with dirty index." ;;
 2940,,,*)
 295        ;;
 296*,,,*)
 297        only_include_assumed="# Explicit paths specified without -i nor -o; assuming --only paths..."
 298        also=
 299        ;;
 300esac
 301unset only
 302case "$all,$also,$#" in
 303t,t,*)
 304        die "Cannot use -a and -i at the same time." ;;
 305t,,[1-9]*)
 306        die "Paths with -a does not make sense." ;;
 307,t,0)
 308        die "No paths with -i does not make sense." ;;
 309esac
 310
 311################################################################
 312# Prepare index to have a tree to be committed
 313
 314TOP=`git-rev-parse --show-cdup`
 315if test -z "$TOP"
 316then
 317        TOP=./
 318fi
 319
 320case "$all,$also" in
 321t,)
 322        save_index &&
 323        (
 324                cd "$TOP"
 325                GIT_INDEX_FILE="$NEXT_INDEX"
 326                export GIT_INDEX_FILE
 327                git-diff-files --name-only -z |
 328                git-update-index --remove -z --stdin
 329        )
 330        ;;
 331,t)
 332        save_index &&
 333        git-ls-files --error-unmatch -- "$@" >/dev/null || exit
 334
 335        git-diff-files --name-only -z -- "$@"  |
 336        (
 337                cd "$TOP"
 338                GIT_INDEX_FILE="$NEXT_INDEX"
 339                export GIT_INDEX_FILE
 340                git-update-index --remove -z --stdin
 341        )
 342        ;;
 343,)
 344        case "$#" in
 345        0)
 346                ;; # commit as-is
 347        *)
 348                if test -f "$GIT_DIR/MERGE_HEAD"
 349                then
 350                        refuse_partial "Cannot do a partial commit during a merge."
 351                fi
 352                TMP_INDEX="$GIT_DIR/tmp-index$$"
 353                commit_only=`git-ls-files --error-unmatch -- "$@"` || exit
 354
 355                # Build a temporary index and update the real index
 356                # the same way.
 357                if test -z "$initial_commit"
 358                then
 359                        cp "$THIS_INDEX" "$TMP_INDEX"
 360                        GIT_INDEX_FILE="$TMP_INDEX" git-read-tree -m HEAD
 361                else
 362                        rm -f "$TMP_INDEX"
 363                fi || exit
 364
 365                echo "$commit_only" |
 366                GIT_INDEX_FILE="$TMP_INDEX" \
 367                git-update-index --add --remove --stdin &&
 368
 369                save_index &&
 370                echo "$commit_only" |
 371                (
 372                        GIT_INDEX_FILE="$NEXT_INDEX"
 373                        export GIT_INDEX_FILE
 374                        git-update-index --remove --stdin
 375                ) || exit
 376                ;;
 377        esac
 378        ;;
 379esac
 380
 381################################################################
 382# If we do as-is commit, the index file will be THIS_INDEX,
 383# otherwise NEXT_INDEX after we make this commit.  We leave
 384# the index as is if we abort.
 385
 386if test -f "$NEXT_INDEX"
 387then
 388        USE_INDEX="$NEXT_INDEX"
 389else
 390        USE_INDEX="$THIS_INDEX"
 391fi
 392
 393GIT_INDEX_FILE="$USE_INDEX" \
 394        git-update-index -q $unmerged_ok_if_status --refresh || exit
 395
 396################################################################
 397# If the request is status, just show it and exit.
 398
 399case "$0" in
 400*status)
 401        run_status
 402        exit $?
 403esac
 404
 405################################################################
 406# Grab commit message, write out tree and make commit.
 407
 408if test t = "$verify" && test -x "$GIT_DIR"/hooks/pre-commit
 409then
 410        if test "$TMP_INDEX"
 411        then
 412                GIT_INDEX_FILE="$TMP_INDEX" "$GIT_DIR"/hooks/pre-commit
 413        else
 414                GIT_INDEX_FILE="$USE_INDEX" "$GIT_DIR"/hooks/pre-commit
 415        fi || exit
 416fi
 417
 418if test "$log_message" != ''
 419then
 420        echo "$log_message"
 421elif test "$logfile" != ""
 422then
 423        if test "$logfile" = -
 424        then
 425                test -t 0 &&
 426                echo >&2 "(reading log message from standard input)"
 427                cat
 428        else
 429                cat <"$logfile"
 430        fi
 431elif test "$use_commit" != ""
 432then
 433        git-cat-file commit "$use_commit" | sed -e '1,/^$/d'
 434elif test -f "$GIT_DIR/MERGE_MSG"
 435then
 436        cat "$GIT_DIR/MERGE_MSG"
 437elif test -f "$GIT_DIR/SQUASH_MSG"
 438then
 439        cat "$GIT_DIR/SQUASH_MSG"
 440fi | git-stripspace >"$GIT_DIR"/COMMIT_EDITMSG
 441
 442case "$signoff" in
 443t)
 444        {
 445                echo
 446                git-var GIT_COMMITTER_IDENT | sed -e '
 447                        s/>.*/>/
 448                        s/^/Signed-off-by: /
 449                '
 450        } >>"$GIT_DIR"/COMMIT_EDITMSG
 451        ;;
 452esac
 453
 454if test -f "$GIT_DIR/MERGE_HEAD" && test -z "$no_edit"; then
 455        echo "#"
 456        echo "# It looks like you may be committing a MERGE."
 457        echo "# If this is not correct, please remove the file"
 458        echo "# $GIT_DIR/MERGE_HEAD"
 459        echo "# and try again"
 460        echo "#"
 461fi >>"$GIT_DIR"/COMMIT_EDITMSG
 462
 463# Author
 464if test '' != "$force_author"
 465then
 466        GIT_AUTHOR_NAME=`expr "z$force_author" : 'z\(.*[^ ]\) *<.*'` &&
 467        GIT_AUTHOR_EMAIL=`expr "z$force_author" : '.*\(<.*\)'` &&
 468        test '' != "$GIT_AUTHOR_NAME" &&
 469        test '' != "$GIT_AUTHOR_EMAIL" ||
 470        die "malformed --author parameter"
 471        export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
 472elif test '' != "$use_commit"
 473then
 474        pick_author_script='
 475        /^author /{
 476                s/'\''/'\''\\'\'\''/g
 477                h
 478                s/^author \([^<]*\) <[^>]*> .*$/\1/
 479                s/'\''/'\''\'\'\''/g
 480                s/.*/GIT_AUTHOR_NAME='\''&'\''/p
 481
 482                g
 483                s/^author [^<]* <\([^>]*\)> .*$/\1/
 484                s/'\''/'\''\'\'\''/g
 485                s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
 486
 487                g
 488                s/^author [^<]* <[^>]*> \(.*\)$/\1/
 489                s/'\''/'\''\'\'\''/g
 490                s/.*/GIT_AUTHOR_DATE='\''&'\''/p
 491
 492                q
 493        }
 494        '
 495        set_author_env=`git-cat-file commit "$use_commit" |
 496        LANG=C LC_ALL=C sed -ne "$pick_author_script"`
 497        eval "$set_author_env"
 498        export GIT_AUTHOR_NAME
 499        export GIT_AUTHOR_EMAIL
 500        export GIT_AUTHOR_DATE
 501fi
 502
 503PARENTS="-p HEAD"
 504if test -z "$initial_commit"
 505then
 506        rloga='commit'
 507        if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
 508                rloga='commit (merge)'
 509                PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
 510        elif test -n "$amend"; then
 511                rloga='commit (amend)'
 512                PARENTS=$(git-cat-file commit HEAD |
 513                        sed -n -e '/^$/q' -e 's/^parent /-p /p')
 514        fi
 515        current="$(git-rev-parse --verify HEAD)"
 516else
 517        if [ -z "$(git-ls-files)" ]; then
 518                echo >&2 Nothing to commit
 519                exit 1
 520        fi
 521        PARENTS=""
 522        rloga='commit (initial)'
 523        current=''
 524fi
 525
 526if test -z "$no_edit"
 527then
 528        {
 529                echo ""
 530                echo "# Please enter the commit message for your changes."
 531                echo "# (Comment lines starting with '#' will not be included)"
 532                test -z "$only_include_assumed" || echo "$only_include_assumed"
 533                run_status
 534        } >>"$GIT_DIR"/COMMIT_EDITMSG
 535else
 536        # we need to check if there is anything to commit
 537        run_status >/dev/null 
 538fi
 539if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" -a -z "$amend" ]
 540then
 541        rm -f "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
 542        run_status
 543        exit 1
 544fi
 545
 546case "$no_edit" in
 547'')
 548        case "${VISUAL:-$EDITOR},$TERM" in
 549        ,dumb)
 550                echo >&2 "Terminal is dumb but no VISUAL nor EDITOR defined."
 551                echo >&2 "Please supply the commit log message using either"
 552                echo >&2 "-m or -F option.  A boilerplate log message has"
 553                echo >&2 "been prepared in $GIT_DIR/COMMIT_EDITMSG"
 554                exit 1
 555                ;;
 556        esac
 557        git-var GIT_AUTHOR_IDENT > /dev/null  || die
 558        git-var GIT_COMMITTER_IDENT > /dev/null  || die
 559        ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR/COMMIT_EDITMSG"
 560        ;;
 561esac
 562
 563case "$verify" in
 564t)
 565        if test -x "$GIT_DIR"/hooks/commit-msg
 566        then
 567                "$GIT_DIR"/hooks/commit-msg "$GIT_DIR"/COMMIT_EDITMSG || exit
 568        fi
 569esac
 570
 571if test -z "$no_edit"
 572then
 573    sed -e '
 574        /^diff --git a\/.*/{
 575            s///
 576            q
 577        }
 578        /^#/d
 579    ' "$GIT_DIR"/COMMIT_EDITMSG
 580else
 581    cat "$GIT_DIR"/COMMIT_EDITMSG
 582fi |
 583git-stripspace >"$GIT_DIR"/COMMIT_MSG
 584
 585if cnt=`grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
 586        git-stripspace |
 587        wc -l` &&
 588   test 0 -lt $cnt
 589then
 590        if test -z "$TMP_INDEX"
 591        then
 592                tree=$(GIT_INDEX_FILE="$USE_INDEX" git-write-tree)
 593        else
 594                tree=$(GIT_INDEX_FILE="$TMP_INDEX" git-write-tree) &&
 595                rm -f "$TMP_INDEX"
 596        fi &&
 597        commit=$(cat "$GIT_DIR"/COMMIT_MSG | git-commit-tree $tree $PARENTS) &&
 598        rlogm=$(sed -e 1q "$GIT_DIR"/COMMIT_MSG) &&
 599        git-update-ref -m "$rloga: $rlogm" HEAD $commit "$current" &&
 600        rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" &&
 601        if test -f "$NEXT_INDEX"
 602        then
 603                mv "$NEXT_INDEX" "$THIS_INDEX"
 604        else
 605                : ;# happy
 606        fi
 607else
 608        echo >&2 "* no commit message?  aborting commit."
 609        false
 610fi
 611ret="$?"
 612rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
 613if test -d "$GIT_DIR/rr-cache"
 614then
 615        git-rerere
 616fi
 617
 618if test -x "$GIT_DIR"/hooks/post-commit && test "$ret" = 0
 619then
 620        "$GIT_DIR"/hooks/post-commit
 621fi
 622
 623test "$ret" = 0 && git-diff-tree --summary --root --no-commit-id HEAD
 624
 625exit "$ret"