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,
17 # if it was not rewritten, take the original
18 if test -r "$workdir/../map/$1"
20 cat "$workdir/../map/$1"
26 # if you run 'skip_commit "$@"' in a commit filter, it will print
27 # the (mapped) parents, effectively skipping the commit.
40 # override die(): this version puts in an extra line break, so that
41 # the progress is still visible
50 # When piped a commit, output a script to set the ident of either
51 # "author" or "committer
54 lid
="$(echo "$1" | tr "A-Z
" "a-z
")"
55 uid
="$(echo "$1" | tr "a-z
" "A-Z
")"
60 s/^'$lid' \([^<]*\) <[^>]*> .*$/\1/
62 s
/.
*/export GIT_
'$uid'_NAME
='\''&'\''/p
65 s
/^
'$lid' [^
<]* <\
([^
>]*\
)> .
*$
/\
1/
67 s/.*/export GIT_'$uid'_EMAIL='\''&'\''/p
70 s/^'$lid' [^<]* <[^>]*> \(.*\)$/\1/
72 s
/.
*/export GIT_
'$uid'_DATE
='\''&'\''/p
78 LANG=C LC_ALL=C sed -ne "$pick_id_script"
79 # Ensure non-empty id name.
80 echo "[ -n \"\$GIT_${uid}_NAME\" ] || export GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\""
83 # This script can be sourced by the commit filter to get the functions
84 test "a$SOURCE_FUNCTIONS" = a1 && return
85 this_script="$(cd "$(dirname "$0")"; pwd)"/$(basename "$0")
88 USAGE="[--env-filter <command>] [--tree-filter <command>] \
89 [--index-filter <command>] [--parent-filter <command>] \
90 [--msg-filter <command>] [--commit-filter <command>] \
91 [--tag-name-filter <command>] [--subdirectory-filter <directory>] \
92 [--original <namespace>] [-d <directory>] [-f | --force] \
93 [<rev-list options>...]"
103 filter_commit='git commit-tree
"$@"'
106 orig_namespace=refs/original/
127 # all switches take one argument
129 case "$#" in 1) usage ;; esac
142 filter_tree="$OPTARG"
145 filter_index="$OPTARG"
148 filter_parent="$OPTARG"
154 filter_commit='SOURCE_FUNCTIONS
=1 .
"$this_script";'" $OPTARG"
157 filter_tag_name="$OPTARG"
159 --subdirectory-filter)
160 filter_subdir="$OPTARG"
163 orig_namespace=$(expr "$OPTARG/" : '\
(.
*[^
/]\
)/*$
')/
176 test -d "$tempdir" &&
177 die "$tempdir already exists, please remove it"
179 mkdir -p "$tempdir/t" &&
180 tempdir="$(cd "$tempdir"; pwd)" &&
185 # Make sure refs/original is empty
186 git for-each-ref > "$tempdir"/backup-refs
187 while read sha1 type name
189 case "$force,$name" in
191 die "Namespace $orig_namespace not empty"
194 git update-ref -d "$name" $sha1
197 done < "$tempdir"/backup-refs
199 export GIT_DIR GIT_WORK_TREE=.
201 # These refs should be updated if their heads were rewritten
203 git rev-parse --revs-only --symbolic "$@" |
209 ref="$(git symbolic-ref "$ref")"
214 ref="$(git for-each-ref --format='%(refname
)' |
218 git check-ref-format "$ref" && echo "$ref"
219 done > "$tempdir"/heads
221 test -s "$tempdir"/heads ||
222 die "Which ref do you want to rewrite?"
224 export GIT_INDEX_FILE="$(pwd)/../index"
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 --full-history "$@" -- "$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 git read-tree -i -m $commit:"$filter_subdir"
258 esac || die "Could not initialize the index"
260 export GIT_COMMIT=$commit
261 git cat-file commit "$commit" >../commit ||
262 die "Cannot read commit $commit"
264 eval "$(set_ident AUTHOR <../commit)" ||
265 die "setting author failed for commit $commit"
266 eval "$(set_ident COMMITTER <../commit)" ||
267 die "setting committer failed for commit $commit"
268 eval "$filter_env" < /dev/null ||
269 die "env filter failed: $filter_env"
271 if [ "$filter_tree" ]; then
272 git checkout-index -f -u -a ||
273 die "Could not checkout the index"
274 # files that $commit removed are now still in the working tree;
275 # remove them, else they would be added again
276 git ls-files -z --others | xargs -0 rm -f
277 eval "$filter_tree" < /dev/null ||
278 die "tree filter failed: $filter_tree"
280 git diff-index -r $commit | cut -f 2- | tr '\n' '\
0' | \
281 xargs -0 git update-index --add --replace --remove
282 git ls-files -z --others | \
283 xargs -0 git update-index --add --replace --remove
286 eval "$filter_index" < /dev/null ||
287 die "index filter failed: $filter_index"
290 for parent in $parents; do
291 for reparent in $(map "$parent"); do
292 parentstr="$parentstr -p $reparent"
295 if [ "$filter_parent" ]; then
296 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
297 die "parent filter failed: $filter_parent"
300 sed -e '1,/^$
/d
' <../commit | \
301 eval "$filter_msg" > ../message ||
302 die "msg filter failed: $filter_msg"
303 sh -c "$filter_commit" "git commit-tree" \
304 $(git write-tree) $parentstr < ../message > ../map/$commit
307 # In case of a subdirectory filter, it is possible that a specified head
308 # is not in the set of rewritten commits, because it was pruned by the
309 # revision walker. Fix it by mapping these heads to the next rewritten
310 # ancestor(s), i.e. the boundaries in the set of rewritten commits.
312 # NEEDSWORK: we should sort the unmapped refs topologically first
315 sha1=$(git rev-parse "$ref"^0)
316 test -f "$workdir"/../map/$sha1 && continue
317 # Assign the boundarie(s) in the set of rewritten commits
318 # as the replacement commit(s).
319 # (This would look a bit nicer if --not --stdin worked.)
320 for p in $( (cd "$workdir"/../map; ls | sed "s/^/^/") |
321 git rev-list $ref --boundary --stdin |
324 map $p >> "$workdir"/../map/$sha1
326 done < "$tempdir"/heads
328 # Finally update the refs
330 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
331 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
336 # avoid rewriting a ref twice
337 test -f "$orig_namespace$ref" && continue
339 sha1=$(git rev-parse "$ref"^0)
340 rewritten=$(map $sha1)
342 test $sha1 = "$rewritten" &&
343 warn "WARNING: Ref '$ref' is unchanged" &&
348 echo "Ref '$ref' was deleted"
349 git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
350 die "Could not delete $ref"
353 echo "Ref '$ref' was rewritten"
354 git update-ref -m "filter-branch: rewrite" \
355 "$ref" $rewritten $sha1 ||
356 die "Could not rewrite $ref"
359 # NEEDSWORK: possibly add -Werror, making this an error
360 warn "WARNING: '$ref' was rewritten into multiple commits:"
362 warn "WARNING: Ref '$ref' points to the first one now."
363 rewritten=$(echo "$rewritten" | head -n 1)
364 git update-ref -m "filter-branch: rewrite to first" \
365 "$ref" $rewritten $sha1 ||
366 die "Could not rewrite $ref"
369 git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1
371 done < "$tempdir"/heads
373 # TODO: This should possibly go, with the semantics that all positive given
374 # refs are updated, and their original heads stored in refs/original/
377 if [ "$filter_tag_name" ]; then
378 git for-each-ref --format='%(objectname
) %(objecttype
) %(refname
)' refs/tags |
379 while read sha1 type ref; do
380 ref="${ref#refs/tags/}"
381 # XXX: Rewrite tagged trees as well?
382 if [ "$type" != "commit" -a "$type" != "tag" ]; then
386 if [ "$type" = "tag" ]; then
387 # Dereference to a commit
389 sha1="$(git rev-parse "$sha1"^{commit} 2>/dev/null)" || continue
392 [ -f "../map/$sha1" ] || continue
393 new_sha1="$(cat "../map/$sha1")"
394 export GIT_COMMIT="$sha1"
395 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
396 die "tag name filter failed: $filter_tag_name"
398 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
400 if [ "$type" = "tag" ]; then
401 # Warn that we are not rewriting the tag object itself.
402 warn "unreferencing tag object $sha1t"
405 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
406 die "Could not write tag $new_ref"
413 test $count -gt 0 && echo "These refs were rewritten:"
414 git show-ref | grep ^"$orig_namespace"