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,
13 USAGE
="git-filter-branch [-d TEMPDIR] [FILTERS] DESTBRANCH [REV-RANGE]"
22 # if it was not rewritten, take the original
23 if test -r "$workdir/../map/$1"
25 cat "$workdir/../map/$1"
31 # override die(): this version puts in an extra line break, so that
32 # the progress is still visible
41 # When piped a commit, output a script to set the ident of either
42 # "author" or "committer
45 lid
="$(echo "$1" | tr "A-Z
" "a-z
")"
46 uid
="$(echo "$1" | tr "a-z
" "A-Z
")"
51 s/^'$lid' \([^<]*\) <[^>]*> .*$/\1/
53 s
/.
*/export GIT_
'$uid'_NAME
='\''&'\''/p
56 s
/^
'$lid' [^
<]* <\
([^
>]*\
)> .
*$
/\
1/
58 s/.*/export GIT_'$uid'_EMAIL='\''&'\''/p
61 s/^'$lid' [^<]* <[^>]*> \(.*\)$/\1/
63 s
/.
*/export GIT_
'$uid'_DATE
='\''&'\''/p
69 LANG=C LC_ALL=C sed -ne "$pick_id_script"
70 # Ensure non-empty id name.
71 echo "[ -n \"\$GIT_${uid}_NAME\" ] || export GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\""
80 filter_commit='git commit-tree
"$@"'
83 while case "$#" in 0) usage;; esac
96 # all switches take one argument
98 case "$#" in 1) usage ;; esac
111 filter_tree="$OPTARG"
114 filter_index="$OPTARG"
117 filter_parent="$OPTARG"
123 filter_commit="$OPTARG"
126 filter_tag_name="$OPTARG"
128 --subdirectory-filter)
129 filter_subdir="$OPTARG"
139 test -n "$dstbranch" || die "missing branch name"
140 git show-ref "refs/heads/$dstbranch" 2> /dev/null &&
141 die "branch $dstbranch already exists"
143 test ! -e "$tempdir" || die "$tempdir already exists, please remove it"
144 mkdir -p "$tempdir/t"
152 GIT_DIR="$(pwd)/../../$GIT_DIR"
155 export GIT_DIR GIT_WORK_TREE=.
157 export GIT_INDEX_FILE="$(pwd)/../index"
158 git read-tree # seed the index file
163 mkdir ../map # map old->new commit ids for rewriting parents
165 case "$filter_subdir" in
167 git rev-list --reverse --topo-order --default HEAD \
171 git rev-list --reverse --topo-order --default HEAD \
172 --parents --full-history "$@" -- "$filter_subdir"
174 commits=$(wc -l <../revs | tr -d " ")
176 test $commits -eq 0 && die "Found nothing to rewrite"
179 while read commit parents; do
181 printf "\rRewrite $commit ($i/$commits)"
183 case "$filter_subdir" in
185 git read-tree -i -m $commit
188 git read-tree -i -m $commit:"$filter_subdir"
191 export GIT_COMMIT=$commit
192 git cat-file commit "$commit" >../commit
194 eval "$(set_ident AUTHOR <../commit)" ||
195 die "setting author failed for commit $commit"
196 eval "$(set_ident COMMITTER <../commit)" ||
197 die "setting committer failed for commit $commit"
198 eval "$filter_env" < /dev/null ||
199 die "env filter failed: $filter_env"
201 if [ "$filter_tree" ]; then
202 git checkout-index -f -u -a
203 # files that $commit removed are now still in the working tree;
204 # remove them, else they would be added again
205 git ls-files -z --others | xargs -0 rm -f
206 eval "$filter_tree" < /dev/null ||
207 die "tree filter failed: $filter_tree"
209 git diff-index -r $commit | cut -f 2- | tr '\n' '\
0' | \
210 xargs -0 git update-index --add --replace --remove
211 git ls-files -z --others | \
212 xargs -0 git update-index --add --replace --remove
215 eval "$filter_index" < /dev/null ||
216 die "index filter failed: $filter_index"
219 for parent in $parents; do
220 for reparent in $(map "$parent"); do
221 parentstr="$parentstr -p $reparent"
224 if [ "$filter_parent" ]; then
225 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
226 die "parent filter failed: $filter_parent"
229 sed -e '1,/^$
/d
' <../commit | \
230 eval "$filter_msg" > ../message ||
231 die "msg filter failed: $filter_msg"
232 sh -c "$filter_commit" "git commit-tree" \
233 $(git write-tree) $parentstr < ../message > ../map/$commit
236 src_head=$(tail -n 1 ../revs | sed -e 's
/ .
*//')
237 target_head=$(head -n 1 ../map/$src_head)
238 case "$target_head" in
240 echo Nothing rewritten
243 git update-ref refs/heads/"$dstbranch" $target_head
244 if [ $(wc -l <../map/$src_head) -gt 1 ]; then
245 echo "WARNING: Your commit filter caused the head commit to expand to several rewritten commits. Only the first such commit was recorded as the current $dstbranch head but you will need to resolve the situation now (probably by manually merging the other commits). These are all the commits:" >&2
246 sed 's
/^
/ /' ../map/$src_head >&2
252 if [ "$filter_tag_name" ]; then
253 git for-each-ref --format='%(objectname
) %(objecttype
) %(refname
)' refs/tags |
254 while read sha1 type ref; do
255 ref="${ref#refs/tags/}"
256 # XXX: Rewrite tagged trees as well?
257 if [ "$type" != "commit" -a "$type" != "tag" ]; then
261 if [ "$type" = "tag" ]; then
262 # Dereference to a commit
264 sha1="$(git rev-parse "$sha1"^{commit} 2>/dev/null)" || continue
267 [ -f "../map/$sha1" ] || continue
268 new_sha1="$(cat "../map/$sha1")"
269 export GIT_COMMIT="$sha1"
270 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
271 die "tag name filter failed: $filter_tag_name"
273 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
275 if [ "$type" = "tag" ]; then
276 # Warn that we are not rewriting the tag object itself.
277 warn "unreferencing tag object $sha1t"
280 git update-ref "refs/tags/$new_ref" "$new_sha1"
286 printf "\nRewritten history saved to the $dstbranch branch\n"