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