filter-branch: return to original dir after filtering
[git/mingw.git] / git-filter-branch.sh
blob244253653db99ef1d41bfe2854be954bef47abe8
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 # The following functions will also be available in the commit filter:
13 functions=$(cat << \EOF
14 warn () {
15 echo "$*" >&2
18 map()
20 # if it was not rewritten, take the original
21 if test -r "$workdir/../map/$1"
22 then
23 cat "$workdir/../map/$1"
24 else
25 echo "$1"
29 # if you run 'skip_commit "$@"' in a commit filter, it will print
30 # the (mapped) parents, effectively skipping the commit.
32 skip_commit()
34 shift;
35 while [ -n "$1" ];
37 shift;
38 map "$1";
39 shift;
40 done;
43 # if you run 'git_commit_non_empty_tree "$@"' in a commit filter,
44 # it will skip commits that leave the tree untouched, commit the other.
45 git_commit_non_empty_tree()
47 if test $# = 3 && test "$1" = $(git rev-parse "$3^{tree}"); then
48 map "$3"
49 else
50 git commit-tree "$@"
53 # override die(): this version puts in an extra line break, so that
54 # the progress is still visible
56 die()
58 echo >&2
59 echo "$*" >&2
60 exit 1
62 EOF
65 eval "$functions"
67 # When piped a commit, output a script to set the ident of either
68 # "author" or "committer
70 set_ident () {
71 lid="$(echo "$1" | tr "[A-Z]" "[a-z]")"
72 uid="$(echo "$1" | tr "[a-z]" "[A-Z]")"
73 pick_id_script='
74 /^'$lid' /{
75 s/'\''/'\''\\'\'\''/g
77 s/^'$lid' \([^<]*\) <[^>]*> .*$/\1/
78 s/'\''/'\''\'\'\''/g
79 s/.*/GIT_'$uid'_NAME='\''&'\''; export GIT_'$uid'_NAME/p
82 s/^'$lid' [^<]* <\([^>]*\)> .*$/\1/
83 s/'\''/'\''\'\'\''/g
84 s/.*/GIT_'$uid'_EMAIL='\''&'\''; export GIT_'$uid'_EMAIL/p
87 s/^'$lid' [^<]* <[^>]*> \(.*\)$/@\1/
88 s/'\''/'\''\'\'\''/g
89 s/.*/GIT_'$uid'_DATE='\''&'\''; export GIT_'$uid'_DATE/p
95 LANG=C LC_ALL=C sed -ne "$pick_id_script"
96 # Ensure non-empty id name.
97 echo "case \"\$GIT_${uid}_NAME\" in \"\") GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\" && export GIT_${uid}_NAME;; esac"
100 USAGE="[--env-filter <command>] [--tree-filter <command>]
101 [--index-filter <command>] [--parent-filter <command>]
102 [--msg-filter <command>] [--commit-filter <command>]
103 [--tag-name-filter <command>] [--subdirectory-filter <directory>]
104 [--original <namespace>] [-d <directory>] [-f | --force]
105 [<rev-list options>...]"
107 OPTIONS_SPEC=
108 . git-sh-setup
110 if [ "$(is_bare_repository)" = false ]; then
111 require_clean_work_tree 'rewrite branches'
114 tempdir=.git-rewrite
115 filter_env=
116 filter_tree=
117 filter_index=
118 filter_parent=
119 filter_msg=cat
120 filter_commit=
121 filter_tag_name=
122 filter_subdir=
123 orig_namespace=refs/original/
124 force=
125 prune_empty=
126 remap_to_ancestor=
127 while :
129 case "$1" in
131 shift
132 break
134 --force|-f)
135 shift
136 force=t
137 continue
139 --remap-to-ancestor)
140 # deprecated ($remap_to_ancestor is set now automatically)
141 shift
142 remap_to_ancestor=t
143 continue
145 --prune-empty)
146 shift
147 prune_empty=t
148 continue
153 break;
154 esac
156 # all switches take one argument
157 ARG="$1"
158 case "$#" in 1) usage ;; esac
159 shift
160 OPTARG="$1"
161 shift
163 case "$ARG" in
165 tempdir="$OPTARG"
167 --env-filter)
168 filter_env="$OPTARG"
170 --tree-filter)
171 filter_tree="$OPTARG"
173 --index-filter)
174 filter_index="$OPTARG"
176 --parent-filter)
177 filter_parent="$OPTARG"
179 --msg-filter)
180 filter_msg="$OPTARG"
182 --commit-filter)
183 filter_commit="$functions; $OPTARG"
185 --tag-name-filter)
186 filter_tag_name="$OPTARG"
188 --subdirectory-filter)
189 filter_subdir="$OPTARG"
190 remap_to_ancestor=t
192 --original)
193 orig_namespace=$(expr "$OPTARG/" : '\(.*[^/]\)/*$')/
196 usage
198 esac
199 done
201 case "$prune_empty,$filter_commit" in
203 filter_commit='git commit-tree "$@"';;
205 filter_commit="$functions;"' git_commit_non_empty_tree "$@"';;
209 die "Cannot set --prune-empty and --commit-filter at the same time"
210 esac
212 case "$force" in
214 rm -rf "$tempdir"
217 test -d "$tempdir" &&
218 die "$tempdir already exists, please remove it"
219 esac
220 orig_dir=$(pwd)
221 mkdir -p "$tempdir/t" &&
222 tempdir="$(cd "$tempdir"; pwd)" &&
223 cd "$tempdir/t" &&
224 workdir="$(pwd)" ||
225 die ""
227 # Remove tempdir on exit
228 trap 'cd "$orig_dir"; rm -rf "$tempdir"' 0
230 ORIG_GIT_DIR="$GIT_DIR"
231 ORIG_GIT_WORK_TREE="$GIT_WORK_TREE"
232 ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE"
233 GIT_WORK_TREE=.
234 export GIT_DIR GIT_WORK_TREE
236 # Make sure refs/original is empty
237 git for-each-ref > "$tempdir"/backup-refs || exit
238 while read sha1 type name
240 case "$force,$name" in
241 ,$orig_namespace*)
242 die "Cannot create a new backup.
243 A previous backup already exists in $orig_namespace
244 Force overwriting the backup with -f"
246 t,$orig_namespace*)
247 git update-ref -d "$name" $sha1
249 esac
250 done < "$tempdir"/backup-refs
252 # The refs should be updated if their heads were rewritten
253 git rev-parse --no-flags --revs-only --symbolic-full-name \
254 --default HEAD "$@" > "$tempdir"/raw-heads || exit
255 sed -e '/^^/d' "$tempdir"/raw-heads >"$tempdir"/heads
257 test -s "$tempdir"/heads ||
258 die "Which ref do you want to rewrite?"
260 GIT_INDEX_FILE="$(pwd)/../index"
261 export GIT_INDEX_FILE
263 # map old->new commit ids for rewriting parents
264 mkdir ../map || die "Could not create map/ directory"
266 # we need "--" only if there are no path arguments in $@
267 nonrevs=$(git rev-parse --no-revs "$@") || exit
268 if test -z "$nonrevs"
269 then
270 dashdash=--
271 else
272 dashdash=
273 remap_to_ancestor=t
276 rev_args=$(git rev-parse --revs-only "$@")
278 case "$filter_subdir" in
280 eval set -- "$(git rev-parse --sq --no-revs "$@")"
283 eval set -- "$(git rev-parse --sq --no-revs "$@" $dashdash \
284 "$filter_subdir")"
286 esac
288 git rev-list --reverse --topo-order --default HEAD \
289 --parents --simplify-merges $rev_args "$@" > ../revs ||
290 die "Could not get the commits"
291 commits=$(wc -l <../revs | tr -d " ")
293 test $commits -eq 0 && die "Found nothing to rewrite"
295 # Rewrite the commits
297 git_filter_branch__commit_count=0
298 while read commit parents; do
299 git_filter_branch__commit_count=$(($git_filter_branch__commit_count+1))
300 printf "\rRewrite $commit ($git_filter_branch__commit_count/$commits)"
302 case "$filter_subdir" in
304 git read-tree -i -m $commit
307 # The commit may not have the subdirectory at all
308 err=$(git read-tree -i -m $commit:"$filter_subdir" 2>&1) || {
309 if ! git rev-parse -q --verify $commit:"$filter_subdir"
310 then
311 rm -f "$GIT_INDEX_FILE"
312 else
313 echo >&2 "$err"
314 false
317 esac || die "Could not initialize the index"
319 GIT_COMMIT=$commit
320 export GIT_COMMIT
321 git cat-file commit "$commit" >../commit ||
322 die "Cannot read commit $commit"
324 eval "$(set_ident AUTHOR <../commit)" ||
325 die "setting author failed for commit $commit"
326 eval "$(set_ident COMMITTER <../commit)" ||
327 die "setting committer failed for commit $commit"
328 eval "$filter_env" < /dev/null ||
329 die "env filter failed: $filter_env"
331 if [ "$filter_tree" ]; then
332 git checkout-index -f -u -a ||
333 die "Could not checkout the index"
334 # files that $commit removed are now still in the working tree;
335 # remove them, else they would be added again
336 git clean -d -q -f -x
337 eval "$filter_tree" < /dev/null ||
338 die "tree filter failed: $filter_tree"
341 git diff-index -r --name-only --ignore-submodules $commit &&
342 git ls-files --others
343 ) > "$tempdir"/tree-state || exit
344 git update-index --add --replace --remove --stdin \
345 < "$tempdir"/tree-state || exit
348 eval "$filter_index" < /dev/null ||
349 die "index filter failed: $filter_index"
351 parentstr=
352 for parent in $parents; do
353 for reparent in $(map "$parent"); do
354 parentstr="$parentstr -p $reparent"
355 done
356 done
357 if [ "$filter_parent" ]; then
358 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
359 die "parent filter failed: $filter_parent"
362 sed -e '1,/^$/d' <../commit | \
363 eval "$filter_msg" > ../message ||
364 die "msg filter failed: $filter_msg"
365 workdir=$workdir @SHELL_PATH@ -c "$filter_commit" "git commit-tree" \
366 $(git write-tree) $parentstr < ../message > ../map/$commit ||
367 die "could not write rewritten commit"
368 done <../revs
370 # If we are filtering for paths, as in the case of a subdirectory
371 # filter, it is possible that a specified head is not in the set of
372 # rewritten commits, because it was pruned by the revision walker.
373 # Ancestor remapping fixes this by mapping these heads to the unique
374 # nearest ancestor that survived the pruning.
376 if test "$remap_to_ancestor" = t
377 then
378 while read ref
380 sha1=$(git rev-parse "$ref"^0)
381 test -f "$workdir"/../map/$sha1 && continue
382 ancestor=$(git rev-list --simplify-merges -1 "$ref" "$@")
383 test "$ancestor" && echo $(map $ancestor) >> "$workdir"/../map/$sha1
384 done < "$tempdir"/heads
387 # Finally update the refs
389 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
390 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
391 echo
392 while read ref
394 # avoid rewriting a ref twice
395 test -f "$orig_namespace$ref" && continue
397 sha1=$(git rev-parse "$ref"^0)
398 rewritten=$(map $sha1)
400 test $sha1 = "$rewritten" &&
401 warn "WARNING: Ref '$ref' is unchanged" &&
402 continue
404 case "$rewritten" in
406 echo "Ref '$ref' was deleted"
407 git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
408 die "Could not delete $ref"
410 $_x40)
411 echo "Ref '$ref' was rewritten"
412 if ! git update-ref -m "filter-branch: rewrite" \
413 "$ref" $rewritten $sha1 2>/dev/null; then
414 if test $(git cat-file -t "$ref") = tag; then
415 if test -z "$filter_tag_name"; then
416 warn "WARNING: You said to rewrite tagged commits, but not the corresponding tag."
417 warn "WARNING: Perhaps use '--tag-name-filter cat' to rewrite the tag."
419 else
420 die "Could not rewrite $ref"
425 # NEEDSWORK: possibly add -Werror, making this an error
426 warn "WARNING: '$ref' was rewritten into multiple commits:"
427 warn "$rewritten"
428 warn "WARNING: Ref '$ref' points to the first one now."
429 rewritten=$(echo "$rewritten" | head -n 1)
430 git update-ref -m "filter-branch: rewrite to first" \
431 "$ref" $rewritten $sha1 ||
432 die "Could not rewrite $ref"
434 esac
435 git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1 ||
436 exit
437 done < "$tempdir"/heads
439 # TODO: This should possibly go, with the semantics that all positive given
440 # refs are updated, and their original heads stored in refs/original/
441 # Filter tags
443 if [ "$filter_tag_name" ]; then
444 git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags |
445 while read sha1 type ref; do
446 ref="${ref#refs/tags/}"
447 # XXX: Rewrite tagged trees as well?
448 if [ "$type" != "commit" -a "$type" != "tag" ]; then
449 continue;
452 if [ "$type" = "tag" ]; then
453 # Dereference to a commit
454 sha1t="$sha1"
455 sha1="$(git rev-parse -q "$sha1"^{commit})" || continue
458 [ -f "../map/$sha1" ] || continue
459 new_sha1="$(cat "../map/$sha1")"
460 GIT_COMMIT="$sha1"
461 export GIT_COMMIT
462 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
463 die "tag name filter failed: $filter_tag_name"
465 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
467 if [ "$type" = "tag" ]; then
468 new_sha1=$( ( printf 'object %s\ntype commit\ntag %s\n' \
469 "$new_sha1" "$new_ref"
470 git cat-file tag "$ref" |
471 sed -n \
472 -e '1,/^$/{
473 /^object /d
474 /^type /d
475 /^tag /d
476 }' \
477 -e '/^-----BEGIN PGP SIGNATURE-----/q' \
478 -e 'p' ) |
479 git mktag) ||
480 die "Could not create new tag object for $ref"
481 if git cat-file tag "$ref" | \
482 sane_grep '^-----BEGIN PGP SIGNATURE-----' >/dev/null 2>&1
483 then
484 warn "gpg signature stripped from tag object $sha1t"
488 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
489 die "Could not write tag $new_ref"
490 done
493 cd "$orig_dir"
494 rm -rf "$tempdir"
496 trap - 0
498 unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
499 test -z "$ORIG_GIT_DIR" || {
500 GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR
502 test -z "$ORIG_GIT_WORK_TREE" || {
503 GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" &&
504 export GIT_WORK_TREE
506 test -z "$ORIG_GIT_INDEX_FILE" || {
507 GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" &&
508 export GIT_INDEX_FILE
511 if [ "$(is_bare_repository)" = false ]; then
512 git read-tree -u -m HEAD || exit
515 exit 0