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