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/
151 # all switches take one argument
153 case "$#" in 1) usage ;; esac
166 filter_tree="$OPTARG"
169 filter_index="$OPTARG"
172 filter_parent="$OPTARG"
178 filter_commit="$functions; $OPTARG"
181 filter_tag_name="$OPTARG"
183 --subdirectory-filter)
184 filter_subdir="$OPTARG"
187 orig_namespace=$(expr "$OPTARG/" : '\
(.
*[^
/]\
)/*$
')/
195 case "$prune_empty,$filter_commit" in
197 filter_commit='git commit-tree
"$@"';;
199 filter_commit="$functions;"' git_commit_non_empty_tree
"$@"';;
203 die "Cannot set --prune-empty and --filter-commit at the same time"
211 test -d "$tempdir" &&
212 die "$tempdir already exists, please remove it"
214 mkdir -p "$tempdir/t" &&
215 tempdir="$(cd "$tempdir"; pwd)" &&
220 # Remove tempdir on exit
221 trap 'cd ..
/..
; rm -rf "$tempdir"' 0
223 ORIG_GIT_DIR="$GIT_DIR"
224 ORIG_GIT_WORK_TREE="$GIT_WORK_TREE"
225 ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE"
227 export GIT_DIR GIT_WORK_TREE
229 # Make sure refs/original is empty
230 git for-each-ref > "$tempdir"/backup-refs || exit
231 while read sha1 type name
233 case "$force,$name" in
235 die "Namespace $orig_namespace not empty"
238 git update-ref -d "$name" $sha1
241 done < "$tempdir"/backup-refs
243 # The refs should be updated if their heads were rewritten
244 git rev-parse --no-flags --revs-only --symbolic-full-name \
245 --default HEAD "$@" > "$tempdir"/raw-heads || exit
246 sed -e '/^^
/d
' "$tempdir"/raw-heads >"$tempdir"/heads
248 test -s "$tempdir"/heads ||
249 die "Which ref do you want to rewrite?"
251 GIT_INDEX_FILE="$(pwd)/../index"
252 export GIT_INDEX_FILE
253 git read-tree || die "Could not seed the index"
255 # map old->new commit ids for rewriting parents
256 mkdir ../map || die "Could not create map/ directory"
258 case "$filter_subdir" in
260 git rev-list --reverse --topo-order --default HEAD \
261 --parents --simplify-merges "$@"
264 git rev-list --reverse --topo-order --default HEAD \
265 --parents --simplify-merges "$@" -- "$filter_subdir"
266 esac > ../revs || die "Could not get the commits"
267 commits=$(wc -l <../revs | tr -d " ")
269 test $commits -eq 0 && die "Found nothing to rewrite"
271 # Rewrite the commits
274 while read commit parents; do
276 printf "\rRewrite $commit ($i/$commits)"
278 case "$filter_subdir" in
280 git read-tree -i -m $commit
283 # The commit may not have the subdirectory at all
284 err=$(git read-tree -i -m $commit:"$filter_subdir" 2>&1) || {
285 if ! git rev-parse -q --verify $commit:"$filter_subdir"
287 rm -f "$GIT_INDEX_FILE"
293 esac || die "Could not initialize the index"
297 git cat-file commit "$commit" >../commit ||
298 die "Cannot read commit $commit"
300 eval "$(set_ident AUTHOR <../commit)" ||
301 die "setting author failed for commit $commit"
302 eval "$(set_ident COMMITTER <../commit)" ||
303 die "setting committer failed for commit $commit"
304 eval "$filter_env" < /dev/null ||
305 die "env filter failed: $filter_env"
307 if [ "$filter_tree" ]; then
308 git checkout-index -f -u -a ||
309 die "Could not checkout the index"
310 # files that $commit removed are now still in the working tree;
311 # remove them, else they would be added again
312 git clean -d -q -f -x
313 eval "$filter_tree" < /dev/null ||
314 die "tree filter failed: $filter_tree"
317 git diff-index -r --name-only $commit &&
318 git ls-files --others
319 ) > "$tempdir"/tree-state || exit
320 git update-index --add --replace --remove --stdin \
321 < "$tempdir"/tree-state || exit
324 eval "$filter_index" < /dev/null ||
325 die "index filter failed: $filter_index"
328 for parent in $parents; do
329 for reparent in $(map "$parent"); do
330 parentstr="$parentstr -p $reparent"
333 if [ "$filter_parent" ]; then
334 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
335 die "parent filter failed: $filter_parent"
338 sed -e '1,/^$
/d
' <../commit | \
339 eval "$filter_msg" > ../message ||
340 die "msg filter failed: $filter_msg"
341 @SHELL_PATH@ -c "$filter_commit" "git commit-tree" \
342 $(git write-tree) $parentstr < ../message > ../map/$commit ||
343 die "could not write rewritten commit"
346 # In case of a subdirectory filter, it is possible that a specified head
347 # is not in the set of rewritten commits, because it was pruned by the
348 # revision walker. Fix it by mapping these heads to the unique nearest
349 # ancestor that survived the pruning.
351 if test "$filter_subdir"
355 sha1=$(git rev-parse "$ref"^0)
356 test -f "$workdir"/../map/$sha1 && continue
357 ancestor=$(git rev-list --simplify-merges -1 \
358 $ref -- "$filter_subdir")
359 test "$ancestor" && echo $(map $ancestor) >> "$workdir"/../map/$sha1
360 done < "$tempdir"/heads
363 # Finally update the refs
365 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
366 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
370 # avoid rewriting a ref twice
371 test -f "$orig_namespace$ref" && continue
373 sha1=$(git rev-parse "$ref"^0)
374 rewritten=$(map $sha1)
376 test $sha1 = "$rewritten" &&
377 warn "WARNING: Ref '$ref' is unchanged" &&
382 echo "Ref '$ref' was deleted"
383 git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
384 die "Could not delete $ref"
387 echo "Ref '$ref' was rewritten"
388 if ! git update-ref -m "filter-branch: rewrite" \
389 "$ref" $rewritten $sha1 2>/dev/null; then
390 if test $(git cat-file -t "$ref") = tag; then
391 if test -z "$filter_tag_name"; then
392 warn "WARNING: You said to rewrite tagged commits, but not the corresponding tag."
393 warn "WARNING: Perhaps use '--tag-name-filter cat' to rewrite the tag."
396 die "Could not rewrite $ref"
401 # NEEDSWORK: possibly add -Werror, making this an error
402 warn "WARNING: '$ref' was rewritten into multiple commits:"
404 warn "WARNING: Ref '$ref' points to the first one now."
405 rewritten=$(echo "$rewritten" | head -n 1)
406 git update-ref -m "filter-branch: rewrite to first" \
407 "$ref" $rewritten $sha1 ||
408 die "Could not rewrite $ref"
411 git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1 ||
413 done < "$tempdir"/heads
415 # TODO: This should possibly go, with the semantics that all positive given
416 # refs are updated, and their original heads stored in refs/original/
419 if [ "$filter_tag_name" ]; then
420 git for-each-ref --format='%(objectname
) %(objecttype
) %(refname
)' refs/tags |
421 while read sha1 type ref; do
422 ref="${ref#refs/tags/}"
423 # XXX: Rewrite tagged trees as well?
424 if [ "$type" != "commit" -a "$type" != "tag" ]; then
428 if [ "$type" = "tag" ]; then
429 # Dereference to a commit
431 sha1="$(git rev-parse "$sha1"^{commit} 2>/dev/null)" || continue
434 [ -f "../map/$sha1" ] || continue
435 new_sha1="$(cat "../map/$sha1")"
438 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
439 die "tag name filter failed: $filter_tag_name"
441 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
443 if [ "$type" = "tag" ]; then
444 new_sha1=$( ( printf 'object
%s
\ntype commit
\ntag
%s
\n' \
445 "$new_sha1" "$new_ref"
446 git cat-file tag "$ref" |
453 -e '/^
-----BEGIN PGP SIGNATURE----
-/q
' \
456 die "Could not create new tag object for $ref"
457 if git cat-file tag "$ref" | \
458 grep '^
-----BEGIN PGP SIGNATURE-----
' >/dev/null 2>&1
460 warn "gpg signature stripped from tag object $sha1t"
464 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
465 die "Could not write tag $new_ref"
474 unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
475 test -z "$ORIG_GIT_DIR" || {
476 GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR
478 test -z "$ORIG_GIT_WORK_TREE" || {
479 GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" &&
482 test -z "$ORIG_GIT_INDEX_FILE" || {
483 GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" &&
484 export GIT_INDEX_FILE
487 if [ "$(is_bare_repository)" = false ]; then
488 git read-tree -u -m HEAD || exit