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