]> git.ipfire.org Git - thirdparty/git.git/blame - git-filter-branch.sh
Sync with 2.16.6
[thirdparty/git.git] / git-filter-branch.sh
CommitLineData
6f6826c5
JS
1#!/bin/sh
2#
3# Rewrite revision history
4# Copyright (c) Petr Baudis, 2006
5# Minimal changes to "port" it to core-git (c) Johannes Schindelin, 2007
6#
c401b33c
JS
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.
6f6826c5 10
16ed34ad
JS
11# The following functions will also be available in the commit filter:
12
13functions=$(cat << \EOF
b5669a05 14warn () {
285c6cbf 15 echo "$*" >&2
b5669a05
SP
16}
17
6f6826c5
JS
18map()
19{
3520e1e8 20 # if it was not rewritten, take the original
c57a3494
JS
21 if test -r "$workdir/../map/$1"
22 then
23 cat "$workdir/../map/$1"
24 else
25 echo "$1"
26 fi
6f6826c5
JS
27}
28
f95eef15
JS
29# if you run 'skip_commit "$@"' in a commit filter, it will print
30# the (mapped) parents, effectively skipping the commit.
31
32skip_commit()
33{
34 shift;
35 while [ -n "$1" ];
36 do
37 shift;
38 map "$1";
39 shift;
40 done;
41}
42
d3240d93
PH
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.
45git_commit_non_empty_tree()
46{
47 if test $# = 3 && test "$1" = $(git rev-parse "$3^{tree}"); then
48 map "$3"
a582a82d
DP
49 elif test $# = 1 && test "$1" = 4b825dc642cb6eb9a060e54bf8d69288fbee4904; then
50 :
d3240d93
PH
51 else
52 git commit-tree "$@"
53 fi
54}
8c1ce0f4
JS
55# override die(): this version puts in an extra line break, so that
56# the progress is still visible
57
58die()
59{
60 echo >&2
61 echo "$*" >&2
62 exit 1
63}
16ed34ad
JS
64EOF
65)
66
67eval "$functions"
8c1ce0f4 68
3c730fab
JK
69finish_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"
76}
6f6826c5
JS
77
78set_ident () {
3c730fab
JK
79 parse_ident_from_commit author AUTHOR committer COMMITTER
80 finish_ident AUTHOR
81 finish_ident COMMITTER
6f6826c5
JS
82}
83
e336afdf 84USAGE="[--setup <command>] [--subdirectory-filter <directory>] [--env-filter <command>]
3b117f73
AH
85 [--tree-filter <command>] [--index-filter <command>]
86 [--parent-filter <command>] [--msg-filter <command>]
87 [--commit-filter <command>] [--tag-name-filter <command>]
e336afdf 88 [--original <namespace>]
bd2c79fb 89 [-d <directory>] [-f | --force] [--state-branch <branch>]
d612975e 90 [--] [<rev-list options>...]"
7e0f1704 91
8f321a39 92OPTIONS_SPEC=
7e0f1704
JS
93. git-sh-setup
94
a4661b01 95if [ "$(is_bare_repository)" = false ]; then
5347a50f 96 require_clean_work_tree 'rewrite branches'
a4661b01 97fi
46eb449c 98
6f6826c5 99tempdir=.git-rewrite
3b117f73 100filter_setup=
6f6826c5
JS
101filter_env=
102filter_tree=
103filter_index=
104filter_parent=
105filter_msg=cat
d3240d93 106filter_commit=
6f6826c5 107filter_tag_name=
685ef546 108filter_subdir=
bd2c79fb 109state_branch=
dfd05e38
JS
110orig_namespace=refs/original/
111force=
d3240d93 112prune_empty=
f2f3a6b8 113remap_to_ancestor=
822f7c73 114while :
6f6826c5
JS
115do
116 case "$1" in
117 --)
118 shift
119 break
120 ;;
dfd05e38
JS
121 --force|-f)
122 shift
123 force=t
124 continue
125 ;;
f2f3a6b8 126 --remap-to-ancestor)
7ec344d8 127 # deprecated ($remap_to_ancestor is set now automatically)
f2f3a6b8
TR
128 shift
129 remap_to_ancestor=t
130 continue
131 ;;
d3240d93
PH
132 --prune-empty)
133 shift
134 prune_empty=t
135 continue
136 ;;
6f6826c5
JS
137 -*)
138 ;;
139 *)
140 break;
141 esac
142
143 # all switches take one argument
144 ARG="$1"
145 case "$#" in 1) usage ;; esac
146 shift
147 OPTARG="$1"
148 shift
149
150 case "$ARG" in
151 -d)
152 tempdir="$OPTARG"
153 ;;
3b117f73
AH
154 --setup)
155 filter_setup="$OPTARG"
156 ;;
07c49845
DG
157 --subdirectory-filter)
158 filter_subdir="$OPTARG"
159 remap_to_ancestor=t
160 ;;
6f6826c5
JS
161 --env-filter)
162 filter_env="$OPTARG"
163 ;;
164 --tree-filter)
165 filter_tree="$OPTARG"
166 ;;
167 --index-filter)
168 filter_index="$OPTARG"
169 ;;
170 --parent-filter)
171 filter_parent="$OPTARG"
172 ;;
173 --msg-filter)
174 filter_msg="$OPTARG"
175 ;;
176 --commit-filter)
16ed34ad 177 filter_commit="$functions; $OPTARG"
6f6826c5
JS
178 ;;
179 --tag-name-filter)
180 filter_tag_name="$OPTARG"
181 ;;
dfd05e38 182 --original)
55ced83d 183 orig_namespace=$(expr "$OPTARG/" : '\(.*[^/]\)/*$')/
dfd05e38 184 ;;
bd2c79fb
IC
185 --state-branch)
186 state_branch="$OPTARG"
187 ;;
6f6826c5
JS
188 *)
189 usage
190 ;;
191 esac
192done
193
d3240d93
PH
194case "$prune_empty,$filter_commit" in
195,)
196 filter_commit='git commit-tree "$@"';;
197t,)
198 filter_commit="$functions;"' git_commit_non_empty_tree "$@"';;
199,*)
200 ;;
201*)
5da81713 202 die "Cannot set --prune-empty and --commit-filter at the same time"
d3240d93
PH
203esac
204
dfd05e38
JS
205case "$force" in
206t)
207 rm -rf "$tempdir"
208;;
209'')
210 test -d "$tempdir" &&
211 die "$tempdir already exists, please remove it"
212esac
97276019 213orig_dir=$(pwd)
af580e9c 214mkdir -p "$tempdir/t" &&
dfd05e38 215tempdir="$(cd "$tempdir"; pwd)" &&
af580e9c
JS
216cd "$tempdir/t" &&
217workdir="$(pwd)" ||
218die ""
6f6826c5 219
def16e71 220# Remove tempdir on exit
97276019 221trap 'cd "$orig_dir"; rm -rf "$tempdir"' 0
def16e71 222
88e38808
LN
223ORIG_GIT_DIR="$GIT_DIR"
224ORIG_GIT_WORK_TREE="$GIT_WORK_TREE"
225ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE"
7b1378bd
IC
226ORIG_GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME"
227ORIG_GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL"
228ORIG_GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE"
229ORIG_GIT_COMMITTER_NAME="$GIT_COMMITTER_NAME"
230ORIG_GIT_COMMITTER_EMAIL="$GIT_COMMITTER_EMAIL"
231ORIG_GIT_COMMITTER_DATE="$GIT_COMMITTER_DATE"
232
88e38808
LN
233GIT_WORK_TREE=.
234export GIT_DIR GIT_WORK_TREE
235
dfd05e38 236# Make sure refs/original is empty
0ea29cce 237git for-each-ref > "$tempdir"/backup-refs || exit
dfd05e38
JS
238while read sha1 type name
239do
240 case "$force,$name" in
241 ,$orig_namespace*)
734cd572
JT
242 die "Cannot create a new backup.
243A previous backup already exists in $orig_namespace
244Force overwriting the backup with -f"
dfd05e38
JS
245 ;;
246 t,$orig_namespace*)
247 git update-ref -d "$name" $sha1
248 ;;
249 esac
250done < "$tempdir"/backup-refs
251
418fa3a5 252# The refs should be updated if their heads were rewritten
0ea29cce
EK
253git rev-parse --no-flags --revs-only --symbolic-full-name \
254 --default HEAD "$@" > "$tempdir"/raw-heads || exit
255sed -e '/^^/d' "$tempdir"/raw-heads >"$tempdir"/heads
dfd05e38
JS
256
257test -s "$tempdir"/heads ||
69638939 258 die "You must specify a ref to rewrite."
dfd05e38 259
3addc94a
JS
260GIT_INDEX_FILE="$(pwd)/../index"
261export GIT_INDEX_FILE
6f6826c5 262
af580e9c
JS
263# map old->new commit ids for rewriting parents
264mkdir ../map || die "Could not create map/ directory"
6f6826c5 265
bd2c79fb
IC
266if test -n "$state_branch"
267then
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;
278 }
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
283 fi
284fi
285
2c1d2d81
TR
286# we need "--" only if there are no path arguments in $@
287nonrevs=$(git rev-parse --no-revs "$@") || exit
7ec344d8
CH
288if test -z "$nonrevs"
289then
290 dashdash=--
291else
292 dashdash=
293 remap_to_ancestor=t
294fi
295
3361a548 296git rev-parse --revs-only "$@" >../parse
2c1d2d81 297
685ef546
JS
298case "$filter_subdir" in
299"")
2c1d2d81 300 eval set -- "$(git rev-parse --sq --no-revs "$@")"
685ef546
JS
301 ;;
302*)
2c1d2d81
TR
303 eval set -- "$(git rev-parse --sq --no-revs "$@" $dashdash \
304 "$filter_subdir")"
305 ;;
306esac
307
308git rev-list --reverse --topo-order --default HEAD \
3361a548 309 --parents --simplify-merges --stdin "$@" <../parse >../revs ||
2c1d2d81 310 die "Could not get the commits"
9d6f220c 311commits=$(wc -l <../revs | tr -d " ")
6f6826c5
JS
312
313test $commits -eq 0 && die "Found nothing to rewrite"
314
dfd05e38 315# Rewrite the commits
6a9d16a0
GB
316report_progress ()
317{
318 if test -n "$progress" &&
319 test $git_filter_branch__commit_count -gt $next_sample_at
320 then
71400d97
JH
321 count=$git_filter_branch__commit_count
322
323 now=$(date +%s)
324 elapsed=$(($now - $start_timestamp))
325 remaining=$(( ($commits - $count) * $elapsed / $count ))
326 if test $elapsed -gt 0
6a9d16a0 327 then
71400d97 328 next_sample_at=$(( ($elapsed + 1) * $count / $elapsed ))
6a9d16a0
GB
329 else
330 next_sample_at=$(($next_sample_at + 1))
331 fi
71400d97 332 progress=" ($elapsed seconds passed, remaining $remaining predicted)"
6a9d16a0 333 fi
71400d97 334 printf "\rRewrite $commit ($count/$commits)$progress "
6a9d16a0 335}
dfd05e38 336
d5b0c97d 337git_filter_branch__commit_count=0
6a9d16a0
GB
338
339progress= start_timestamp=
340if date '+%s' 2>/dev/null | grep -q '^[0-9][0-9]*$'
341then
342 next_sample_at=0
343 progress="dummy to ensure this is not empty"
344 start_timestamp=$(date '+%s')
345fi
346
348d4f2f
JK
347if test -n "$filter_index" ||
348 test -n "$filter_tree" ||
349 test -n "$filter_subdir"
350then
351 need_index=t
352else
353 need_index=
354fi
355
3b117f73
AH
356eval "$filter_setup" < /dev/null ||
357 die "filter setup failed: $filter_setup"
358
813b4734 359while read commit parents; do
d5b0c97d 360 git_filter_branch__commit_count=$(($git_filter_branch__commit_count+1))
6a9d16a0
GB
361
362 report_progress
6f6826c5 363
685ef546
JS
364 case "$filter_subdir" in
365 "")
348d4f2f
JK
366 if test -n "$need_index"
367 then
368 GIT_ALLOW_NULL_SHA1=1 git read-tree -i -m $commit
369 fi
685ef546
JS
370 ;;
371 *)
5b044ac3 372 # The commit may not have the subdirectory at all
83bd7437
JK
373 err=$(GIT_ALLOW_NULL_SHA1=1 \
374 git read-tree -i -m $commit:"$filter_subdir" 2>&1) || {
d69da76d 375 if ! git rev-parse -q --verify $commit:"$filter_subdir"
5b044ac3
JH
376 then
377 rm -f "$GIT_INDEX_FILE"
378 else
379 echo >&2 "$err"
380 false
381 fi
382 }
af580e9c 383 esac || die "Could not initialize the index"
6f6826c5 384
3addc94a
JS
385 GIT_COMMIT=$commit
386 export GIT_COMMIT
af580e9c
JS
387 git cat-file commit "$commit" >../commit ||
388 die "Cannot read commit $commit"
6f6826c5 389
3c730fab
JK
390 eval "$(set_ident <../commit)" ||
391 die "setting author/committer failed for commit $commit"
8c1ce0f4
JS
392 eval "$filter_env" < /dev/null ||
393 die "env filter failed: $filter_env"
6f6826c5
JS
394
395 if [ "$filter_tree" ]; then
af580e9c
JS
396 git checkout-index -f -u -a ||
397 die "Could not checkout the index"
6f6826c5
JS
398 # files that $commit removed are now still in the working tree;
399 # remove them, else they would be added again
6a589fda 400 git clean -d -q -f -x
8c1ce0f4
JS
401 eval "$filter_tree" < /dev/null ||
402 die "tree filter failed: $filter_tree"
403
1fe32cb9 404 (
4d2a3646 405 git diff-index -r --name-only --ignore-submodules $commit -- &&
1fe32cb9 406 git ls-files --others
0ea29cce
EK
407 ) > "$tempdir"/tree-state || exit
408 git update-index --add --replace --remove --stdin \
409 < "$tempdir"/tree-state || exit
6f6826c5
JS
410 fi
411
8c1ce0f4
JS
412 eval "$filter_index" < /dev/null ||
413 die "index filter failed: $filter_index"
6f6826c5
JS
414
415 parentstr=
813b4734 416 for parent in $parents; do
3520e1e8 417 for reparent in $(map "$parent"); do
79bc4ef3
CB
418 case "$parentstr " in
419 *" -p $reparent "*)
420 ;;
421 *)
422 parentstr="$parentstr -p $reparent"
423 ;;
424 esac
3520e1e8 425 done
6f6826c5
JS
426 done
427 if [ "$filter_parent" ]; then
8c1ce0f4
JS
428 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
429 die "parent filter failed: $filter_parent"
6f6826c5
JS
430 fi
431
df062010 432 {
a5a4b3ff 433 while IFS='' read -r header_line && test -n "$header_line"
df062010
JK
434 do
435 # skip header lines...
436 :;
437 done
438 # and output the actual commit message
439 cat
440 } <../commit |
8c1ce0f4
JS
441 eval "$filter_msg" > ../message ||
442 die "msg filter failed: $filter_msg"
348d4f2f
JK
443
444 if test -n "$need_index"
445 then
446 tree=$(git write-tree)
447 else
1dc413eb 448 tree=$(git rev-parse "$commit^{tree}")
348d4f2f 449 fi
0906f6e1 450 workdir=$workdir @SHELL_PATH@ -c "$filter_commit" "git commit-tree" \
348d4f2f 451 "$tree" $parentstr < ../message > ../map/$commit ||
0ea29cce 452 die "could not write rewritten commit"
6f6826c5
JS
453done <../revs
454
f2f3a6b8
TR
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.
dfd05e38 460
f2f3a6b8 461if test "$remap_to_ancestor" = t
a0e46390
TR
462then
463 while read ref
dfd05e38 464 do
a0e46390
TR
465 sha1=$(git rev-parse "$ref"^0)
466 test -f "$workdir"/../map/$sha1 && continue
2c1d2d81 467 ancestor=$(git rev-list --simplify-merges -1 "$ref" "$@")
a0e46390
TR
468 test "$ancestor" && echo $(map $ancestor) >> "$workdir"/../map/$sha1
469 done < "$tempdir"/heads
470fi
dfd05e38
JS
471
472# Finally update the refs
473
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"
dfd05e38
JS
476echo
477while read ref
478do
479 # avoid rewriting a ref twice
480 test -f "$orig_namespace$ref" && continue
481
482 sha1=$(git rev-parse "$ref"^0)
483 rewritten=$(map $sha1)
484
485 test $sha1 = "$rewritten" &&
486 warn "WARNING: Ref '$ref' is unchanged" &&
487 continue
488
489 case "$rewritten" in
490 '')
491 echo "Ref '$ref' was deleted"
492 git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
493 die "Could not delete $ref"
98409060 494 ;;
dfd05e38
JS
495 $_x40)
496 echo "Ref '$ref' was rewritten"
261044e8
TR
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."
503 fi
504 else
505 die "Could not rewrite $ref"
506 fi
507 fi
98409060 508 ;;
dfd05e38
JS
509 *)
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"
518 ;;
519 esac
0ea29cce
EK
520 git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1 ||
521 exit
dfd05e38
JS
522done < "$tempdir"/heads
523
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
6f6826c5
JS
527
528if [ "$filter_tag_name" ]; then
5be60078 529 git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags |
6f6826c5
JS
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;
535 fi
536
537 if [ "$type" = "tag" ]; then
538 # Dereference to a commit
539 sha1t="$sha1"
7bd93c1c 540 sha1="$(git rev-parse -q "$sha1"^{commit})" || continue
6f6826c5
JS
541 fi
542
543 [ -f "../map/$sha1" ] || continue
544 new_sha1="$(cat "../map/$sha1")"
3addc94a
JS
545 GIT_COMMIT="$sha1"
546 export GIT_COMMIT
8c1ce0f4
JS
547 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
548 die "tag name filter failed: $filter_tag_name"
6f6826c5
JS
549
550 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
551
552 if [ "$type" = "tag" ]; then
a9da1663
JS
553 new_sha1=$( ( printf 'object %s\ntype commit\ntag %s\n' \
554 "$new_sha1" "$new_ref"
555 git cat-file tag "$ref" |
1bf6551e 556 sed -n \
9524cf29 557 -e '1,/^$/{
a9da1663
JS
558 /^object /d
559 /^type /d
560 /^tag /d
9524cf29 561 }' \
1bf6551e 562 -e '/^-----BEGIN PGP SIGNATURE-----/q' \
a9da1663 563 -e 'p' ) |
b2c1ca6b 564 git hash-object -t tag -w --stdin) ||
1bf6551e
BC
565 die "Could not create new tag object for $ref"
566 if git cat-file tag "$ref" | \
e1622bfc 567 sane_grep '^-----BEGIN PGP SIGNATURE-----' >/dev/null 2>&1
1bf6551e
BC
568 then
569 warn "gpg signature stripped from tag object $sha1t"
570 fi
6f6826c5
JS
571 fi
572
af580e9c
JS
573 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
574 die "Could not write tag $new_ref"
6f6826c5
JS
575 done
576fi
577
9273b562 578unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
7b1378bd
IC
579unset GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
580unset GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL GIT_COMMITTER_DATE
9273b562
EK
581test -z "$ORIG_GIT_DIR" || {
582 GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR
583}
584test -z "$ORIG_GIT_WORK_TREE" || {
585 GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" &&
586 export GIT_WORK_TREE
587}
588test -z "$ORIG_GIT_INDEX_FILE" || {
589 GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" &&
590 export GIT_INDEX_FILE
591}
7b1378bd
IC
592test -z "$ORIG_GIT_AUTHOR_NAME" || {
593 GIT_AUTHOR_NAME="$ORIG_GIT_AUTHOR_NAME" &&
594 export GIT_AUTHOR_NAME
595}
596test -z "$ORIG_GIT_AUTHOR_EMAIL" || {
597 GIT_AUTHOR_EMAIL="$ORIG_GIT_AUTHOR_EMAIL" &&
598 export GIT_AUTHOR_EMAIL
599}
600test -z "$ORIG_GIT_AUTHOR_DATE" || {
601 GIT_AUTHOR_DATE="$ORIG_GIT_AUTHOR_DATE" &&
602 export GIT_AUTHOR_DATE
603}
604test -z "$ORIG_GIT_COMMITTER_NAME" || {
605 GIT_COMMITTER_NAME="$ORIG_GIT_COMMITTER_NAME" &&
606 export GIT_COMMITTER_NAME
607}
608test -z "$ORIG_GIT_COMMITTER_EMAIL" || {
609 GIT_COMMITTER_EMAIL="$ORIG_GIT_COMMITTER_EMAIL" &&
610 export GIT_COMMITTER_EMAIL
611}
612test -z "$ORIG_GIT_COMMITTER_DATE" || {
613 GIT_COMMITTER_DATE="$ORIG_GIT_COMMITTER_DATE" &&
614 export GIT_COMMITTER_DATE
615}
9273b562 616
bd2c79fb
IC
617if test -n "$state_branch"
618then
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;
628 }
629 close(H) or die;' || die "Unable to save state")
206a6ae0 630 state_tree=$(printf '100644 blob %s\tfilter.map\n' "$state_blob" | git mktree)
bd2c79fb
IC
631 if test -n "$state_commit"
632 then
206a6ae0 633 state_commit=$(echo "Sync" | git commit-tree "$state_tree" -p "$state_commit")
bd2c79fb 634 else
206a6ae0 635 state_commit=$(echo "Sync" | git commit-tree "$state_tree" )
bd2c79fb
IC
636 fi
637 git update-ref "$state_branch" "$state_commit"
638fi
639
d24813c4
IC
640cd "$orig_dir"
641rm -rf "$tempdir"
642
643trap - 0
644
a4661b01 645if [ "$(is_bare_repository)" = false ]; then
0ea29cce 646 git read-tree -u -m HEAD || exit
a4661b01 647fi
46eb449c 648
0ea29cce 649exit 0