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
20 # if it was not rewritten, take the original
21 if test -r "$workdir/../map/$1"
23 cat "$workdir/../map/$1"
29 # if you run 'skip_commit "$@"' in a commit filter, it will print
30 # the (mapped) parents, effectively skipping the commit.
43 # if you run 'git_commit_non_empty_tree "$@"' in a commit filter,
44 # it will skip commits that leave the tree untouched, commit the other.
45 git_commit_non_empty_tree()
47 if test $# = 3 && test "$1" = $(git rev-parse "$3^{tree}"); then
49 elif test $# = 1 && test "$1" = 4b825dc642cb6eb9a060e54bf8d69288fbee4904; then
55 # override die(): this version puts in an extra line break, so that
56 # the progress is still visible
70 # Ensure non-empty id name.
71 echo "case \"\$GIT_$1_NAME\" in \"\") GIT_$1_NAME=\"\${GIT_$1_EMAIL%%@*}\" && export GIT_$1_NAME;; esac"
72 # And make sure everything is exported.
73 echo "export GIT_$1_NAME"
74 echo "export GIT_$1_EMAIL"
75 echo "export GIT_$1_DATE"
79 parse_ident_from_commit author AUTHOR committer COMMITTER
81 finish_ident COMMITTER
84 USAGE="[--setup <command>] [--env-filter <command>]
85 [--tree-filter <command>] [--index-filter <command>]
86 [--parent-filter <command>] [--msg-filter <command>]
87 [--commit-filter <command>] [--tag-name-filter <command>]
88 [--subdirectory-filter <directory>] [--original <namespace>]
89 [-d <directory>] [-f | --force]
90 [--] [<rev-list options>...]"
95 if [ "$(is_bare_repository)" = false ]; then
96 require_clean_work_tree 'rewrite branches'
109 orig_namespace=refs/original/
126 # deprecated ($remap_to_ancestor is set now automatically)
142 # all switches take one argument
144 case "$#" in 1) usage ;; esac
154 filter_setup="$OPTARG"
160 filter_tree="$OPTARG"
163 filter_index="$OPTARG"
166 filter_parent="$OPTARG"
172 filter_commit="$functions; $OPTARG"
175 filter_tag_name="$OPTARG"
177 --subdirectory-filter)
178 filter_subdir="$OPTARG"
182 orig_namespace=$(expr "$OPTARG/" : '\(.*[^/]\)/*$')/
190 case "$prune_empty,$filter_commit" in
192 filter_commit='git commit-tree "$@"';;
194 filter_commit="$functions;"' git_commit_non_empty_tree "$@"';;
198 die "Cannot set --prune-empty and --commit-filter at the same time"
206 test -d "$tempdir" &&
207 die "$tempdir already exists, please remove it"
210 mkdir -p "$tempdir/t" &&
211 tempdir="$(cd "$tempdir"; pwd)" &&
216 # Remove tempdir on exit
217 trap 'cd "$orig_dir"; rm -rf "$tempdir"' 0
219 ORIG_GIT_DIR="$GIT_DIR"
220 ORIG_GIT_WORK_TREE="$GIT_WORK_TREE"
221 ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE"
223 export GIT_DIR GIT_WORK_TREE
225 # Make sure refs/original is empty
226 git for-each-ref > "$tempdir"/backup-refs || exit
227 while read sha1 type name
229 case "$force,$name" in
231 die "Cannot create a new backup.
232 A previous backup already exists in $orig_namespace
233 Force overwriting the backup with -f"
236 git update-ref -d "$name" $sha1
239 done < "$tempdir"/backup-refs
241 # The refs should be updated if their heads were rewritten
242 git rev-parse --no-flags --revs-only --symbolic-full-name \
243 --default HEAD "$@" > "$tempdir"/raw-heads || exit
244 sed -e '/^^/d' "$tempdir"/raw-heads >"$tempdir"/heads
246 test -s "$tempdir"/heads ||
247 die "You must specify a ref to rewrite."
249 GIT_INDEX_FILE="$(pwd)/../index"
250 export GIT_INDEX_FILE
252 # map old->new commit ids for rewriting parents
253 mkdir ../map || die "Could not create map/ directory"
255 # we need "--" only if there are no path arguments in $@
256 nonrevs=$(git rev-parse --no-revs "$@") || exit
257 if test -z "$nonrevs"
265 git rev-parse --revs-only "$@" >../parse
267 case "$filter_subdir" in
269 eval set -- "$(git rev-parse --sq --no-revs "$@")"
272 eval set -- "$(git rev-parse --sq --no-revs "$@" $dashdash \
277 git rev-list --reverse --topo-order --default HEAD \
278 --parents --simplify-merges --stdin "$@" <../parse >../revs ||
279 die "Could not get the commits"
280 commits=$(wc -l <../revs | tr -d " ")
282 test $commits -eq 0 && die "Found nothing to rewrite"
284 # Rewrite the commits
287 if test -n "$progress" &&
288 test $git_filter_branch__commit_count -gt $next_sample_at
290 count=$git_filter_branch__commit_count
293 elapsed=$(($now - $start_timestamp))
294 remaining=$(( ($commits - $count) * $elapsed / $count ))
295 if test $elapsed -gt 0
297 next_sample_at=$(( ($elapsed + 1) * $count / $elapsed ))
299 next_sample_at=$(($next_sample_at + 1))
301 progress=" ($elapsed seconds passed, remaining $remaining predicted)"
303 printf "\rRewrite $commit ($count/$commits)$progress "
306 git_filter_branch__commit_count=0
308 progress= start_timestamp=
309 if date '+%s' 2>/dev/null | grep -q '^[0-9][0-9]*$'
312 progress="dummy to ensure this is not empty"
313 start_timestamp=$(date '+%s')
316 if test -n "$filter_index" ||
317 test -n "$filter_tree" ||
318 test -n "$filter_subdir"
325 eval "$filter_setup" < /dev/null ||
326 die "filter setup failed: $filter_setup"
328 while read commit parents; do
329 git_filter_branch__commit_count=$(($git_filter_branch__commit_count+1))
333 case "$filter_subdir" in
335 if test -n "$need_index"
337 GIT_ALLOW_NULL_SHA1=1 git read-tree -i -m $commit
341 # The commit may not have the subdirectory at all
342 err=$(GIT_ALLOW_NULL_SHA1=1 \
343 git read-tree -i -m $commit:"$filter_subdir" 2>&1) || {
344 if ! git rev-parse -q --verify $commit:"$filter_subdir"
346 rm -f "$GIT_INDEX_FILE"
352 esac || die "Could not initialize the index"
356 git cat-file commit "$commit" >../commit ||
357 die "Cannot read commit $commit"
359 eval "$(set_ident <../commit)" ||
360 die "setting author/committer failed for commit $commit"
361 eval "$filter_env" < /dev/null ||
362 die "env filter failed: $filter_env"
364 if [ "$filter_tree" ]; then
365 git checkout-index -f -u -a ||
366 die "Could not checkout the index"
367 # files that $commit removed are now still in the working tree;
368 # remove them, else they would be added again
369 git clean -d -q -f -x
370 eval "$filter_tree" < /dev/null ||
371 die "tree filter failed: $filter_tree"
374 git diff-index -r --name-only --ignore-submodules $commit -- &&
375 git ls-files --others
376 ) > "$tempdir"/tree-state || exit
377 git update-index --add --replace --remove --stdin \
378 < "$tempdir"/tree-state || exit
381 eval "$filter_index" < /dev/null ||
382 die "index filter failed: $filter_index"
385 for parent in $parents; do
386 for reparent in $(map "$parent"); do
387 case "$parentstr " in
391 parentstr="$parentstr -p $reparent"
396 if [ "$filter_parent" ]; then
397 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
398 die "parent filter failed: $filter_parent"
402 while IFS='' read -r header_line && test -n "$header_line"
404 # skip header lines...
407 # and output the actual commit message
410 eval "$filter_msg" > ../message ||
411 die "msg filter failed: $filter_msg"
413 if test -n "$need_index"
415 tree=$(git write-tree)
417 tree=$(git rev-parse "$commit^{tree}")
419 workdir=$workdir @SHELL_PATH@ -c "$filter_commit" "git commit-tree" \
420 "$tree" $parentstr < ../message > ../map/$commit ||
421 die "could not write rewritten commit"
424 # If we are filtering for paths, as in the case of a subdirectory
425 # filter, it is possible that a specified head is not in the set of
426 # rewritten commits, because it was pruned by the revision walker.
427 # Ancestor remapping fixes this by mapping these heads to the unique
428 # nearest ancestor that survived the pruning.
430 if test "$remap_to_ancestor" = t
434 sha1=$(git rev-parse "$ref"^0)
435 test -f "$workdir"/../map/$sha1 && continue
436 ancestor=$(git rev-list --simplify-merges -1 "$ref" "$@")
437 test "$ancestor" && echo $(map $ancestor) >> "$workdir"/../map/$sha1
438 done < "$tempdir"/heads
441 # Finally update the refs
443 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
444 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
448 # avoid rewriting a ref twice
449 test -f "$orig_namespace$ref" && continue
451 sha1=$(git rev-parse "$ref"^0)
452 rewritten=$(map $sha1)
454 test $sha1 = "$rewritten" &&
455 warn "WARNING: Ref '$ref' is unchanged" &&
460 echo "Ref '$ref' was deleted"
461 git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
462 die "Could not delete $ref"
465 echo "Ref '$ref' was rewritten"
466 if ! git update-ref -m "filter-branch: rewrite" \
467 "$ref" $rewritten $sha1 2>/dev/null; then
468 if test $(git cat-file -t "$ref") = tag; then
469 if test -z "$filter_tag_name"; then
470 warn "WARNING: You said to rewrite tagged commits, but not the corresponding tag."
471 warn "WARNING: Perhaps use '--tag-name-filter cat' to rewrite the tag."
474 die "Could not rewrite $ref"
479 # NEEDSWORK: possibly add -Werror, making this an error
480 warn "WARNING: '$ref' was rewritten into multiple commits:"
482 warn "WARNING: Ref '$ref' points to the first one now."
483 rewritten=$(echo "$rewritten" | head -n 1)
484 git update-ref -m "filter-branch: rewrite to first" \
485 "$ref" $rewritten $sha1 ||
486 die "Could not rewrite $ref"
489 git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1 ||
491 done < "$tempdir"/heads
493 # TODO: This should possibly go, with the semantics that all positive given
494 # refs are updated, and their original heads stored in refs/original/
497 if [ "$filter_tag_name" ]; then
498 git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags |
499 while read sha1 type ref; do
500 ref="${ref#refs/tags/}"
501 # XXX: Rewrite tagged trees as well?
502 if [ "$type" != "commit" -a "$type" != "tag" ]; then
506 if [ "$type" = "tag" ]; then
507 # Dereference to a commit
509 sha1="$(git rev-parse -q "$sha1"^{commit})" || continue
512 [ -f "../map/$sha1" ] || continue
513 new_sha1="$(cat "../map/$sha1")"
516 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
517 die "tag name filter failed: $filter_tag_name"
519 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
521 if [ "$type" = "tag" ]; then
522 new_sha1=$( ( printf 'object %s\ntype commit\ntag %s\n' \
523 "$new_sha1" "$new_ref"
524 git cat-file tag "$ref" |
531 -e '/^-----BEGIN PGP SIGNATURE-----/q' \
534 die "Could not create new tag object for $ref"
535 if git cat-file tag "$ref" | \
536 sane_grep '^-----BEGIN PGP SIGNATURE-----' >/dev/null 2>&1
538 warn "gpg signature stripped from tag object $sha1t"
542 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
543 die "Could not write tag $new_ref"
552 unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
553 test -z "$ORIG_GIT_DIR" || {
554 GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR
556 test -z "$ORIG_GIT_WORK_TREE" || {
557 GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" &&
560 test -z "$ORIG_GIT_INDEX_FILE" || {
561 GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" &&
562 export GIT_INDEX_FILE
565 if [ "$(is_bare_repository)" = false ]; then
566 git read-tree -u -m HEAD || exit