git-filter-branch.shon commit send-email: document --no-[to|cc|bcc] (564eba4)
   1#!/bin/sh
   2#
   3# Rewrite revision history
   4# Copyright (c) Petr Baudis, 2006
   5# Minimal changes to "port" it to core-git (c) Johannes Schindelin, 2007
   6#
   7# Lets you rewrite the revision history of the current branch, creating
   8# a new branch. You can specify a number of filters to modify the commits,
   9# files and trees.
  10
  11# The following functions will also be available in the commit filter:
  12
  13functions=$(cat << \EOF
  14EMPTY_TREE=$(git hash-object -t tree /dev/null)
  15
  16warn () {
  17        echo "$*" >&2
  18}
  19
  20map()
  21{
  22        # if it was not rewritten, take the original
  23        if test -r "$workdir/../map/$1"
  24        then
  25                cat "$workdir/../map/$1"
  26        else
  27                echo "$1"
  28        fi
  29}
  30
  31# if you run 'skip_commit "$@"' in a commit filter, it will print
  32# the (mapped) parents, effectively skipping the commit.
  33
  34skip_commit()
  35{
  36        shift;
  37        while [ -n "$1" ];
  38        do
  39                shift;
  40                map "$1";
  41                shift;
  42        done;
  43}
  44
  45# if you run 'git_commit_non_empty_tree "$@"' in a commit filter,
  46# it will skip commits that leave the tree untouched, commit the other.
  47git_commit_non_empty_tree()
  48{
  49        if test $# = 3 && test "$1" = $(git rev-parse "$3^{tree}"); then
  50                map "$3"
  51        elif test $# = 1 && test "$1" = $EMPTY_TREE; then
  52                :
  53        else
  54                git commit-tree "$@"
  55        fi
  56}
  57# override die(): this version puts in an extra line break, so that
  58# the progress is still visible
  59
  60die()
  61{
  62        echo >&2
  63        echo "$*" >&2
  64        exit 1
  65}
  66EOF
  67)
  68
  69eval "$functions"
  70
  71finish_ident() {
  72        # Ensure non-empty id name.
  73        echo "case \"\$GIT_$1_NAME\" in \"\") GIT_$1_NAME=\"\${GIT_$1_EMAIL%%@*}\" && export GIT_$1_NAME;; esac"
  74        # And make sure everything is exported.
  75        echo "export GIT_$1_NAME"
  76        echo "export GIT_$1_EMAIL"
  77        echo "export GIT_$1_DATE"
  78}
  79
  80set_ident () {
  81        parse_ident_from_commit author AUTHOR committer COMMITTER
  82        finish_ident AUTHOR
  83        finish_ident COMMITTER
  84}
  85
  86USAGE="[--setup <command>] [--subdirectory-filter <directory>] [--env-filter <command>]
  87        [--tree-filter <command>] [--index-filter <command>]
  88        [--parent-filter <command>] [--msg-filter <command>]
  89        [--commit-filter <command>] [--tag-name-filter <command>]
  90        [--original <namespace>]
  91        [-d <directory>] [-f | --force] [--state-branch <branch>]
  92        [--] [<rev-list options>...]"
  93
  94OPTIONS_SPEC=
  95. git-sh-setup
  96
  97if [ "$(is_bare_repository)" = false ]; then
  98        require_clean_work_tree 'rewrite branches'
  99fi
 100
 101tempdir=.git-rewrite
 102filter_setup=
 103filter_env=
 104filter_tree=
 105filter_index=
 106filter_parent=
 107filter_msg=cat
 108filter_commit=
 109filter_tag_name=
 110filter_subdir=
 111state_branch=
 112orig_namespace=refs/original/
 113force=
 114prune_empty=
 115remap_to_ancestor=
 116while :
 117do
 118        case "$1" in
 119        --)
 120                shift
 121                break
 122                ;;
 123        --force|-f)
 124                shift
 125                force=t
 126                continue
 127                ;;
 128        --remap-to-ancestor)
 129                # deprecated ($remap_to_ancestor is set now automatically)
 130                shift
 131                remap_to_ancestor=t
 132                continue
 133                ;;
 134        --prune-empty)
 135                shift
 136                prune_empty=t
 137                continue
 138                ;;
 139        -*)
 140                ;;
 141        *)
 142                break;
 143        esac
 144
 145        # all switches take one argument
 146        ARG="$1"
 147        case "$#" in 1) usage ;; esac
 148        shift
 149        OPTARG="$1"
 150        shift
 151
 152        case "$ARG" in
 153        -d)
 154                tempdir="$OPTARG"
 155                ;;
 156        --setup)
 157                filter_setup="$OPTARG"
 158                ;;
 159        --subdirectory-filter)
 160                filter_subdir="$OPTARG"
 161                remap_to_ancestor=t
 162                ;;
 163        --env-filter)
 164                filter_env="$OPTARG"
 165                ;;
 166        --tree-filter)
 167                filter_tree="$OPTARG"
 168                ;;
 169        --index-filter)
 170                filter_index="$OPTARG"
 171                ;;
 172        --parent-filter)
 173                filter_parent="$OPTARG"
 174                ;;
 175        --msg-filter)
 176                filter_msg="$OPTARG"
 177                ;;
 178        --commit-filter)
 179                filter_commit="$functions; $OPTARG"
 180                ;;
 181        --tag-name-filter)
 182                filter_tag_name="$OPTARG"
 183                ;;
 184        --original)
 185                orig_namespace=$(expr "$OPTARG/" : '\(.*[^/]\)/*$')/
 186                ;;
 187        --state-branch)
 188                state_branch="$OPTARG"
 189                ;;
 190        *)
 191                usage
 192                ;;
 193        esac
 194done
 195
 196case "$prune_empty,$filter_commit" in
 197,)
 198        filter_commit='git commit-tree "$@"';;
 199t,)
 200        filter_commit="$functions;"' git_commit_non_empty_tree "$@"';;
 201,*)
 202        ;;
 203*)
 204        die "Cannot set --prune-empty and --commit-filter at the same time"
 205esac
 206
 207case "$force" in
 208t)
 209        rm -rf "$tempdir"
 210;;
 211'')
 212        test -d "$tempdir" &&
 213                die "$tempdir already exists, please remove it"
 214esac
 215orig_dir=$(pwd)
 216mkdir -p "$tempdir/t" &&
 217tempdir="$(cd "$tempdir"; pwd)" &&
 218cd "$tempdir/t" &&
 219workdir="$(pwd)" ||
 220die ""
 221
 222# Remove tempdir on exit
 223trap 'cd "$orig_dir"; rm -rf "$tempdir"' 0
 224
 225ORIG_GIT_DIR="$GIT_DIR"
 226ORIG_GIT_WORK_TREE="$GIT_WORK_TREE"
 227ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE"
 228ORIG_GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME"
 229ORIG_GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL"
 230ORIG_GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE"
 231ORIG_GIT_COMMITTER_NAME="$GIT_COMMITTER_NAME"
 232ORIG_GIT_COMMITTER_EMAIL="$GIT_COMMITTER_EMAIL"
 233ORIG_GIT_COMMITTER_DATE="$GIT_COMMITTER_DATE"
 234
 235GIT_WORK_TREE=.
 236export GIT_DIR GIT_WORK_TREE
 237
 238# Make sure refs/original is empty
 239git for-each-ref > "$tempdir"/backup-refs || exit
 240while read sha1 type name
 241do
 242        case "$force,$name" in
 243        ,$orig_namespace*)
 244                die "Cannot create a new backup.
 245A previous backup already exists in $orig_namespace
 246Force overwriting the backup with -f"
 247        ;;
 248        t,$orig_namespace*)
 249                git update-ref -d "$name" $sha1
 250        ;;
 251        esac
 252done < "$tempdir"/backup-refs
 253
 254# The refs should be updated if their heads were rewritten
 255git rev-parse --no-flags --revs-only --symbolic-full-name \
 256        --default HEAD "$@" > "$tempdir"/raw-refs || exit
 257while read ref
 258do
 259        case "$ref" in ^?*) continue ;; esac
 260
 261        if git rev-parse --verify "$ref"^0 >/dev/null 2>&1
 262        then
 263                echo "$ref"
 264        else
 265                warn "WARNING: not rewriting '$ref' (not a committish)"
 266        fi
 267done >"$tempdir"/heads <"$tempdir"/raw-refs
 268
 269test -s "$tempdir"/heads ||
 270        die "You must specify a ref to rewrite."
 271
 272GIT_INDEX_FILE="$(pwd)/../index"
 273export GIT_INDEX_FILE
 274
 275# map old->new commit ids for rewriting parents
 276mkdir ../map || die "Could not create map/ directory"
 277
 278if test -n "$state_branch"
 279then
 280        state_commit=$(git rev-parse --no-flags --revs-only "$state_branch")
 281        if test -n "$state_commit"
 282        then
 283                echo "Populating map from $state_branch ($state_commit)" 1>&2
 284                perl -e'open(MAP, "-|", "git show $ARGV[0]:filter.map") or die;
 285                        while (<MAP>) {
 286                                m/(.*):(.*)/ or die;
 287                                open F, ">../map/$1" or die;
 288                                print F "$2" or die;
 289                                close(F) or die;
 290                        }
 291                        close(MAP) or die;' "$state_commit" \
 292                                || die "Unable to load state from $state_branch:filter.map"
 293        else
 294                echo "Branch $state_branch does not exist. Will create" 1>&2
 295        fi
 296fi
 297
 298# we need "--" only if there are no path arguments in $@
 299nonrevs=$(git rev-parse --no-revs "$@") || exit
 300if test -z "$nonrevs"
 301then
 302        dashdash=--
 303else
 304        dashdash=
 305        remap_to_ancestor=t
 306fi
 307
 308git rev-parse --revs-only "$@" >../parse
 309
 310case "$filter_subdir" in
 311"")
 312        eval set -- "$(git rev-parse --sq --no-revs "$@")"
 313        ;;
 314*)
 315        eval set -- "$(git rev-parse --sq --no-revs "$@" $dashdash \
 316                "$filter_subdir")"
 317        ;;
 318esac
 319
 320git rev-list --reverse --topo-order --default HEAD \
 321        --parents --simplify-merges --stdin "$@" <../parse >../revs ||
 322        die "Could not get the commits"
 323commits=$(wc -l <../revs | tr -d " ")
 324
 325test $commits -eq 0 && die_with_status 2 "Found nothing to rewrite"
 326
 327# Rewrite the commits
 328report_progress ()
 329{
 330        if test -n "$progress" &&
 331                test $git_filter_branch__commit_count -gt $next_sample_at
 332        then
 333                count=$git_filter_branch__commit_count
 334
 335                now=$(date +%s)
 336                elapsed=$(($now - $start_timestamp))
 337                remaining=$(( ($commits - $count) * $elapsed / $count ))
 338                if test $elapsed -gt 0
 339                then
 340                        next_sample_at=$(( ($elapsed + 1) * $count / $elapsed ))
 341                else
 342                        next_sample_at=$(($next_sample_at + 1))
 343                fi
 344                progress=" ($elapsed seconds passed, remaining $remaining predicted)"
 345        fi
 346        printf "\rRewrite $commit ($count/$commits)$progress    "
 347}
 348
 349git_filter_branch__commit_count=0
 350
 351progress= start_timestamp=
 352if date '+%s' 2>/dev/null | grep -q '^[0-9][0-9]*$'
 353then
 354        next_sample_at=0
 355        progress="dummy to ensure this is not empty"
 356        start_timestamp=$(date '+%s')
 357fi
 358
 359if test -n "$filter_index" ||
 360   test -n "$filter_tree" ||
 361   test -n "$filter_subdir"
 362then
 363        need_index=t
 364else
 365        need_index=
 366fi
 367
 368eval "$filter_setup" < /dev/null ||
 369        die "filter setup failed: $filter_setup"
 370
 371while read commit parents; do
 372        git_filter_branch__commit_count=$(($git_filter_branch__commit_count+1))
 373
 374        report_progress
 375
 376        case "$filter_subdir" in
 377        "")
 378                if test -n "$need_index"
 379                then
 380                        GIT_ALLOW_NULL_SHA1=1 git read-tree -i -m $commit
 381                fi
 382                ;;
 383        *)
 384                # The commit may not have the subdirectory at all
 385                err=$(GIT_ALLOW_NULL_SHA1=1 \
 386                      git read-tree -i -m $commit:"$filter_subdir" 2>&1) || {
 387                        if ! git rev-parse -q --verify $commit:"$filter_subdir"
 388                        then
 389                                rm -f "$GIT_INDEX_FILE"
 390                        else
 391                                echo >&2 "$err"
 392                                false
 393                        fi
 394                }
 395        esac || die "Could not initialize the index"
 396
 397        GIT_COMMIT=$commit
 398        export GIT_COMMIT
 399        git cat-file commit "$commit" >../commit ||
 400                die "Cannot read commit $commit"
 401
 402        eval "$(set_ident <../commit)" ||
 403                die "setting author/committer failed for commit $commit"
 404        eval "$filter_env" < /dev/null ||
 405                die "env filter failed: $filter_env"
 406
 407        if [ "$filter_tree" ]; then
 408                git checkout-index -f -u -a ||
 409                        die "Could not checkout the index"
 410                # files that $commit removed are now still in the working tree;
 411                # remove them, else they would be added again
 412                git clean -d -q -f -x
 413                eval "$filter_tree" < /dev/null ||
 414                        die "tree filter failed: $filter_tree"
 415
 416                (
 417                        git diff-index -r --name-only --ignore-submodules $commit -- &&
 418                        git ls-files --others
 419                ) > "$tempdir"/tree-state || exit
 420                git update-index --add --replace --remove --stdin \
 421                        < "$tempdir"/tree-state || exit
 422        fi
 423
 424        eval "$filter_index" < /dev/null ||
 425                die "index filter failed: $filter_index"
 426
 427        parentstr=
 428        for parent in $parents; do
 429                for reparent in $(map "$parent"); do
 430                        case "$parentstr " in
 431                        *" -p $reparent "*)
 432                                ;;
 433                        *)
 434                                parentstr="$parentstr -p $reparent"
 435                                ;;
 436                        esac
 437                done
 438        done
 439        if [ "$filter_parent" ]; then
 440                parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
 441                                die "parent filter failed: $filter_parent"
 442        fi
 443
 444        {
 445                while IFS='' read -r header_line && test -n "$header_line"
 446                do
 447                        # skip header lines...
 448                        :;
 449                done
 450                # and output the actual commit message
 451                cat
 452        } <../commit |
 453                eval "$filter_msg" > ../message ||
 454                        die "msg filter failed: $filter_msg"
 455
 456        if test -n "$need_index"
 457        then
 458                tree=$(git write-tree)
 459        else
 460                tree=$(git rev-parse "$commit^{tree}")
 461        fi
 462        workdir=$workdir @SHELL_PATH@ -c "$filter_commit" "git commit-tree" \
 463                "$tree" $parentstr < ../message > ../map/$commit ||
 464                        die "could not write rewritten commit"
 465done <../revs
 466
 467# If we are filtering for paths, as in the case of a subdirectory
 468# filter, it is possible that a specified head is not in the set of
 469# rewritten commits, because it was pruned by the revision walker.
 470# Ancestor remapping fixes this by mapping these heads to the unique
 471# nearest ancestor that survived the pruning.
 472
 473if test "$remap_to_ancestor" = t
 474then
 475        while read ref
 476        do
 477                sha1=$(git rev-parse "$ref"^0)
 478                test -f "$workdir"/../map/$sha1 && continue
 479                ancestor=$(git rev-list --simplify-merges -1 "$ref" "$@")
 480                test "$ancestor" && echo $(map $ancestor) >> "$workdir"/../map/$sha1
 481        done < "$tempdir"/heads
 482fi
 483
 484# Finally update the refs
 485
 486_x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
 487_x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
 488echo
 489while read ref
 490do
 491        # avoid rewriting a ref twice
 492        test -f "$orig_namespace$ref" && continue
 493
 494        sha1=$(git rev-parse "$ref"^0)
 495        rewritten=$(map $sha1)
 496
 497        test $sha1 = "$rewritten" &&
 498                warn "WARNING: Ref '$ref' is unchanged" &&
 499                continue
 500
 501        case "$rewritten" in
 502        '')
 503                echo "Ref '$ref' was deleted"
 504                git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
 505                        die "Could not delete $ref"
 506        ;;
 507        $_x40)
 508                echo "Ref '$ref' was rewritten"
 509                if ! git update-ref -m "filter-branch: rewrite" \
 510                                        "$ref" $rewritten $sha1 2>/dev/null; then
 511                        if test $(git cat-file -t "$ref") = tag; then
 512                                if test -z "$filter_tag_name"; then
 513                                        warn "WARNING: You said to rewrite tagged commits, but not the corresponding tag."
 514                                        warn "WARNING: Perhaps use '--tag-name-filter cat' to rewrite the tag."
 515                                fi
 516                        else
 517                                die "Could not rewrite $ref"
 518                        fi
 519                fi
 520        ;;
 521        *)
 522                # NEEDSWORK: possibly add -Werror, making this an error
 523                warn "WARNING: '$ref' was rewritten into multiple commits:"
 524                warn "$rewritten"
 525                warn "WARNING: Ref '$ref' points to the first one now."
 526                rewritten=$(echo "$rewritten" | head -n 1)
 527                git update-ref -m "filter-branch: rewrite to first" \
 528                                "$ref" $rewritten $sha1 ||
 529                        die "Could not rewrite $ref"
 530        ;;
 531        esac
 532        git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1 ||
 533                 exit
 534done < "$tempdir"/heads
 535
 536# TODO: This should possibly go, with the semantics that all positive given
 537#       refs are updated, and their original heads stored in refs/original/
 538# Filter tags
 539
 540if [ "$filter_tag_name" ]; then
 541        git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags |
 542        while read sha1 type ref; do
 543                ref="${ref#refs/tags/}"
 544                # XXX: Rewrite tagged trees as well?
 545                if [ "$type" != "commit" -a "$type" != "tag" ]; then
 546                        continue;
 547                fi
 548
 549                if [ "$type" = "tag" ]; then
 550                        # Dereference to a commit
 551                        sha1t="$sha1"
 552                        sha1="$(git rev-parse -q "$sha1"^{commit})" || continue
 553                fi
 554
 555                [ -f "../map/$sha1" ] || continue
 556                new_sha1="$(cat "../map/$sha1")"
 557                GIT_COMMIT="$sha1"
 558                export GIT_COMMIT
 559                new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
 560                        die "tag name filter failed: $filter_tag_name"
 561
 562                echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
 563
 564                if [ "$type" = "tag" ]; then
 565                        new_sha1=$( ( printf 'object %s\ntype commit\ntag %s\n' \
 566                                                "$new_sha1" "$new_ref"
 567                                git cat-file tag "$ref" |
 568                                sed -n \
 569                                    -e '1,/^$/{
 570                                          /^object /d
 571                                          /^type /d
 572                                          /^tag /d
 573                                        }' \
 574                                    -e '/^-----BEGIN PGP SIGNATURE-----/q' \
 575                                    -e 'p' ) |
 576                                git hash-object -t tag -w --stdin) ||
 577                                die "Could not create new tag object for $ref"
 578                        if git cat-file tag "$ref" | \
 579                           sane_grep '^-----BEGIN PGP SIGNATURE-----' >/dev/null 2>&1
 580                        then
 581                                warn "gpg signature stripped from tag object $sha1t"
 582                        fi
 583                fi
 584
 585                git update-ref "refs/tags/$new_ref" "$new_sha1" ||
 586                        die "Could not write tag $new_ref"
 587        done
 588fi
 589
 590unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
 591unset GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
 592unset GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL GIT_COMMITTER_DATE
 593test -z "$ORIG_GIT_DIR" || {
 594        GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR
 595}
 596test -z "$ORIG_GIT_WORK_TREE" || {
 597        GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" &&
 598        export GIT_WORK_TREE
 599}
 600test -z "$ORIG_GIT_INDEX_FILE" || {
 601        GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" &&
 602        export GIT_INDEX_FILE
 603}
 604test -z "$ORIG_GIT_AUTHOR_NAME" || {
 605        GIT_AUTHOR_NAME="$ORIG_GIT_AUTHOR_NAME" &&
 606        export GIT_AUTHOR_NAME
 607}
 608test -z "$ORIG_GIT_AUTHOR_EMAIL" || {
 609        GIT_AUTHOR_EMAIL="$ORIG_GIT_AUTHOR_EMAIL" &&
 610        export GIT_AUTHOR_EMAIL
 611}
 612test -z "$ORIG_GIT_AUTHOR_DATE" || {
 613        GIT_AUTHOR_DATE="$ORIG_GIT_AUTHOR_DATE" &&
 614        export GIT_AUTHOR_DATE
 615}
 616test -z "$ORIG_GIT_COMMITTER_NAME" || {
 617        GIT_COMMITTER_NAME="$ORIG_GIT_COMMITTER_NAME" &&
 618        export GIT_COMMITTER_NAME
 619}
 620test -z "$ORIG_GIT_COMMITTER_EMAIL" || {
 621        GIT_COMMITTER_EMAIL="$ORIG_GIT_COMMITTER_EMAIL" &&
 622        export GIT_COMMITTER_EMAIL
 623}
 624test -z "$ORIG_GIT_COMMITTER_DATE" || {
 625        GIT_COMMITTER_DATE="$ORIG_GIT_COMMITTER_DATE" &&
 626        export GIT_COMMITTER_DATE
 627}
 628
 629if test -n "$state_branch"
 630then
 631        echo "Saving rewrite state to $state_branch" 1>&2
 632        state_blob=$(
 633                perl -e'opendir D, "../map" or die;
 634                        open H, "|-", "git hash-object -w --stdin" or die;
 635                        foreach (sort readdir(D)) {
 636                                next if m/^\.\.?$/;
 637                                open F, "<../map/$_" or die;
 638                                chomp($f = <F>);
 639                                print H "$_:$f\n" or die;
 640                        }
 641                        close(H) or die;' || die "Unable to save state")
 642        state_tree=$(printf '100644 blob %s\tfilter.map\n' "$state_blob" | git mktree)
 643        if test -n "$state_commit"
 644        then
 645                state_commit=$(echo "Sync" | git commit-tree "$state_tree" -p "$state_commit")
 646        else
 647                state_commit=$(echo "Sync" | git commit-tree "$state_tree" )
 648        fi
 649        git update-ref "$state_branch" "$state_commit"
 650fi
 651
 652cd "$orig_dir"
 653rm -rf "$tempdir"
 654
 655trap - 0
 656
 657if [ "$(is_bare_repository)" = false ]; then
 658        git read-tree -u -m HEAD || exit
 659fi
 660
 661exit 0