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