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
53 # override die(): this version puts in an extra line break, so that
54 # the progress is still visible
67 # When piped a commit, output a script to set the ident of either
68 # "author" or "committer
71 lid
="$(echo "$1" | tr "[A-Z
]" "[a-z
]")"
72 uid
="$(echo "$1" | tr "[a-z
]" "[A-Z
]")"
77 s/^'$lid' \([^<]*\) <[^>]*> .*$/\1/
79 s
/.
*/GIT_
'$uid'_NAME
='\''&'\''; export GIT_
'$uid'_NAME
/p
82 s
/^
'$lid' [^
<]* <\
([^
>]*\
)> .
*$
/\
1/
84 s/.*/GIT_'$uid'_EMAIL='\''&'\''; export GIT_'$uid'_EMAIL/p
87 s/^'$lid' [^<]* <[^>]*> \(.*\)$/\1/
89 s
/.
*/GIT_
'$uid'_DATE
='\''&'\''; export GIT_
'$uid'_DATE
/p
95 LANG=C LC_ALL=C sed -ne "$pick_id_script"
96 # Ensure non-empty id name.
97 echo "case \"\$GIT_${uid}_NAME\" in \"\") GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\" && export GIT_${uid}_NAME;; esac"
100 USAGE="[--env-filter <command>] [--tree-filter <command>]
101 [--index-filter <command>] [--parent-filter <command>]
102 [--msg-filter <command>] [--commit-filter <command>]
103 [--tag-name-filter <command>] [--subdirectory-filter <directory>]
104 [--original <namespace>] [-d <directory>] [-f | --force]
105 [<rev-list options>...]"
110 if [ "$(is_bare_repository)" = false ]; then
111 git diff-files --ignore-submodules --quiet &&
112 git diff-index --cached --quiet HEAD -- ||
113 die "Cannot rewrite branch(es) with a dirty working directory."
125 orig_namespace=refs/original/
142 # deprecated ($remap_to_ancestor is set now automatically)
158 # all switches take one argument
160 case "$#" in 1) usage ;; esac
173 filter_tree="$OPTARG"
176 filter_index="$OPTARG"
179 filter_parent="$OPTARG"
185 filter_commit="$functions; $OPTARG"
188 filter_tag_name="$OPTARG"
190 --subdirectory-filter)
191 filter_subdir="$OPTARG"
195 orig_namespace=$(expr "$OPTARG/" : '\
(.
*[^
/]\
)/*$
')/
203 case "$prune_empty,$filter_commit" in
205 filter_commit='git commit-tree
"$@"';;
207 filter_commit="$functions;"' git_commit_non_empty_tree
"$@"';;
211 die "Cannot set --prune-empty and --commit-filter at the same time"
219 test -d "$tempdir" &&
220 die "$tempdir already exists, please remove it"
222 mkdir -p "$tempdir/t" &&
223 tempdir="$(cd "$tempdir"; pwd)" &&
228 # Remove tempdir on exit
229 trap 'cd ..
/..
; rm -rf "$tempdir"' 0
231 ORIG_GIT_DIR="$GIT_DIR"
232 ORIG_GIT_WORK_TREE="$GIT_WORK_TREE"
233 ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE"
235 export GIT_DIR GIT_WORK_TREE
237 # Make sure refs/original is empty
238 git for-each-ref > "$tempdir"/backup-refs || exit
239 while read sha1 type name
241 case "$force,$name" in
243 die "Cannot create a new backup.
244 A previous backup already exists in $orig_namespace
245 Force overwriting the backup with -f"
248 git update-ref -d "$name" $sha1
251 done < "$tempdir"/backup-refs
253 # The refs should be updated if their heads were rewritten
254 git rev-parse --no-flags --revs-only --symbolic-full-name \
255 --default HEAD "$@" > "$tempdir"/raw-heads || exit
256 sed -e '/^^
/d
' "$tempdir"/raw-heads >"$tempdir"/heads
258 test -s "$tempdir"/heads ||
259 die "Which ref do you want to rewrite?"
261 GIT_INDEX_FILE="$(pwd)/../index"
262 export GIT_INDEX_FILE
264 # map old->new commit ids for rewriting parents
265 mkdir ../map || die "Could not create map/ directory"
267 # we need "--" only if there are no path arguments in $@
268 nonrevs=$(git rev-parse --no-revs "$@") || exit
269 if test -z "$nonrevs"
277 rev_args=$(git rev-parse --revs-only "$@")
279 case "$filter_subdir" in
281 eval set -- "$(git rev-parse --sq --no-revs "$@")"
284 eval set -- "$(git rev-parse --sq --no-revs "$@" $dashdash \
289 git rev-list --reverse --topo-order --default HEAD \
290 --parents --simplify-merges $rev_args "$@" > ../revs ||
291 die "Could not get the commits"
292 commits=$(wc -l <../revs | tr -d " ")
294 test $commits -eq 0 && die "Found nothing to rewrite"
296 # Rewrite the commits
298 git_filter_branch__commit_count=0
299 while read commit parents; do
300 git_filter_branch__commit_count=$(($git_filter_branch__commit_count+1))
301 printf "\rRewrite $commit ($git_filter_branch__commit_count/$commits)"
303 case "$filter_subdir" in
305 git read-tree -i -m $commit
308 # The commit may not have the subdirectory at all
309 err=$(git read-tree -i -m $commit:"$filter_subdir" 2>&1) || {
310 if ! git rev-parse -q --verify $commit:"$filter_subdir"
312 rm -f "$GIT_INDEX_FILE"
318 esac || die "Could not initialize the index"
322 git cat-file commit "$commit" >../commit ||
323 die "Cannot read commit $commit"
325 eval "$(set_ident AUTHOR <../commit)" ||
326 die "setting author failed for commit $commit"
327 eval "$(set_ident COMMITTER <../commit)" ||
328 die "setting committer failed for commit $commit"
329 eval "$filter_env" < /dev/null ||
330 die "env filter failed: $filter_env"
332 if [ "$filter_tree" ]; then
333 git checkout-index -f -u -a ||
334 die "Could not checkout the index"
335 # files that $commit removed are now still in the working tree;
336 # remove them, else they would be added again
337 git clean -d -q -f -x
338 eval "$filter_tree" < /dev/null ||
339 die "tree filter failed: $filter_tree"
342 git diff-index -r --name-only --ignore-submodules $commit &&
343 git ls-files --others
344 ) > "$tempdir"/tree-state || exit
345 git update-index --add --replace --remove --stdin \
346 < "$tempdir"/tree-state || exit
349 eval "$filter_index" < /dev/null ||
350 die "index filter failed: $filter_index"
353 for parent in $parents; do
354 for reparent in $(map "$parent"); do
355 parentstr="$parentstr -p $reparent"
358 if [ "$filter_parent" ]; then
359 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
360 die "parent filter failed: $filter_parent"
363 sed -e '1,/^$
/d
' <../commit | \
364 eval "$filter_msg" > ../message ||
365 die "msg filter failed: $filter_msg"
366 @SHELL_PATH@ -c "$filter_commit" "git commit-tree" \
367 $(git write-tree) $parentstr < ../message > ../map/$commit ||
368 die "could not write rewritten commit"
371 # If we are filtering for paths, as in the case of a subdirectory
372 # filter, it is possible that a specified head is not in the set of
373 # rewritten commits, because it was pruned by the revision walker.
374 # Ancestor remapping fixes this by mapping these heads to the unique
375 # nearest ancestor that survived the pruning.
377 if test "$remap_to_ancestor" = t
381 sha1=$(git rev-parse "$ref"^0)
382 test -f "$workdir"/../map/$sha1 && continue
383 ancestor=$(git rev-list --simplify-merges -1 "$ref" "$@")
384 test "$ancestor" && echo $(map $ancestor) >> "$workdir"/../map/$sha1
385 done < "$tempdir"/heads
388 # Finally update the refs
390 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
391 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
395 # avoid rewriting a ref twice
396 test -f "$orig_namespace$ref" && continue
398 sha1=$(git rev-parse "$ref"^0)
399 rewritten=$(map $sha1)
401 test $sha1 = "$rewritten" &&
402 warn "WARNING: Ref '$ref' is unchanged" &&
407 echo "Ref '$ref' was deleted"
408 git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
409 die "Could not delete $ref"
412 echo "Ref '$ref' was rewritten"
413 if ! git update-ref -m "filter-branch: rewrite" \
414 "$ref" $rewritten $sha1 2>/dev/null; then
415 if test $(git cat-file -t "$ref") = tag; then
416 if test -z "$filter_tag_name"; then
417 warn "WARNING: You said to rewrite tagged commits, but not the corresponding tag."
418 warn "WARNING: Perhaps use '--tag-name-filter cat' to rewrite the tag."
421 die "Could not rewrite $ref"
426 # NEEDSWORK: possibly add -Werror, making this an error
427 warn "WARNING: '$ref' was rewritten into multiple commits:"
429 warn "WARNING: Ref '$ref' points to the first one now."
430 rewritten=$(echo "$rewritten" | head -n 1)
431 git update-ref -m "filter-branch: rewrite to first" \
432 "$ref" $rewritten $sha1 ||
433 die "Could not rewrite $ref"
436 git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1 ||
438 done < "$tempdir"/heads
440 # TODO: This should possibly go, with the semantics that all positive given
441 # refs are updated, and their original heads stored in refs/original/
444 if [ "$filter_tag_name" ]; then
445 git for-each-ref --format='%(objectname
) %(objecttype
) %(refname
)' refs/tags |
446 while read sha1 type ref; do
447 ref="${ref#refs/tags/}"
448 # XXX: Rewrite tagged trees as well?
449 if [ "$type" != "commit" -a "$type" != "tag" ]; then
453 if [ "$type" = "tag" ]; then
454 # Dereference to a commit
456 sha1="$(git rev-parse -q "$sha1"^{commit})" || continue
459 [ -f "../map/$sha1" ] || continue
460 new_sha1="$(cat "../map/$sha1")"
463 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
464 die "tag name filter failed: $filter_tag_name"
466 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
468 if [ "$type" = "tag" ]; then
469 new_sha1=$( ( printf 'object
%s
\ntype commit
\ntag
%s
\n' \
470 "$new_sha1" "$new_ref"
471 git cat-file tag "$ref" |
478 -e '/^
-----BEGIN PGP SIGNATURE----
-/q
' \
481 die "Could not create new tag object for $ref"
482 if git cat-file tag "$ref" | \
483 sane_grep '^
-----BEGIN PGP SIGNATURE-----
' >/dev/null 2>&1
485 warn "gpg signature stripped from tag object $sha1t"
489 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
490 die "Could not write tag $new_ref"
499 unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
500 test -z "$ORIG_GIT_DIR" || {
501 GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR
503 test -z "$ORIG_GIT_WORK_TREE" || {
504 GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" &&
507 test -z "$ORIG_GIT_INDEX_FILE" || {
508 GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" &&
509 export GIT_INDEX_FILE
512 if [ "$(is_bare_repository)" = false ]; then
513 git read-tree -u -m HEAD || exit