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 functions
=$
(cat << \EOF
14 EMPTY_TREE
=$
(git hash-object
-t tree
/dev
/null
)
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
51 elif test $# = 1 && test "$1" = $EMPTY_TREE; then
57 # override die(): this version puts in an extra line break, so that
58 # the progress is still visible
72 # Ensure non-empty id name.
73 echo "case \"\$GIT_$1_NAME\" in \"\") GIT_$1_NAME=\"\${GIT_$1_EMAIL%%@*}\" && export GIT_$1_NAME;; esac"
74 # And make sure everything is exported.
75 echo "export GIT_$1_NAME"
76 echo "export GIT_$1_EMAIL"
77 echo "export GIT_$1_DATE"
81 parse_ident_from_commit author AUTHOR committer COMMITTER
83 finish_ident COMMITTER
86 if test -z "$FILTER_BRANCH_SQUELCH_WARNING$GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS"
89 WARNING: git-filter-branch has a glut of gotchas generating mangled history
90 rewrites. Hit Ctrl-C before proceeding to abort, then use an
91 alternative filtering tool such as 'git filter-repo'
92 (https://github.com/newren/git-filter-repo/) instead. See the
93 filter-branch manual page for more details; to squelch this warning,
94 set FILTER_BRANCH_SQUELCH_WARNING=1.
97 printf "Proceeding with filter-branch...\n\n"
100 USAGE
="[--setup <command>] [--subdirectory-filter <directory>] [--env-filter <command>]
101 [--tree-filter <command>] [--index-filter <command>]
102 [--parent-filter <command>] [--msg-filter <command>]
103 [--commit-filter <command>] [--tag-name-filter <command>]
104 [--original <namespace>]
105 [-d <directory>] [-f | --force] [--state-branch <branch>]
106 [--] [<rev-list options>...]"
111 if [ "$(is_bare_repository)" = false
]; then
112 require_clean_work_tree
'rewrite branches'
126 orig_namespace
=refs
/original
/
143 # deprecated ($remap_to_ancestor is set now automatically)
159 # all switches take one argument
161 case "$#" in 1) usage
;; esac
171 filter_setup
="$OPTARG"
173 --subdirectory-filter)
174 filter_subdir
="$OPTARG"
181 filter_tree
="$OPTARG"
184 filter_index
="$OPTARG"
187 filter_parent
="$OPTARG"
193 filter_commit
="$functions; $OPTARG"
196 filter_tag_name
="$OPTARG"
199 orig_namespace
=$
(expr "$OPTARG/" : '\(.*[^/]\)/*$')/
202 state_branch
="$OPTARG"
210 case "$prune_empty,$filter_commit" in
212 filter_commit
='git commit-tree "$@"';;
214 filter_commit
="$functions;"' git_commit_non_empty_tree "$@"';;
218 die
"Cannot set --prune-empty and --commit-filter at the same time"
226 test -d "$tempdir" &&
227 die
"$tempdir already exists, please remove it"
230 mkdir
-p "$tempdir/t" &&
231 tempdir
="$(cd "$tempdir"; pwd)" &&
236 # Remove tempdir on exit
237 trap 'cd "$orig_dir"; rm -rf "$tempdir"' 0
239 ORIG_GIT_DIR
="$GIT_DIR"
240 ORIG_GIT_WORK_TREE
="$GIT_WORK_TREE"
241 ORIG_GIT_INDEX_FILE
="$GIT_INDEX_FILE"
242 ORIG_GIT_AUTHOR_NAME
="$GIT_AUTHOR_NAME"
243 ORIG_GIT_AUTHOR_EMAIL
="$GIT_AUTHOR_EMAIL"
244 ORIG_GIT_AUTHOR_DATE
="$GIT_AUTHOR_DATE"
245 ORIG_GIT_COMMITTER_NAME
="$GIT_COMMITTER_NAME"
246 ORIG_GIT_COMMITTER_EMAIL
="$GIT_COMMITTER_EMAIL"
247 ORIG_GIT_COMMITTER_DATE
="$GIT_COMMITTER_DATE"
250 export GIT_DIR GIT_WORK_TREE
252 # Make sure refs/original is empty
253 git for-each-ref
> "$tempdir"/backup-refs ||
exit
254 while read sha1
type name
256 case "$force,$name" in
258 die
"Cannot create a new backup.
259 A previous backup already exists in $orig_namespace
260 Force overwriting the backup with -f"
263 git update-ref
-d "$name" $sha1
266 done < "$tempdir"/backup-refs
268 # The refs should be updated if their heads were rewritten
269 git rev-parse
--no-flags --revs-only --symbolic-full-name \
270 --default HEAD
"$@" > "$tempdir"/raw-refs ||
exit
273 case "$ref" in ^?
*) continue ;; esac
275 if git rev-parse
--verify "$ref"^
0 >/dev
/null
2>&1
279 warn
"WARNING: not rewriting '$ref' (not a committish)"
281 done >"$tempdir"/heads
<"$tempdir"/raw-refs
283 test -s "$tempdir"/heads ||
284 die
"You must specify a ref to rewrite."
286 GIT_INDEX_FILE
="$(pwd)/../index"
287 export GIT_INDEX_FILE
289 # map old->new commit ids for rewriting parents
290 mkdir ..
/map || die
"Could not create map/ directory"
292 if test -n "$state_branch"
294 state_commit
=$
(git rev-parse
--no-flags --revs-only "$state_branch")
295 if test -n "$state_commit"
297 echo "Populating map from $state_branch ($state_commit)" 1>&2
298 perl
-e'open(MAP, "-|", "git show $ARGV[0]:filter.map") or die;
301 open F, ">../map/$1" or die;
305 close(MAP) or die;' "$state_commit" \
306 || die
"Unable to load state from $state_branch:filter.map"
308 echo "Branch $state_branch does not exist. Will create" 1>&2
312 # we need "--" only if there are no path arguments in $@
313 nonrevs
=$
(git rev-parse
--no-revs "$@") ||
exit
314 if test -z "$nonrevs"
322 git rev-parse
--revs-only "$@" >..
/parse
324 case "$filter_subdir" in
326 eval set -- "$(git rev-parse --sq --no-revs "$@
")"
329 eval set -- "$(git rev-parse --sq --no-revs "$@
" $dashdash \
334 git rev-list
--reverse --topo-order --default HEAD \
335 --parents --simplify-merges --stdin "$@" <..
/parse
>..
/revs ||
336 die
"Could not get the commits"
337 commits
=$
(wc -l <..
/revs |
tr -d " ")
339 test $commits -eq 0 && die_with_status
2 "Found nothing to rewrite"
341 # Rewrite the commits
344 if test -n "$progress" &&
345 test $git_filter_branch__commit_count -gt $next_sample_at
347 count
=$git_filter_branch__commit_count
350 elapsed
=$
(($now - $start_timestamp))
351 remaining
=$
(( ($commits - $count) * $elapsed / $count ))
352 if test $elapsed -gt 0
354 next_sample_at
=$
(( ($elapsed + 1) * $count / $elapsed ))
356 next_sample_at
=$
(($next_sample_at + 1))
358 progress
=" ($elapsed seconds passed, remaining $remaining predicted)"
360 printf "\rRewrite $commit ($count/$commits)$progress "
363 git_filter_branch__commit_count
=0
365 progress
= start_timestamp
=
366 if date '+%s' 2>/dev
/null |
grep -q '^[0-9][0-9]*$'
369 progress
="dummy to ensure this is not empty"
370 start_timestamp
=$
(date '+%s')
373 if test -n "$filter_index" ||
374 test -n "$filter_tree" ||
375 test -n "$filter_subdir"
382 eval "$filter_setup" < /dev
/null ||
383 die
"filter setup failed: $filter_setup"
385 while read commit parents
; do
386 git_filter_branch__commit_count
=$
(($git_filter_branch__commit_count+1))
389 test -f "$workdir"/..
/map
/$commit && continue
391 case "$filter_subdir" in
393 if test -n "$need_index"
395 GIT_ALLOW_NULL_SHA1
=1 git read-tree
-i -m $commit
399 # The commit may not have the subdirectory at all
400 err
=$
(GIT_ALLOW_NULL_SHA1
=1 \
401 git read-tree
-i -m $commit:"$filter_subdir" 2>&1) ||
{
402 if ! git rev-parse
-q --verify $commit:"$filter_subdir"
404 rm -f "$GIT_INDEX_FILE"
410 esac || die
"Could not initialize the index"
414 git cat-file commit
"$commit" >..
/commit ||
415 die
"Cannot read commit $commit"
417 eval "$(set_ident <../commit)" ||
418 die
"setting author/committer failed for commit $commit"
419 eval "$filter_env" < /dev
/null ||
420 die
"env filter failed: $filter_env"
422 if [ "$filter_tree" ]; then
423 git checkout-index
-f -u -a ||
424 die
"Could not checkout the index"
425 # files that $commit removed are now still in the working tree;
426 # remove them, else they would be added again
427 git clean
-d -q -f -x
428 eval "$filter_tree" < /dev
/null ||
429 die
"tree filter failed: $filter_tree"
432 git diff-index
-r --name-only --ignore-submodules $commit -- &&
433 git ls-files
--others
434 ) > "$tempdir"/tree-state ||
exit
435 git update-index
--add --replace --remove --stdin \
436 < "$tempdir"/tree-state ||
exit
439 eval "$filter_index" < /dev
/null ||
440 die
"index filter failed: $filter_index"
443 for parent
in $parents; do
444 for reparent
in $
(map
"$parent"); do
445 case "$parentstr " in
449 parentstr
="$parentstr -p $reparent"
454 if [ "$filter_parent" ]; then
455 parentstr
="$(echo "$parentstr" | eval "$filter_parent")" ||
456 die
"parent filter failed: $filter_parent"
460 while IFS
='' read -r header_line
&& test -n "$header_line"
462 # skip header lines...
465 # and output the actual commit message
468 eval "$filter_msg" > ..
/message ||
469 die
"msg filter failed: $filter_msg"
471 if test -n "$need_index"
473 tree
=$
(git write-tree
)
475 tree
=$
(git rev-parse
"$commit^{tree}")
477 workdir
=$workdir @SHELL_PATH@
-c "$filter_commit" "git commit-tree" \
478 "$tree" $parentstr < ..
/message
> ..
/map
/$commit ||
479 die
"could not write rewritten commit"
482 # If we are filtering for paths, as in the case of a subdirectory
483 # filter, it is possible that a specified head is not in the set of
484 # rewritten commits, because it was pruned by the revision walker.
485 # Ancestor remapping fixes this by mapping these heads to the unique
486 # nearest ancestor that survived the pruning.
488 if test "$remap_to_ancestor" = t
492 sha1
=$
(git rev-parse
"$ref"^
0)
493 test -f "$workdir"/..
/map
/$sha1 && continue
494 ancestor
=$
(git rev-list
--simplify-merges -1 "$ref" "$@")
495 test "$ancestor" && echo $
(map
$ancestor) >"$workdir"/..
/map
/$sha1
496 done < "$tempdir"/heads
499 # Finally update the refs
504 # avoid rewriting a ref twice
505 test -f "$orig_namespace$ref" && continue
507 sha1
=$
(git rev-parse
"$ref"^
0)
508 rewritten
=$
(map
$sha1)
510 test $sha1 = "$rewritten" &&
511 warn
"WARNING: Ref '$ref' is unchanged" &&
516 echo "Ref '$ref' was deleted"
517 git update-ref
-m "filter-branch: delete" -d "$ref" $sha1 ||
518 die
"Could not delete $ref"
521 echo "Ref '$ref' was rewritten"
522 if ! git update-ref
-m "filter-branch: rewrite" \
523 "$ref" $rewritten $sha1 2>/dev
/null
; then
524 if test $
(git cat-file
-t "$ref") = tag
; then
525 if test -z "$filter_tag_name"; then
526 warn
"WARNING: You said to rewrite tagged commits, but not the corresponding tag."
527 warn
"WARNING: Perhaps use '--tag-name-filter cat' to rewrite the tag."
530 die
"Could not rewrite $ref"
535 git update-ref
-m "filter-branch: backup" "$orig_namespace$ref" $sha1 ||
537 done < "$tempdir"/heads
539 # TODO: This should possibly go, with the semantics that all positive given
540 # refs are updated, and their original heads stored in refs/original/
543 if [ "$filter_tag_name" ]; then
544 git for-each-ref
--format='%(objectname) %(objecttype) %(refname)' refs
/tags |
545 while read sha1
type ref
; do
546 ref
="${ref#refs/tags/}"
547 # XXX: Rewrite tagged trees as well?
548 if [ "$type" != "commit" -a "$type" != "tag" ]; then
552 if [ "$type" = "tag" ]; then
553 # Dereference to a commit
555 sha1
="$(git rev-parse -q "$sha1"^{commit})" ||
continue
558 [ -f "../map/$sha1" ] ||
continue
559 new_sha1
="$(cat "..
/map
/$sha1")"
562 new_ref
="$(echo "$ref" | eval "$filter_tag_name")" ||
563 die
"tag name filter failed: $filter_tag_name"
565 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
567 if [ "$type" = "tag" ]; then
568 new_sha1
=$
( ( printf 'object %s\ntype commit\ntag %s\n' \
569 "$new_sha1" "$new_ref"
570 git cat-file tag
"$ref" |
577 -e '/^-----BEGIN PGP SIGNATURE-----/q' \
579 git hash-object
-t tag
-w --stdin) ||
580 die
"Could not create new tag object for $ref"
581 if git cat-file tag
"$ref" | \
582 sane_grep
'^-----BEGIN PGP SIGNATURE-----' >/dev
/null
2>&1
584 warn
"gpg signature stripped from tag object $sha1t"
588 git update-ref
"refs/tags/$new_ref" "$new_sha1" ||
589 die
"Could not write tag $new_ref"
593 unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
594 unset GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
595 unset GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL GIT_COMMITTER_DATE
596 test -z "$ORIG_GIT_DIR" ||
{
597 GIT_DIR
="$ORIG_GIT_DIR" && export GIT_DIR
599 test -z "$ORIG_GIT_WORK_TREE" ||
{
600 GIT_WORK_TREE
="$ORIG_GIT_WORK_TREE" &&
603 test -z "$ORIG_GIT_INDEX_FILE" ||
{
604 GIT_INDEX_FILE
="$ORIG_GIT_INDEX_FILE" &&
605 export GIT_INDEX_FILE
607 test -z "$ORIG_GIT_AUTHOR_NAME" ||
{
608 GIT_AUTHOR_NAME
="$ORIG_GIT_AUTHOR_NAME" &&
609 export GIT_AUTHOR_NAME
611 test -z "$ORIG_GIT_AUTHOR_EMAIL" ||
{
612 GIT_AUTHOR_EMAIL
="$ORIG_GIT_AUTHOR_EMAIL" &&
613 export GIT_AUTHOR_EMAIL
615 test -z "$ORIG_GIT_AUTHOR_DATE" ||
{
616 GIT_AUTHOR_DATE
="$ORIG_GIT_AUTHOR_DATE" &&
617 export GIT_AUTHOR_DATE
619 test -z "$ORIG_GIT_COMMITTER_NAME" ||
{
620 GIT_COMMITTER_NAME
="$ORIG_GIT_COMMITTER_NAME" &&
621 export GIT_COMMITTER_NAME
623 test -z "$ORIG_GIT_COMMITTER_EMAIL" ||
{
624 GIT_COMMITTER_EMAIL
="$ORIG_GIT_COMMITTER_EMAIL" &&
625 export GIT_COMMITTER_EMAIL
627 test -z "$ORIG_GIT_COMMITTER_DATE" ||
{
628 GIT_COMMITTER_DATE
="$ORIG_GIT_COMMITTER_DATE" &&
629 export GIT_COMMITTER_DATE
632 if test -n "$state_branch"
634 echo "Saving rewrite state to $state_branch" 1>&2
636 perl
-e'opendir D, "../map" or die;
637 open H, "|-", "git hash-object -w --stdin" or die;
638 foreach (sort readdir(D)) {
640 open F, "<../map/$_" or die;
642 print H "$_:$f\n" or die;
644 close(H) or die;' || die
"Unable to save state")
645 state_tree
=$
(printf '100644 blob %s\tfilter.map\n' "$state_blob" | git mktree
)
646 if test -n "$state_commit"
648 state_commit
=$
(echo "Sync" | git commit-tree
"$state_tree" -p "$state_commit")
650 state_commit
=$
(echo "Sync" | git commit-tree
"$state_tree" )
652 git update-ref
"$state_branch" "$state_commit"
660 if [ "$(is_bare_repository)" = false
]; then
661 git read-tree
-u -m HEAD ||
exit