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