filter-branch: make output nicer
[git/spearce.git] / git-filter-branch.sh
blobc22266b48650bc274bb890f164032ca8d64aee46
1 #!/bin/sh
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,
9 # files and trees.
11 set -e
13 USAGE="git-filter-branch [-d TEMPDIR] [FILTERS] DESTBRANCH [REV-RANGE]"
14 . git-sh-setup
16 map()
18 # if it was not rewritten, take the original
19 if test -r "$workdir/../map/$1"
20 then
21 cat "$workdir/../map/$1"
22 else
23 echo "$1"
27 # When piped a commit, output a script to set the ident of either
28 # "author" or "committer
30 set_ident () {
31 lid="$(echo "$1" | tr "A-Z" "a-z")"
32 uid="$(echo "$1" | tr "a-z" "A-Z")"
33 pick_id_script='
34 /^'$lid' /{
35 s/'\''/'\''\\'\'\''/g
37 s/^'$lid' \([^<]*\) <[^>]*> .*$/\1/
38 s/'\''/'\''\'\'\''/g
39 s/.*/export GIT_'$uid'_NAME='\''&'\''/p
42 s/^'$lid' [^<]* <\([^>]*\)> .*$/\1/
43 s/'\''/'\''\'\'\''/g
44 s/.*/export GIT_'$uid'_EMAIL='\''&'\''/p
47 s/^'$lid' [^<]* <[^>]*> \(.*\)$/\1/
48 s/'\''/'\''\'\'\''/g
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%%@*}\""
60 tempdir=.git-rewrite
61 filter_env=
62 filter_tree=
63 filter_index=
64 filter_parent=
65 filter_msg=cat
66 filter_commit='git commit-tree "$@"'
67 filter_tag_name=
68 filter_subdir=
69 while case "$#" in 0) usage;; esac
71 case "$1" in
72 --)
73 shift
74 break
76 -*)
79 break;
80 esac
82 # all switches take one argument
83 ARG="$1"
84 case "$#" in 1) usage ;; esac
85 shift
86 OPTARG="$1"
87 shift
89 case "$ARG" in
90 -d)
91 tempdir="$OPTARG"
93 --env-filter)
94 filter_env="$OPTARG"
96 --tree-filter)
97 filter_tree="$OPTARG"
99 --index-filter)
100 filter_index="$OPTARG"
102 --parent-filter)
103 filter_parent="$OPTARG"
105 --msg-filter)
106 filter_msg="$OPTARG"
108 --commit-filter)
109 filter_commit="$OPTARG"
111 --tag-name-filter)
112 filter_tag_name="$OPTARG"
114 --subdirectory-filter)
115 filter_subdir="$OPTARG"
118 usage
120 esac
121 done
123 dstbranch="$1"
124 shift
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"
131 cd "$tempdir/t"
132 workdir="$(pwd)"
134 case "$GIT_DIR" in
138 GIT_DIR="$(pwd)/../../$GIT_DIR"
140 esac
141 export GIT_DIR GIT_WORK_TREE=.
143 export GIT_INDEX_FILE="$(pwd)/../index"
144 git read-tree # seed the index file
146 ret=0
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 \
154 --parents "$@"
157 git rev-list --reverse --topo-order --default HEAD \
158 --parents --full-history "$@" -- "$filter_subdir"
159 esac > ../revs
160 commits=$(cat ../revs | wc -l | tr -d " ")
162 test $commits -eq 0 && die "Found nothing to rewrite"
165 while read commit parents; do
166 i=$(($i+1))
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"
175 esac
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
198 parentstr=
199 for parent in $parents; do
200 for reparent in $(map "$parent"); do
201 parentstr="$parentstr -p $reparent"
202 done
203 done
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
212 done <../revs
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
225 ret=1
228 esac
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
236 continue;
239 if [ "$type" = "tag" ]; then
240 # Dereference to a commit
241 sha1t="$sha1"
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"
258 done
261 cd ../..
262 rm -rf "$tempdir"
263 printf "\nRewritten history saved to the $dstbranch branch\n"
265 exit $ret