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 USAGE
="[--env-filter <command>] [--tree-filter <command>] \
12 [--index-filter <command>] [--parent-filter <command>] \
13 [--msg-filter <command>] [--commit-filter <command>] \
14 [--tag-name-filter <command>] [--subdirectory-filter <directory>] \
15 [--original <namespace>] [-d <directory>] [-f | --force] \
16 [<rev-list options>...]"
26 # if it was not rewritten, take the original
27 if test -r "$workdir/../map/$1"
29 cat "$workdir/../map/$1"
35 # override die(): this version puts in an extra line break, so that
36 # the progress is still visible
45 # When piped a commit, output a script to set the ident of either
46 # "author" or "committer
49 lid
="$(echo "$1" | tr "A-Z
" "a-z
")"
50 uid
="$(echo "$1" | tr "a-z
" "A-Z
")"
55 s/^'$lid' \([^<]*\) <[^>]*> .*$/\1/
57 s
/.
*/export GIT_
'$uid'_NAME
='\''&'\''/p
60 s
/^
'$lid' [^
<]* <\
([^
>]*\
)> .
*$
/\
1/
62 s/.*/export GIT_'$uid'_EMAIL='\''&'\''/p
65 s/^'$lid' [^<]* <[^>]*> \(.*\)$/\1/
67 s
/.
*/export GIT_
'$uid'_DATE
='\''&'\''/p
73 LANG=C LC_ALL=C sed -ne "$pick_id_script"
74 # Ensure non-empty id name.
75 echo "[ -n \"\$GIT_${uid}_NAME\" ] || export GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\""
84 filter_commit='git commit-tree
"$@"'
87 orig_namespace=refs/original/
89 while case "$#" in 0) usage;; esac
107 # all switches take one argument
109 case "$#" in 1) usage ;; esac
122 filter_tree="$OPTARG"
125 filter_index="$OPTARG"
128 filter_parent="$OPTARG"
134 filter_commit="$OPTARG"
137 filter_tag_name="$OPTARG"
139 --subdirectory-filter)
140 filter_subdir="$OPTARG"
143 orig_namespace="$OPTARG"
156 test -d "$tempdir" &&
157 die "$tempdir already exists, please remove it"
159 mkdir -p "$tempdir/t" &&
160 tempdir="$(cd "$tempdir"; pwd)" &&
165 # Make sure refs/original is empty
166 git for-each-ref > "$tempdir"/backup-refs
167 while read sha1 type name
169 case "$force,$name" in
171 die "Namespace $orig_namespace not empty"
174 git update-ref -d "$name" $sha1
177 done < "$tempdir"/backup-refs
179 export GIT_DIR GIT_WORK_TREE=.
181 # These refs should be updated if their heads were rewritten
183 git rev-parse --revs-only --symbolic "$@" |
189 ref="$(git symbolic-ref "$ref")"
194 ref="$(git for-each-ref --format='%(refname
)' |
198 git check-ref-format "$ref" && echo "$ref"
199 done > "$tempdir"/heads
201 test -s "$tempdir"/heads ||
202 die "Which ref do you want to rewrite?"
204 export GIT_INDEX_FILE="$(pwd)/../index"
205 git read-tree || die "Could not seed the index"
209 # map old->new commit ids for rewriting parents
210 mkdir ../map || die "Could not create map/ directory"
212 case "$filter_subdir" in
214 git rev-list --reverse --topo-order --default HEAD \
218 git rev-list --reverse --topo-order --default HEAD \
219 --parents --full-history "$@" -- "$filter_subdir"
220 esac > ../revs || die "Could not get the commits"
221 commits=$(wc -l <../revs | tr -d " ")
223 test $commits -eq 0 && die "Found nothing to rewrite"
225 # Rewrite the commits
228 while read commit parents; do
230 printf "\rRewrite $commit ($i/$commits)"
232 case "$filter_subdir" in
234 git read-tree -i -m $commit
237 git read-tree -i -m $commit:"$filter_subdir"
238 esac || die "Could not initialize the index"
240 export GIT_COMMIT=$commit
241 git cat-file commit "$commit" >../commit ||
242 die "Cannot read commit $commit"
244 eval "$(set_ident AUTHOR <../commit)" ||
245 die "setting author failed for commit $commit"
246 eval "$(set_ident COMMITTER <../commit)" ||
247 die "setting committer failed for commit $commit"
248 eval "$filter_env" < /dev/null ||
249 die "env filter failed: $filter_env"
251 if [ "$filter_tree" ]; then
252 git checkout-index -f -u -a ||
253 die "Could not checkout the index"
254 # files that $commit removed are now still in the working tree;
255 # remove them, else they would be added again
256 git ls-files -z --others | xargs -0 rm -f
257 eval "$filter_tree" < /dev/null ||
258 die "tree filter failed: $filter_tree"
260 git diff-index -r $commit | cut -f 2- | tr '\n' '\
0' | \
261 xargs -0 git update-index --add --replace --remove
262 git ls-files -z --others | \
263 xargs -0 git update-index --add --replace --remove
266 eval "$filter_index" < /dev/null ||
267 die "index filter failed: $filter_index"
270 for parent in $parents; do
271 for reparent in $(map "$parent"); do
272 parentstr="$parentstr -p $reparent"
275 if [ "$filter_parent" ]; then
276 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
277 die "parent filter failed: $filter_parent"
280 sed -e '1,/^$
/d
' <../commit | \
281 eval "$filter_msg" > ../message ||
282 die "msg filter failed: $filter_msg"
283 sh -c "$filter_commit" "git commit-tree" \
284 $(git write-tree) $parentstr < ../message > ../map/$commit
287 # In case of a subdirectory filter, it is possible that a specified head
288 # is not in the set of rewritten commits, because it was pruned by the
289 # revision walker. Fix it by mapping these heads to the next rewritten
290 # ancestor(s), i.e. the boundaries in the set of rewritten commits.
292 # NEEDSWORK: we should sort the unmapped refs topologically first
295 sha1=$(git rev-parse "$ref"^0)
296 test -f "$workdir"/../map/$sha1 && continue
297 # Assign the boundarie(s) in the set of rewritten commits
298 # as the replacement commit(s).
299 # (This would look a bit nicer if --not --stdin worked.)
300 for p in $( (cd "$workdir"/../map; ls | sed "s/^/^/") |
301 git rev-list $ref --boundary --stdin |
304 map $p >> "$workdir"/../map/$sha1
306 done < "$tempdir"/heads
308 # Finally update the refs
310 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
311 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
316 # avoid rewriting a ref twice
317 test -f "$orig_namespace$ref" && continue
319 sha1=$(git rev-parse "$ref"^0)
320 rewritten=$(map $sha1)
322 test $sha1 = "$rewritten" &&
323 warn "WARNING: Ref '$ref' is unchanged" &&
328 echo "Ref '$ref' was deleted"
329 git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
330 die "Could not delete $ref"
333 echo "Ref '$ref' was rewritten"
334 git update-ref -m "filter-branch: rewrite" \
335 "$ref" $rewritten $sha1 ||
336 die "Could not rewrite $ref"
339 # NEEDSWORK: possibly add -Werror, making this an error
340 warn "WARNING: '$ref' was rewritten into multiple commits:"
342 warn "WARNING: Ref '$ref' points to the first one now."
343 rewritten=$(echo "$rewritten" | head -n 1)
344 git update-ref -m "filter-branch: rewrite to first" \
345 "$ref" $rewritten $sha1 ||
346 die "Could not rewrite $ref"
349 git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1
351 done < "$tempdir"/heads
353 # TODO: This should possibly go, with the semantics that all positive given
354 # refs are updated, and their original heads stored in refs/original/
357 if [ "$filter_tag_name" ]; then
358 git for-each-ref --format='%(objectname
) %(objecttype
) %(refname
)' refs/tags |
359 while read sha1 type ref; do
360 ref="${ref#refs/tags/}"
361 # XXX: Rewrite tagged trees as well?
362 if [ "$type" != "commit" -a "$type" != "tag" ]; then
366 if [ "$type" = "tag" ]; then
367 # Dereference to a commit
369 sha1="$(git rev-parse "$sha1"^{commit} 2>/dev/null)" || continue
372 [ -f "../map/$sha1" ] || continue
373 new_sha1="$(cat "../map/$sha1")"
374 export GIT_COMMIT="$sha1"
375 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
376 die "tag name filter failed: $filter_tag_name"
378 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
380 if [ "$type" = "tag" ]; then
381 # Warn that we are not rewriting the tag object itself.
382 warn "unreferencing tag object $sha1t"
385 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
386 die "Could not write tag $new_ref"
393 test $count -gt 0 && echo "These refs were rewritten:"
394 git show-ref | grep ^"$orig_namespace"