Use start_comand() in builtin-fetch-pack.c instead of explicit fork/exec.
[git/jnareb-git.git] / git-filter-branch.sh
bloba12f6c2d4c7ad3c4f69a783f7255b3010224d0c3
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 # if you run 'skip_commit "$@"' in a commit filter, it will print
27 # the (mapped) parents, effectively skipping the commit.
29 skip_commit()
31 shift;
32 while [ -n "$1" ];
34 shift;
35 map "$1";
36 shift;
37 done;
40 # override die(): this version puts in an extra line break, so that
41 # the progress is still visible
43 die()
45 echo >&2
46 echo "$*" >&2
47 exit 1
50 # When piped a commit, output a script to set the ident of either
51 # "author" or "committer
53 set_ident () {
54 lid="$(echo "$1" | tr "A-Z" "a-z")"
55 uid="$(echo "$1" | tr "a-z" "A-Z")"
56 pick_id_script='
57 /^'$lid' /{
58 s/'\''/'\''\\'\'\''/g
60 s/^'$lid' \([^<]*\) <[^>]*> .*$/\1/
61 s/'\''/'\''\'\'\''/g
62 s/.*/export GIT_'$uid'_NAME='\''&'\''/p
65 s/^'$lid' [^<]* <\([^>]*\)> .*$/\1/
66 s/'\''/'\''\'\'\''/g
67 s/.*/export GIT_'$uid'_EMAIL='\''&'\''/p
70 s/^'$lid' [^<]* <[^>]*> \(.*\)$/\1/
71 s/'\''/'\''\'\'\''/g
72 s/.*/export GIT_'$uid'_DATE='\''&'\''/p
78 LANG=C LC_ALL=C sed -ne "$pick_id_script"
79 # Ensure non-empty id name.
80 echo "[ -n \"\$GIT_${uid}_NAME\" ] || export GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\""
83 # This script can be sourced by the commit filter to get the functions
84 test "a$SOURCE_FUNCTIONS" = a1 && return
85 this_script="$(cd "$(dirname "$0")"; pwd)"/$(basename "$0")
86 export this_script
88 USAGE="[--env-filter <command>] [--tree-filter <command>] \
89 [--index-filter <command>] [--parent-filter <command>] \
90 [--msg-filter <command>] [--commit-filter <command>] \
91 [--tag-name-filter <command>] [--subdirectory-filter <directory>] \
92 [--original <namespace>] [-d <directory>] [-f | --force] \
93 [<rev-list options>...]"
95 . git-sh-setup
97 tempdir=.git-rewrite
98 filter_env=
99 filter_tree=
100 filter_index=
101 filter_parent=
102 filter_msg=cat
103 filter_commit='git commit-tree "$@"'
104 filter_tag_name=
105 filter_subdir=
106 orig_namespace=refs/original/
107 force=
108 while :
110 test $# = 0 && usage
111 case "$1" in
113 shift
114 break
116 --force|-f)
117 shift
118 force=t
119 continue
124 break;
125 esac
127 # all switches take one argument
128 ARG="$1"
129 case "$#" in 1) usage ;; esac
130 shift
131 OPTARG="$1"
132 shift
134 case "$ARG" in
136 tempdir="$OPTARG"
138 --env-filter)
139 filter_env="$OPTARG"
141 --tree-filter)
142 filter_tree="$OPTARG"
144 --index-filter)
145 filter_index="$OPTARG"
147 --parent-filter)
148 filter_parent="$OPTARG"
150 --msg-filter)
151 filter_msg="$OPTARG"
153 --commit-filter)
154 filter_commit='SOURCE_FUNCTIONS=1 . "$this_script";'" $OPTARG"
156 --tag-name-filter)
157 filter_tag_name="$OPTARG"
159 --subdirectory-filter)
160 filter_subdir="$OPTARG"
162 --original)
163 orig_namespace=$(expr "$OPTARG/" : '\(.*[^/]\)/*$')/
166 usage
168 esac
169 done
171 case "$force" in
173 rm -rf "$tempdir"
176 test -d "$tempdir" &&
177 die "$tempdir already exists, please remove it"
178 esac
179 mkdir -p "$tempdir/t" &&
180 tempdir="$(cd "$tempdir"; pwd)" &&
181 cd "$tempdir/t" &&
182 workdir="$(pwd)" ||
183 die ""
185 # Make sure refs/original is empty
186 git for-each-ref > "$tempdir"/backup-refs
187 while read sha1 type name
189 case "$force,$name" in
190 ,$orig_namespace*)
191 die "Namespace $orig_namespace not empty"
193 t,$orig_namespace*)
194 git update-ref -d "$name" $sha1
196 esac
197 done < "$tempdir"/backup-refs
199 export GIT_DIR GIT_WORK_TREE=.
201 # These refs should be updated if their heads were rewritten
203 git rev-parse --revs-only --symbolic "$@" |
204 while read ref
206 # normalize ref
207 case "$ref" in
208 HEAD)
209 ref="$(git symbolic-ref "$ref")"
211 refs/*)
214 ref="$(git for-each-ref --format='%(refname)' |
215 grep /"$ref")"
216 esac
218 git check-ref-format "$ref" && echo "$ref"
219 done > "$tempdir"/heads
221 test -s "$tempdir"/heads ||
222 die "Which ref do you want to rewrite?"
224 export GIT_INDEX_FILE="$(pwd)/../index"
225 git read-tree || die "Could not seed the index"
227 ret=0
229 # map old->new commit ids for rewriting parents
230 mkdir ../map || die "Could not create map/ directory"
232 case "$filter_subdir" in
234 git rev-list --reverse --topo-order --default HEAD \
235 --parents "$@"
238 git rev-list --reverse --topo-order --default HEAD \
239 --parents --full-history "$@" -- "$filter_subdir"
240 esac > ../revs || die "Could not get the commits"
241 commits=$(wc -l <../revs | tr -d " ")
243 test $commits -eq 0 && die "Found nothing to rewrite"
245 # Rewrite the commits
248 while read commit parents; do
249 i=$(($i+1))
250 printf "\rRewrite $commit ($i/$commits)"
252 case "$filter_subdir" in
254 git read-tree -i -m $commit
257 git read-tree -i -m $commit:"$filter_subdir"
258 esac || die "Could not initialize the index"
260 export GIT_COMMIT=$commit
261 git cat-file commit "$commit" >../commit ||
262 die "Cannot read commit $commit"
264 eval "$(set_ident AUTHOR <../commit)" ||
265 die "setting author failed for commit $commit"
266 eval "$(set_ident COMMITTER <../commit)" ||
267 die "setting committer failed for commit $commit"
268 eval "$filter_env" < /dev/null ||
269 die "env filter failed: $filter_env"
271 if [ "$filter_tree" ]; then
272 git checkout-index -f -u -a ||
273 die "Could not checkout the index"
274 # files that $commit removed are now still in the working tree;
275 # remove them, else they would be added again
276 git ls-files -z --others | xargs -0 rm -f
277 eval "$filter_tree" < /dev/null ||
278 die "tree filter failed: $filter_tree"
280 git diff-index -r $commit | cut -f 2- | tr '\n' '\0' | \
281 xargs -0 git update-index --add --replace --remove
282 git ls-files -z --others | \
283 xargs -0 git update-index --add --replace --remove
286 eval "$filter_index" < /dev/null ||
287 die "index filter failed: $filter_index"
289 parentstr=
290 for parent in $parents; do
291 for reparent in $(map "$parent"); do
292 parentstr="$parentstr -p $reparent"
293 done
294 done
295 if [ "$filter_parent" ]; then
296 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
297 die "parent filter failed: $filter_parent"
300 sed -e '1,/^$/d' <../commit | \
301 eval "$filter_msg" > ../message ||
302 die "msg filter failed: $filter_msg"
303 sh -c "$filter_commit" "git commit-tree" \
304 $(git write-tree) $parentstr < ../message > ../map/$commit
305 done <../revs
307 # In case of a subdirectory filter, it is possible that a specified head
308 # is not in the set of rewritten commits, because it was pruned by the
309 # revision walker. Fix it by mapping these heads to the next rewritten
310 # ancestor(s), i.e. the boundaries in the set of rewritten commits.
312 # NEEDSWORK: we should sort the unmapped refs topologically first
313 while read ref
315 sha1=$(git rev-parse "$ref"^0)
316 test -f "$workdir"/../map/$sha1 && continue
317 # Assign the boundarie(s) in the set of rewritten commits
318 # as the replacement commit(s).
319 # (This would look a bit nicer if --not --stdin worked.)
320 for p in $( (cd "$workdir"/../map; ls | sed "s/^/^/") |
321 git rev-list $ref --boundary --stdin |
322 sed -n "s/^-//p")
324 map $p >> "$workdir"/../map/$sha1
325 done
326 done < "$tempdir"/heads
328 # Finally update the refs
330 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
331 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
332 count=0
333 echo
334 while read ref
336 # avoid rewriting a ref twice
337 test -f "$orig_namespace$ref" && continue
339 sha1=$(git rev-parse "$ref"^0)
340 rewritten=$(map $sha1)
342 test $sha1 = "$rewritten" &&
343 warn "WARNING: Ref '$ref' is unchanged" &&
344 continue
346 case "$rewritten" in
348 echo "Ref '$ref' was deleted"
349 git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
350 die "Could not delete $ref"
352 $_x40)
353 echo "Ref '$ref' was rewritten"
354 git update-ref -m "filter-branch: rewrite" \
355 "$ref" $rewritten $sha1 ||
356 die "Could not rewrite $ref"
359 # NEEDSWORK: possibly add -Werror, making this an error
360 warn "WARNING: '$ref' was rewritten into multiple commits:"
361 warn "$rewritten"
362 warn "WARNING: Ref '$ref' points to the first one now."
363 rewritten=$(echo "$rewritten" | head -n 1)
364 git update-ref -m "filter-branch: rewrite to first" \
365 "$ref" $rewritten $sha1 ||
366 die "Could not rewrite $ref"
368 esac
369 git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1
370 count=$(($count+1))
371 done < "$tempdir"/heads
373 # TODO: This should possibly go, with the semantics that all positive given
374 # refs are updated, and their original heads stored in refs/original/
375 # Filter tags
377 if [ "$filter_tag_name" ]; then
378 git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags |
379 while read sha1 type ref; do
380 ref="${ref#refs/tags/}"
381 # XXX: Rewrite tagged trees as well?
382 if [ "$type" != "commit" -a "$type" != "tag" ]; then
383 continue;
386 if [ "$type" = "tag" ]; then
387 # Dereference to a commit
388 sha1t="$sha1"
389 sha1="$(git rev-parse "$sha1"^{commit} 2>/dev/null)" || continue
392 [ -f "../map/$sha1" ] || continue
393 new_sha1="$(cat "../map/$sha1")"
394 export GIT_COMMIT="$sha1"
395 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
396 die "tag name filter failed: $filter_tag_name"
398 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
400 if [ "$type" = "tag" ]; then
401 # Warn that we are not rewriting the tag object itself.
402 warn "unreferencing tag object $sha1t"
405 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
406 die "Could not write tag $new_ref"
407 done
410 cd ../..
411 rm -rf "$tempdir"
412 echo
413 test $count -gt 0 && echo "These refs were rewritten:"
414 git show-ref | grep ^"$orig_namespace"
416 exit $ret