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 # The following functions will also be available in the commit filter:
13 functions=$(cat << \EOF
20 # if it was not rewritten, take the original
21 if test -r "$workdir/../map/$1"
23 cat "$workdir/../map/$1"
29 # if you run 'skip_commit "$@"' in a commit filter, it will print
30 # the (mapped) parents, effectively skipping the commit.
43 # override die(): this version puts in an extra line break, so that
44 # the progress is still visible
57 # When piped a commit, output a script to set the ident of either
58 # "author" or "committer
61 lid="$(echo "$1" | tr "A-Z" "a-z")"
62 uid="$(echo "$1" | tr "a-z" "A-Z")"
67 s/^'$lid' \([^<]*\) <[^>]*> .*$/\1/
69 s/.*/export GIT_'$uid'_NAME='\''&'\''/p
72 s/^'$lid' [^<]* <\([^>]*\)> .*$/\1/
74 s/.*/export GIT_'$uid'_EMAIL='\''&'\''/p
77 s/^'$lid' [^<]* <[^>]*> \(.*\)$/\1/
79 s/.*/export GIT_'$uid'_DATE='\''&'\''/p
85 LANG=C LC_ALL=C sed -ne "$pick_id_script"
86 # Ensure non-empty id name.
87 echo "[ -n \"\$GIT_${uid}_NAME\" ] || export GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\""
90 USAGE="[--env-filter <command>] [--tree-filter <command>] \
91 [--index-filter <command>] [--parent-filter <command>] \
92 [--msg-filter <command>] [--commit-filter <command>] \
93 [--tag-name-filter <command>] [--subdirectory-filter <directory>] \
94 [--original <namespace>] [-d <directory>] [-f | --force] \
95 [<rev-list options>...]"
100 git diff-files --quiet &&
101 git diff-index --cached --quiet HEAD -- ||
102 die "Cannot rewrite branch(es) with a dirty working directory."
110 filter_commit='git commit-tree "$@"'
113 orig_namespace=refs/original/
134 # all switches take one argument
136 case "$#" in 1) usage ;; esac
149 filter_tree="$OPTARG"
152 filter_index="$OPTARG"
155 filter_parent="$OPTARG"
161 filter_commit="$functions; $OPTARG"
164 filter_tag_name="$OPTARG"
166 --subdirectory-filter)
167 filter_subdir="$OPTARG"
170 orig_namespace=$(expr "$OPTARG/" : '\(.*[^/]\)/*$')/
183 test -d "$tempdir" &&
184 die "$tempdir already exists, please remove it"
186 mkdir -p "$tempdir/t" &&
187 tempdir="$(cd "$tempdir"; pwd)" &&
192 # Make sure refs/original is empty
193 git for-each-ref > "$tempdir"/backup-refs
194 while read sha1 type name
196 case "$force,$name" in
198 die "Namespace $orig_namespace not empty"
201 git update-ref -d "$name" $sha1
204 done < "$tempdir"/backup-refs
206 ORIG_GIT_DIR="$GIT_DIR"
207 ORIG_GIT_WORK_TREE="$GIT_WORK_TREE"
208 ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE"
209 export GIT_DIR GIT_WORK_TREE=.
211 # These refs should be updated if their heads were rewritten
213 git rev-parse --revs-only --symbolic "$@" |
219 ref="$(git symbolic-ref "$ref")"
224 ref="$(git for-each-ref --format='%(refname)' |
228 git check-ref-format "$ref" && echo "$ref"
229 done > "$tempdir"/heads
231 test -s "$tempdir"/heads ||
232 die "Which ref do you want to rewrite?"
234 export GIT_INDEX_FILE="$(pwd)/../index"
235 git read-tree || die "Could not seed the index"
239 # map old->new commit ids for rewriting parents
240 mkdir ../map || die "Could not create map/ directory"
242 case "$filter_subdir" in
244 git rev-list --reverse --topo-order --default HEAD \
248 git rev-list --reverse --topo-order --default HEAD \
249 --parents --full-history "$@" -- "$filter_subdir"
250 esac > ../revs || die "Could not get the commits"
251 commits=$(wc -l <../revs | tr -d " ")
253 test $commits -eq 0 && die "Found nothing to rewrite"
255 # Rewrite the commits
258 while read commit parents; do
260 printf "\rRewrite $commit ($i/$commits)"
262 case "$filter_subdir" in
264 git read-tree -i -m $commit
267 git read-tree -i -m $commit:"$filter_subdir"
268 esac || die "Could not initialize the index"
270 export GIT_COMMIT=$commit
271 git cat-file commit "$commit" >../commit ||
272 die "Cannot read commit $commit"
274 eval "$(set_ident AUTHOR <../commit)" ||
275 die "setting author failed for commit $commit"
276 eval "$(set_ident COMMITTER <../commit)" ||
277 die "setting committer failed for commit $commit"
278 eval "$filter_env" < /dev/null ||
279 die "env filter failed: $filter_env"
281 if [ "$filter_tree" ]; then
282 git checkout-index -f -u -a ||
283 die "Could not checkout the index"
284 # files that $commit removed are now still in the working tree;
285 # remove them, else they would be added again
286 git ls-files -z --others | xargs -0 rm -f
287 eval "$filter_tree" < /dev/null ||
288 die "tree filter failed: $filter_tree"
290 git diff-index -r $commit | cut -f 2- | tr '\n' '\0' | \
291 xargs -0 git update-index --add --replace --remove
292 git ls-files -z --others | \
293 xargs -0 git update-index --add --replace --remove
296 eval "$filter_index" < /dev/null ||
297 die "index filter failed: $filter_index"
300 for parent in $parents; do
301 for reparent in $(map "$parent"); do
302 parentstr="$parentstr -p $reparent"
305 if [ "$filter_parent" ]; then
306 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
307 die "parent filter failed: $filter_parent"
310 sed -e '1,/^$/d' <../commit | \
311 eval "$filter_msg" > ../message ||
312 die "msg filter failed: $filter_msg"
313 sh -c "$filter_commit" "git commit-tree" \
314 $(git write-tree) $parentstr < ../message > ../map/$commit
317 # In case of a subdirectory filter, it is possible that a specified head
318 # is not in the set of rewritten commits, because it was pruned by the
319 # revision walker. Fix it by mapping these heads to the next rewritten
320 # ancestor(s), i.e. the boundaries in the set of rewritten commits.
322 # NEEDSWORK: we should sort the unmapped refs topologically first
325 sha1=$(git rev-parse "$ref"^0)
326 test -f "$workdir"/../map/$sha1 && continue
327 # Assign the boundarie(s) in the set of rewritten commits
328 # as the replacement commit(s).
329 # (This would look a bit nicer if --not --stdin worked.)
330 for p in $( (cd "$workdir"/../map; ls | sed "s/^/^/") |
331 git rev-list $ref --boundary --stdin |
334 map $p >> "$workdir"/../map/$sha1
336 done < "$tempdir"/heads
338 # Finally update the refs
340 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
341 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
346 # avoid rewriting a ref twice
347 test -f "$orig_namespace$ref" && continue
349 sha1=$(git rev-parse "$ref"^0)
350 rewritten=$(map $sha1)
352 test $sha1 = "$rewritten" &&
353 warn "WARNING: Ref '$ref' is unchanged" &&
358 echo "Ref '$ref' was deleted"
359 git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
360 die "Could not delete $ref"
363 echo "Ref '$ref' was rewritten"
364 git update-ref -m "filter-branch: rewrite" \
365 "$ref" $rewritten $sha1 ||
366 die "Could not rewrite $ref"
369 # NEEDSWORK: possibly add -Werror, making this an error
370 warn "WARNING: '$ref' was rewritten into multiple commits:"
372 warn "WARNING: Ref '$ref' points to the first one now."
373 rewritten=$(echo "$rewritten" | head -n 1)
374 git update-ref -m "filter-branch: rewrite to first" \
375 "$ref" $rewritten $sha1 ||
376 die "Could not rewrite $ref"
379 git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1
381 done < "$tempdir"/heads
383 # TODO: This should possibly go, with the semantics that all positive given
384 # refs are updated, and their original heads stored in refs/original/
387 if [ "$filter_tag_name" ]; then
388 git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags |
389 while read sha1 type ref; do
390 ref="${ref#refs/tags/}"
391 # XXX: Rewrite tagged trees as well?
392 if [ "$type" != "commit" -a "$type" != "tag" ]; then
396 if [ "$type" = "tag" ]; then
397 # Dereference to a commit
399 sha1="$(git rev-parse "$sha1"^{commit} 2>/dev/null)" || continue
402 [ -f "../map/$sha1" ] || continue
403 new_sha1="$(cat "../map/$sha1")"
404 export GIT_COMMIT="$sha1"
405 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
406 die "tag name filter failed: $filter_tag_name"
408 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
410 if [ "$type" = "tag" ]; then
411 # Warn that we are not rewriting the tag object itself.
412 warn "unreferencing tag object $sha1t"
415 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
416 die "Could not write tag $new_ref"
423 test $count -gt 0 && echo "These refs were rewritten:"
424 git show-ref | grep ^"$orig_namespace"
426 unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
427 test -z "$ORIG_GIT_DIR" || GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR
428 test -z "$ORIG_GIT_WORK_TREE" || GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" &&
430 test -z "$ORIG_GIT_INDEX_FILE" || GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" &&
431 export GIT_INDEX_FILE
432 git read-tree -u -m HEAD