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