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 # override die(): this version puts in an extra line break, so that
44 # the progress is still visible
57 # When piped a commit, output a script to set the ident of either
58 # "author" or "committer
61 lid
="$(echo "$1" | tr "[A-Z
]" "[a-z
]")"
62 uid
="$(echo "$1" | tr "[a-z
]" "[A-Z
]")"
67 s/^'$lid' \([^<]*\) <[^>]*> .*$/\1/
69 s
/.
*/GIT_
'$uid'_NAME
='\''&'\''; export GIT_
'$uid'_NAME
/p
72 s
/^
'$lid' [^
<]* <\
([^
>]*\
)> .
*$
/\
1/
74 s/.*/GIT_'$uid'_EMAIL='\''&'\''; export GIT_'$uid'_EMAIL/p
77 s/^'$lid' [^<]* <[^>]*> \(.*\)$/\1/
79 s
/.
*/GIT_
'$uid'_DATE
='\''&'\''; export GIT_
'$uid'_DATE
/p
85 LANG=C LC_ALL=C sed -ne "$pick_id_script"
86 # Ensure non-empty id name.
87 echo "case \"\$GIT_${uid}_NAME\" in \"\") GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\" && export GIT_${uid}_NAME;; esac"
90 USAGE="[--env-filter <command>] [--tree-filter <command>] \
91 [--index-filter <command>] [--parent-filter <command>] \
92 [--msg-filter <command>] [--commit-filter <command>] \
93 [--tag-name-filter <command>] [--subdirectory-filter <directory>] \
94 [--original <namespace>] [-d <directory>] [-f | --force] \
95 [<rev-list options>...]"
100 if [ "$(is_bare_repository)" = false ]; then
101 git diff-files --quiet &&
102 git diff-index --cached --quiet HEAD -- ||
103 die "Cannot rewrite branch(es) with a dirty working directory."
112 filter_commit='git commit-tree
"$@"'
115 orig_namespace=refs/original/
135 # all switches take one argument
137 case "$#" in 1) usage ;; esac
150 filter_tree="$OPTARG"
153 filter_index="$OPTARG"
156 filter_parent="$OPTARG"
162 filter_commit="$functions; $OPTARG"
165 filter_tag_name="$OPTARG"
167 --subdirectory-filter)
168 filter_subdir="$OPTARG"
171 orig_namespace=$(expr "$OPTARG/" : '\
(.
*[^
/]\
)/*$
')/
184 test -d "$tempdir" &&
185 die "$tempdir already exists, please remove it"
187 mkdir -p "$tempdir/t" &&
188 tempdir="$(cd "$tempdir"; pwd)" &&
193 # Remove tempdir on exit
194 trap 'cd ..
/..
; rm -rf "$tempdir"' 0
196 # Make sure refs/original is empty
197 git for-each-ref > "$tempdir"/backup-refs
198 while read sha1 type name
200 case "$force,$name" in
202 die "Namespace $orig_namespace not empty"
205 git update-ref -d "$name" $sha1
208 done < "$tempdir"/backup-refs
210 ORIG_GIT_DIR="$GIT_DIR"
211 ORIG_GIT_WORK_TREE="$GIT_WORK_TREE"
212 ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE"
214 export GIT_DIR GIT_WORK_TREE
216 # The refs should be updated if their heads were rewritten
217 git rev-parse --no-flags --revs-only --symbolic-full-name --default HEAD "$@" |
218 sed -e '/^^
/d
' >"$tempdir"/heads
220 test -s "$tempdir"/heads ||
221 die "Which ref do you want to rewrite?"
223 GIT_INDEX_FILE="$(pwd)/../index"
224 export GIT_INDEX_FILE
225 git read-tree || die "Could not seed the index"
229 # map old->new commit ids for rewriting parents
230 mkdir ../map || die "Could not create map/ directory"
232 case "$filter_subdir" in
234 git rev-list --reverse --topo-order --default HEAD \
238 git rev-list --reverse --topo-order --default HEAD \
239 --parents "$@" -- "$filter_subdir"
240 esac > ../revs || die "Could not get the commits"
241 commits=$(wc -l <../revs | tr -d " ")
243 test $commits -eq 0 && die "Found nothing to rewrite"
245 # Rewrite the commits
248 while read commit parents; do
250 printf "\rRewrite $commit ($i/$commits)"
252 case "$filter_subdir" in
254 git read-tree -i -m $commit
257 # The commit may not have the subdirectory at all
258 err=$(git read-tree -i -m $commit:"$filter_subdir" 2>&1) || {
259 if ! git rev-parse --verify $commit:"$filter_subdir" 2>/dev/null
261 rm -f "$GIT_INDEX_FILE"
267 esac || die "Could not initialize the index"
271 git cat-file commit "$commit" >../commit ||
272 die "Cannot read commit $commit"
274 eval "$(set_ident AUTHOR <../commit)" ||
275 die "setting author failed for commit $commit"
276 eval "$(set_ident COMMITTER <../commit)" ||
277 die "setting committer failed for commit $commit"
278 eval "$filter_env" < /dev/null ||
279 die "env filter failed: $filter_env"
281 if [ "$filter_tree" ]; then
282 git checkout-index -f -u -a ||
283 die "Could not checkout the index"
284 # files that $commit removed are now still in the working tree;
285 # remove them, else they would be added again
286 git clean -d -q -f -x
287 eval "$filter_tree" < /dev/null ||
288 die "tree filter failed: $filter_tree"
291 git diff-index -r --name-only $commit
292 git ls-files --others
294 git update-index --add --replace --remove --stdin
297 eval "$filter_index" < /dev/null ||
298 die "index filter failed: $filter_index"
301 for parent in $parents; do
302 for reparent in $(map "$parent"); do
303 parentstr="$parentstr -p $reparent"
306 if [ "$filter_parent" ]; then
307 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
308 die "parent filter failed: $filter_parent"
311 sed -e '1,/^$
/d
' <../commit | \
312 eval "$filter_msg" > ../message ||
313 die "msg filter failed: $filter_msg"
314 @SHELL_PATH@ -c "$filter_commit" "git commit-tree" \
315 $(git write-tree) $parentstr < ../message > ../map/$commit
318 # In case of a subdirectory filter, it is possible that a specified head
319 # is not in the set of rewritten commits, because it was pruned by the
320 # revision walker. Fix it by mapping these heads to the unique nearest
321 # ancestor that survived the pruning.
323 if test "$filter_subdir"
327 sha1=$(git rev-parse "$ref"^0)
328 test -f "$workdir"/../map/$sha1 && continue
329 ancestor=$(git rev-list -1 $ref -- "$filter_subdir")
330 test "$ancestor" && echo $(map $ancestor) >> "$workdir"/../map/$sha1
331 done < "$tempdir"/heads
334 # Finally update the refs
336 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
337 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
341 # avoid rewriting a ref twice
342 test -f "$orig_namespace$ref" && continue
344 sha1=$(git rev-parse "$ref"^0)
345 rewritten=$(map $sha1)
347 test $sha1 = "$rewritten" &&
348 warn "WARNING: Ref '$ref' is unchanged" &&
353 echo "Ref '$ref' was deleted"
354 git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
355 die "Could not delete $ref"
358 echo "Ref '$ref' was rewritten"
359 if ! git update-ref -m "filter-branch: rewrite" \
360 "$ref" $rewritten $sha1 2>/dev/null; then
361 if test $(git cat-file -t "$ref") = tag; then
362 if test -z "$filter_tag_name"; then
363 warn "WARNING: You said to rewrite tagged commits, but not the corresponding tag."
364 warn "WARNING: Perhaps use '--tag-name-filter cat' to rewrite the tag."
367 die "Could not rewrite $ref"
372 # NEEDSWORK: possibly add -Werror, making this an error
373 warn "WARNING: '$ref' was rewritten into multiple commits:"
375 warn "WARNING: Ref '$ref' points to the first one now."
376 rewritten=$(echo "$rewritten" | head -n 1)
377 git update-ref -m "filter-branch: rewrite to first" \
378 "$ref" $rewritten $sha1 ||
379 die "Could not rewrite $ref"
382 git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1
383 done < "$tempdir"/heads
385 # TODO: This should possibly go, with the semantics that all positive given
386 # refs are updated, and their original heads stored in refs/original/
389 if [ "$filter_tag_name" ]; then
390 git for-each-ref --format='%(objectname
) %(objecttype
) %(refname
)' refs/tags |
391 while read sha1 type ref; do
392 ref="${ref#refs/tags/}"
393 # XXX: Rewrite tagged trees as well?
394 if [ "$type" != "commit" -a "$type" != "tag" ]; then
398 if [ "$type" = "tag" ]; then
399 # Dereference to a commit
401 sha1="$(git rev-parse "$sha1"^{commit} 2>/dev/null)" || continue
404 [ -f "../map/$sha1" ] || continue
405 new_sha1="$(cat "../map/$sha1")"
408 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
409 die "tag name filter failed: $filter_tag_name"
411 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
413 if [ "$type" = "tag" ]; then
414 new_sha1=$(git cat-file tag "$ref" |
417 s/^object .*/object $new_sha1/
418 s/^type .*/type commit/
419 s/^tag .*/tag $new_ref/
421 -e '/^
-----BEGIN PGP SIGNATURE----
-/q
' \
424 die "Could not create new tag object for $ref"
425 if git cat-file tag "$ref" | \
426 grep '^
-----BEGIN PGP SIGNATURE-----
' >/dev/null 2>&1
428 warn "gpg signature stripped from tag object $sha1t"
432 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
433 die "Could not write tag $new_ref"
442 if [ "$(is_bare_repository)" = false ]; then
443 unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
444 test -z "$ORIG_GIT_DIR" || {
445 GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR
447 test -z "$ORIG_GIT_WORK_TREE" || {
448 GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" &&
451 test -z "$ORIG_GIT_INDEX_FILE" || {
452 GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" &&
453 export GIT_INDEX_FILE
455 git read-tree -u -m HEAD