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 # override die(): this version puts in an extra line break, so that
27 # the progress is still visible
36 # When piped a commit, output a script to set the ident of either
37 # "author" or "committer
40 lid
="$(echo "$1" | tr "A-Z
" "a-z
")"
41 uid
="$(echo "$1" | tr "a-z
" "A-Z
")"
46 s/^'$lid' \([^<]*\) <[^>]*> .*$/\1/
48 s
/.
*/export GIT_
'$uid'_NAME
='\''&'\''/p
51 s
/^
'$lid' [^
<]* <\
([^
>]*\
)> .
*$
/\
1/
53 s/.*/export GIT_'$uid'_EMAIL='\''&'\''/p
56 s/^'$lid' [^<]* <[^>]*> \(.*\)$/\1/
58 s
/.
*/export GIT_
'$uid'_DATE
='\''&'\''/p
64 LANG=C LC_ALL=C sed -ne "$pick_id_script"
65 # Ensure non-empty id name.
66 echo "[ -n \"\$GIT_${uid}_NAME\" ] || export GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\""
69 # This script can be sourced by the commit filter to get the functions
70 test "a$SOURCE_FUNCTIONS" = a1 && return
71 this_script="$(cd "$(dirname "$0")"; pwd)"/$(basename "$0")
74 USAGE="[--env-filter <command>] [--tree-filter <command>] \
75 [--index-filter <command>] [--parent-filter <command>] \
76 [--msg-filter <command>] [--commit-filter <command>] \
77 [--tag-name-filter <command>] [--subdirectory-filter <directory>] \
78 [--original <namespace>] [-d <directory>] [-f | --force] \
79 [<rev-list options>...]"
89 filter_commit='git commit-tree
"$@"'
92 orig_namespace=refs/original/
94 while case "$#" in 0) usage;; esac
112 # all switches take one argument
114 case "$#" in 1) usage ;; esac
127 filter_tree="$OPTARG"
130 filter_index="$OPTARG"
133 filter_parent="$OPTARG"
139 filter_commit='SOURCE_FUNCTIONS
=1 .
"$this_script";'" $OPTARG"
142 filter_tag_name="$OPTARG"
144 --subdirectory-filter)
145 filter_subdir="$OPTARG"
148 orig_namespace=$(expr "$OPTARG/" : '\
(.
*[^
/]\
)/*$
')/
161 test -d "$tempdir" &&
162 die "$tempdir already exists, please remove it"
164 mkdir -p "$tempdir/t" &&
165 tempdir="$(cd "$tempdir"; pwd)" &&
170 # Make sure refs/original is empty
171 git for-each-ref > "$tempdir"/backup-refs
172 while read sha1 type name
174 case "$force,$name" in
176 die "Namespace $orig_namespace not empty"
179 git update-ref -d "$name" $sha1
182 done < "$tempdir"/backup-refs
184 export GIT_DIR GIT_WORK_TREE=.
186 # These refs should be updated if their heads were rewritten
188 git rev-parse --revs-only --symbolic "$@" |
194 ref="$(git symbolic-ref "$ref")"
199 ref="$(git for-each-ref --format='%(refname
)' |
203 git check-ref-format "$ref" && echo "$ref"
204 done > "$tempdir"/heads
206 test -s "$tempdir"/heads ||
207 die "Which ref do you want to rewrite?"
209 export GIT_INDEX_FILE="$(pwd)/../index"
210 git read-tree || die "Could not seed the index"
214 # map old->new commit ids for rewriting parents
215 mkdir ../map || die "Could not create map/ directory"
217 case "$filter_subdir" in
219 git rev-list --reverse --topo-order --default HEAD \
223 git rev-list --reverse --topo-order --default HEAD \
224 --parents --full-history "$@" -- "$filter_subdir"
225 esac > ../revs || die "Could not get the commits"
226 commits=$(wc -l <../revs | tr -d " ")
228 test $commits -eq 0 && die "Found nothing to rewrite"
230 # Rewrite the commits
233 while read commit parents; do
235 printf "\rRewrite $commit ($i/$commits)"
237 case "$filter_subdir" in
239 git read-tree -i -m $commit
242 git read-tree -i -m $commit:"$filter_subdir"
243 esac || die "Could not initialize the index"
245 export GIT_COMMIT=$commit
246 git cat-file commit "$commit" >../commit ||
247 die "Cannot read commit $commit"
249 eval "$(set_ident AUTHOR <../commit)" ||
250 die "setting author failed for commit $commit"
251 eval "$(set_ident COMMITTER <../commit)" ||
252 die "setting committer failed for commit $commit"
253 eval "$filter_env" < /dev/null ||
254 die "env filter failed: $filter_env"
256 if [ "$filter_tree" ]; then
257 git checkout-index -f -u -a ||
258 die "Could not checkout the index"
259 # files that $commit removed are now still in the working tree;
260 # remove them, else they would be added again
261 git ls-files -z --others | xargs -0 rm -f
262 eval "$filter_tree" < /dev/null ||
263 die "tree filter failed: $filter_tree"
265 git diff-index -r $commit | cut -f 2- | tr '\n' '\
0' | \
266 xargs -0 git update-index --add --replace --remove
267 git ls-files -z --others | \
268 xargs -0 git update-index --add --replace --remove
271 eval "$filter_index" < /dev/null ||
272 die "index filter failed: $filter_index"
275 for parent in $parents; do
276 for reparent in $(map "$parent"); do
277 parentstr="$parentstr -p $reparent"
280 if [ "$filter_parent" ]; then
281 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
282 die "parent filter failed: $filter_parent"
285 sed -e '1,/^$
/d
' <../commit | \
286 eval "$filter_msg" > ../message ||
287 die "msg filter failed: $filter_msg"
288 sh -c "$filter_commit" "git commit-tree" \
289 $(git write-tree) $parentstr < ../message > ../map/$commit
292 # In case of a subdirectory filter, it is possible that a specified head
293 # is not in the set of rewritten commits, because it was pruned by the
294 # revision walker. Fix it by mapping these heads to the next rewritten
295 # ancestor(s), i.e. the boundaries in the set of rewritten commits.
297 # NEEDSWORK: we should sort the unmapped refs topologically first
300 sha1=$(git rev-parse "$ref"^0)
301 test -f "$workdir"/../map/$sha1 && continue
302 # Assign the boundarie(s) in the set of rewritten commits
303 # as the replacement commit(s).
304 # (This would look a bit nicer if --not --stdin worked.)
305 for p in $( (cd "$workdir"/../map; ls | sed "s/^/^/") |
306 git rev-list $ref --boundary --stdin |
309 map $p >> "$workdir"/../map/$sha1
311 done < "$tempdir"/heads
313 # Finally update the refs
315 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
316 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
321 # avoid rewriting a ref twice
322 test -f "$orig_namespace$ref" && continue
324 sha1=$(git rev-parse "$ref"^0)
325 rewritten=$(map $sha1)
327 test $sha1 = "$rewritten" &&
328 warn "WARNING: Ref '$ref' is unchanged" &&
333 echo "Ref '$ref' was deleted"
334 git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
335 die "Could not delete $ref"
338 echo "Ref '$ref' was rewritten"
339 git update-ref -m "filter-branch: rewrite" \
340 "$ref" $rewritten $sha1 ||
341 die "Could not rewrite $ref"
344 # NEEDSWORK: possibly add -Werror, making this an error
345 warn "WARNING: '$ref' was rewritten into multiple commits:"
347 warn "WARNING: Ref '$ref' points to the first one now."
348 rewritten=$(echo "$rewritten" | head -n 1)
349 git update-ref -m "filter-branch: rewrite to first" \
350 "$ref" $rewritten $sha1 ||
351 die "Could not rewrite $ref"
354 git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1
356 done < "$tempdir"/heads
358 # TODO: This should possibly go, with the semantics that all positive given
359 # refs are updated, and their original heads stored in refs/original/
362 if [ "$filter_tag_name" ]; then
363 git for-each-ref --format='%(objectname
) %(objecttype
) %(refname
)' refs/tags |
364 while read sha1 type ref; do
365 ref="${ref#refs/tags/}"
366 # XXX: Rewrite tagged trees as well?
367 if [ "$type" != "commit" -a "$type" != "tag" ]; then
371 if [ "$type" = "tag" ]; then
372 # Dereference to a commit
374 sha1="$(git rev-parse "$sha1"^{commit} 2>/dev/null)" || continue
377 [ -f "../map/$sha1" ] || continue
378 new_sha1="$(cat "../map/$sha1")"
379 export GIT_COMMIT="$sha1"
380 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
381 die "tag name filter failed: $filter_tag_name"
383 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
385 if [ "$type" = "tag" ]; then
386 # Warn that we are not rewriting the tag object itself.
387 warn "unreferencing tag object $sha1t"
390 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
391 die "Could not write tag $new_ref"
398 test $count -gt 0 && echo "These refs were rewritten:"
399 git show-ref | grep ^"$orig_namespace"