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>...]"
98 git diff-files --quiet &&
99 git diff-index --cached --quiet HEAD ||
100 die "Cannot rewrite branch(es) with a dirty working directory."
108 filter_commit='git commit-tree "$@"'
111 orig_namespace=refs/original/
132 # all switches take one argument
134 case "$#" in 1) usage ;; esac
147 filter_tree="$OPTARG"
150 filter_index="$OPTARG"
153 filter_parent="$OPTARG"
159 filter_commit='SOURCE_FUNCTIONS=1 . "$this_script";'" $OPTARG"
162 filter_tag_name="$OPTARG"
164 --subdirectory-filter)
165 filter_subdir="$OPTARG"
168 orig_namespace=$(expr "$OPTARG/" : '\(.*[^/]\)/*$')/
181 test -d "$tempdir" &&
182 die "$tempdir already exists, please remove it"
184 mkdir -p "$tempdir/t" &&
185 tempdir="$(cd "$tempdir"; pwd)" &&
190 # Make sure refs/original is empty
191 git for-each-ref > "$tempdir"/backup-refs
192 while read sha1 type name
194 case "$force,$name" in
196 die "Namespace $orig_namespace not empty"
199 git update-ref -d "$name" $sha1
202 done < "$tempdir"/backup-refs
204 ORIG_GIT_DIR="$GIT_DIR"
205 ORIG_GIT_WORK_TREE="$GIT_WORK_TREE"
206 ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE"
207 export GIT_DIR GIT_WORK_TREE=.
209 # These refs should be updated if their heads were rewritten
211 git rev-parse --revs-only --symbolic "$@" |
217 ref="$(git symbolic-ref "$ref")"
222 ref="$(git for-each-ref --format='%(refname)' |
226 git check-ref-format "$ref" && echo "$ref"
227 done > "$tempdir"/heads
229 test -s "$tempdir"/heads ||
230 die "Which ref do you want to rewrite?"
232 export GIT_INDEX_FILE="$(pwd)/../index"
233 git read-tree || die "Could not seed the index"
237 # map old->new commit ids for rewriting parents
238 mkdir ../map || die "Could not create map/ directory"
240 case "$filter_subdir" in
242 git rev-list --reverse --topo-order --default HEAD \
246 git rev-list --reverse --topo-order --default HEAD \
247 --parents --full-history "$@" -- "$filter_subdir"
248 esac > ../revs || die "Could not get the commits"
249 commits=$(wc -l <../revs | tr -d " ")
251 test $commits -eq 0 && die "Found nothing to rewrite"
253 # Rewrite the commits
256 while read commit parents; do
258 printf "\rRewrite $commit ($i/$commits)"
260 case "$filter_subdir" in
262 git read-tree -i -m $commit
265 git read-tree -i -m $commit:"$filter_subdir"
266 esac || die "Could not initialize the index"
268 export GIT_COMMIT=$commit
269 git cat-file commit "$commit" >../commit ||
270 die "Cannot read commit $commit"
272 eval "$(set_ident AUTHOR <../commit)" ||
273 die "setting author failed for commit $commit"
274 eval "$(set_ident COMMITTER <../commit)" ||
275 die "setting committer failed for commit $commit"
276 eval "$filter_env" < /dev/null ||
277 die "env filter failed: $filter_env"
279 if [ "$filter_tree" ]; then
280 git checkout-index -f -u -a ||
281 die "Could not checkout the index"
282 # files that $commit removed are now still in the working tree;
283 # remove them, else they would be added again
284 git ls-files -z --others | xargs -0 rm -f
285 eval "$filter_tree" < /dev/null ||
286 die "tree filter failed: $filter_tree"
288 git diff-index -r $commit | cut -f 2- | tr '\n' '\0' | \
289 xargs -0 git update-index --add --replace --remove
290 git ls-files -z --others | \
291 xargs -0 git update-index --add --replace --remove
294 eval "$filter_index" < /dev/null ||
295 die "index filter failed: $filter_index"
298 for parent in $parents; do
299 for reparent in $(map "$parent"); do
300 parentstr="$parentstr -p $reparent"
303 if [ "$filter_parent" ]; then
304 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
305 die "parent filter failed: $filter_parent"
308 sed -e '1,/^$/d' <../commit | \
309 eval "$filter_msg" > ../message ||
310 die "msg filter failed: $filter_msg"
311 sh -c "$filter_commit" "git commit-tree" \
312 $(git write-tree) $parentstr < ../message > ../map/$commit
315 # In case of a subdirectory filter, it is possible that a specified head
316 # is not in the set of rewritten commits, because it was pruned by the
317 # revision walker. Fix it by mapping these heads to the next rewritten
318 # ancestor(s), i.e. the boundaries in the set of rewritten commits.
320 # NEEDSWORK: we should sort the unmapped refs topologically first
323 sha1=$(git rev-parse "$ref"^0)
324 test -f "$workdir"/../map/$sha1 && continue
325 # Assign the boundarie(s) in the set of rewritten commits
326 # as the replacement commit(s).
327 # (This would look a bit nicer if --not --stdin worked.)
328 for p in $( (cd "$workdir"/../map; ls | sed "s/^/^/") |
329 git rev-list $ref --boundary --stdin |
332 map $p >> "$workdir"/../map/$sha1
334 done < "$tempdir"/heads
336 # Finally update the refs
338 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
339 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
344 # avoid rewriting a ref twice
345 test -f "$orig_namespace$ref" && continue
347 sha1=$(git rev-parse "$ref"^0)
348 rewritten=$(map $sha1)
350 test $sha1 = "$rewritten" &&
351 warn "WARNING: Ref '$ref' is unchanged" &&
356 echo "Ref '$ref' was deleted"
357 git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
358 die "Could not delete $ref"
361 echo "Ref '$ref' was rewritten"
362 git update-ref -m "filter-branch: rewrite" \
363 "$ref" $rewritten $sha1 ||
364 die "Could not rewrite $ref"
367 # NEEDSWORK: possibly add -Werror, making this an error
368 warn "WARNING: '$ref' was rewritten into multiple commits:"
370 warn "WARNING: Ref '$ref' points to the first one now."
371 rewritten=$(echo "$rewritten" | head -n 1)
372 git update-ref -m "filter-branch: rewrite to first" \
373 "$ref" $rewritten $sha1 ||
374 die "Could not rewrite $ref"
377 git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1
379 done < "$tempdir"/heads
381 # TODO: This should possibly go, with the semantics that all positive given
382 # refs are updated, and their original heads stored in refs/original/
385 if [ "$filter_tag_name" ]; then
386 git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags |
387 while read sha1 type ref; do
388 ref="${ref#refs/tags/}"
389 # XXX: Rewrite tagged trees as well?
390 if [ "$type" != "commit" -a "$type" != "tag" ]; then
394 if [ "$type" = "tag" ]; then
395 # Dereference to a commit
397 sha1="$(git rev-parse "$sha1"^{commit} 2>/dev/null)" || continue
400 [ -f "../map/$sha1" ] || continue
401 new_sha1="$(cat "../map/$sha1")"
402 export GIT_COMMIT="$sha1"
403 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
404 die "tag name filter failed: $filter_tag_name"
406 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
408 if [ "$type" = "tag" ]; then
409 # Warn that we are not rewriting the tag object itself.
410 warn "unreferencing tag object $sha1t"
413 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
414 die "Could not write tag $new_ref"
421 test $count -gt 0 && echo "These refs were rewritten:"
422 git show-ref | grep ^"$orig_namespace"
424 unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
425 test -z "$ORIG_GIT_DIR" || GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR
426 test -z "$ORIG_GIT_WORK_TREE" || GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" &&
428 test -z "$ORIG_GIT_INDEX_FILE" || GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" &&
429 export GIT_INDEX_FILE
430 git read-tree -u -m HEAD