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 USAGE
="[--setup <command>] [--subdirectory-filter <directory>] [--env-filter <command>]
87 [--tree-filter <command>] [--index-filter <command>]
88 [--parent-filter <command>] [--msg-filter <command>]
89 [--commit-filter <command>] [--tag-name-filter <command>]
90 [--original <namespace>]
91 [-d <directory>] [-f | --force] [--state-branch <branch>]
92 [--] [<rev-list options>...]"
97 if [ "$(is_bare_repository)" = false
]; then
98 require_clean_work_tree
'rewrite branches'
112 orig_namespace
=refs
/original
/
129 # deprecated ($remap_to_ancestor is set now automatically)
145 # all switches take one argument
147 case "$#" in 1) usage
;; esac
157 filter_setup
="$OPTARG"
159 --subdirectory-filter)
160 filter_subdir
="$OPTARG"
167 filter_tree
="$OPTARG"
170 filter_index
="$OPTARG"
173 filter_parent
="$OPTARG"
179 filter_commit
="$functions; $OPTARG"
182 filter_tag_name
="$OPTARG"
185 orig_namespace
=$
(expr "$OPTARG/" : '\(.*[^/]\)/*$')/
188 state_branch
="$OPTARG"
196 case "$prune_empty,$filter_commit" in
198 filter_commit
='git commit-tree "$@"';;
200 filter_commit
="$functions;"' git_commit_non_empty_tree "$@"';;
204 die
"Cannot set --prune-empty and --commit-filter at the same time"
212 test -d "$tempdir" &&
213 die
"$tempdir already exists, please remove it"
216 mkdir
-p "$tempdir/t" &&
217 tempdir
="$(cd "$tempdir"; pwd)" &&
222 # Remove tempdir on exit
223 trap 'cd "$orig_dir"; rm -rf "$tempdir"' 0
225 ORIG_GIT_DIR
="$GIT_DIR"
226 ORIG_GIT_WORK_TREE
="$GIT_WORK_TREE"
227 ORIG_GIT_INDEX_FILE
="$GIT_INDEX_FILE"
228 ORIG_GIT_AUTHOR_NAME
="$GIT_AUTHOR_NAME"
229 ORIG_GIT_AUTHOR_EMAIL
="$GIT_AUTHOR_EMAIL"
230 ORIG_GIT_AUTHOR_DATE
="$GIT_AUTHOR_DATE"
231 ORIG_GIT_COMMITTER_NAME
="$GIT_COMMITTER_NAME"
232 ORIG_GIT_COMMITTER_EMAIL
="$GIT_COMMITTER_EMAIL"
233 ORIG_GIT_COMMITTER_DATE
="$GIT_COMMITTER_DATE"
236 export GIT_DIR GIT_WORK_TREE
238 # Make sure refs/original is empty
239 git for-each-ref
> "$tempdir"/backup-refs ||
exit
240 while read sha1
type name
242 case "$force,$name" in
244 die
"Cannot create a new backup.
245 A previous backup already exists in $orig_namespace
246 Force overwriting the backup with -f"
249 git update-ref
-d "$name" $sha1
252 done < "$tempdir"/backup-refs
254 # The refs should be updated if their heads were rewritten
255 git rev-parse
--no-flags --revs-only --symbolic-full-name \
256 --default HEAD
"$@" > "$tempdir"/raw-refs ||
exit
259 case "$ref" in ^?
*) continue ;; esac
261 if git rev-parse
--verify "$ref"^
0 >/dev
/null
2>&1
265 warn
"WARNING: not rewriting '$ref' (not a committish)"
267 done >"$tempdir"/heads
<"$tempdir"/raw-refs
269 test -s "$tempdir"/heads ||
270 die
"You must specify a ref to rewrite."
272 GIT_INDEX_FILE
="$(pwd)/../index"
273 export GIT_INDEX_FILE
275 # map old->new commit ids for rewriting parents
276 mkdir ..
/map || die
"Could not create map/ directory"
278 if test -n "$state_branch"
280 state_commit
=$
(git rev-parse
--no-flags --revs-only "$state_branch")
281 if test -n "$state_commit"
283 echo "Populating map from $state_branch ($state_commit)" 1>&2
284 perl
-e'open(MAP, "-|", "git show $ARGV[0]:filter.map") or die;
287 open F, ">../map/$1" or die;
291 close(MAP) or die;' "$state_commit" \
292 || die
"Unable to load state from $state_branch:filter.map"
294 echo "Branch $state_branch does not exist. Will create" 1>&2
298 # we need "--" only if there are no path arguments in $@
299 nonrevs
=$
(git rev-parse
--no-revs "$@") ||
exit
300 if test -z "$nonrevs"
308 git rev-parse
--revs-only "$@" >..
/parse
310 case "$filter_subdir" in
312 eval set -- "$(git rev-parse --sq --no-revs "$@
")"
315 eval set -- "$(git rev-parse --sq --no-revs "$@
" $dashdash \
320 git rev-list
--reverse --topo-order --default HEAD \
321 --parents --simplify-merges --stdin "$@" <..
/parse
>..
/revs ||
322 die
"Could not get the commits"
323 commits
=$
(wc -l <..
/revs |
tr -d " ")
325 test $commits -eq 0 && die_with_status
2 "Found nothing to rewrite"
327 # Rewrite the commits
330 if test -n "$progress" &&
331 test $git_filter_branch__commit_count -gt $next_sample_at
333 count
=$git_filter_branch__commit_count
336 elapsed
=$
(($now - $start_timestamp))
337 remaining
=$
(( ($commits - $count) * $elapsed / $count ))
338 if test $elapsed -gt 0
340 next_sample_at
=$
(( ($elapsed + 1) * $count / $elapsed ))
342 next_sample_at
=$
(($next_sample_at + 1))
344 progress
=" ($elapsed seconds passed, remaining $remaining predicted)"
346 printf "\rRewrite $commit ($count/$commits)$progress "
349 git_filter_branch__commit_count
=0
351 progress
= start_timestamp
=
352 if date '+%s' 2>/dev
/null |
grep -q '^[0-9][0-9]*$'
355 progress
="dummy to ensure this is not empty"
356 start_timestamp
=$
(date '+%s')
359 if test -n "$filter_index" ||
360 test -n "$filter_tree" ||
361 test -n "$filter_subdir"
368 eval "$filter_setup" < /dev
/null ||
369 die
"filter setup failed: $filter_setup"
371 while read commit parents
; do
372 git_filter_branch__commit_count
=$
(($git_filter_branch__commit_count+1))
375 test -f "$workdir"/..
/map
/$commit && continue
377 case "$filter_subdir" in
379 if test -n "$need_index"
381 GIT_ALLOW_NULL_SHA1
=1 git read-tree
-i -m $commit
385 # The commit may not have the subdirectory at all
386 err
=$
(GIT_ALLOW_NULL_SHA1
=1 \
387 git read-tree
-i -m $commit:"$filter_subdir" 2>&1) ||
{
388 if ! git rev-parse
-q --verify $commit:"$filter_subdir"
390 rm -f "$GIT_INDEX_FILE"
396 esac || die
"Could not initialize the index"
400 git cat-file commit
"$commit" >..
/commit ||
401 die
"Cannot read commit $commit"
403 eval "$(set_ident <../commit)" ||
404 die
"setting author/committer failed for commit $commit"
405 eval "$filter_env" < /dev
/null ||
406 die
"env filter failed: $filter_env"
408 if [ "$filter_tree" ]; then
409 git checkout-index
-f -u -a ||
410 die
"Could not checkout the index"
411 # files that $commit removed are now still in the working tree;
412 # remove them, else they would be added again
413 git clean
-d -q -f -x
414 eval "$filter_tree" < /dev
/null ||
415 die
"tree filter failed: $filter_tree"
418 git diff-index
-r --name-only --ignore-submodules $commit -- &&
419 git ls-files
--others
420 ) > "$tempdir"/tree-state ||
exit
421 git update-index
--add --replace --remove --stdin \
422 < "$tempdir"/tree-state ||
exit
425 eval "$filter_index" < /dev
/null ||
426 die
"index filter failed: $filter_index"
429 for parent
in $parents; do
430 for reparent
in $
(map
"$parent"); do
431 case "$parentstr " in
435 parentstr
="$parentstr -p $reparent"
440 if [ "$filter_parent" ]; then
441 parentstr
="$(echo "$parentstr" | eval "$filter_parent")" ||
442 die
"parent filter failed: $filter_parent"
446 while IFS
='' read -r header_line
&& test -n "$header_line"
448 # skip header lines...
451 # and output the actual commit message
454 eval "$filter_msg" > ..
/message ||
455 die
"msg filter failed: $filter_msg"
457 if test -n "$need_index"
459 tree
=$
(git write-tree
)
461 tree
=$
(git rev-parse
"$commit^{tree}")
463 workdir
=$workdir @SHELL_PATH@
-c "$filter_commit" "git commit-tree" \
464 "$tree" $parentstr < ..
/message
> ..
/map
/$commit ||
465 die
"could not write rewritten commit"
468 # If we are filtering for paths, as in the case of a subdirectory
469 # filter, it is possible that a specified head is not in the set of
470 # rewritten commits, because it was pruned by the revision walker.
471 # Ancestor remapping fixes this by mapping these heads to the unique
472 # nearest ancestor that survived the pruning.
474 if test "$remap_to_ancestor" = t
478 sha1
=$
(git rev-parse
"$ref"^
0)
479 test -f "$workdir"/..
/map
/$sha1 && continue
480 ancestor
=$
(git rev-list
--simplify-merges -1 "$ref" "$@")
481 test "$ancestor" && echo $
(map
$ancestor) >> "$workdir"/..
/map
/$sha1
482 done < "$tempdir"/heads
485 # Finally update the refs
487 _x40
='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
488 _x40
="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
492 # avoid rewriting a ref twice
493 test -f "$orig_namespace$ref" && continue
495 sha1
=$
(git rev-parse
"$ref"^
0)
496 rewritten
=$
(map
$sha1)
498 test $sha1 = "$rewritten" &&
499 warn
"WARNING: Ref '$ref' is unchanged" &&
504 echo "Ref '$ref' was deleted"
505 git update-ref
-m "filter-branch: delete" -d "$ref" $sha1 ||
506 die
"Could not delete $ref"
509 echo "Ref '$ref' was rewritten"
510 if ! git update-ref
-m "filter-branch: rewrite" \
511 "$ref" $rewritten $sha1 2>/dev
/null
; then
512 if test $
(git cat-file
-t "$ref") = tag
; then
513 if test -z "$filter_tag_name"; then
514 warn
"WARNING: You said to rewrite tagged commits, but not the corresponding tag."
515 warn
"WARNING: Perhaps use '--tag-name-filter cat' to rewrite the tag."
518 die
"Could not rewrite $ref"
523 # NEEDSWORK: possibly add -Werror, making this an error
524 warn
"WARNING: '$ref' was rewritten into multiple commits:"
526 warn
"WARNING: Ref '$ref' points to the first one now."
527 rewritten
=$
(echo "$rewritten" |
head -n 1)
528 git update-ref
-m "filter-branch: rewrite to first" \
529 "$ref" $rewritten $sha1 ||
530 die
"Could not rewrite $ref"
533 git update-ref
-m "filter-branch: backup" "$orig_namespace$ref" $sha1 ||
535 done < "$tempdir"/heads
537 # TODO: This should possibly go, with the semantics that all positive given
538 # refs are updated, and their original heads stored in refs/original/
541 if [ "$filter_tag_name" ]; then
542 git for-each-ref
--format='%(objectname) %(objecttype) %(refname)' refs
/tags |
543 while read sha1
type ref
; do
544 ref
="${ref#refs/tags/}"
545 # XXX: Rewrite tagged trees as well?
546 if [ "$type" != "commit" -a "$type" != "tag" ]; then
550 if [ "$type" = "tag" ]; then
551 # Dereference to a commit
553 sha1
="$(git rev-parse -q "$sha1"^{commit})" ||
continue
556 [ -f "../map/$sha1" ] ||
continue
557 new_sha1
="$(cat "..
/map
/$sha1")"
560 new_ref
="$(echo "$ref" | eval "$filter_tag_name")" ||
561 die
"tag name filter failed: $filter_tag_name"
563 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
565 if [ "$type" = "tag" ]; then
566 new_sha1
=$
( ( printf 'object %s\ntype commit\ntag %s\n' \
567 "$new_sha1" "$new_ref"
568 git cat-file tag
"$ref" |
575 -e '/^-----BEGIN PGP SIGNATURE-----/q' \
577 git hash-object
-t tag
-w --stdin) ||
578 die
"Could not create new tag object for $ref"
579 if git cat-file tag
"$ref" | \
580 sane_grep
'^-----BEGIN PGP SIGNATURE-----' >/dev
/null
2>&1
582 warn
"gpg signature stripped from tag object $sha1t"
586 git update-ref
"refs/tags/$new_ref" "$new_sha1" ||
587 die
"Could not write tag $new_ref"
591 unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
592 unset GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
593 unset GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL GIT_COMMITTER_DATE
594 test -z "$ORIG_GIT_DIR" ||
{
595 GIT_DIR
="$ORIG_GIT_DIR" && export GIT_DIR
597 test -z "$ORIG_GIT_WORK_TREE" ||
{
598 GIT_WORK_TREE
="$ORIG_GIT_WORK_TREE" &&
601 test -z "$ORIG_GIT_INDEX_FILE" ||
{
602 GIT_INDEX_FILE
="$ORIG_GIT_INDEX_FILE" &&
603 export GIT_INDEX_FILE
605 test -z "$ORIG_GIT_AUTHOR_NAME" ||
{
606 GIT_AUTHOR_NAME
="$ORIG_GIT_AUTHOR_NAME" &&
607 export GIT_AUTHOR_NAME
609 test -z "$ORIG_GIT_AUTHOR_EMAIL" ||
{
610 GIT_AUTHOR_EMAIL
="$ORIG_GIT_AUTHOR_EMAIL" &&
611 export GIT_AUTHOR_EMAIL
613 test -z "$ORIG_GIT_AUTHOR_DATE" ||
{
614 GIT_AUTHOR_DATE
="$ORIG_GIT_AUTHOR_DATE" &&
615 export GIT_AUTHOR_DATE
617 test -z "$ORIG_GIT_COMMITTER_NAME" ||
{
618 GIT_COMMITTER_NAME
="$ORIG_GIT_COMMITTER_NAME" &&
619 export GIT_COMMITTER_NAME
621 test -z "$ORIG_GIT_COMMITTER_EMAIL" ||
{
622 GIT_COMMITTER_EMAIL
="$ORIG_GIT_COMMITTER_EMAIL" &&
623 export GIT_COMMITTER_EMAIL
625 test -z "$ORIG_GIT_COMMITTER_DATE" ||
{
626 GIT_COMMITTER_DATE
="$ORIG_GIT_COMMITTER_DATE" &&
627 export GIT_COMMITTER_DATE
630 if test -n "$state_branch"
632 echo "Saving rewrite state to $state_branch" 1>&2
634 perl
-e'opendir D, "../map" or die;
635 open H, "|-", "git hash-object -w --stdin" or die;
636 foreach (sort readdir(D)) {
638 open F, "<../map/$_" or die;
640 print H "$_:$f\n" or die;
642 close(H) or die;' || die
"Unable to save state")
643 state_tree
=$
(printf '100644 blob %s\tfilter.map\n' "$state_blob" | git mktree
)
644 if test -n "$state_commit"
646 state_commit
=$
(echo "Sync" | git commit-tree
"$state_tree" -p "$state_commit")
648 state_commit
=$
(echo "Sync" | git commit-tree
"$state_tree" )
650 git update-ref
"$state_branch" "$state_commit"
658 if [ "$(is_bare_repository)" = false
]; then
659 git read-tree
-u -m HEAD ||
exit