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