git-filter-branch.shon commit Fix parsing of @{-1}@{1} (aa9c55b)
   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
  67# When piped a commit, output a script to set the ident of either
  68# "author" or "committer
  69
  70set_ident () {
  71        lid="$(echo "$1" | tr "[A-Z]" "[a-z]")"
  72        uid="$(echo "$1" | tr "[a-z]" "[A-Z]")"
  73        pick_id_script='
  74                /^'$lid' /{
  75                        s/'\''/'\''\\'\'\''/g
  76                        h
  77                        s/^'$lid' \([^<]*\) <[^>]*> .*$/\1/
  78                        s/'\''/'\''\'\'\''/g
  79                        s/.*/GIT_'$uid'_NAME='\''&'\''; export GIT_'$uid'_NAME/p
  80
  81                        g
  82                        s/^'$lid' [^<]* <\([^>]*\)> .*$/\1/
  83                        s/'\''/'\''\'\'\''/g
  84                        s/.*/GIT_'$uid'_EMAIL='\''&'\''; export GIT_'$uid'_EMAIL/p
  85
  86                        g
  87                        s/^'$lid' [^<]* <[^>]*> \(.*\)$/\1/
  88                        s/'\''/'\''\'\'\''/g
  89                        s/.*/GIT_'$uid'_DATE='\''&'\''; export GIT_'$uid'_DATE/p
  90
  91                        q
  92                }
  93        '
  94
  95        LANG=C LC_ALL=C sed -ne "$pick_id_script"
  96        # Ensure non-empty id name.
  97        echo "case \"\$GIT_${uid}_NAME\" in \"\") GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\" && export GIT_${uid}_NAME;; esac"
  98}
  99
 100USAGE="[--env-filter <command>] [--tree-filter <command>] \
 101[--index-filter <command>] [--parent-filter <command>] \
 102[--msg-filter <command>] [--commit-filter <command>] \
 103[--tag-name-filter <command>] [--subdirectory-filter <directory>] \
 104[--original <namespace>] [-d <directory>] [-f | --force] \
 105[<rev-list options>...]"
 106
 107OPTIONS_SPEC=
 108. git-sh-setup
 109
 110if [ "$(is_bare_repository)" = false ]; then
 111        git diff-files --quiet &&
 112        git diff-index --cached --quiet HEAD -- ||
 113        die "Cannot rewrite branch(es) with a dirty working directory."
 114fi
 115
 116tempdir=.git-rewrite
 117filter_env=
 118filter_tree=
 119filter_index=
 120filter_parent=
 121filter_msg=cat
 122filter_commit=
 123filter_tag_name=
 124filter_subdir=
 125orig_namespace=refs/original/
 126force=
 127prune_empty=
 128while :
 129do
 130        case "$1" in
 131        --)
 132                shift
 133                break
 134                ;;
 135        --force|-f)
 136                shift
 137                force=t
 138                continue
 139                ;;
 140        --prune-empty)
 141                shift
 142                prune_empty=t
 143                continue
 144                ;;
 145        -*)
 146                ;;
 147        *)
 148                break;
 149        esac
 150
 151        # all switches take one argument
 152        ARG="$1"
 153        case "$#" in 1) usage ;; esac
 154        shift
 155        OPTARG="$1"
 156        shift
 157
 158        case "$ARG" in
 159        -d)
 160                tempdir="$OPTARG"
 161                ;;
 162        --env-filter)
 163                filter_env="$OPTARG"
 164                ;;
 165        --tree-filter)
 166                filter_tree="$OPTARG"
 167                ;;
 168        --index-filter)
 169                filter_index="$OPTARG"
 170                ;;
 171        --parent-filter)
 172                filter_parent="$OPTARG"
 173                ;;
 174        --msg-filter)
 175                filter_msg="$OPTARG"
 176                ;;
 177        --commit-filter)
 178                filter_commit="$functions; $OPTARG"
 179                ;;
 180        --tag-name-filter)
 181                filter_tag_name="$OPTARG"
 182                ;;
 183        --subdirectory-filter)
 184                filter_subdir="$OPTARG"
 185                ;;
 186        --original)
 187                orig_namespace=$(expr "$OPTARG/" : '\(.*[^/]\)/*$')/
 188                ;;
 189        *)
 190                usage
 191                ;;
 192        esac
 193done
 194
 195case "$prune_empty,$filter_commit" in
 196,)
 197        filter_commit='git commit-tree "$@"';;
 198t,)
 199        filter_commit="$functions;"' git_commit_non_empty_tree "$@"';;
 200,*)
 201        ;;
 202*)
 203        die "Cannot set --prune-empty and --filter-commit at the same time"
 204esac
 205
 206case "$force" in
 207t)
 208        rm -rf "$tempdir"
 209;;
 210'')
 211        test -d "$tempdir" &&
 212                die "$tempdir already exists, please remove it"
 213esac
 214mkdir -p "$tempdir/t" &&
 215tempdir="$(cd "$tempdir"; pwd)" &&
 216cd "$tempdir/t" &&
 217workdir="$(pwd)" ||
 218die ""
 219
 220# Remove tempdir on exit
 221trap 'cd ../..; rm -rf "$tempdir"' 0
 222
 223# Make sure refs/original is empty
 224git for-each-ref > "$tempdir"/backup-refs
 225while read sha1 type name
 226do
 227        case "$force,$name" in
 228        ,$orig_namespace*)
 229                die "Namespace $orig_namespace not empty"
 230        ;;
 231        t,$orig_namespace*)
 232                git update-ref -d "$name" $sha1
 233        ;;
 234        esac
 235done < "$tempdir"/backup-refs
 236
 237ORIG_GIT_DIR="$GIT_DIR"
 238ORIG_GIT_WORK_TREE="$GIT_WORK_TREE"
 239ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE"
 240GIT_WORK_TREE=.
 241export GIT_DIR GIT_WORK_TREE
 242
 243# The refs should be updated if their heads were rewritten
 244git rev-parse --no-flags --revs-only --symbolic-full-name --default HEAD "$@" |
 245sed -e '/^^/d' >"$tempdir"/heads
 246
 247test -s "$tempdir"/heads ||
 248        die "Which ref do you want to rewrite?"
 249
 250GIT_INDEX_FILE="$(pwd)/../index"
 251export GIT_INDEX_FILE
 252git read-tree || die "Could not seed the index"
 253
 254ret=0
 255
 256# map old->new commit ids for rewriting parents
 257mkdir ../map || die "Could not create map/ directory"
 258
 259case "$filter_subdir" in
 260"")
 261        git rev-list --reverse --topo-order --default HEAD \
 262                --parents --simplify-merges "$@"
 263        ;;
 264*)
 265        git rev-list --reverse --topo-order --default HEAD \
 266                --parents --simplify-merges "$@" -- "$filter_subdir"
 267esac > ../revs || die "Could not get the commits"
 268commits=$(wc -l <../revs | tr -d " ")
 269
 270test $commits -eq 0 && die "Found nothing to rewrite"
 271
 272# Rewrite the commits
 273
 274i=0
 275while read commit parents; do
 276        i=$(($i+1))
 277        printf "\rRewrite $commit ($i/$commits)"
 278
 279        case "$filter_subdir" in
 280        "")
 281                git read-tree -i -m $commit
 282                ;;
 283        *)
 284                # The commit may not have the subdirectory at all
 285                err=$(git read-tree -i -m $commit:"$filter_subdir" 2>&1) || {
 286                        if ! git rev-parse -q --verify $commit:"$filter_subdir"
 287                        then
 288                                rm -f "$GIT_INDEX_FILE"
 289                        else
 290                                echo >&2 "$err"
 291                                false
 292                        fi
 293                }
 294        esac || die "Could not initialize the index"
 295
 296        GIT_COMMIT=$commit
 297        export GIT_COMMIT
 298        git cat-file commit "$commit" >../commit ||
 299                die "Cannot read commit $commit"
 300
 301        eval "$(set_ident AUTHOR <../commit)" ||
 302                die "setting author failed for commit $commit"
 303        eval "$(set_ident COMMITTER <../commit)" ||
 304                die "setting committer failed for commit $commit"
 305        eval "$filter_env" < /dev/null ||
 306                die "env filter failed: $filter_env"
 307
 308        if [ "$filter_tree" ]; then
 309                git checkout-index -f -u -a ||
 310                        die "Could not checkout the index"
 311                # files that $commit removed are now still in the working tree;
 312                # remove them, else they would be added again
 313                git clean -d -q -f -x
 314                eval "$filter_tree" < /dev/null ||
 315                        die "tree filter failed: $filter_tree"
 316
 317                (
 318                        git diff-index -r --name-only $commit
 319                        git ls-files --others
 320                ) |
 321                git update-index --add --replace --remove --stdin
 322        fi
 323
 324        eval "$filter_index" < /dev/null ||
 325                die "index filter failed: $filter_index"
 326
 327        parentstr=
 328        for parent in $parents; do
 329                for reparent in $(map "$parent"); do
 330                        parentstr="$parentstr -p $reparent"
 331                done
 332        done
 333        if [ "$filter_parent" ]; then
 334                parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
 335                                die "parent filter failed: $filter_parent"
 336        fi
 337
 338        sed -e '1,/^$/d' <../commit | \
 339                eval "$filter_msg" > ../message ||
 340                        die "msg filter failed: $filter_msg"
 341        @SHELL_PATH@ -c "$filter_commit" "git commit-tree" \
 342                $(git write-tree) $parentstr < ../message > ../map/$commit
 343done <../revs
 344
 345# In case of a subdirectory filter, it is possible that a specified head
 346# is not in the set of rewritten commits, because it was pruned by the
 347# revision walker.  Fix it by mapping these heads to the unique nearest
 348# ancestor that survived the pruning.
 349
 350if test "$filter_subdir"
 351then
 352        while read ref
 353        do
 354                sha1=$(git rev-parse "$ref"^0)
 355                test -f "$workdir"/../map/$sha1 && continue
 356                ancestor=$(git rev-list --simplify-merges -1 \
 357                                $ref -- "$filter_subdir")
 358                test "$ancestor" && echo $(map $ancestor) >> "$workdir"/../map/$sha1
 359        done < "$tempdir"/heads
 360fi
 361
 362# Finally update the refs
 363
 364_x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
 365_x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
 366echo
 367while read ref
 368do
 369        # avoid rewriting a ref twice
 370        test -f "$orig_namespace$ref" && continue
 371
 372        sha1=$(git rev-parse "$ref"^0)
 373        rewritten=$(map $sha1)
 374
 375        test $sha1 = "$rewritten" &&
 376                warn "WARNING: Ref '$ref' is unchanged" &&
 377                continue
 378
 379        case "$rewritten" in
 380        '')
 381                echo "Ref '$ref' was deleted"
 382                git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
 383                        die "Could not delete $ref"
 384        ;;
 385        $_x40)
 386                echo "Ref '$ref' was rewritten"
 387                if ! git update-ref -m "filter-branch: rewrite" \
 388                                        "$ref" $rewritten $sha1 2>/dev/null; then
 389                        if test $(git cat-file -t "$ref") = tag; then
 390                                if test -z "$filter_tag_name"; then
 391                                        warn "WARNING: You said to rewrite tagged commits, but not the corresponding tag."
 392                                        warn "WARNING: Perhaps use '--tag-name-filter cat' to rewrite the tag."
 393                                fi
 394                        else
 395                                die "Could not rewrite $ref"
 396                        fi
 397                fi
 398        ;;
 399        *)
 400                # NEEDSWORK: possibly add -Werror, making this an error
 401                warn "WARNING: '$ref' was rewritten into multiple commits:"
 402                warn "$rewritten"
 403                warn "WARNING: Ref '$ref' points to the first one now."
 404                rewritten=$(echo "$rewritten" | head -n 1)
 405                git update-ref -m "filter-branch: rewrite to first" \
 406                                "$ref" $rewritten $sha1 ||
 407                        die "Could not rewrite $ref"
 408        ;;
 409        esac
 410        git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1
 411done < "$tempdir"/heads
 412
 413# TODO: This should possibly go, with the semantics that all positive given
 414#       refs are updated, and their original heads stored in refs/original/
 415# Filter tags
 416
 417if [ "$filter_tag_name" ]; then
 418        git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags |
 419        while read sha1 type ref; do
 420                ref="${ref#refs/tags/}"
 421                # XXX: Rewrite tagged trees as well?
 422                if [ "$type" != "commit" -a "$type" != "tag" ]; then
 423                        continue;
 424                fi
 425
 426                if [ "$type" = "tag" ]; then
 427                        # Dereference to a commit
 428                        sha1t="$sha1"
 429                        sha1="$(git rev-parse "$sha1"^{commit} 2>/dev/null)" || continue
 430                fi
 431
 432                [ -f "../map/$sha1" ] || continue
 433                new_sha1="$(cat "../map/$sha1")"
 434                GIT_COMMIT="$sha1"
 435                export GIT_COMMIT
 436                new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
 437                        die "tag name filter failed: $filter_tag_name"
 438
 439                echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
 440
 441                if [ "$type" = "tag" ]; then
 442                        new_sha1=$( ( printf 'object %s\ntype commit\ntag %s\n' \
 443                                                "$new_sha1" "$new_ref"
 444                                git cat-file tag "$ref" |
 445                                sed -n \
 446                                    -e "1,/^$/{
 447                                          /^object /d
 448                                          /^type /d
 449                                          /^tag /d
 450                                        }" \
 451                                    -e '/^-----BEGIN PGP SIGNATURE-----/q' \
 452                                    -e 'p' ) |
 453                                git mktag) ||
 454                                die "Could not create new tag object for $ref"
 455                        if git cat-file tag "$ref" | \
 456                           grep '^-----BEGIN PGP SIGNATURE-----' >/dev/null 2>&1
 457                        then
 458                                warn "gpg signature stripped from tag object $sha1t"
 459                        fi
 460                fi
 461
 462                git update-ref "refs/tags/$new_ref" "$new_sha1" ||
 463                        die "Could not write tag $new_ref"
 464        done
 465fi
 466
 467cd ../..
 468rm -rf "$tempdir"
 469
 470trap - 0
 471
 472if [ "$(is_bare_repository)" = false ]; then
 473        unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
 474        test -z "$ORIG_GIT_DIR" || {
 475                GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR
 476        }
 477        test -z "$ORIG_GIT_WORK_TREE" || {
 478                GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" &&
 479                export GIT_WORK_TREE
 480        }
 481        test -z "$ORIG_GIT_INDEX_FILE" || {
 482                GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" &&
 483                export GIT_INDEX_FILE
 484        }
 485        git read-tree -u -m HEAD
 486fi
 487
 488exit $ret