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