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
="git-filter-branch [-d TEMPDIR] [FILTERS] [REV-RANGE]"
20 # if it was not rewritten, take the original
21 if test -r "$workdir/../map/$1"
23 cat "$workdir/../map/$1"
29 # override die(): this version puts in an extra line break, so that
30 # the progress is still visible
39 # When piped a commit, output a script to set the ident of either
40 # "author" or "committer
43 lid
="$(echo "$1" | tr "A-Z
" "a-z
")"
44 uid
="$(echo "$1" | tr "a-z
" "A-Z
")"
49 s/^'$lid' \([^<]*\) <[^>]*> .*$/\1/
51 s
/.
*/export GIT_
'$uid'_NAME
='\''&'\''/p
54 s
/^
'$lid' [^
<]* <\
([^
>]*\
)> .
*$
/\
1/
56 s/.*/export GIT_'$uid'_EMAIL='\''&'\''/p
59 s/^'$lid' [^<]* <[^>]*> \(.*\)$/\1/
61 s
/.
*/export GIT_
'$uid'_DATE
='\''&'\''/p
67 LANG=C LC_ALL=C sed -ne "$pick_id_script"
68 # Ensure non-empty id name.
69 echo "[ -n \"\$GIT_${uid}_NAME\" ] || export GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\""
78 filter_commit='git commit-tree
"$@"'
81 orig_namespace=refs/original/
83 while case "$#" in 0) usage;; esac
101 # all switches take one argument
103 case "$#" in 1) usage ;; esac
116 filter_tree="$OPTARG"
119 filter_index="$OPTARG"
122 filter_parent="$OPTARG"
128 filter_commit="$OPTARG"
131 filter_tag_name="$OPTARG"
133 --subdirectory-filter)
134 filter_subdir="$OPTARG"
137 orig_namespace="$OPTARG"
150 test -d "$tempdir" &&
151 die "$tempdir already exists, please remove it"
153 mkdir -p "$tempdir/t" &&
154 tempdir="$(cd "$tempdir"; pwd)" &&
159 # Make sure refs/original is empty
160 git for-each-ref > "$tempdir"/backup-refs
161 while read sha1 type name
163 case "$force,$name" in
165 die "Namespace $orig_namespace not empty"
168 git update-ref -d "$name" $sha1
171 done < "$tempdir"/backup-refs
173 export GIT_DIR GIT_WORK_TREE=.
175 # These refs should be updated if their heads were rewritten
177 git rev-parse --revs-only --symbolic "$@" |
183 ref="$(git symbolic-ref "$ref")"
188 ref="$(git for-each-ref --format='%(refname
)' |
192 git check-ref-format "$ref" && echo "$ref"
193 done > "$tempdir"/heads
195 test -s "$tempdir"/heads ||
196 die "Which ref do you want to rewrite?"
198 export GIT_INDEX_FILE="$(pwd)/../index"
199 git read-tree || die "Could not seed the index"
203 # map old->new commit ids for rewriting parents
204 mkdir ../map || die "Could not create map/ directory"
206 case "$filter_subdir" in
208 git rev-list --reverse --topo-order --default HEAD \
212 git rev-list --reverse --topo-order --default HEAD \
213 --parents --full-history "$@" -- "$filter_subdir"
214 esac > ../revs || die "Could not get the commits"
215 commits=$(wc -l <../revs | tr -d " ")
217 test $commits -eq 0 && die "Found nothing to rewrite"
219 # Rewrite the commits
222 while read commit parents; do
224 printf "\rRewrite $commit ($i/$commits)"
226 case "$filter_subdir" in
228 git read-tree -i -m $commit
231 git read-tree -i -m $commit:"$filter_subdir"
232 esac || die "Could not initialize the index"
234 export GIT_COMMIT=$commit
235 git cat-file commit "$commit" >../commit ||
236 die "Cannot read commit $commit"
238 eval "$(set_ident AUTHOR <../commit)" ||
239 die "setting author failed for commit $commit"
240 eval "$(set_ident COMMITTER <../commit)" ||
241 die "setting committer failed for commit $commit"
242 eval "$filter_env" < /dev/null ||
243 die "env filter failed: $filter_env"
245 if [ "$filter_tree" ]; then
246 git checkout-index -f -u -a ||
247 die "Could not checkout the index"
248 # files that $commit removed are now still in the working tree;
249 # remove them, else they would be added again
250 git ls-files -z --others | xargs -0 rm -f
251 eval "$filter_tree" < /dev/null ||
252 die "tree filter failed: $filter_tree"
254 git diff-index -r $commit | cut -f 2- | tr '\n' '\
0' | \
255 xargs -0 git update-index --add --replace --remove
256 git ls-files -z --others | \
257 xargs -0 git update-index --add --replace --remove
260 eval "$filter_index" < /dev/null ||
261 die "index filter failed: $filter_index"
264 for parent in $parents; do
265 for reparent in $(map "$parent"); do
266 parentstr="$parentstr -p $reparent"
269 if [ "$filter_parent" ]; then
270 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
271 die "parent filter failed: $filter_parent"
274 sed -e '1,/^$
/d
' <../commit | \
275 eval "$filter_msg" > ../message ||
276 die "msg filter failed: $filter_msg"
277 sh -c "$filter_commit" "git commit-tree" \
278 $(git write-tree) $parentstr < ../message > ../map/$commit
281 # In case of a subdirectory filter, it is possible that a specified head
282 # is not in the set of rewritten commits, because it was pruned by the
283 # revision walker. Fix it by mapping these heads to the next rewritten
284 # ancestor(s), i.e. the boundaries in the set of rewritten commits.
286 # NEEDSWORK: we should sort the unmapped refs topologically first
289 sha1=$(git rev-parse "$ref"^0)
290 test -f "$workdir"/../map/$sha1 && continue
291 # Assign the boundarie(s) in the set of rewritten commits
292 # as the replacement commit(s).
293 # (This would look a bit nicer if --not --stdin worked.)
294 for p in $( (cd "$workdir"/../map; ls | sed "s/^/^/") |
295 git rev-list $ref --boundary --stdin |
298 map $p >> "$workdir"/../map/$sha1
300 done < "$tempdir"/heads
302 # Finally update the refs
304 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
305 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
310 # avoid rewriting a ref twice
311 test -f "$orig_namespace$ref" && continue
313 sha1=$(git rev-parse "$ref"^0)
314 rewritten=$(map $sha1)
316 test $sha1 = "$rewritten" &&
317 warn "WARNING: Ref '$ref' is unchanged" &&
322 echo "Ref '$ref' was deleted"
323 git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
324 die "Could not delete $ref"
327 echo "Ref '$ref' was rewritten"
328 git update-ref -m "filter-branch: rewrite" \
329 "$ref" $rewritten $sha1 ||
330 die "Could not rewrite $ref"
333 # NEEDSWORK: possibly add -Werror, making this an error
334 warn "WARNING: '$ref' was rewritten into multiple commits:"
336 warn "WARNING: Ref '$ref' points to the first one now."
337 rewritten=$(echo "$rewritten" | head -n 1)
338 git update-ref -m "filter-branch: rewrite to first" \
339 "$ref" $rewritten $sha1 ||
340 die "Could not rewrite $ref"
343 git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1
345 done < "$tempdir"/heads
347 # TODO: This should possibly go, with the semantics that all positive given
348 # refs are updated, and their original heads stored in refs/original/
351 if [ "$filter_tag_name" ]; then
352 git for-each-ref --format='%(objectname
) %(objecttype
) %(refname
)' refs/tags |
353 while read sha1 type ref; do
354 ref="${ref#refs/tags/}"
355 # XXX: Rewrite tagged trees as well?
356 if [ "$type" != "commit" -a "$type" != "tag" ]; then
360 if [ "$type" = "tag" ]; then
361 # Dereference to a commit
363 sha1="$(git rev-parse "$sha1"^{commit} 2>/dev/null)" || continue
366 [ -f "../map/$sha1" ] || continue
367 new_sha1="$(cat "../map/$sha1")"
368 export GIT_COMMIT="$sha1"
369 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
370 die "tag name filter failed: $filter_tag_name"
372 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
374 if [ "$type" = "tag" ]; then
375 # Warn that we are not rewriting the tag object itself.
376 warn "unreferencing tag object $sha1t"
379 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
380 die "Could not write tag $new_ref"
387 test $count -gt 0 && echo "These refs were rewritten:"
388 git show-ref | grep ^"$orig_namespace"