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/
108 while case "$#" in 0) usage;; esac
126 # all switches take one argument
128 case "$#" in 1) usage ;; esac
141 filter_tree="$OPTARG"
144 filter_index="$OPTARG"
147 filter_parent="$OPTARG"
153 filter_commit='SOURCE_FUNCTIONS
=1 .
"$this_script";'" $OPTARG"
156 filter_tag_name="$OPTARG"
158 --subdirectory-filter)
159 filter_subdir="$OPTARG"
162 orig_namespace=$(expr "$OPTARG/" : '\
(.
*[^
/]\
)/*$
')/
175 test -d "$tempdir" &&
176 die "$tempdir already exists, please remove it"
178 mkdir -p "$tempdir/t" &&
179 tempdir="$(cd "$tempdir"; pwd)" &&
184 # Make sure refs/original is empty
185 git for-each-ref > "$tempdir"/backup-refs
186 while read sha1 type name
188 case "$force,$name" in
190 die "Namespace $orig_namespace not empty"
193 git update-ref -d "$name" $sha1
196 done < "$tempdir"/backup-refs
198 export GIT_DIR GIT_WORK_TREE=.
200 # These refs should be updated if their heads were rewritten
202 git rev-parse --revs-only --symbolic "$@" |
208 ref="$(git symbolic-ref "$ref")"
213 ref="$(git for-each-ref --format='%(refname
)' |
217 git check-ref-format "$ref" && echo "$ref"
218 done > "$tempdir"/heads
220 test -s "$tempdir"/heads ||
221 die "Which ref do you want to rewrite?"
223 export GIT_INDEX_FILE="$(pwd)/../index"
224 git read-tree || die "Could not seed the index"
228 # map old->new commit ids for rewriting parents
229 mkdir ../map || die "Could not create map/ directory"
231 case "$filter_subdir" in
233 git rev-list --reverse --topo-order --default HEAD \
237 git rev-list --reverse --topo-order --default HEAD \
238 --parents --full-history "$@" -- "$filter_subdir"
239 esac > ../revs || die "Could not get the commits"
240 commits=$(wc -l <../revs | tr -d " ")
242 test $commits -eq 0 && die "Found nothing to rewrite"
244 # Rewrite the commits
247 while read commit parents; do
249 printf "\rRewrite $commit ($i/$commits)"
251 case "$filter_subdir" in
253 git read-tree -i -m $commit
256 git read-tree -i -m $commit:"$filter_subdir"
257 esac || die "Could not initialize the index"
259 export GIT_COMMIT=$commit
260 git cat-file commit "$commit" >../commit ||
261 die "Cannot read commit $commit"
263 eval "$(set_ident AUTHOR <../commit)" ||
264 die "setting author failed for commit $commit"
265 eval "$(set_ident COMMITTER <../commit)" ||
266 die "setting committer failed for commit $commit"
267 eval "$filter_env" < /dev/null ||
268 die "env filter failed: $filter_env"
270 if [ "$filter_tree" ]; then
271 git checkout-index -f -u -a ||
272 die "Could not checkout the index"
273 # files that $commit removed are now still in the working tree;
274 # remove them, else they would be added again
275 git ls-files -z --others | xargs -0 rm -f
276 eval "$filter_tree" < /dev/null ||
277 die "tree filter failed: $filter_tree"
279 git diff-index -r $commit | cut -f 2- | tr '\n' '\
0' | \
280 xargs -0 git update-index --add --replace --remove
281 git ls-files -z --others | \
282 xargs -0 git update-index --add --replace --remove
285 eval "$filter_index" < /dev/null ||
286 die "index filter failed: $filter_index"
289 for parent in $parents; do
290 for reparent in $(map "$parent"); do
291 parentstr="$parentstr -p $reparent"
294 if [ "$filter_parent" ]; then
295 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
296 die "parent filter failed: $filter_parent"
299 sed -e '1,/^$
/d
' <../commit | \
300 eval "$filter_msg" > ../message ||
301 die "msg filter failed: $filter_msg"
302 sh -c "$filter_commit" "git commit-tree" \
303 $(git write-tree) $parentstr < ../message > ../map/$commit
306 # In case of a subdirectory filter, it is possible that a specified head
307 # is not in the set of rewritten commits, because it was pruned by the
308 # revision walker. Fix it by mapping these heads to the next rewritten
309 # ancestor(s), i.e. the boundaries in the set of rewritten commits.
311 # NEEDSWORK: we should sort the unmapped refs topologically first
314 sha1=$(git rev-parse "$ref"^0)
315 test -f "$workdir"/../map/$sha1 && continue
316 # Assign the boundarie(s) in the set of rewritten commits
317 # as the replacement commit(s).
318 # (This would look a bit nicer if --not --stdin worked.)
319 for p in $( (cd "$workdir"/../map; ls | sed "s/^/^/") |
320 git rev-list $ref --boundary --stdin |
323 map $p >> "$workdir"/../map/$sha1
325 done < "$tempdir"/heads
327 # Finally update the refs
329 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
330 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
335 # avoid rewriting a ref twice
336 test -f "$orig_namespace$ref" && continue
338 sha1=$(git rev-parse "$ref"^0)
339 rewritten=$(map $sha1)
341 test $sha1 = "$rewritten" &&
342 warn "WARNING: Ref '$ref' is unchanged" &&
347 echo "Ref '$ref' was deleted"
348 git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
349 die "Could not delete $ref"
352 echo "Ref '$ref' was rewritten"
353 git update-ref -m "filter-branch: rewrite" \
354 "$ref" $rewritten $sha1 ||
355 die "Could not rewrite $ref"
358 # NEEDSWORK: possibly add -Werror, making this an error
359 warn "WARNING: '$ref' was rewritten into multiple commits:"
361 warn "WARNING: Ref '$ref' points to the first one now."
362 rewritten=$(echo "$rewritten" | head -n 1)
363 git update-ref -m "filter-branch: rewrite to first" \
364 "$ref" $rewritten $sha1 ||
365 die "Could not rewrite $ref"
368 git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1
370 done < "$tempdir"/heads
372 # TODO: This should possibly go, with the semantics that all positive given
373 # refs are updated, and their original heads stored in refs/original/
376 if [ "$filter_tag_name" ]; then
377 git for-each-ref --format='%(objectname
) %(objecttype
) %(refname
)' refs/tags |
378 while read sha1 type ref; do
379 ref="${ref#refs/tags/}"
380 # XXX: Rewrite tagged trees as well?
381 if [ "$type" != "commit" -a "$type" != "tag" ]; then
385 if [ "$type" = "tag" ]; then
386 # Dereference to a commit
388 sha1="$(git rev-parse "$sha1"^{commit} 2>/dev/null)" || continue
391 [ -f "../map/$sha1" ] || continue
392 new_sha1="$(cat "../map/$sha1")"
393 export GIT_COMMIT="$sha1"
394 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
395 die "tag name filter failed: $filter_tag_name"
397 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
399 if [ "$type" = "tag" ]; then
400 # Warn that we are not rewriting the tag object itself.
401 warn "unreferencing tag object $sha1t"
404 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
405 die "Could not write tag $new_ref"
412 test $count -gt 0 && echo "These refs were rewritten:"
413 git show-ref | grep ^"$orig_namespace"