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 USAGE
="git-filter-branch [-d TEMPDIR] [FILTERS] DESTBRANCH [REV-RANGE]"
20 # if it was not rewritten, take the original
21 if test -r "$workdir/../map/$1"
23 cat "$workdir/../map/$1"
29 # override die(): this version puts in an extra line break, so that
30 # the progress is still visible
39 # When piped a commit, output a script to set the ident of either
40 # "author" or "committer
43 lid
="$(echo "$1" | tr "A-Z
" "a-z
")"
44 uid
="$(echo "$1" | tr "a-z
" "A-Z
")"
49 s/^'$lid' \([^<]*\) <[^>]*> .*$/\1/
51 s
/.
*/export GIT_
'$uid'_NAME
='\''&'\''/p
54 s
/^
'$lid' [^
<]* <\
([^
>]*\
)> .
*$
/\
1/
56 s/.*/export GIT_'$uid'_EMAIL='\''&'\''/p
59 s/^'$lid' [^<]* <[^>]*> \(.*\)$/\1/
61 s
/.
*/export GIT_
'$uid'_DATE
='\''&'\''/p
67 LANG=C LC_ALL=C sed -ne "$pick_id_script"
68 # Ensure non-empty id name.
69 echo "[ -n \"\$GIT_${uid}_NAME\" ] || export GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\""
78 filter_commit='git commit-tree
"$@"'
81 while case "$#" in 0) usage;; esac
94 # all switches take one argument
96 case "$#" in 1) usage ;; esac
109 filter_tree="$OPTARG"
112 filter_index="$OPTARG"
115 filter_parent="$OPTARG"
121 filter_commit="$OPTARG"
124 filter_tag_name="$OPTARG"
126 --subdirectory-filter)
127 filter_subdir="$OPTARG"
137 test -n "$dstbranch" || die "missing branch name"
138 git show-ref "refs/heads/$dstbranch" 2> /dev/null &&
139 die "branch $dstbranch already exists"
141 test ! -e "$tempdir" || die "$tempdir already exists, please remove it"
142 mkdir -p "$tempdir/t" &&
151 GIT_DIR="$(pwd)/../../$GIT_DIR"
154 export GIT_DIR GIT_WORK_TREE=.
156 export GIT_INDEX_FILE="$(pwd)/../index"
157 git read-tree || die "Could not seed the index"
161 # map old->new commit ids for rewriting parents
162 mkdir ../map || die "Could not create map/ directory"
164 case "$filter_subdir" in
166 git rev-list --reverse --topo-order --default HEAD \
170 git rev-list --reverse --topo-order --default HEAD \
171 --parents --full-history "$@" -- "$filter_subdir"
172 esac > ../revs || die "Could not get the commits"
173 commits=$(wc -l <../revs | tr -d " ")
175 test $commits -eq 0 && die "Found nothing to rewrite"
178 while read commit parents; do
180 printf "\rRewrite $commit ($i/$commits)"
182 case "$filter_subdir" in
184 git read-tree -i -m $commit
187 git read-tree -i -m $commit:"$filter_subdir"
188 esac || die "Could not initialize the index"
190 export GIT_COMMIT=$commit
191 git cat-file commit "$commit" >../commit ||
192 die "Cannot read 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 die "Could not checkout the index"
204 # files that $commit removed are now still in the working tree;
205 # remove them, else they would be added again
206 git ls-files -z --others | xargs -0 rm -f
207 eval "$filter_tree" < /dev/null ||
208 die "tree filter failed: $filter_tree"
210 git diff-index -r $commit | cut -f 2- | tr '\n' '\
0' | \
211 xargs -0 git update-index --add --replace --remove
212 git ls-files -z --others | \
213 xargs -0 git update-index --add --replace --remove
216 eval "$filter_index" < /dev/null ||
217 die "index filter failed: $filter_index"
220 for parent in $parents; do
221 for reparent in $(map "$parent"); do
222 parentstr="$parentstr -p $reparent"
225 if [ "$filter_parent" ]; then
226 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
227 die "parent filter failed: $filter_parent"
230 sed -e '1,/^$
/d
' <../commit | \
231 eval "$filter_msg" > ../message ||
232 die "msg filter failed: $filter_msg"
233 sh -c "$filter_commit" "git commit-tree" \
234 $(git write-tree) $parentstr < ../message > ../map/$commit
237 src_head=$(tail -n 1 ../revs | sed -e 's
/ .
*//')
238 target_head=$(head -n 1 ../map/$src_head)
239 case "$target_head" in
241 echo Nothing rewritten
244 git update-ref refs/heads/"$dstbranch" $target_head ||
245 die "Could not update $dstbranch with $target_head"
246 if [ $(wc -l <../map/$src_head) -gt 1 ]; then
247 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
248 sed 's
/^
/ /' ../map/$src_head >&2
254 if [ "$filter_tag_name" ]; then
255 git for-each-ref --format='%(objectname
) %(objecttype
) %(refname
)' refs/tags |
256 while read sha1 type ref; do
257 ref="${ref#refs/tags/}"
258 # XXX: Rewrite tagged trees as well?
259 if [ "$type" != "commit" -a "$type" != "tag" ]; then
263 if [ "$type" = "tag" ]; then
264 # Dereference to a commit
266 sha1="$(git rev-parse "$sha1"^{commit} 2>/dev/null)" || continue
269 [ -f "../map/$sha1" ] || continue
270 new_sha1="$(cat "../map/$sha1")"
271 export GIT_COMMIT="$sha1"
272 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
273 die "tag name filter failed: $filter_tag_name"
275 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
277 if [ "$type" = "tag" ]; then
278 # Warn that we are not rewriting the tag object itself.
279 warn "unreferencing tag object $sha1t"
282 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
283 die "Could not write tag $new_ref"
289 printf "\nRewritten history saved to the $dstbranch branch\n"