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