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]"
18 # if it was not rewritten, take the original
19 if test -r "$workdir/../map/$1"
21 cat "$workdir/../map/$1"
27 # When piped a commit, output a script to set the ident of either
28 # "author" or "committer
31 lid
="$(echo "$1" | tr "A-Z
" "a-z
")"
32 uid
="$(echo "$1" | tr "a-z
" "A-Z
")"
37 s/^'$lid' \([^<]*\) <[^>]*> .*$/\1/
39 s
/.
*/export GIT_
'$uid'_NAME
='\''&'\''/p
42 s
/^
'$lid' [^
<]* <\
([^
>]*\
)> .
*$
/\
1/
44 s/.*/export GIT_'$uid'_EMAIL='\''&'\''/p
47 s/^'$lid' [^<]* <[^>]*> \(.*\)$/\1/
49 s
/.
*/export GIT_
'$uid'_DATE
='\''&'\''/p
55 LANG=C LC_ALL=C sed -ne "$pick_id_script"
56 # Ensure non-empty id name.
57 echo "[ -n \"\$GIT_${uid}_NAME\" ] || export GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\""
66 filter_commit='git commit-tree
"$@"'
69 while case "$#" in 0) usage;; esac
82 # all switches take one argument
84 case "$#" in 1) usage ;; esac
100 filter_index="$OPTARG"
103 filter_parent="$OPTARG"
109 filter_commit="$OPTARG"
112 filter_tag_name="$OPTARG"
114 --subdirectory-filter)
115 filter_subdir="$OPTARG"
125 test -n "$dstbranch" || die "missing branch name"
126 git show-ref "refs/heads/$dstbranch" 2> /dev/null &&
127 die "branch $dstbranch already exists"
129 test ! -e "$tempdir" || die "$tempdir already exists, please remove it"
130 mkdir -p "$tempdir/t"
138 GIT_DIR="$(pwd)/../../$GIT_DIR"
141 export GIT_DIR GIT_WORK_TREE=.
143 export GIT_INDEX_FILE="$(pwd)/../index"
144 git read-tree # seed the index file
149 mkdir ../map # map old->new commit ids for rewriting parents
151 case "$filter_subdir" in
153 git rev-list --reverse --topo-order --default HEAD \
157 git rev-list --reverse --topo-order --default HEAD \
158 --parents --full-history "$@" -- "$filter_subdir"
160 commits=$(cat ../revs | wc -l | tr -d " ")
162 test $commits -eq 0 && die "Found nothing to rewrite"
165 while read commit parents; do
167 printf "\rRewrite $commit ($i/$commits)"
169 case "$filter_subdir" in
171 git read-tree -i -m $commit
174 git read-tree -i -m $commit:"$filter_subdir"
177 export GIT_COMMIT=$commit
178 git cat-file commit "$commit" >../commit
180 eval "$(set_ident AUTHOR <../commit)"
181 eval "$(set_ident COMMITTER <../commit)"
182 eval "$filter_env" < /dev/null
184 if [ "$filter_tree" ]; then
185 git checkout-index -f -u -a
186 # files that $commit removed are now still in the working tree;
187 # remove them, else they would be added again
188 git ls-files -z --others | xargs -0 rm -f
189 eval "$filter_tree" < /dev/null
190 git diff-index -r $commit | cut -f 2- | tr '\n' '\
0' | \
191 xargs -0 git update-index --add --replace --remove
192 git ls-files -z --others | \
193 xargs -0 git update-index --add --replace --remove
196 eval "$filter_index" < /dev/null
199 for parent in $parents; do
200 for reparent in $(map "$parent"); do
201 parentstr="$parentstr -p $reparent"
204 if [ "$filter_parent" ]; then
205 parentstr="$(echo "$parentstr" | eval "$filter_parent")"
208 sed -e '1,/^$
/d
' <../commit | \
209 eval "$filter_msg" | \
210 sh -c "$filter_commit" "git commit-tree" $(git write-tree) \
211 $parentstr > ../map/$commit
214 src_head=$(tail -n 1 ../revs | sed -e 's
/ .
*//')
215 target_head=$(head -n 1 ../map/$src_head)
216 case "$target_head" in
218 echo Nothing rewritten
221 git update-ref refs/heads/"$dstbranch" $target_head
222 if [ $(cat ../map/$src_head | wc -l) -gt 1 ]; then
223 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
224 sed 's
/^
/ /' ../map/$src_head >&2
230 if [ "$filter_tag_name" ]; then
231 git for-each-ref --format='%(objectname
) %(objecttype
) %(refname
)' refs/tags |
232 while read sha1 type ref; do
233 ref="${ref#refs/tags/}"
234 # XXX: Rewrite tagged trees as well?
235 if [ "$type" != "commit" -a "$type" != "tag" ]; then
239 if [ "$type" = "tag" ]; then
240 # Dereference to a commit
242 sha1="$(git rev-parse "$sha1"^{commit} 2>/dev/null)" || continue
245 [ -f "../map/$sha1" ] || continue
246 new_sha1="$(cat "../map/$sha1")"
247 export GIT_COMMIT="$sha1"
248 new_ref="$(echo "$ref" | eval "$filter_tag_name")"
250 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
252 if [ "$type" = "tag" ]; then
253 # Warn that we are not rewriting the tag object itself.
254 warn "unreferencing tag object $sha1t"
257 git update-ref "refs/tags/$new_ref" "$new_sha1"
263 printf "\nRewritten history saved to the $dstbranch branch\n"