RelNotes: further fixes for 2.14.2 from the master front
[git/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
3b117f73
AH
84USAGE="[--setup <command>] [--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 [--subdirectory-filter <directory>] [--original <namespace>]
89 [-d <directory>] [-f | --force]
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=
dfd05e38
JS
109orig_namespace=refs/original/
110force=
d3240d93 111prune_empty=
f2f3a6b8 112remap_to_ancestor=
822f7c73 113while :
6f6826c5
JS
114do
115 case "$1" in
116 --)
117 shift
118 break
119 ;;
dfd05e38
JS
120 --force|-f)
121 shift
122 force=t
123 continue
124 ;;
f2f3a6b8 125 --remap-to-ancestor)
7ec344d8 126 # deprecated ($remap_to_ancestor is set now automatically)
f2f3a6b8
TR
127 shift
128 remap_to_ancestor=t
129 continue
130 ;;
d3240d93
PH
131 --prune-empty)
132 shift
133 prune_empty=t
134 continue
135 ;;
6f6826c5
JS
136 -*)
137 ;;
138 *)
139 break;
140 esac
141
142 # all switches take one argument
143 ARG="$1"
144 case "$#" in 1) usage ;; esac
145 shift
146 OPTARG="$1"
147 shift
148
149 case "$ARG" in
150 -d)
151 tempdir="$OPTARG"
152 ;;
3b117f73
AH
153 --setup)
154 filter_setup="$OPTARG"
155 ;;
6f6826c5
JS
156 --env-filter)
157 filter_env="$OPTARG"
158 ;;
159 --tree-filter)
160 filter_tree="$OPTARG"
161 ;;
162 --index-filter)
163 filter_index="$OPTARG"
164 ;;
165 --parent-filter)
166 filter_parent="$OPTARG"
167 ;;
168 --msg-filter)
169 filter_msg="$OPTARG"
170 ;;
171 --commit-filter)
16ed34ad 172 filter_commit="$functions; $OPTARG"
6f6826c5
JS
173 ;;
174 --tag-name-filter)
175 filter_tag_name="$OPTARG"
176 ;;
685ef546
JS
177 --subdirectory-filter)
178 filter_subdir="$OPTARG"
f2f3a6b8 179 remap_to_ancestor=t
685ef546 180 ;;
dfd05e38 181 --original)
55ced83d 182 orig_namespace=$(expr "$OPTARG/" : '\(.*[^/]\)/*$')/
dfd05e38 183 ;;
6f6826c5
JS
184 *)
185 usage
186 ;;
187 esac
188done
189
d3240d93
PH
190case "$prune_empty,$filter_commit" in
191,)
192 filter_commit='git commit-tree "$@"';;
193t,)
194 filter_commit="$functions;"' git_commit_non_empty_tree "$@"';;
195,*)
196 ;;
197*)
5da81713 198 die "Cannot set --prune-empty and --commit-filter at the same time"
d3240d93
PH
199esac
200
dfd05e38
JS
201case "$force" in
202t)
203 rm -rf "$tempdir"
204;;
205'')
206 test -d "$tempdir" &&
207 die "$tempdir already exists, please remove it"
208esac
97276019 209orig_dir=$(pwd)
af580e9c 210mkdir -p "$tempdir/t" &&
dfd05e38 211tempdir="$(cd "$tempdir"; pwd)" &&
af580e9c
JS
212cd "$tempdir/t" &&
213workdir="$(pwd)" ||
214die ""
6f6826c5 215
def16e71 216# Remove tempdir on exit
97276019 217trap 'cd "$orig_dir"; rm -rf "$tempdir"' 0
def16e71 218
88e38808
LN
219ORIG_GIT_DIR="$GIT_DIR"
220ORIG_GIT_WORK_TREE="$GIT_WORK_TREE"
221ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE"
222GIT_WORK_TREE=.
223export GIT_DIR GIT_WORK_TREE
224
dfd05e38 225# Make sure refs/original is empty
0ea29cce 226git for-each-ref > "$tempdir"/backup-refs || exit
dfd05e38
JS
227while read sha1 type name
228do
229 case "$force,$name" in
230 ,$orig_namespace*)
734cd572
JT
231 die "Cannot create a new backup.
232A previous backup already exists in $orig_namespace
233Force overwriting the backup with -f"
dfd05e38
JS
234 ;;
235 t,$orig_namespace*)
236 git update-ref -d "$name" $sha1
237 ;;
238 esac
239done < "$tempdir"/backup-refs
240
418fa3a5 241# The refs should be updated if their heads were rewritten
0ea29cce
EK
242git rev-parse --no-flags --revs-only --symbolic-full-name \
243 --default HEAD "$@" > "$tempdir"/raw-heads || exit
244sed -e '/^^/d' "$tempdir"/raw-heads >"$tempdir"/heads
dfd05e38
JS
245
246test -s "$tempdir"/heads ||
69638939 247 die "You must specify a ref to rewrite."
dfd05e38 248
3addc94a
JS
249GIT_INDEX_FILE="$(pwd)/../index"
250export GIT_INDEX_FILE
6f6826c5 251
af580e9c
JS
252# map old->new commit ids for rewriting parents
253mkdir ../map || die "Could not create map/ directory"
6f6826c5 254
2c1d2d81
TR
255# we need "--" only if there are no path arguments in $@
256nonrevs=$(git rev-parse --no-revs "$@") || exit
7ec344d8
CH
257if test -z "$nonrevs"
258then
259 dashdash=--
260else
261 dashdash=
262 remap_to_ancestor=t
263fi
264
3361a548 265git rev-parse --revs-only "$@" >../parse
2c1d2d81 266
685ef546
JS
267case "$filter_subdir" in
268"")
2c1d2d81 269 eval set -- "$(git rev-parse --sq --no-revs "$@")"
685ef546
JS
270 ;;
271*)
2c1d2d81
TR
272 eval set -- "$(git rev-parse --sq --no-revs "$@" $dashdash \
273 "$filter_subdir")"
274 ;;
275esac
276
277git rev-list --reverse --topo-order --default HEAD \
3361a548 278 --parents --simplify-merges --stdin "$@" <../parse >../revs ||
2c1d2d81 279 die "Could not get the commits"
9d6f220c 280commits=$(wc -l <../revs | tr -d " ")
6f6826c5
JS
281
282test $commits -eq 0 && die "Found nothing to rewrite"
283
dfd05e38 284# Rewrite the commits
6a9d16a0
GB
285report_progress ()
286{
287 if test -n "$progress" &&
288 test $git_filter_branch__commit_count -gt $next_sample_at
289 then
71400d97
JH
290 count=$git_filter_branch__commit_count
291
292 now=$(date +%s)
293 elapsed=$(($now - $start_timestamp))
294 remaining=$(( ($commits - $count) * $elapsed / $count ))
295 if test $elapsed -gt 0
6a9d16a0 296 then
71400d97 297 next_sample_at=$(( ($elapsed + 1) * $count / $elapsed ))
6a9d16a0
GB
298 else
299 next_sample_at=$(($next_sample_at + 1))
300 fi
71400d97 301 progress=" ($elapsed seconds passed, remaining $remaining predicted)"
6a9d16a0 302 fi
71400d97 303 printf "\rRewrite $commit ($count/$commits)$progress "
6a9d16a0 304}
dfd05e38 305
d5b0c97d 306git_filter_branch__commit_count=0
6a9d16a0
GB
307
308progress= start_timestamp=
309if date '+%s' 2>/dev/null | grep -q '^[0-9][0-9]*$'
310then
311 next_sample_at=0
312 progress="dummy to ensure this is not empty"
313 start_timestamp=$(date '+%s')
314fi
315
348d4f2f
JK
316if test -n "$filter_index" ||
317 test -n "$filter_tree" ||
318 test -n "$filter_subdir"
319then
320 need_index=t
321else
322 need_index=
323fi
324
3b117f73
AH
325eval "$filter_setup" < /dev/null ||
326 die "filter setup failed: $filter_setup"
327
813b4734 328while read commit parents; do
d5b0c97d 329 git_filter_branch__commit_count=$(($git_filter_branch__commit_count+1))
6a9d16a0
GB
330
331 report_progress
6f6826c5 332
685ef546
JS
333 case "$filter_subdir" in
334 "")
348d4f2f
JK
335 if test -n "$need_index"
336 then
337 GIT_ALLOW_NULL_SHA1=1 git read-tree -i -m $commit
338 fi
685ef546
JS
339 ;;
340 *)
5b044ac3 341 # The commit may not have the subdirectory at all
83bd7437
JK
342 err=$(GIT_ALLOW_NULL_SHA1=1 \
343 git read-tree -i -m $commit:"$filter_subdir" 2>&1) || {
d69da76d 344 if ! git rev-parse -q --verify $commit:"$filter_subdir"
5b044ac3
JH
345 then
346 rm -f "$GIT_INDEX_FILE"
347 else
348 echo >&2 "$err"
349 false
350 fi
351 }
af580e9c 352 esac || die "Could not initialize the index"
6f6826c5 353
3addc94a
JS
354 GIT_COMMIT=$commit
355 export GIT_COMMIT
af580e9c
JS
356 git cat-file commit "$commit" >../commit ||
357 die "Cannot read commit $commit"
6f6826c5 358
3c730fab
JK
359 eval "$(set_ident <../commit)" ||
360 die "setting author/committer failed for commit $commit"
8c1ce0f4
JS
361 eval "$filter_env" < /dev/null ||
362 die "env filter failed: $filter_env"
6f6826c5
JS
363
364 if [ "$filter_tree" ]; then
af580e9c
JS
365 git checkout-index -f -u -a ||
366 die "Could not checkout the index"
6f6826c5
JS
367 # files that $commit removed are now still in the working tree;
368 # remove them, else they would be added again
6a589fda 369 git clean -d -q -f -x
8c1ce0f4
JS
370 eval "$filter_tree" < /dev/null ||
371 die "tree filter failed: $filter_tree"
372
1fe32cb9 373 (
4d2a3646 374 git diff-index -r --name-only --ignore-submodules $commit -- &&
1fe32cb9 375 git ls-files --others
0ea29cce
EK
376 ) > "$tempdir"/tree-state || exit
377 git update-index --add --replace --remove --stdin \
378 < "$tempdir"/tree-state || exit
6f6826c5
JS
379 fi
380
8c1ce0f4
JS
381 eval "$filter_index" < /dev/null ||
382 die "index filter failed: $filter_index"
6f6826c5
JS
383
384 parentstr=
813b4734 385 for parent in $parents; do
3520e1e8 386 for reparent in $(map "$parent"); do
79bc4ef3
CB
387 case "$parentstr " in
388 *" -p $reparent "*)
389 ;;
390 *)
391 parentstr="$parentstr -p $reparent"
392 ;;
393 esac
3520e1e8 394 done
6f6826c5
JS
395 done
396 if [ "$filter_parent" ]; then
8c1ce0f4
JS
397 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
398 die "parent filter failed: $filter_parent"
6f6826c5
JS
399 fi
400
df062010 401 {
a5a4b3ff 402 while IFS='' read -r header_line && test -n "$header_line"
df062010
JK
403 do
404 # skip header lines...
405 :;
406 done
407 # and output the actual commit message
408 cat
409 } <../commit |
8c1ce0f4
JS
410 eval "$filter_msg" > ../message ||
411 die "msg filter failed: $filter_msg"
348d4f2f
JK
412
413 if test -n "$need_index"
414 then
415 tree=$(git write-tree)
416 else
1dc413eb 417 tree=$(git rev-parse "$commit^{tree}")
348d4f2f 418 fi
0906f6e1 419 workdir=$workdir @SHELL_PATH@ -c "$filter_commit" "git commit-tree" \
348d4f2f 420 "$tree" $parentstr < ../message > ../map/$commit ||
0ea29cce 421 die "could not write rewritten commit"
6f6826c5
JS
422done <../revs
423
f2f3a6b8
TR
424# If we are filtering for paths, as in the case of a subdirectory
425# filter, it is possible that a specified head is not in the set of
426# rewritten commits, because it was pruned by the revision walker.
427# Ancestor remapping fixes this by mapping these heads to the unique
428# nearest ancestor that survived the pruning.
dfd05e38 429
f2f3a6b8 430if test "$remap_to_ancestor" = t
a0e46390
TR
431then
432 while read ref
dfd05e38 433 do
a0e46390
TR
434 sha1=$(git rev-parse "$ref"^0)
435 test -f "$workdir"/../map/$sha1 && continue
2c1d2d81 436 ancestor=$(git rev-list --simplify-merges -1 "$ref" "$@")
a0e46390
TR
437 test "$ancestor" && echo $(map $ancestor) >> "$workdir"/../map/$sha1
438 done < "$tempdir"/heads
439fi
dfd05e38
JS
440
441# Finally update the refs
442
443_x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
444_x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
dfd05e38
JS
445echo
446while read ref
447do
448 # avoid rewriting a ref twice
449 test -f "$orig_namespace$ref" && continue
450
451 sha1=$(git rev-parse "$ref"^0)
452 rewritten=$(map $sha1)
453
454 test $sha1 = "$rewritten" &&
455 warn "WARNING: Ref '$ref' is unchanged" &&
456 continue
457
458 case "$rewritten" in
459 '')
460 echo "Ref '$ref' was deleted"
461 git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
462 die "Could not delete $ref"
98409060 463 ;;
dfd05e38
JS
464 $_x40)
465 echo "Ref '$ref' was rewritten"
261044e8
TR
466 if ! git update-ref -m "filter-branch: rewrite" \
467 "$ref" $rewritten $sha1 2>/dev/null; then
468 if test $(git cat-file -t "$ref") = tag; then
469 if test -z "$filter_tag_name"; then
470 warn "WARNING: You said to rewrite tagged commits, but not the corresponding tag."
471 warn "WARNING: Perhaps use '--tag-name-filter cat' to rewrite the tag."
472 fi
473 else
474 die "Could not rewrite $ref"
475 fi
476 fi
98409060 477 ;;
dfd05e38
JS
478 *)
479 # NEEDSWORK: possibly add -Werror, making this an error
480 warn "WARNING: '$ref' was rewritten into multiple commits:"
481 warn "$rewritten"
482 warn "WARNING: Ref '$ref' points to the first one now."
483 rewritten=$(echo "$rewritten" | head -n 1)
484 git update-ref -m "filter-branch: rewrite to first" \
485 "$ref" $rewritten $sha1 ||
486 die "Could not rewrite $ref"
487 ;;
488 esac
0ea29cce
EK
489 git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1 ||
490 exit
dfd05e38
JS
491done < "$tempdir"/heads
492
493# TODO: This should possibly go, with the semantics that all positive given
494# refs are updated, and their original heads stored in refs/original/
495# Filter tags
6f6826c5
JS
496
497if [ "$filter_tag_name" ]; then
5be60078 498 git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags |
6f6826c5
JS
499 while read sha1 type ref; do
500 ref="${ref#refs/tags/}"
501 # XXX: Rewrite tagged trees as well?
502 if [ "$type" != "commit" -a "$type" != "tag" ]; then
503 continue;
504 fi
505
506 if [ "$type" = "tag" ]; then
507 # Dereference to a commit
508 sha1t="$sha1"
7bd93c1c 509 sha1="$(git rev-parse -q "$sha1"^{commit})" || continue
6f6826c5
JS
510 fi
511
512 [ -f "../map/$sha1" ] || continue
513 new_sha1="$(cat "../map/$sha1")"
3addc94a
JS
514 GIT_COMMIT="$sha1"
515 export GIT_COMMIT
8c1ce0f4
JS
516 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
517 die "tag name filter failed: $filter_tag_name"
6f6826c5
JS
518
519 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
520
521 if [ "$type" = "tag" ]; then
a9da1663
JS
522 new_sha1=$( ( printf 'object %s\ntype commit\ntag %s\n' \
523 "$new_sha1" "$new_ref"
524 git cat-file tag "$ref" |
1bf6551e 525 sed -n \
9524cf29 526 -e '1,/^$/{
a9da1663
JS
527 /^object /d
528 /^type /d
529 /^tag /d
9524cf29 530 }' \
1bf6551e 531 -e '/^-----BEGIN PGP SIGNATURE-----/q' \
a9da1663 532 -e 'p' ) |
1bf6551e
BC
533 git mktag) ||
534 die "Could not create new tag object for $ref"
535 if git cat-file tag "$ref" | \
e1622bfc 536 sane_grep '^-----BEGIN PGP SIGNATURE-----' >/dev/null 2>&1
1bf6551e
BC
537 then
538 warn "gpg signature stripped from tag object $sha1t"
539 fi
6f6826c5
JS
540 fi
541
af580e9c
JS
542 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
543 die "Could not write tag $new_ref"
6f6826c5
JS
544 done
545fi
546
97276019 547cd "$orig_dir"
6f6826c5 548rm -rf "$tempdir"
6f6826c5 549
def16e71
BC
550trap - 0
551
9273b562
EK
552unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
553test -z "$ORIG_GIT_DIR" || {
554 GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR
555}
556test -z "$ORIG_GIT_WORK_TREE" || {
557 GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" &&
558 export GIT_WORK_TREE
559}
560test -z "$ORIG_GIT_INDEX_FILE" || {
561 GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" &&
562 export GIT_INDEX_FILE
563}
564
a4661b01 565if [ "$(is_bare_repository)" = false ]; then
0ea29cce 566 git read-tree -u -m HEAD || exit
a4661b01 567fi
46eb449c 568
0ea29cce 569exit 0