git-am.shon commit am: Add --committer-date-is-author-date option (3f01ad6)
   1#!/bin/sh
   2#
   3# Copyright (c) 2005, 2006 Junio C Hamano
   4
   5SUBDIRECTORY_OK=Yes
   6OPTIONS_KEEPDASHDASH=
   7OPTIONS_SPEC="\
   8git am [options] [<mbox>|<Maildir>...]
   9git am [options] (--resolved | --skip | --abort)
  10--
  11d,dotest=       (removed -- do not use)
  12i,interactive   run interactively
  13b,binary        (historical option -- no-op)
  143,3way          allow fall back on 3way merging if needed
  15s,signoff       add a Signed-off-by line to the commit message
  16u,utf8          recode into utf8 (default)
  17k,keep          pass -k flag to git-mailinfo
  18whitespace=     pass it through git-apply
  19directory=      pass it through git-apply
  20C=              pass it through git-apply
  21p=              pass it through git-apply
  22reject          pass it through git-apply
  23resolvemsg=     override error message when patch failure occurs
  24r,resolved      to be used after a patch failure
  25skip            skip the current patch
  26abort           restore the original branch and abort the patching operation.
  27committer-date-is-author-date    lie about committer date
  28rebasing        (internal use for git-rebase)"
  29
  30. git-sh-setup
  31prefix=$(git rev-parse --show-prefix)
  32set_reflog_action am
  33require_work_tree
  34cd_to_toplevel
  35
  36git var GIT_COMMITTER_IDENT >/dev/null ||
  37        die "You need to set your committer info first"
  38
  39sq () {
  40        for sqarg
  41        do
  42                printf "%s" "$sqarg" |
  43                sed -e 's/'\''/'\''\\'\'''\''/g' -e 's/.*/ '\''&'\''/'
  44        done
  45}
  46
  47stop_here () {
  48    echo "$1" >"$dotest/next"
  49    exit 1
  50}
  51
  52stop_here_user_resolve () {
  53    if [ -n "$resolvemsg" ]; then
  54            printf '%s\n' "$resolvemsg"
  55            stop_here $1
  56    fi
  57    cmdline="git am"
  58    if test '' != "$interactive"
  59    then
  60        cmdline="$cmdline -i"
  61    fi
  62    if test '' != "$threeway"
  63    then
  64        cmdline="$cmdline -3"
  65    fi
  66    echo "When you have resolved this problem run \"$cmdline --resolved\"."
  67    echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
  68    echo "To restore the original branch and stop patching run \"$cmdline --abort\"."
  69
  70    stop_here $1
  71}
  72
  73go_next () {
  74        rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
  75                "$dotest/patch" "$dotest/info"
  76        echo "$next" >"$dotest/next"
  77        this=$next
  78}
  79
  80cannot_fallback () {
  81        echo "$1"
  82        echo "Cannot fall back to three-way merge."
  83        exit 1
  84}
  85
  86fall_back_3way () {
  87    O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
  88
  89    rm -fr "$dotest"/patch-merge-*
  90    mkdir "$dotest/patch-merge-tmp-dir"
  91
  92    # First see if the patch records the index info that we can use.
  93    git apply --build-fake-ancestor "$dotest/patch-merge-tmp-index" \
  94        "$dotest/patch" &&
  95    GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
  96    git write-tree >"$dotest/patch-merge-base+" ||
  97    cannot_fallback "Repository lacks necessary blobs to fall back on 3-way merge."
  98
  99    echo Using index info to reconstruct a base tree...
 100    if GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
 101        git apply --cached <"$dotest/patch"
 102    then
 103        mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
 104        mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
 105    else
 106        cannot_fallback "Did you hand edit your patch?
 107It does not apply to blobs recorded in its index."
 108    fi
 109
 110    test -f "$dotest/patch-merge-index" &&
 111    his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git write-tree) &&
 112    orig_tree=$(cat "$dotest/patch-merge-base") &&
 113    rm -fr "$dotest"/patch-merge-* || exit 1
 114
 115    echo Falling back to patching base and 3-way merge...
 116
 117    # This is not so wrong.  Depending on which base we picked,
 118    # orig_tree may be wildly different from ours, but his_tree
 119    # has the same set of wildly different changes in parts the
 120    # patch did not touch, so recursive ends up canceling them,
 121    # saying that we reverted all those changes.
 122
 123    eval GITHEAD_$his_tree='"$FIRSTLINE"'
 124    export GITHEAD_$his_tree
 125    git-merge-recursive $orig_tree -- HEAD $his_tree || {
 126            git rerere
 127            echo Failed to merge in the changes.
 128            exit 1
 129    }
 130    unset GITHEAD_$his_tree
 131}
 132
 133prec=4
 134dotest="$GIT_DIR/rebase-apply"
 135sign= utf8=t keep= skip= interactive= resolved= rebasing= abort=
 136resolvemsg= resume=
 137git_apply_opt=
 138committer_date_is_author_date=
 139
 140while test $# != 0
 141do
 142        case "$1" in
 143        -i|--interactive)
 144                interactive=t ;;
 145        -b|--binary)
 146                : ;;
 147        -3|--3way)
 148                threeway=t ;;
 149        -s|--signoff)
 150                sign=t ;;
 151        -u|--utf8)
 152                utf8=t ;; # this is now default
 153        --no-utf8)
 154                utf8= ;;
 155        -k|--keep)
 156                keep=t ;;
 157        -r|--resolved)
 158                resolved=t ;;
 159        --skip)
 160                skip=t ;;
 161        --abort)
 162                abort=t ;;
 163        --rebasing)
 164                rebasing=t threeway=t keep=t ;;
 165        -d|--dotest)
 166                die "-d option is no longer supported.  Do not use."
 167                ;;
 168        --resolvemsg)
 169                shift; resolvemsg=$1 ;;
 170        --whitespace|--directory)
 171                git_apply_opt="$git_apply_opt $(sq "$1=$2")"; shift ;;
 172        -C|-p)
 173                git_apply_opt="$git_apply_opt $(sq "$1$2")"; shift ;;
 174        --reject)
 175                git_apply_opt="$git_apply_opt $1" ;;
 176        --committer-date-is-author-date)
 177                committer_date_is_author_date=t ;;
 178        --)
 179                shift; break ;;
 180        *)
 181                usage ;;
 182        esac
 183        shift
 184done
 185
 186# If the dotest directory exists, but we have finished applying all the
 187# patches in them, clear it out.
 188if test -d "$dotest" &&
 189   last=$(cat "$dotest/last") &&
 190   next=$(cat "$dotest/next") &&
 191   test $# != 0 &&
 192   test "$next" -gt "$last"
 193then
 194   rm -fr "$dotest"
 195fi
 196
 197if test -d "$dotest"
 198then
 199        case "$#,$skip$resolved$abort" in
 200        0,*t*)
 201                # Explicit resume command and we do not have file, so
 202                # we are happy.
 203                : ;;
 204        0,)
 205                # No file input but without resume parameters; catch
 206                # user error to feed us a patch from standard input
 207                # when there is already $dotest.  This is somewhat
 208                # unreliable -- stdin could be /dev/null for example
 209                # and the caller did not intend to feed us a patch but
 210                # wanted to continue unattended.
 211                tty -s
 212                ;;
 213        *)
 214                false
 215                ;;
 216        esac ||
 217        die "previous rebase directory $dotest still exists but mbox given."
 218        resume=yes
 219
 220        case "$skip,$abort" in
 221        t,)
 222                git rerere clear
 223                git read-tree --reset -u HEAD HEAD
 224                orig_head=$(cat "$GIT_DIR/ORIG_HEAD")
 225                git reset HEAD
 226                git update-ref ORIG_HEAD $orig_head
 227                ;;
 228        ,t)
 229                git rerere clear
 230                git read-tree --reset -u HEAD ORIG_HEAD
 231                git reset ORIG_HEAD
 232                rm -fr "$dotest"
 233                exit ;;
 234        esac
 235else
 236        # Make sure we are not given --skip, --resolved, nor --abort
 237        test "$skip$resolved$abort" = "" ||
 238                die "Resolve operation not in progress, we are not resuming."
 239
 240        # Start afresh.
 241        mkdir -p "$dotest" || exit
 242
 243        if test -n "$prefix" && test $# != 0
 244        then
 245                first=t
 246                for arg
 247                do
 248                        test -n "$first" && {
 249                                set x
 250                                first=
 251                        }
 252                        case "$arg" in
 253                        /*)
 254                                set "$@" "$arg" ;;
 255                        *)
 256                                set "$@" "$prefix$arg" ;;
 257                        esac
 258                done
 259                shift
 260        fi
 261        git mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" ||  {
 262                rm -fr "$dotest"
 263                exit 1
 264        }
 265
 266        # -s, -u, -k, --whitespace, -3, -C and -p flags are kept
 267        # for the resuming session after a patch failure.
 268        # -i can and must be given when resuming.
 269        echo " $git_apply_opt" >"$dotest/apply-opt"
 270        echo "$threeway" >"$dotest/threeway"
 271        echo "$sign" >"$dotest/sign"
 272        echo "$utf8" >"$dotest/utf8"
 273        echo "$keep" >"$dotest/keep"
 274        echo 1 >"$dotest/next"
 275        if test -n "$rebasing"
 276        then
 277                : >"$dotest/rebasing"
 278        else
 279                : >"$dotest/applying"
 280                git update-ref ORIG_HEAD HEAD
 281        fi
 282fi
 283
 284case "$resolved" in
 285'')
 286        files=$(git diff-index --cached --name-only HEAD --) || exit
 287        if [ "$files" ]; then
 288           echo "Dirty index: cannot apply patches (dirty: $files)" >&2
 289           exit 1
 290        fi
 291esac
 292
 293if test "$(cat "$dotest/utf8")" = t
 294then
 295        utf8=-u
 296else
 297        utf8=-n
 298fi
 299if test "$(cat "$dotest/keep")" = t
 300then
 301        keep=-k
 302fi
 303if test "$(cat "$dotest/threeway")" = t
 304then
 305        threeway=t
 306fi
 307git_apply_opt=$(cat "$dotest/apply-opt")
 308if test "$(cat "$dotest/sign")" = t
 309then
 310        SIGNOFF=`git var GIT_COMMITTER_IDENT | sed -e '
 311                        s/>.*/>/
 312                        s/^/Signed-off-by: /'
 313                `
 314else
 315        SIGNOFF=
 316fi
 317
 318last=`cat "$dotest/last"`
 319this=`cat "$dotest/next"`
 320if test "$skip" = t
 321then
 322        this=`expr "$this" + 1`
 323        resume=
 324fi
 325
 326if test "$this" -gt "$last"
 327then
 328        echo Nothing to do.
 329        rm -fr "$dotest"
 330        exit
 331fi
 332
 333while test "$this" -le "$last"
 334do
 335        msgnum=`printf "%0${prec}d" $this`
 336        next=`expr "$this" + 1`
 337        test -f "$dotest/$msgnum" || {
 338                resume=
 339                go_next
 340                continue
 341        }
 342
 343        # If we are not resuming, parse and extract the patch information
 344        # into separate files:
 345        #  - info records the authorship and title
 346        #  - msg is the rest of commit log message
 347        #  - patch is the patch body.
 348        #
 349        # When we are resuming, these files are either already prepared
 350        # by the user, or the user can tell us to do so by --resolved flag.
 351        case "$resume" in
 352        '')
 353                git mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \
 354                        <"$dotest/$msgnum" >"$dotest/info" ||
 355                        stop_here $this
 356
 357                # skip pine's internal folder data
 358                grep '^Author: Mail System Internal Data$' \
 359                        <"$dotest"/info >/dev/null &&
 360                        go_next && continue
 361
 362                test -s "$dotest/patch" || {
 363                        echo "Patch is empty.  Was it split wrong?"
 364                        stop_here $this
 365                }
 366                if test -f "$dotest/rebasing" &&
 367                        commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
 368                                -e q "$dotest/$msgnum") &&
 369                        test "$(git cat-file -t "$commit")" = commit
 370                then
 371                        git cat-file commit "$commit" |
 372                        sed -e '1,/^$/d' >"$dotest/msg-clean"
 373                else
 374                        SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
 375                        case "$keep_subject" in -k)  SUBJECT="[PATCH] $SUBJECT" ;; esac
 376
 377                        (printf '%s\n\n' "$SUBJECT"; cat "$dotest/msg") |
 378                                git stripspace > "$dotest/msg-clean"
 379                fi
 380                ;;
 381        esac
 382
 383        GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
 384        GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
 385        GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
 386
 387        if test -z "$GIT_AUTHOR_EMAIL"
 388        then
 389                echo "Patch does not have a valid e-mail address."
 390                stop_here $this
 391        fi
 392
 393        export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
 394
 395        case "$resume" in
 396        '')
 397            if test '' != "$SIGNOFF"
 398            then
 399                LAST_SIGNED_OFF_BY=`
 400                    sed -ne '/^Signed-off-by: /p' \
 401                    "$dotest/msg-clean" |
 402                    sed -ne '$p'
 403                `
 404                ADD_SIGNOFF=`
 405                    test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
 406                    test '' = "$LAST_SIGNED_OFF_BY" && echo
 407                    echo "$SIGNOFF"
 408                }`
 409            else
 410                ADD_SIGNOFF=
 411            fi
 412            {
 413                if test -s "$dotest/msg-clean"
 414                then
 415                        cat "$dotest/msg-clean"
 416                fi
 417                if test '' != "$ADD_SIGNOFF"
 418                then
 419                        echo "$ADD_SIGNOFF"
 420                fi
 421            } >"$dotest/final-commit"
 422            ;;
 423        *)
 424                case "$resolved$interactive" in
 425                tt)
 426                        # This is used only for interactive view option.
 427                        git diff-index -p --cached HEAD -- >"$dotest/patch"
 428                        ;;
 429                esac
 430        esac
 431
 432        resume=
 433        if test "$interactive" = t
 434        then
 435            test -t 0 ||
 436            die "cannot be interactive without stdin connected to a terminal."
 437            action=again
 438            while test "$action" = again
 439            do
 440                echo "Commit Body is:"
 441                echo "--------------------------"
 442                cat "$dotest/final-commit"
 443                echo "--------------------------"
 444                printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
 445                read reply
 446                case "$reply" in
 447                [yY]*) action=yes ;;
 448                [aA]*) action=yes interactive= ;;
 449                [nN]*) action=skip ;;
 450                [eE]*) git_editor "$dotest/final-commit"
 451                       action=again ;;
 452                [vV]*) action=again
 453                       LESS=-S ${PAGER:-less} "$dotest/patch" ;;
 454                *)     action=again ;;
 455                esac
 456            done
 457        else
 458            action=yes
 459        fi
 460        FIRSTLINE=$(sed 1q "$dotest/final-commit")
 461
 462        if test $action = skip
 463        then
 464                go_next
 465                continue
 466        fi
 467
 468        if test -x "$GIT_DIR"/hooks/applypatch-msg
 469        then
 470                "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
 471                stop_here $this
 472        fi
 473
 474        printf 'Applying: %s\n' "$FIRSTLINE"
 475
 476        case "$resolved" in
 477        '')
 478                eval 'git apply '"$git_apply_opt"' --index "$dotest/patch"'
 479                apply_status=$?
 480                ;;
 481        t)
 482                # Resolved means the user did all the hard work, and
 483                # we do not have to do any patch application.  Just
 484                # trust what the user has in the index file and the
 485                # working tree.
 486                resolved=
 487                git diff-index --quiet --cached HEAD -- && {
 488                        echo "No changes - did you forget to use 'git add'?"
 489                        stop_here_user_resolve $this
 490                }
 491                unmerged=$(git ls-files -u)
 492                if test -n "$unmerged"
 493                then
 494                        echo "You still have unmerged paths in your index"
 495                        echo "did you forget to use 'git add'?"
 496                        stop_here_user_resolve $this
 497                fi
 498                apply_status=0
 499                git rerere
 500                ;;
 501        esac
 502
 503        if test $apply_status = 1 && test "$threeway" = t
 504        then
 505                if (fall_back_3way)
 506                then
 507                    # Applying the patch to an earlier tree and merging the
 508                    # result may have produced the same tree as ours.
 509                    git diff-index --quiet --cached HEAD -- && {
 510                        echo No changes -- Patch already applied.
 511                        go_next
 512                        continue
 513                    }
 514                    # clear apply_status -- we have successfully merged.
 515                    apply_status=0
 516                fi
 517        fi
 518        if test $apply_status != 0
 519        then
 520                printf 'Patch failed at %s %s\n' "$msgnum" "$FIRSTLINE"
 521                stop_here_user_resolve $this
 522        fi
 523
 524        if test -x "$GIT_DIR"/hooks/pre-applypatch
 525        then
 526                "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
 527        fi
 528
 529        tree=$(git write-tree) &&
 530        parent=$(git rev-parse --verify HEAD) &&
 531        commit=$(
 532                if test -n "$committer_date_is_author_date"
 533                then
 534                        GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
 535                        export GIT_COMMITTER_DATE
 536                fi &&
 537                git commit-tree $tree -p $parent <"$dotest/final-commit"
 538        ) &&
 539        git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
 540        stop_here $this
 541
 542        if test -x "$GIT_DIR"/hooks/post-applypatch
 543        then
 544                "$GIT_DIR"/hooks/post-applypatch
 545        fi
 546
 547        go_next
 548done
 549
 550git gc --auto
 551
 552rm -fr "$dotest"