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 # When piped a commit, output a script to set the ident of either
32 # "author" or "committer
35 lid
="$(echo "$1" | tr "A-Z
" "a-z
")"
36 uid
="$(echo "$1" | tr "a-z
" "A-Z
")"
41 s/^'$lid' \([^<]*\) <[^>]*> .*$/\1/
43 s
/.
*/export GIT_
'$uid'_NAME
='\''&'\''/p
46 s
/^
'$lid' [^
<]* <\
([^
>]*\
)> .
*$
/\
1/
48 s/.*/export GIT_'$uid'_EMAIL='\''&'\''/p
51 s/^'$lid' [^<]* <[^>]*> \(.*\)$/\1/
53 s
/.
*/export GIT_
'$uid'_DATE
='\''&'\''/p
59 LANG=C LC_ALL=C sed -ne "$pick_id_script"
60 # Ensure non-empty id name.
61 echo "[ -n \"\$GIT_${uid}_NAME\" ] || export GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\""
70 filter_commit='git commit-tree
"$@"'
73 while case "$#" in 0) usage;; esac
86 # all switches take one argument
88 case "$#" in 1) usage ;; esac
101 filter_tree="$OPTARG"
104 filter_index="$OPTARG"
107 filter_parent="$OPTARG"
113 filter_commit="$OPTARG"
116 filter_tag_name="$OPTARG"
118 --subdirectory-filter)
119 filter_subdir="$OPTARG"
129 test -n "$dstbranch" || die "missing branch name"
130 git show-ref "refs/heads/$dstbranch" 2> /dev/null &&
131 die "branch $dstbranch already exists"
133 test ! -e "$tempdir" || die "$tempdir already exists, please remove it"
134 mkdir -p "$tempdir/t"
142 GIT_DIR="$(pwd)/../../$GIT_DIR"
145 export GIT_DIR GIT_WORK_TREE=.
147 export GIT_INDEX_FILE="$(pwd)/../index"
148 git read-tree # seed the index file
153 mkdir ../map # map old->new commit ids for rewriting parents
155 case "$filter_subdir" in
157 git rev-list --reverse --topo-order --default HEAD \
161 git rev-list --reverse --topo-order --default HEAD \
162 --parents --full-history "$@" -- "$filter_subdir"
164 commits=$(cat ../revs | wc -l | tr -d " ")
166 test $commits -eq 0 && die "Found nothing to rewrite"
169 while read commit parents; do
171 printf "\rRewrite $commit ($i/$commits)"
173 case "$filter_subdir" in
175 git read-tree -i -m $commit
178 git read-tree -i -m $commit:"$filter_subdir"
181 export GIT_COMMIT=$commit
182 git cat-file commit "$commit" >../commit
184 eval "$(set_ident AUTHOR <../commit)"
185 eval "$(set_ident COMMITTER <../commit)"
186 eval "$filter_env" < /dev/null
188 if [ "$filter_tree" ]; then
189 git checkout-index -f -u -a
190 # files that $commit removed are now still in the working tree;
191 # remove them, else they would be added again
192 git ls-files -z --others | xargs -0 rm -f
193 eval "$filter_tree" < /dev/null
194 git diff-index -r $commit | cut -f 2- | tr '\n' '\
0' | \
195 xargs -0 git update-index --add --replace --remove
196 git ls-files -z --others | \
197 xargs -0 git update-index --add --replace --remove
200 eval "$filter_index" < /dev/null
203 for parent in $parents; do
204 for reparent in $(map "$parent"); do
205 parentstr="$parentstr -p $reparent"
208 if [ "$filter_parent" ]; then
209 parentstr="$(echo "$parentstr" | eval "$filter_parent")"
212 sed -e '1,/^$
/d
' <../commit | \
213 eval "$filter_msg" | \
214 sh -c "$filter_commit" "git commit-tree" $(git write-tree) \
215 $parentstr > ../map/$commit
218 src_head=$(tail -n 1 ../revs | sed -e 's
/ .
*//')
219 target_head=$(head -n 1 ../map/$src_head)
220 case "$target_head" in
222 echo Nothing rewritten
225 git update-ref refs/heads/"$dstbranch" $target_head
226 if [ $(cat ../map/$src_head | wc -l) -gt 1 ]; then
227 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
228 sed 's
/^
/ /' ../map/$src_head >&2
234 if [ "$filter_tag_name" ]; then
235 git for-each-ref --format='%(objectname
) %(objecttype
) %(refname
)' refs/tags |
236 while read sha1 type ref; do
237 ref="${ref#refs/tags/}"
238 # XXX: Rewrite tagged trees as well?
239 if [ "$type" != "commit" -a "$type" != "tag" ]; then
243 if [ "$type" = "tag" ]; then
244 # Dereference to a commit
246 sha1="$(git rev-parse "$sha1"^{commit} 2>/dev/null)" || continue
249 [ -f "../map/$sha1" ] || continue
250 new_sha1="$(cat "../map/$sha1")"
251 export GIT_COMMIT="$sha1"
252 new_ref="$(echo "$ref" | eval "$filter_tag_name")"
254 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
256 if [ "$type" = "tag" ]; then
257 # Warn that we are not rewriting the tag object itself.
258 warn "unreferencing tag object $sha1t"
261 git update-ref "refs/tags/$new_ref" "$new_sha1"
267 printf "\nRewritten history saved to the $dstbranch branch\n"