filter-branch: provide the convenience functions also for commit filters
[alt-git.git] / git-filter-branch.sh
blob3b041d81efd7e96a49c40eba0e23fc0b18508c82
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 warn () {
12 echo "$*" >&2
15 map()
17 # if it was not rewritten, take the original
18 if test -r "$workdir/../map/$1"
19 then
20 cat "$workdir/../map/$1"
21 else
22 echo "$1"
26 # override die(): this version puts in an extra line break, so that
27 # the progress is still visible
29 die()
31 echo >&2
32 echo "$*" >&2
33 exit 1
36 # When piped a commit, output a script to set the ident of either
37 # "author" or "committer
39 set_ident () {
40 lid="$(echo "$1" | tr "A-Z" "a-z")"
41 uid="$(echo "$1" | tr "a-z" "A-Z")"
42 pick_id_script='
43 /^'$lid' /{
44 s/'\''/'\''\\'\'\''/g
46 s/^'$lid' \([^<]*\) <[^>]*> .*$/\1/
47 s/'\''/'\''\'\'\''/g
48 s/.*/export GIT_'$uid'_NAME='\''&'\''/p
51 s/^'$lid' [^<]* <\([^>]*\)> .*$/\1/
52 s/'\''/'\''\'\'\''/g
53 s/.*/export GIT_'$uid'_EMAIL='\''&'\''/p
56 s/^'$lid' [^<]* <[^>]*> \(.*\)$/\1/
57 s/'\''/'\''\'\'\''/g
58 s/.*/export GIT_'$uid'_DATE='\''&'\''/p
64 LANG=C LC_ALL=C sed -ne "$pick_id_script"
65 # Ensure non-empty id name.
66 echo "[ -n \"\$GIT_${uid}_NAME\" ] || export GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\""
69 # This script can be sourced by the commit filter to get the functions
70 test "a$SOURCE_FUNCTIONS" = a1 && return
71 this_script="$(cd "$(dirname "$0")"; pwd)"/$(basename "$0")
72 export this_script
74 USAGE="[--env-filter <command>] [--tree-filter <command>] \
75 [--index-filter <command>] [--parent-filter <command>] \
76 [--msg-filter <command>] [--commit-filter <command>] \
77 [--tag-name-filter <command>] [--subdirectory-filter <directory>] \
78 [--original <namespace>] [-d <directory>] [-f | --force] \
79 [<rev-list options>...]"
81 . git-sh-setup
83 tempdir=.git-rewrite
84 filter_env=
85 filter_tree=
86 filter_index=
87 filter_parent=
88 filter_msg=cat
89 filter_commit='git commit-tree "$@"'
90 filter_tag_name=
91 filter_subdir=
92 orig_namespace=refs/original/
93 force=
94 while case "$#" in 0) usage;; esac
96 case "$1" in
97 --)
98 shift
99 break
101 --force|-f)
102 shift
103 force=t
104 continue
109 break;
110 esac
112 # all switches take one argument
113 ARG="$1"
114 case "$#" in 1) usage ;; esac
115 shift
116 OPTARG="$1"
117 shift
119 case "$ARG" in
121 tempdir="$OPTARG"
123 --env-filter)
124 filter_env="$OPTARG"
126 --tree-filter)
127 filter_tree="$OPTARG"
129 --index-filter)
130 filter_index="$OPTARG"
132 --parent-filter)
133 filter_parent="$OPTARG"
135 --msg-filter)
136 filter_msg="$OPTARG"
138 --commit-filter)
139 filter_commit='SOURCE_FUNCTIONS=1 . "$this_script";'" $OPTARG"
141 --tag-name-filter)
142 filter_tag_name="$OPTARG"
144 --subdirectory-filter)
145 filter_subdir="$OPTARG"
147 --original)
148 orig_namespace=$(expr "$OPTARG/" : '\(.*[^/]\)/*$')/
151 usage
153 esac
154 done
156 case "$force" in
158 rm -rf "$tempdir"
161 test -d "$tempdir" &&
162 die "$tempdir already exists, please remove it"
163 esac
164 mkdir -p "$tempdir/t" &&
165 tempdir="$(cd "$tempdir"; pwd)" &&
166 cd "$tempdir/t" &&
167 workdir="$(pwd)" ||
168 die ""
170 # Make sure refs/original is empty
171 git for-each-ref > "$tempdir"/backup-refs
172 while read sha1 type name
174 case "$force,$name" in
175 ,$orig_namespace*)
176 die "Namespace $orig_namespace not empty"
178 t,$orig_namespace*)
179 git update-ref -d "$name" $sha1
181 esac
182 done < "$tempdir"/backup-refs
184 export GIT_DIR GIT_WORK_TREE=.
186 # These refs should be updated if their heads were rewritten
188 git rev-parse --revs-only --symbolic "$@" |
189 while read ref
191 # normalize ref
192 case "$ref" in
193 HEAD)
194 ref="$(git symbolic-ref "$ref")"
196 refs/*)
199 ref="$(git for-each-ref --format='%(refname)' |
200 grep /"$ref")"
201 esac
203 git check-ref-format "$ref" && echo "$ref"
204 done > "$tempdir"/heads
206 test -s "$tempdir"/heads ||
207 die "Which ref do you want to rewrite?"
209 export GIT_INDEX_FILE="$(pwd)/../index"
210 git read-tree || die "Could not seed the index"
212 ret=0
214 # map old->new commit ids for rewriting parents
215 mkdir ../map || die "Could not create map/ directory"
217 case "$filter_subdir" in
219 git rev-list --reverse --topo-order --default HEAD \
220 --parents "$@"
223 git rev-list --reverse --topo-order --default HEAD \
224 --parents --full-history "$@" -- "$filter_subdir"
225 esac > ../revs || die "Could not get the commits"
226 commits=$(wc -l <../revs | tr -d " ")
228 test $commits -eq 0 && die "Found nothing to rewrite"
230 # Rewrite the commits
233 while read commit parents; do
234 i=$(($i+1))
235 printf "\rRewrite $commit ($i/$commits)"
237 case "$filter_subdir" in
239 git read-tree -i -m $commit
242 git read-tree -i -m $commit:"$filter_subdir"
243 esac || die "Could not initialize the index"
245 export GIT_COMMIT=$commit
246 git cat-file commit "$commit" >../commit ||
247 die "Cannot read commit $commit"
249 eval "$(set_ident AUTHOR <../commit)" ||
250 die "setting author failed for commit $commit"
251 eval "$(set_ident COMMITTER <../commit)" ||
252 die "setting committer failed for commit $commit"
253 eval "$filter_env" < /dev/null ||
254 die "env filter failed: $filter_env"
256 if [ "$filter_tree" ]; then
257 git checkout-index -f -u -a ||
258 die "Could not checkout the index"
259 # files that $commit removed are now still in the working tree;
260 # remove them, else they would be added again
261 git ls-files -z --others | xargs -0 rm -f
262 eval "$filter_tree" < /dev/null ||
263 die "tree filter failed: $filter_tree"
265 git diff-index -r $commit | cut -f 2- | tr '\n' '\0' | \
266 xargs -0 git update-index --add --replace --remove
267 git ls-files -z --others | \
268 xargs -0 git update-index --add --replace --remove
271 eval "$filter_index" < /dev/null ||
272 die "index filter failed: $filter_index"
274 parentstr=
275 for parent in $parents; do
276 for reparent in $(map "$parent"); do
277 parentstr="$parentstr -p $reparent"
278 done
279 done
280 if [ "$filter_parent" ]; then
281 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
282 die "parent filter failed: $filter_parent"
285 sed -e '1,/^$/d' <../commit | \
286 eval "$filter_msg" > ../message ||
287 die "msg filter failed: $filter_msg"
288 sh -c "$filter_commit" "git commit-tree" \
289 $(git write-tree) $parentstr < ../message > ../map/$commit
290 done <../revs
292 # In case of a subdirectory filter, it is possible that a specified head
293 # is not in the set of rewritten commits, because it was pruned by the
294 # revision walker. Fix it by mapping these heads to the next rewritten
295 # ancestor(s), i.e. the boundaries in the set of rewritten commits.
297 # NEEDSWORK: we should sort the unmapped refs topologically first
298 while read ref
300 sha1=$(git rev-parse "$ref"^0)
301 test -f "$workdir"/../map/$sha1 && continue
302 # Assign the boundarie(s) in the set of rewritten commits
303 # as the replacement commit(s).
304 # (This would look a bit nicer if --not --stdin worked.)
305 for p in $( (cd "$workdir"/../map; ls | sed "s/^/^/") |
306 git rev-list $ref --boundary --stdin |
307 sed -n "s/^-//p")
309 map $p >> "$workdir"/../map/$sha1
310 done
311 done < "$tempdir"/heads
313 # Finally update the refs
315 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
316 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
317 count=0
318 echo
319 while read ref
321 # avoid rewriting a ref twice
322 test -f "$orig_namespace$ref" && continue
324 sha1=$(git rev-parse "$ref"^0)
325 rewritten=$(map $sha1)
327 test $sha1 = "$rewritten" &&
328 warn "WARNING: Ref '$ref' is unchanged" &&
329 continue
331 case "$rewritten" in
333 echo "Ref '$ref' was deleted"
334 git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
335 die "Could not delete $ref"
337 $_x40)
338 echo "Ref '$ref' was rewritten"
339 git update-ref -m "filter-branch: rewrite" \
340 "$ref" $rewritten $sha1 ||
341 die "Could not rewrite $ref"
344 # NEEDSWORK: possibly add -Werror, making this an error
345 warn "WARNING: '$ref' was rewritten into multiple commits:"
346 warn "$rewritten"
347 warn "WARNING: Ref '$ref' points to the first one now."
348 rewritten=$(echo "$rewritten" | head -n 1)
349 git update-ref -m "filter-branch: rewrite to first" \
350 "$ref" $rewritten $sha1 ||
351 die "Could not rewrite $ref"
353 esac
354 git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1
355 count=$(($count+1))
356 done < "$tempdir"/heads
358 # TODO: This should possibly go, with the semantics that all positive given
359 # refs are updated, and their original heads stored in refs/original/
360 # Filter tags
362 if [ "$filter_tag_name" ]; then
363 git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags |
364 while read sha1 type ref; do
365 ref="${ref#refs/tags/}"
366 # XXX: Rewrite tagged trees as well?
367 if [ "$type" != "commit" -a "$type" != "tag" ]; then
368 continue;
371 if [ "$type" = "tag" ]; then
372 # Dereference to a commit
373 sha1t="$sha1"
374 sha1="$(git rev-parse "$sha1"^{commit} 2>/dev/null)" || continue
377 [ -f "../map/$sha1" ] || continue
378 new_sha1="$(cat "../map/$sha1")"
379 export GIT_COMMIT="$sha1"
380 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
381 die "tag name filter failed: $filter_tag_name"
383 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
385 if [ "$type" = "tag" ]; then
386 # Warn that we are not rewriting the tag object itself.
387 warn "unreferencing tag object $sha1t"
390 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
391 die "Could not write tag $new_ref"
392 done
395 cd ../..
396 rm -rf "$tempdir"
397 echo
398 test $count -gt 0 && echo "These refs were rewritten:"
399 git show-ref | grep ^"$orig_namespace"
401 exit $ret