diff: properly error out when combining multiple pickaxe options
[git.git] / git-filter-branch.sh
blob1b7e4b2cdbdf36e090c4b1f531dcc48222d8f8b1
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 elif test $# = 1 && test "$1" = 4b825dc642cb6eb9a060e54bf8d69288fbee4904; then
51 else
52 git commit-tree "$@"
55 # override die(): this version puts in an extra line break, so that
56 # the progress is still visible
58 die()
60 echo >&2
61 echo "$*" >&2
62 exit 1
64 EOF
67 eval "$functions"
69 finish_ident() {
70 # Ensure non-empty id name.
71 echo "case \"\$GIT_$1_NAME\" in \"\") GIT_$1_NAME=\"\${GIT_$1_EMAIL%%@*}\" && export GIT_$1_NAME;; esac"
72 # And make sure everything is exported.
73 echo "export GIT_$1_NAME"
74 echo "export GIT_$1_EMAIL"
75 echo "export GIT_$1_DATE"
78 set_ident () {
79 parse_ident_from_commit author AUTHOR committer COMMITTER
80 finish_ident AUTHOR
81 finish_ident COMMITTER
84 USAGE="[--setup <command>] [--subdirectory-filter <directory>] [--env-filter <command>]
85 [--tree-filter <command>] [--index-filter <command>]
86 [--parent-filter <command>] [--msg-filter <command>]
87 [--commit-filter <command>] [--tag-name-filter <command>]
88 [--original <namespace>]
89 [-d <directory>] [-f | --force] [--state-branch <branch>]
90 [--] [<rev-list options>...]"
92 OPTIONS_SPEC=
93 . git-sh-setup
95 if [ "$(is_bare_repository)" = false ]; then
96 require_clean_work_tree 'rewrite branches'
99 tempdir=.git-rewrite
100 filter_setup=
101 filter_env=
102 filter_tree=
103 filter_index=
104 filter_parent=
105 filter_msg=cat
106 filter_commit=
107 filter_tag_name=
108 filter_subdir=
109 state_branch=
110 orig_namespace=refs/original/
111 force=
112 prune_empty=
113 remap_to_ancestor=
114 while :
116 case "$1" in
118 shift
119 break
121 --force|-f)
122 shift
123 force=t
124 continue
126 --remap-to-ancestor)
127 # deprecated ($remap_to_ancestor is set now automatically)
128 shift
129 remap_to_ancestor=t
130 continue
132 --prune-empty)
133 shift
134 prune_empty=t
135 continue
140 break;
141 esac
143 # all switches take one argument
144 ARG="$1"
145 case "$#" in 1) usage ;; esac
146 shift
147 OPTARG="$1"
148 shift
150 case "$ARG" in
152 tempdir="$OPTARG"
154 --setup)
155 filter_setup="$OPTARG"
157 --subdirectory-filter)
158 filter_subdir="$OPTARG"
159 remap_to_ancestor=t
161 --env-filter)
162 filter_env="$OPTARG"
164 --tree-filter)
165 filter_tree="$OPTARG"
167 --index-filter)
168 filter_index="$OPTARG"
170 --parent-filter)
171 filter_parent="$OPTARG"
173 --msg-filter)
174 filter_msg="$OPTARG"
176 --commit-filter)
177 filter_commit="$functions; $OPTARG"
179 --tag-name-filter)
180 filter_tag_name="$OPTARG"
182 --original)
183 orig_namespace=$(expr "$OPTARG/" : '\(.*[^/]\)/*$')/
185 --state-branch)
186 state_branch="$OPTARG"
189 usage
191 esac
192 done
194 case "$prune_empty,$filter_commit" in
196 filter_commit='git commit-tree "$@"';;
198 filter_commit="$functions;"' git_commit_non_empty_tree "$@"';;
202 die "Cannot set --prune-empty and --commit-filter at the same time"
203 esac
205 case "$force" in
207 rm -rf "$tempdir"
210 test -d "$tempdir" &&
211 die "$tempdir already exists, please remove it"
212 esac
213 orig_dir=$(pwd)
214 mkdir -p "$tempdir/t" &&
215 tempdir="$(cd "$tempdir"; pwd)" &&
216 cd "$tempdir/t" &&
217 workdir="$(pwd)" ||
218 die ""
220 # Remove tempdir on exit
221 trap 'cd "$orig_dir"; rm -rf "$tempdir"' 0
223 ORIG_GIT_DIR="$GIT_DIR"
224 ORIG_GIT_WORK_TREE="$GIT_WORK_TREE"
225 ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE"
226 ORIG_GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME"
227 ORIG_GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL"
228 ORIG_GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE"
229 ORIG_GIT_COMMITTER_NAME="$GIT_COMMITTER_NAME"
230 ORIG_GIT_COMMITTER_EMAIL="$GIT_COMMITTER_EMAIL"
231 ORIG_GIT_COMMITTER_DATE="$GIT_COMMITTER_DATE"
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 "You must specify a ref 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 if test -n "$state_branch"
267 then
268 state_commit=$(git rev-parse --no-flags --revs-only "$state_branch")
269 if test -n "$state_commit"
270 then
271 echo "Populating map from $state_branch ($state_commit)" 1>&2
272 perl -e'open(MAP, "-|", "git show $ARGV[0]:filter.map") or die;
273 while (<MAP>) {
274 m/(.*):(.*)/ or die;
275 open F, ">../map/$1" or die;
276 print F "$2" or die;
277 close(F) or die;
279 close(MAP) or die;' "$state_commit" \
280 || die "Unable to load state from $state_branch:filter.map"
281 else
282 echo "Branch $state_branch does not exist. Will create" 1>&2
286 # we need "--" only if there are no path arguments in $@
287 nonrevs=$(git rev-parse --no-revs "$@") || exit
288 if test -z "$nonrevs"
289 then
290 dashdash=--
291 else
292 dashdash=
293 remap_to_ancestor=t
296 git rev-parse --revs-only "$@" >../parse
298 case "$filter_subdir" in
300 eval set -- "$(git rev-parse --sq --no-revs "$@")"
303 eval set -- "$(git rev-parse --sq --no-revs "$@" $dashdash \
304 "$filter_subdir")"
306 esac
308 git rev-list --reverse --topo-order --default HEAD \
309 --parents --simplify-merges --stdin "$@" <../parse >../revs ||
310 die "Could not get the commits"
311 commits=$(wc -l <../revs | tr -d " ")
313 test $commits -eq 0 && die "Found nothing to rewrite"
315 # Rewrite the commits
316 report_progress ()
318 if test -n "$progress" &&
319 test $git_filter_branch__commit_count -gt $next_sample_at
320 then
321 count=$git_filter_branch__commit_count
323 now=$(date +%s)
324 elapsed=$(($now - $start_timestamp))
325 remaining=$(( ($commits - $count) * $elapsed / $count ))
326 if test $elapsed -gt 0
327 then
328 next_sample_at=$(( ($elapsed + 1) * $count / $elapsed ))
329 else
330 next_sample_at=$(($next_sample_at + 1))
332 progress=" ($elapsed seconds passed, remaining $remaining predicted)"
334 printf "\rRewrite $commit ($count/$commits)$progress "
337 git_filter_branch__commit_count=0
339 progress= start_timestamp=
340 if date '+%s' 2>/dev/null | grep -q '^[0-9][0-9]*$'
341 then
342 next_sample_at=0
343 progress="dummy to ensure this is not empty"
344 start_timestamp=$(date '+%s')
347 if test -n "$filter_index" ||
348 test -n "$filter_tree" ||
349 test -n "$filter_subdir"
350 then
351 need_index=t
352 else
353 need_index=
356 eval "$filter_setup" < /dev/null ||
357 die "filter setup failed: $filter_setup"
359 while read commit parents; do
360 git_filter_branch__commit_count=$(($git_filter_branch__commit_count+1))
362 report_progress
364 case "$filter_subdir" in
366 if test -n "$need_index"
367 then
368 GIT_ALLOW_NULL_SHA1=1 git read-tree -i -m $commit
372 # The commit may not have the subdirectory at all
373 err=$(GIT_ALLOW_NULL_SHA1=1 \
374 git read-tree -i -m $commit:"$filter_subdir" 2>&1) || {
375 if ! git rev-parse -q --verify $commit:"$filter_subdir"
376 then
377 rm -f "$GIT_INDEX_FILE"
378 else
379 echo >&2 "$err"
380 false
383 esac || die "Could not initialize the index"
385 GIT_COMMIT=$commit
386 export GIT_COMMIT
387 git cat-file commit "$commit" >../commit ||
388 die "Cannot read commit $commit"
390 eval "$(set_ident <../commit)" ||
391 die "setting author/committer failed for commit $commit"
392 eval "$filter_env" < /dev/null ||
393 die "env filter failed: $filter_env"
395 if [ "$filter_tree" ]; then
396 git checkout-index -f -u -a ||
397 die "Could not checkout the index"
398 # files that $commit removed are now still in the working tree;
399 # remove them, else they would be added again
400 git clean -d -q -f -x
401 eval "$filter_tree" < /dev/null ||
402 die "tree filter failed: $filter_tree"
405 git diff-index -r --name-only --ignore-submodules $commit -- &&
406 git ls-files --others
407 ) > "$tempdir"/tree-state || exit
408 git update-index --add --replace --remove --stdin \
409 < "$tempdir"/tree-state || exit
412 eval "$filter_index" < /dev/null ||
413 die "index filter failed: $filter_index"
415 parentstr=
416 for parent in $parents; do
417 for reparent in $(map "$parent"); do
418 case "$parentstr " in
419 *" -p $reparent "*)
422 parentstr="$parentstr -p $reparent"
424 esac
425 done
426 done
427 if [ "$filter_parent" ]; then
428 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
429 die "parent filter failed: $filter_parent"
433 while IFS='' read -r header_line && test -n "$header_line"
435 # skip header lines...
437 done
438 # and output the actual commit message
440 } <../commit |
441 eval "$filter_msg" > ../message ||
442 die "msg filter failed: $filter_msg"
444 if test -n "$need_index"
445 then
446 tree=$(git write-tree)
447 else
448 tree=$(git rev-parse "$commit^{tree}")
450 workdir=$workdir @SHELL_PATH@ -c "$filter_commit" "git commit-tree" \
451 "$tree" $parentstr < ../message > ../map/$commit ||
452 die "could not write rewritten commit"
453 done <../revs
455 # If we are filtering for paths, as in the case of a subdirectory
456 # filter, it is possible that a specified head is not in the set of
457 # rewritten commits, because it was pruned by the revision walker.
458 # Ancestor remapping fixes this by mapping these heads to the unique
459 # nearest ancestor that survived the pruning.
461 if test "$remap_to_ancestor" = t
462 then
463 while read ref
465 sha1=$(git rev-parse "$ref"^0)
466 test -f "$workdir"/../map/$sha1 && continue
467 ancestor=$(git rev-list --simplify-merges -1 "$ref" "$@")
468 test "$ancestor" && echo $(map $ancestor) >> "$workdir"/../map/$sha1
469 done < "$tempdir"/heads
472 # Finally update the refs
474 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
475 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
476 echo
477 while read ref
479 # avoid rewriting a ref twice
480 test -f "$orig_namespace$ref" && continue
482 sha1=$(git rev-parse "$ref"^0)
483 rewritten=$(map $sha1)
485 test $sha1 = "$rewritten" &&
486 warn "WARNING: Ref '$ref' is unchanged" &&
487 continue
489 case "$rewritten" in
491 echo "Ref '$ref' was deleted"
492 git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
493 die "Could not delete $ref"
495 $_x40)
496 echo "Ref '$ref' was rewritten"
497 if ! git update-ref -m "filter-branch: rewrite" \
498 "$ref" $rewritten $sha1 2>/dev/null; then
499 if test $(git cat-file -t "$ref") = tag; then
500 if test -z "$filter_tag_name"; then
501 warn "WARNING: You said to rewrite tagged commits, but not the corresponding tag."
502 warn "WARNING: Perhaps use '--tag-name-filter cat' to rewrite the tag."
504 else
505 die "Could not rewrite $ref"
510 # NEEDSWORK: possibly add -Werror, making this an error
511 warn "WARNING: '$ref' was rewritten into multiple commits:"
512 warn "$rewritten"
513 warn "WARNING: Ref '$ref' points to the first one now."
514 rewritten=$(echo "$rewritten" | head -n 1)
515 git update-ref -m "filter-branch: rewrite to first" \
516 "$ref" $rewritten $sha1 ||
517 die "Could not rewrite $ref"
519 esac
520 git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1 ||
521 exit
522 done < "$tempdir"/heads
524 # TODO: This should possibly go, with the semantics that all positive given
525 # refs are updated, and their original heads stored in refs/original/
526 # Filter tags
528 if [ "$filter_tag_name" ]; then
529 git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags |
530 while read sha1 type ref; do
531 ref="${ref#refs/tags/}"
532 # XXX: Rewrite tagged trees as well?
533 if [ "$type" != "commit" -a "$type" != "tag" ]; then
534 continue;
537 if [ "$type" = "tag" ]; then
538 # Dereference to a commit
539 sha1t="$sha1"
540 sha1="$(git rev-parse -q "$sha1"^{commit})" || continue
543 [ -f "../map/$sha1" ] || continue
544 new_sha1="$(cat "../map/$sha1")"
545 GIT_COMMIT="$sha1"
546 export GIT_COMMIT
547 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
548 die "tag name filter failed: $filter_tag_name"
550 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
552 if [ "$type" = "tag" ]; then
553 new_sha1=$( ( printf 'object %s\ntype commit\ntag %s\n' \
554 "$new_sha1" "$new_ref"
555 git cat-file tag "$ref" |
556 sed -n \
557 -e '1,/^$/{
558 /^object /d
559 /^type /d
560 /^tag /d
561 }' \
562 -e '/^-----BEGIN PGP SIGNATURE-----/q' \
563 -e 'p' ) |
564 git hash-object -t tag -w --stdin) ||
565 die "Could not create new tag object for $ref"
566 if git cat-file tag "$ref" | \
567 sane_grep '^-----BEGIN PGP SIGNATURE-----' >/dev/null 2>&1
568 then
569 warn "gpg signature stripped from tag object $sha1t"
573 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
574 die "Could not write tag $new_ref"
575 done
578 unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
579 unset GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
580 unset GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL GIT_COMMITTER_DATE
581 test -z "$ORIG_GIT_DIR" || {
582 GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR
584 test -z "$ORIG_GIT_WORK_TREE" || {
585 GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" &&
586 export GIT_WORK_TREE
588 test -z "$ORIG_GIT_INDEX_FILE" || {
589 GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" &&
590 export GIT_INDEX_FILE
592 test -z "$ORIG_GIT_AUTHOR_NAME" || {
593 GIT_AUTHOR_NAME="$ORIG_GIT_AUTHOR_NAME" &&
594 export GIT_AUTHOR_NAME
596 test -z "$ORIG_GIT_AUTHOR_EMAIL" || {
597 GIT_AUTHOR_EMAIL="$ORIG_GIT_AUTHOR_EMAIL" &&
598 export GIT_AUTHOR_EMAIL
600 test -z "$ORIG_GIT_AUTHOR_DATE" || {
601 GIT_AUTHOR_DATE="$ORIG_GIT_AUTHOR_DATE" &&
602 export GIT_AUTHOR_DATE
604 test -z "$ORIG_GIT_COMMITTER_NAME" || {
605 GIT_COMMITTER_NAME="$ORIG_GIT_COMMITTER_NAME" &&
606 export GIT_COMMITTER_NAME
608 test -z "$ORIG_GIT_COMMITTER_EMAIL" || {
609 GIT_COMMITTER_EMAIL="$ORIG_GIT_COMMITTER_EMAIL" &&
610 export GIT_COMMITTER_EMAIL
612 test -z "$ORIG_GIT_COMMITTER_DATE" || {
613 GIT_COMMITTER_DATE="$ORIG_GIT_COMMITTER_DATE" &&
614 export GIT_COMMITTER_DATE
617 if test -n "$state_branch"
618 then
619 echo "Saving rewrite state to $state_branch" 1>&2
620 state_blob=$(
621 perl -e'opendir D, "../map" or die;
622 open H, "|-", "git hash-object -w --stdin" or die;
623 foreach (sort readdir(D)) {
624 next if m/^\.\.?$/;
625 open F, "<../map/$_" or die;
626 chomp($f = <F>);
627 print H "$_:$f\n" or die;
629 close(H) or die;' || die "Unable to save state")
630 state_tree=$(/bin/echo -e "100644 blob $state_blob\tfilter.map" | git mktree)
631 if test -n "$state_commit"
632 then
633 state_commit=$(/bin/echo "Sync" | git commit-tree "$state_tree" -p "$state_commit")
634 else
635 state_commit=$(/bin/echo "Sync" | git commit-tree "$state_tree" )
637 git update-ref "$state_branch" "$state_commit"
640 cd "$orig_dir"
641 rm -rf "$tempdir"
643 trap - 0
645 if [ "$(is_bare_repository)" = false ]; then
646 git read-tree -u -m HEAD || exit
649 exit 0