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