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>...]"
97 git diff-files --quiet &&
98 git diff-index --cached --quiet HEAD ||
99 die "Cannot rewrite branch(es) with a dirty working directory."
107 filter_commit='git commit-tree
"$@"'
110 orig_namespace=refs/original/
131 # all switches take one argument
133 case "$#" in 1) usage ;; esac
146 filter_tree="$OPTARG"
149 filter_index="$OPTARG"
152 filter_parent="$OPTARG"
158 filter_commit='SOURCE_FUNCTIONS
=1 .
"$this_script";'" $OPTARG"
161 filter_tag_name="$OPTARG"
163 --subdirectory-filter)
164 filter_subdir="$OPTARG"
167 orig_namespace=$(expr "$OPTARG/" : '\
(.
*[^
/]\
)/*$
')/
180 test -d "$tempdir" &&
181 die "$tempdir already exists, please remove it"
183 mkdir -p "$tempdir/t" &&
184 tempdir="$(cd "$tempdir"; pwd)" &&
189 # Make sure refs/original is empty
190 git for-each-ref > "$tempdir"/backup-refs
191 while read sha1 type name
193 case "$force,$name" in
195 die "Namespace $orig_namespace not empty"
198 git update-ref -d "$name" $sha1
201 done < "$tempdir"/backup-refs
203 ORIG_GIT_DIR="$GIT_DIR"
204 ORIG_GIT_WORK_TREE="$GIT_WORK_TREE"
205 ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE"
206 export GIT_DIR GIT_WORK_TREE=.
208 # These refs should be updated if their heads were rewritten
210 git rev-parse --revs-only --symbolic "$@" |
216 ref="$(git symbolic-ref "$ref")"
221 ref="$(git for-each-ref --format='%(refname
)' |
225 git check-ref-format "$ref" && echo "$ref"
226 done > "$tempdir"/heads
228 test -s "$tempdir"/heads ||
229 die "Which ref do you want to rewrite?"
231 export GIT_INDEX_FILE="$(pwd)/../index"
232 git read-tree || die "Could not seed the index"
236 # map old->new commit ids for rewriting parents
237 mkdir ../map || die "Could not create map/ directory"
239 case "$filter_subdir" in
241 git rev-list --reverse --topo-order --default HEAD \
245 git rev-list --reverse --topo-order --default HEAD \
246 --parents --full-history "$@" -- "$filter_subdir"
247 esac > ../revs || die "Could not get the commits"
248 commits=$(wc -l <../revs | tr -d " ")
250 test $commits -eq 0 && die "Found nothing to rewrite"
252 # Rewrite the commits
255 while read commit parents; do
257 printf "\rRewrite $commit ($i/$commits)"
259 case "$filter_subdir" in
261 git read-tree -i -m $commit
264 git read-tree -i -m $commit:"$filter_subdir"
265 esac || die "Could not initialize the index"
267 export GIT_COMMIT=$commit
268 git cat-file commit "$commit" >../commit ||
269 die "Cannot read commit $commit"
271 eval "$(set_ident AUTHOR <../commit)" ||
272 die "setting author failed for commit $commit"
273 eval "$(set_ident COMMITTER <../commit)" ||
274 die "setting committer failed for commit $commit"
275 eval "$filter_env" < /dev/null ||
276 die "env filter failed: $filter_env"
278 if [ "$filter_tree" ]; then
279 git checkout-index -f -u -a ||
280 die "Could not checkout the index"
281 # files that $commit removed are now still in the working tree;
282 # remove them, else they would be added again
283 git ls-files -z --others | xargs -0 rm -f
284 eval "$filter_tree" < /dev/null ||
285 die "tree filter failed: $filter_tree"
287 git diff-index -r $commit | cut -f 2- | tr '\n' '\
0' | \
288 xargs -0 git update-index --add --replace --remove
289 git ls-files -z --others | \
290 xargs -0 git update-index --add --replace --remove
293 eval "$filter_index" < /dev/null ||
294 die "index filter failed: $filter_index"
297 for parent in $parents; do
298 for reparent in $(map "$parent"); do
299 parentstr="$parentstr -p $reparent"
302 if [ "$filter_parent" ]; then
303 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
304 die "parent filter failed: $filter_parent"
307 sed -e '1,/^$
/d
' <../commit | \
308 eval "$filter_msg" > ../message ||
309 die "msg filter failed: $filter_msg"
310 sh -c "$filter_commit" "git commit-tree" \
311 $(git write-tree) $parentstr < ../message > ../map/$commit
314 # In case of a subdirectory filter, it is possible that a specified head
315 # is not in the set of rewritten commits, because it was pruned by the
316 # revision walker. Fix it by mapping these heads to the next rewritten
317 # ancestor(s), i.e. the boundaries in the set of rewritten commits.
319 # NEEDSWORK: we should sort the unmapped refs topologically first
322 sha1=$(git rev-parse "$ref"^0)
323 test -f "$workdir"/../map/$sha1 && continue
324 # Assign the boundarie(s) in the set of rewritten commits
325 # as the replacement commit(s).
326 # (This would look a bit nicer if --not --stdin worked.)
327 for p in $( (cd "$workdir"/../map; ls | sed "s/^/^/") |
328 git rev-list $ref --boundary --stdin |
331 map $p >> "$workdir"/../map/$sha1
333 done < "$tempdir"/heads
335 # Finally update the refs
337 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
338 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
343 # avoid rewriting a ref twice
344 test -f "$orig_namespace$ref" && continue
346 sha1=$(git rev-parse "$ref"^0)
347 rewritten=$(map $sha1)
349 test $sha1 = "$rewritten" &&
350 warn "WARNING: Ref '$ref' is unchanged" &&
355 echo "Ref '$ref' was deleted"
356 git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
357 die "Could not delete $ref"
360 echo "Ref '$ref' was rewritten"
361 git update-ref -m "filter-branch: rewrite" \
362 "$ref" $rewritten $sha1 ||
363 die "Could not rewrite $ref"
366 # NEEDSWORK: possibly add -Werror, making this an error
367 warn "WARNING: '$ref' was rewritten into multiple commits:"
369 warn "WARNING: Ref '$ref' points to the first one now."
370 rewritten=$(echo "$rewritten" | head -n 1)
371 git update-ref -m "filter-branch: rewrite to first" \
372 "$ref" $rewritten $sha1 ||
373 die "Could not rewrite $ref"
376 git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1
378 done < "$tempdir"/heads
380 # TODO: This should possibly go, with the semantics that all positive given
381 # refs are updated, and their original heads stored in refs/original/
384 if [ "$filter_tag_name" ]; then
385 git for-each-ref --format='%(objectname
) %(objecttype
) %(refname
)' refs/tags |
386 while read sha1 type ref; do
387 ref="${ref#refs/tags/}"
388 # XXX: Rewrite tagged trees as well?
389 if [ "$type" != "commit" -a "$type" != "tag" ]; then
393 if [ "$type" = "tag" ]; then
394 # Dereference to a commit
396 sha1="$(git rev-parse "$sha1"^{commit} 2>/dev/null)" || continue
399 [ -f "../map/$sha1" ] || continue
400 new_sha1="$(cat "../map/$sha1")"
401 export GIT_COMMIT="$sha1"
402 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
403 die "tag name filter failed: $filter_tag_name"
405 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
407 if [ "$type" = "tag" ]; then
408 # Warn that we are not rewriting the tag object itself.
409 warn "unreferencing tag object $sha1t"
412 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
413 die "Could not write tag $new_ref"
420 test $count -gt 0 && echo "These refs were rewritten:"
421 git show-ref | grep ^"$orig_namespace"
423 unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
424 test -z "$ORIG_GIT_DIR" || GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR
425 test -z "$ORIG_GIT_WORK_TREE" || GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" &&
427 test -z "$ORIG_GIT_INDEX_FILE" || GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" &&
428 export GIT_INDEX_FILE
429 git read-tree -u -m HEAD