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