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