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