3 # Rewrite revision history
4 # Copyright (c) Petr Baudis, 2006
5 # Minimal changes to "port" it to core-git (c) Johannes Schindelin, 2007
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,
11 # The following functions will also be available in the commit filter:
13 export PATH=/usr/lib/git-core:$PATH
15 functions=$(cat << \EOF
22 # if it was not rewritten, take the original
23 if test -r "$workdir/../map/$1"
25 cat "$workdir/../map/$1"
31 # if you run 'skip_commit "$@"' in a commit filter, it will print
32 # the (mapped) parents, effectively skipping the commit.
45 # if you run 'git_commit_non_empty_tree "$@"' in a commit filter,
46 # it will skip commits that leave the tree untouched, commit the other.
47 git_commit_non_empty_tree()
49 if test $# = 3 && test "$1" = $(git rev-parse "$3^{tree}"); then
55 # override die(): this version puts in an extra line break, so that
56 # the progress is still visible
69 # When piped a commit, output a script to set the ident of either
70 # "author" or "committer
73 lid="$(echo "$1" | tr "[A-Z]" "[a-z]")"
74 uid="$(echo "$1" | tr "[a-z]" "[A-Z]")"
79 s/^'$lid' \([^<]*\) <[^>]*> .*$/\1/
81 s/.*/GIT_'$uid'_NAME='\''&'\''; export GIT_'$uid'_NAME/p
84 s/^'$lid' [^<]* <\([^>]*\)> .*$/\1/
86 s/.*/GIT_'$uid'_EMAIL='\''&'\''; export GIT_'$uid'_EMAIL/p
89 s/^'$lid' [^<]* <[^>]*> \(.*\)$/\1/
91 s/.*/GIT_'$uid'_DATE='\''&'\''; export GIT_'$uid'_DATE/p
97 LANG=C LC_ALL=C sed -ne "$pick_id_script"
98 # Ensure non-empty id name.
99 echo "case \"\$GIT_${uid}_NAME\" in \"\") GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\" && export GIT_${uid}_NAME;; esac"
102 USAGE="[--env-filter <command>] [--tree-filter <command>]
103 [--index-filter <command>] [--parent-filter <command>]
104 [--msg-filter <command>] [--commit-filter <command>]
105 [--tag-name-filter <command>] [--subdirectory-filter <directory>]
106 [--original <namespace>] [-d <directory>] [-f | --force]
107 [--state-branch <branch>]
108 [<rev-list options>...]"
113 if [ "$(is_bare_repository)" = false ]; then
114 require_clean_work_tree 'rewrite branches'
127 orig_namespace=refs/original/
144 # deprecated ($remap_to_ancestor is set now automatically)
160 # all switches take one argument
162 case "$#" in 1) usage ;; esac
175 filter_tree="$OPTARG"
178 filter_index="$OPTARG"
181 filter_parent="$OPTARG"
187 filter_commit="$functions; $OPTARG"
190 filter_tag_name="$OPTARG"
192 --subdirectory-filter)
193 filter_subdir="$OPTARG"
197 orig_namespace=$(expr "$OPTARG/" : '\(.*[^/]\)/*$')/
200 state_branch="$OPTARG"
208 case "$prune_empty,$filter_commit" in
210 filter_commit='git commit-tree "$@"';;
212 filter_commit="$functions;"' git_commit_non_empty_tree "$@"';;
216 die "Cannot set --prune-empty and --commit-filter at the same time"
224 test -d "$tempdir" &&
225 die "$tempdir already exists, please remove it"
227 mkdir -p "$tempdir/t" &&
228 tempdir="$(cd "$tempdir"; pwd)" &&
233 # Remove tempdir on exit
234 trap 'cd ../..; rm -rf "$tempdir"' 0
236 ORIG_GIT_DIR="$GIT_DIR"
237 ORIG_GIT_WORK_TREE="$GIT_WORK_TREE"
238 ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE"
240 export GIT_DIR GIT_WORK_TREE
242 # Make sure refs/original is empty
243 git for-each-ref > "$tempdir"/backup-refs || exit
244 while read sha1 type name
246 case "$force,$name" in
248 die "Cannot create a new backup.
249 A previous backup already exists in $orig_namespace
250 Force overwriting the backup with -f"
253 git update-ref -d "$name" $sha1
256 done < "$tempdir"/backup-refs
258 # The refs should be updated if their heads were rewritten
259 git rev-parse --no-flags --revs-only --symbolic-full-name \
260 --default HEAD "$@" > "$tempdir"/raw-heads || exit
261 sed -e '/^^/d' "$tempdir"/raw-heads >"$tempdir"/heads
263 test -s "$tempdir"/heads ||
264 die "Which ref do you want to rewrite?"
266 GIT_INDEX_FILE="$(pwd)/../index"
267 export GIT_INDEX_FILE
269 # map old->new commit ids for rewriting parents
270 mkdir ../map || die "Could not create map/ directory"
272 if [ -n "$state_branch" ] ; then
273 state_commit=`git show-ref -s "$state_branch"`
274 if [ -n "$state_commit" ] ; then
275 echo "Populating map from $state_branch ($state_commit)" 1>&2
276 git show "$state_commit":filter.map |
277 perl -n -e 'm/(.*):(.*)/ or die;
278 open F, ">../map/$1" or die;
282 echo "Branch $state_branch does not exist. Will create" 1>&2
286 # we need "--" only if there are no path arguments in $@
287 nonrevs=$(git rev-parse --no-revs "$@") || exit
288 if test -z "$nonrevs"
296 rev_args=$(git rev-parse --revs-only "$@")
298 case "$filter_subdir" in
300 eval set -- "$(git rev-parse --sq --no-revs "$@")"
303 eval set -- "$(git rev-parse --sq --no-revs "$@" $dashdash \
308 git rev-list --reverse --topo-order --default HEAD \
309 --parents --simplify-merges $rev_args "$@" > ../revs ||
310 die "Could not get the commits"
311 commits=$(wc -l <../revs | tr -d " ")
313 test $commits -eq 0 && die "Found nothing to rewrite"
315 # Rewrite the commits
317 git_filter_branch__commit_count=0
318 while read commit parents; do
319 git_filter_branch__commit_count=$(($git_filter_branch__commit_count+1))
320 printf "\rRewrite $commit ($git_filter_branch__commit_count/$commits)"
322 case "$filter_subdir" in
324 git read-tree -i -m $commit
327 # The commit may not have the subdirectory at all
328 err=$(git read-tree -i -m $commit:"$filter_subdir" 2>&1) || {
329 if ! git rev-parse -q --verify $commit:"$filter_subdir"
331 rm -f "$GIT_INDEX_FILE"
337 esac || die "Could not initialize the index"
341 git cat-file commit "$commit" >../commit ||
342 die "Cannot read commit $commit"
344 eval "$(set_ident AUTHOR <../commit)" ||
345 die "setting author failed for commit $commit"
346 eval "$(set_ident COMMITTER <../commit)" ||
347 die "setting committer failed for commit $commit"
348 eval "$filter_env" < /dev/null ||
349 die "env filter failed: $filter_env"
351 if [ "$filter_tree" ]; then
352 git checkout-index -f -u -a ||
353 die "Could not checkout the index"
354 # files that $commit removed are now still in the working tree;
355 # remove them, else they would be added again
356 git clean -d -q -f -x
357 eval "$filter_tree" < /dev/null ||
358 die "tree filter failed: $filter_tree"
361 git diff-index -r --name-only --ignore-submodules $commit &&
362 git ls-files --others
363 ) > "$tempdir"/tree-state || exit
364 git update-index --add --replace --remove --stdin \
365 < "$tempdir"/tree-state || exit
368 eval "$filter_index" < /dev/null ||
369 die "index filter failed: $filter_index"
372 for parent in $parents; do
373 for reparent in $(map "$parent"); do
374 parentstr="$parentstr -p $reparent"
377 if [ "$filter_parent" ]; then
378 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
379 die "parent filter failed: $filter_parent"
382 sed -e '1,/^$/d' <../commit | \
383 eval "$filter_msg" > ../message ||
384 die "msg filter failed: $filter_msg"
385 workdir=$workdir /bin/sh -c "$filter_commit" "git commit-tree" \
386 $(git write-tree) $parentstr < ../message > ../map/$commit ||
387 die "could not write rewritten commit"
390 # If we are filtering for paths, as in the case of a subdirectory
391 # filter, it is possible that a specified head is not in the set of
392 # rewritten commits, because it was pruned by the revision walker.
393 # Ancestor remapping fixes this by mapping these heads to the unique
394 # nearest ancestor that survived the pruning.
396 if test "$remap_to_ancestor" = t
400 sha1=$(git rev-parse "$ref"^0)
401 test -f "$workdir"/../map/$sha1 && continue
402 ancestor=$(git rev-list --simplify-merges -1 "$ref" "$@")
403 test "$ancestor" && echo $(map $ancestor) >> "$workdir"/../map/$sha1
404 done < "$tempdir"/heads
407 # Finally update the refs
409 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
410 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
414 # avoid rewriting a ref twice
415 test -f "$orig_namespace$ref" && continue
417 sha1=$(git rev-parse "$ref"^0)
418 rewritten=$(map $sha1)
420 test $sha1 = "$rewritten" &&
421 warn "WARNING: Ref '$ref' is unchanged" &&
426 echo "Ref '$ref' was deleted"
427 git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
428 die "Could not delete $ref"
431 echo "Ref '$ref' was rewritten"
432 if ! git update-ref -m "filter-branch: rewrite" \
433 "$ref" $rewritten $sha1 2>/dev/null; then
434 if test $(git cat-file -t "$ref") = tag; then
435 if test -z "$filter_tag_name"; then
436 warn "WARNING: You said to rewrite tagged commits, but not the corresponding tag."
437 warn "WARNING: Perhaps use '--tag-name-filter cat' to rewrite the tag."
440 die "Could not rewrite $ref"
445 # NEEDSWORK: possibly add -Werror, making this an error
446 warn "WARNING: '$ref' was rewritten into multiple commits:"
448 warn "WARNING: Ref '$ref' points to the first one now."
449 rewritten=$(echo "$rewritten" | head -n 1)
450 git update-ref -m "filter-branch: rewrite to first" \
451 "$ref" $rewritten $sha1 ||
452 die "Could not rewrite $ref"
455 git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1 ||
457 done < "$tempdir"/heads
459 # TODO: This should possibly go, with the semantics that all positive given
460 # refs are updated, and their original heads stored in refs/original/
463 if [ "$filter_tag_name" ]; then
464 git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags |
465 while read sha1 type ref; do
466 ref="${ref#refs/tags/}"
467 # XXX: Rewrite tagged trees as well?
468 if [ "$type" != "commit" -a "$type" != "tag" ]; then
472 if [ "$type" = "tag" ]; then
473 # Dereference to a commit
475 sha1="$(git rev-parse -q "$sha1"^{commit})" || continue
478 [ -f "../map/$sha1" ] || continue
479 new_sha1="$(cat "../map/$sha1")"
482 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
483 die "tag name filter failed: $filter_tag_name"
485 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
487 if [ "$type" = "tag" ]; then
488 new_sha1=$( ( printf 'object %s\ntype commit\ntag %s\n' \
489 "$new_sha1" "$new_ref"
490 git cat-file tag "$ref" |
491 awk '/^tagger/ { tagged=1 }
492 /^$/ { if (!tagged && !done) { print "tagger Unknown <unknown@example.com> 0 +0000" } ; done=1 }
500 -e '/^-----BEGIN PGP SIGNATURE-----/q' \
503 die "Could not create new tag object for $ref"
504 if git cat-file tag "$ref" | \
505 sane_grep '^-----BEGIN PGP SIGNATURE-----' >/dev/null 2>&1
507 warn "gpg signature stripped from tag object $sha1t"
511 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
512 die "Could not write tag $new_ref"
516 if [ -n "$state_branch" ] ; then
517 echo "Saving rewrite state to $state_branch" 1>&2
518 STATE_BLOB=$(ls ../map |
520 open F, "<../map/$_" or die;
521 chomp($f = <F>); print "$_:$f\n";' |
522 git hash-object -w --stdin )
523 STATE_TREE=$(/bin/echo -e "100644 blob $STATE_BLOB\tfilter.map" | git mktree)
524 STATE_PARENT=$(git show-ref -s "$state_branch")
525 unset GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
526 unset GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL GIT_COMMITTER_DATE
527 if [ -n "$STATE_PARENT" ] ; then
528 STATE_COMMIT=$(/bin/echo "Sync" | git commit-tree "$STATE_TREE" -p "$STATE_PARENT")
530 STATE_COMMIT=$(/bin/echo "Sync" | git commit-tree "$STATE_TREE" )
532 git update-ref "$state_branch" "$STATE_COMMIT"
540 unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
541 test -z "$ORIG_GIT_DIR" || {
542 GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR
544 test -z "$ORIG_GIT_WORK_TREE" || {
545 GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" &&
548 test -z "$ORIG_GIT_INDEX_FILE" || {
549 GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" &&
550 export GIT_INDEX_FILE
553 if [ "$(is_bare_repository)" = false ]; then
554 git read-tree -u -m HEAD || exit