Handle format.subjectprefix for every command which accepts --pretty
[git/fastimport.git] / git-filter-branch.sh
blob22fb5bf6ad929e2028ec1cf98f9073b14cdd8b51
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 test -r "$workdir/../map/$1" || echo "$1"
20 cat "$workdir/../map/$1"
23 # When piped a commit, output a script to set the ident of either
24 # "author" or "committer
26 set_ident () {
27 lid="$(echo "$1" | tr "A-Z" "a-z")"
28 uid="$(echo "$1" | tr "a-z" "A-Z")"
29 pick_id_script='
30 /^'$lid' /{
31 s/'\''/'\''\\'\'\''/g
33 s/^'$lid' \([^<]*\) <[^>]*> .*$/\1/
34 s/'\''/'\''\'\'\''/g
35 s/.*/export GIT_'$uid'_NAME='\''&'\''/p
38 s/^'$lid' [^<]* <\([^>]*\)> .*$/\1/
39 s/'\''/'\''\'\'\''/g
40 s/.*/export GIT_'$uid'_EMAIL='\''&'\''/p
43 s/^'$lid' [^<]* <[^>]*> \(.*\)$/\1/
44 s/'\''/'\''\'\'\''/g
45 s/.*/export GIT_'$uid'_DATE='\''&'\''/p
51 LANG=C LC_ALL=C sed -ne "$pick_id_script"
52 # Ensure non-empty id name.
53 echo "[ -n \"\$GIT_${uid}_NAME\" ] || export GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\""
56 tempdir=.git-rewrite
57 filter_env=
58 filter_tree=
59 filter_index=
60 filter_parent=
61 filter_msg=cat
62 filter_commit='git commit-tree "$@"'
63 filter_tag_name=
64 filter_subdir=
65 while case "$#" in 0) usage;; esac
67 case "$1" in
68 --)
69 shift
70 break
72 -*)
75 break;
76 esac
78 # all switches take one argument
79 ARG="$1"
80 case "$#" in 1) usage ;; esac
81 shift
82 OPTARG="$1"
83 shift
85 case "$ARG" in
86 -d)
87 tempdir="$OPTARG"
89 --env-filter)
90 filter_env="$OPTARG"
92 --tree-filter)
93 filter_tree="$OPTARG"
95 --index-filter)
96 filter_index="$OPTARG"
98 --parent-filter)
99 filter_parent="$OPTARG"
101 --msg-filter)
102 filter_msg="$OPTARG"
104 --commit-filter)
105 filter_commit="$OPTARG"
107 --tag-name-filter)
108 filter_tag_name="$OPTARG"
110 --subdirectory-filter)
111 filter_subdir="$OPTARG"
114 usage
116 esac
117 done
119 dstbranch="$1"
120 shift
121 test -n "$dstbranch" || die "missing branch name"
122 git show-ref "refs/heads/$dstbranch" 2> /dev/null &&
123 die "branch $dstbranch already exists"
125 test ! -e "$tempdir" || die "$tempdir already exists, please remove it"
126 mkdir -p "$tempdir/t"
127 cd "$tempdir/t"
128 workdir="$(pwd)"
130 case "$GIT_DIR" in
134 GIT_DIR="$(pwd)/../../$GIT_DIR"
136 esac
137 export GIT_DIR GIT_WORK_TREE=.
139 export GIT_INDEX_FILE="$(pwd)/../index"
140 git read-tree # seed the index file
142 ret=0
145 mkdir ../map # map old->new commit ids for rewriting parents
147 case "$filter_subdir" in
149 git rev-list --reverse --topo-order --default HEAD \
150 --parents "$@"
153 git rev-list --reverse --topo-order --default HEAD \
154 --parents --full-history "$@" -- "$filter_subdir"
155 esac > ../revs
156 commits=$(cat ../revs | wc -l | tr -d " ")
158 test $commits -eq 0 && die "Found nothing to rewrite"
161 while read commit parents; do
162 i=$(($i+1))
163 printf "$commit ($i/$commits) "
165 case "$filter_subdir" in
167 git read-tree -i -m $commit
170 git read-tree -i -m $commit:"$filter_subdir"
171 esac
173 export GIT_COMMIT=$commit
174 git cat-file commit "$commit" >../commit
176 eval "$(set_ident AUTHOR <../commit)"
177 eval "$(set_ident COMMITTER <../commit)"
178 eval "$filter_env" < /dev/null
180 if [ "$filter_tree" ]; then
181 git checkout-index -f -u -a
182 # files that $commit removed are now still in the working tree;
183 # remove them, else they would be added again
184 git ls-files -z --others | xargs -0 rm -f
185 eval "$filter_tree" < /dev/null
186 git diff-index -r $commit | cut -f 2- | tr '\n' '\0' | \
187 xargs -0 git update-index --add --replace --remove
188 git ls-files -z --others | \
189 xargs -0 git update-index --add --replace --remove
192 eval "$filter_index" < /dev/null
194 parentstr=
195 for parent in $parents; do
196 for reparent in $(map "$parent"); do
197 parentstr="$parentstr -p $reparent"
198 done
199 done
200 if [ "$filter_parent" ]; then
201 parentstr="$(echo "$parentstr" | eval "$filter_parent")"
204 sed -e '1,/^$/d' <../commit | \
205 eval "$filter_msg" | \
206 sh -c "$filter_commit" "git commit-tree" $(git write-tree) $parentstr | \
207 tee ../map/$commit
208 done <../revs
210 src_head=$(tail -n 1 ../revs | sed -e 's/ .*//')
211 target_head=$(head -n 1 ../map/$src_head)
212 case "$target_head" in
214 echo Nothing rewritten
217 git update-ref refs/heads/"$dstbranch" $target_head
218 if [ $(cat ../map/$src_head | wc -l) -gt 1 ]; then
219 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
220 sed 's/^/ /' ../map/$src_head >&2
221 ret=1
224 esac
226 if [ "$filter_tag_name" ]; then
227 git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags |
228 while read sha1 type ref; do
229 ref="${ref#refs/tags/}"
230 # XXX: Rewrite tagged trees as well?
231 if [ "$type" != "commit" -a "$type" != "tag" ]; then
232 continue;
235 if [ "$type" = "tag" ]; then
236 # Dereference to a commit
237 sha1t="$sha1"
238 sha1="$(git rev-parse "$sha1"^{commit} 2>/dev/null)" || continue
241 [ -f "../map/$sha1" ] || continue
242 new_sha1="$(cat "../map/$sha1")"
243 export GIT_COMMIT="$sha1"
244 new_ref="$(echo "$ref" | eval "$filter_tag_name")"
246 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
248 if [ "$type" = "tag" ]; then
249 # Warn that we are not rewriting the tag object itself.
250 warn "unreferencing tag object $sha1t"
253 git update-ref "refs/tags/$new_ref" "$new_sha1"
254 done
257 cd ../..
258 rm -rf "$tempdir"
259 echo "Rewritten history saved to the $dstbranch branch"
261 exit $ret