git-filter-branch.shon commit Merge branch 'maint' of git://linux-nfs.org/~bfields/git into maint (dc3e3ea)
   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
  11warn () {
  12        echo "$*" >&2
  13}
  14
  15map()
  16{
  17        # if it was not rewritten, take the original
  18        if test -r "$workdir/../map/$1"
  19        then
  20                cat "$workdir/../map/$1"
  21        else
  22                echo "$1"
  23        fi
  24}
  25
  26# if you run 'skip_commit "$@"' in a commit filter, it will print
  27# the (mapped) parents, effectively skipping the commit.
  28
  29skip_commit()
  30{
  31        shift;
  32        while [ -n "$1" ];
  33        do
  34                shift;
  35                map "$1";
  36                shift;
  37        done;
  38}
  39
  40# override die(): this version puts in an extra line break, so that
  41# the progress is still visible
  42
  43die()
  44{
  45        echo >&2
  46        echo "$*" >&2
  47        exit 1
  48}
  49
  50# When piped a commit, output a script to set the ident of either
  51# "author" or "committer
  52
  53set_ident () {
  54        lid="$(echo "$1" | tr "A-Z" "a-z")"
  55        uid="$(echo "$1" | tr "a-z" "A-Z")"
  56        pick_id_script='
  57                /^'$lid' /{
  58                        s/'\''/'\''\\'\'\''/g
  59                        h
  60                        s/^'$lid' \([^<]*\) <[^>]*> .*$/\1/
  61                        s/'\''/'\''\'\'\''/g
  62                        s/.*/export GIT_'$uid'_NAME='\''&'\''/p
  63
  64                        g
  65                        s/^'$lid' [^<]* <\([^>]*\)> .*$/\1/
  66                        s/'\''/'\''\'\'\''/g
  67                        s/.*/export GIT_'$uid'_EMAIL='\''&'\''/p
  68
  69                        g
  70                        s/^'$lid' [^<]* <[^>]*> \(.*\)$/\1/
  71                        s/'\''/'\''\'\'\''/g
  72                        s/.*/export GIT_'$uid'_DATE='\''&'\''/p
  73
  74                        q
  75                }
  76        '
  77
  78        LANG=C LC_ALL=C sed -ne "$pick_id_script"
  79        # Ensure non-empty id name.
  80        echo "[ -n \"\$GIT_${uid}_NAME\" ] || export GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\""
  81}
  82
  83# This script can be sourced by the commit filter to get the functions
  84test "a$SOURCE_FUNCTIONS" = a1 && return
  85this_script="$(cd "$(dirname "$0")"; pwd)"/$(basename "$0")
  86export this_script
  87
  88USAGE="[--env-filter <command>] [--tree-filter <command>] \
  89[--index-filter <command>] [--parent-filter <command>] \
  90[--msg-filter <command>] [--commit-filter <command>] \
  91[--tag-name-filter <command>] [--subdirectory-filter <directory>] \
  92[--original <namespace>] [-d <directory>] [-f | --force] \
  93[<rev-list options>...]"
  94
  95. git-sh-setup
  96
  97git diff-files --quiet &&
  98        git diff-index --cached --quiet HEAD ||
  99        die "Cannot rewrite branch(es) with a dirty working directory."
 100
 101tempdir=.git-rewrite
 102filter_env=
 103filter_tree=
 104filter_index=
 105filter_parent=
 106filter_msg=cat
 107filter_commit='git commit-tree "$@"'
 108filter_tag_name=
 109filter_subdir=
 110orig_namespace=refs/original/
 111force=
 112while :
 113do
 114        test $# = 0 && usage
 115        case "$1" in
 116        --)
 117                shift
 118                break
 119                ;;
 120        --force|-f)
 121                shift
 122                force=t
 123                continue
 124                ;;
 125        -*)
 126                ;;
 127        *)
 128                break;
 129        esac
 130
 131        # all switches take one argument
 132        ARG="$1"
 133        case "$#" in 1) usage ;; esac
 134        shift
 135        OPTARG="$1"
 136        shift
 137
 138        case "$ARG" in
 139        -d)
 140                tempdir="$OPTARG"
 141                ;;
 142        --env-filter)
 143                filter_env="$OPTARG"
 144                ;;
 145        --tree-filter)
 146                filter_tree="$OPTARG"
 147                ;;
 148        --index-filter)
 149                filter_index="$OPTARG"
 150                ;;
 151        --parent-filter)
 152                filter_parent="$OPTARG"
 153                ;;
 154        --msg-filter)
 155                filter_msg="$OPTARG"
 156                ;;
 157        --commit-filter)
 158                filter_commit='SOURCE_FUNCTIONS=1 . "$this_script";'" $OPTARG"
 159                ;;
 160        --tag-name-filter)
 161                filter_tag_name="$OPTARG"
 162                ;;
 163        --subdirectory-filter)
 164                filter_subdir="$OPTARG"
 165                ;;
 166        --original)
 167                orig_namespace=$(expr "$OPTARG/" : '\(.*[^/]\)/*$')/
 168                ;;
 169        *)
 170                usage
 171                ;;
 172        esac
 173done
 174
 175case "$force" in
 176t)
 177        rm -rf "$tempdir"
 178;;
 179'')
 180        test -d "$tempdir" &&
 181                die "$tempdir already exists, please remove it"
 182esac
 183mkdir -p "$tempdir/t" &&
 184tempdir="$(cd "$tempdir"; pwd)" &&
 185cd "$tempdir/t" &&
 186workdir="$(pwd)" ||
 187die ""
 188
 189# Make sure refs/original is empty
 190git for-each-ref > "$tempdir"/backup-refs
 191while read sha1 type name
 192do
 193        case "$force,$name" in
 194        ,$orig_namespace*)
 195                die "Namespace $orig_namespace not empty"
 196        ;;
 197        t,$orig_namespace*)
 198                git update-ref -d "$name" $sha1
 199        ;;
 200        esac
 201done < "$tempdir"/backup-refs
 202
 203ORIG_GIT_DIR="$GIT_DIR"
 204ORIG_GIT_WORK_TREE="$GIT_WORK_TREE"
 205ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE"
 206export GIT_DIR GIT_WORK_TREE=.
 207
 208# These refs should be updated if their heads were rewritten
 209
 210git rev-parse --revs-only --symbolic "$@" |
 211while read ref
 212do
 213        # normalize ref
 214        case "$ref" in
 215        HEAD)
 216                ref="$(git symbolic-ref "$ref")"
 217        ;;
 218        refs/*)
 219        ;;
 220        *)
 221                ref="$(git for-each-ref --format='%(refname)' |
 222                        grep /"$ref")"
 223        esac
 224
 225        git check-ref-format "$ref" && echo "$ref"
 226done > "$tempdir"/heads
 227
 228test -s "$tempdir"/heads ||
 229        die "Which ref do you want to rewrite?"
 230
 231export GIT_INDEX_FILE="$(pwd)/../index"
 232git read-tree || die "Could not seed the index"
 233
 234ret=0
 235
 236# map old->new commit ids for rewriting parents
 237mkdir ../map || die "Could not create map/ directory"
 238
 239case "$filter_subdir" in
 240"")
 241        git rev-list --reverse --topo-order --default HEAD \
 242                --parents "$@"
 243        ;;
 244*)
 245        git rev-list --reverse --topo-order --default HEAD \
 246                --parents --full-history "$@" -- "$filter_subdir"
 247esac > ../revs || die "Could not get the commits"
 248commits=$(wc -l <../revs | tr -d " ")
 249
 250test $commits -eq 0 && die "Found nothing to rewrite"
 251
 252# Rewrite the commits
 253
 254i=0
 255while read commit parents; do
 256        i=$(($i+1))
 257        printf "\rRewrite $commit ($i/$commits)"
 258
 259        case "$filter_subdir" in
 260        "")
 261                git read-tree -i -m $commit
 262                ;;
 263        *)
 264                git read-tree -i -m $commit:"$filter_subdir"
 265        esac || die "Could not initialize the index"
 266
 267        export GIT_COMMIT=$commit
 268        git cat-file commit "$commit" >../commit ||
 269                die "Cannot read commit $commit"
 270
 271        eval "$(set_ident AUTHOR <../commit)" ||
 272                die "setting author failed for commit $commit"
 273        eval "$(set_ident COMMITTER <../commit)" ||
 274                die "setting committer failed for commit $commit"
 275        eval "$filter_env" < /dev/null ||
 276                die "env filter failed: $filter_env"
 277
 278        if [ "$filter_tree" ]; then
 279                git checkout-index -f -u -a ||
 280                        die "Could not checkout the index"
 281                # files that $commit removed are now still in the working tree;
 282                # remove them, else they would be added again
 283                git ls-files -z --others | xargs -0 rm -f
 284                eval "$filter_tree" < /dev/null ||
 285                        die "tree filter failed: $filter_tree"
 286
 287                git diff-index -r $commit | cut -f 2- | tr '\n' '\0' | \
 288                        xargs -0 git update-index --add --replace --remove
 289                git ls-files -z --others | \
 290                        xargs -0 git update-index --add --replace --remove
 291        fi
 292
 293        eval "$filter_index" < /dev/null ||
 294                die "index filter failed: $filter_index"
 295
 296        parentstr=
 297        for parent in $parents; do
 298                for reparent in $(map "$parent"); do
 299                        parentstr="$parentstr -p $reparent"
 300                done
 301        done
 302        if [ "$filter_parent" ]; then
 303                parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
 304                                die "parent filter failed: $filter_parent"
 305        fi
 306
 307        sed -e '1,/^$/d' <../commit | \
 308                eval "$filter_msg" > ../message ||
 309                        die "msg filter failed: $filter_msg"
 310        sh -c "$filter_commit" "git commit-tree" \
 311                $(git write-tree) $parentstr < ../message > ../map/$commit
 312done <../revs
 313
 314# In case of a subdirectory filter, it is possible that a specified head
 315# is not in the set of rewritten commits, because it was pruned by the
 316# revision walker.  Fix it by mapping these heads to the next rewritten
 317# ancestor(s), i.e. the boundaries in the set of rewritten commits.
 318
 319# NEEDSWORK: we should sort the unmapped refs topologically first
 320while read ref
 321do
 322        sha1=$(git rev-parse "$ref"^0)
 323        test -f "$workdir"/../map/$sha1 && continue
 324        # Assign the boundarie(s) in the set of rewritten commits
 325        # as the replacement commit(s).
 326        # (This would look a bit nicer if --not --stdin worked.)
 327        for p in $( (cd "$workdir"/../map; ls | sed "s/^/^/") |
 328                git rev-list $ref --boundary --stdin |
 329                sed -n "s/^-//p")
 330        do
 331                map $p >> "$workdir"/../map/$sha1
 332        done
 333done < "$tempdir"/heads
 334
 335# Finally update the refs
 336
 337_x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
 338_x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
 339count=0
 340echo
 341while read ref
 342do
 343        # avoid rewriting a ref twice
 344        test -f "$orig_namespace$ref" && continue
 345
 346        sha1=$(git rev-parse "$ref"^0)
 347        rewritten=$(map $sha1)
 348
 349        test $sha1 = "$rewritten" &&
 350                warn "WARNING: Ref '$ref' is unchanged" &&
 351                continue
 352
 353        case "$rewritten" in
 354        '')
 355                echo "Ref '$ref' was deleted"
 356                git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
 357                        die "Could not delete $ref"
 358        ;;
 359        $_x40)
 360                echo "Ref '$ref' was rewritten"
 361                git update-ref -m "filter-branch: rewrite" \
 362                                "$ref" $rewritten $sha1 ||
 363                        die "Could not rewrite $ref"
 364        ;;
 365        *)
 366                # NEEDSWORK: possibly add -Werror, making this an error
 367                warn "WARNING: '$ref' was rewritten into multiple commits:"
 368                warn "$rewritten"
 369                warn "WARNING: Ref '$ref' points to the first one now."
 370                rewritten=$(echo "$rewritten" | head -n 1)
 371                git update-ref -m "filter-branch: rewrite to first" \
 372                                "$ref" $rewritten $sha1 ||
 373                        die "Could not rewrite $ref"
 374        ;;
 375        esac
 376        git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1
 377        count=$(($count+1))
 378done < "$tempdir"/heads
 379
 380# TODO: This should possibly go, with the semantics that all positive given
 381#       refs are updated, and their original heads stored in refs/original/
 382# Filter tags
 383
 384if [ "$filter_tag_name" ]; then
 385        git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags |
 386        while read sha1 type ref; do
 387                ref="${ref#refs/tags/}"
 388                # XXX: Rewrite tagged trees as well?
 389                if [ "$type" != "commit" -a "$type" != "tag" ]; then
 390                        continue;
 391                fi
 392
 393                if [ "$type" = "tag" ]; then
 394                        # Dereference to a commit
 395                        sha1t="$sha1"
 396                        sha1="$(git rev-parse "$sha1"^{commit} 2>/dev/null)" || continue
 397                fi
 398
 399                [ -f "../map/$sha1" ] || continue
 400                new_sha1="$(cat "../map/$sha1")"
 401                export GIT_COMMIT="$sha1"
 402                new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
 403                        die "tag name filter failed: $filter_tag_name"
 404
 405                echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
 406
 407                if [ "$type" = "tag" ]; then
 408                        # Warn that we are not rewriting the tag object itself.
 409                        warn "unreferencing tag object $sha1t"
 410                fi
 411
 412                git update-ref "refs/tags/$new_ref" "$new_sha1" ||
 413                        die "Could not write tag $new_ref"
 414        done
 415fi
 416
 417cd ../..
 418rm -rf "$tempdir"
 419echo
 420test $count -gt 0 && echo "These refs were rewritten:"
 421git show-ref | grep ^"$orig_namespace"
 422
 423unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
 424test -z "$ORIG_GIT_DIR" || GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR
 425test -z "$ORIG_GIT_WORK_TREE" || GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" &&
 426        export GIT_WORK_TREE
 427test -z "$ORIG_GIT_INDEX_FILE" || GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" &&
 428        export GIT_INDEX_FILE
 429git read-tree -u -m HEAD
 430
 431exit $ret