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