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#
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.
10
11# The following functions will also be available in the commit filter:
12
13functions=$(cat << \EOF
14warn () {
15 echo "$*" >&2
16}
17
18map()
19{
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"
26 fi
27}
28
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
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"
49 elif test $# = 1 && test "$1" = 4b825dc642cb6eb9a060e54bf8d69288fbee4904; then
50 :
51 else
52 git commit-tree "$@"
53 fi
54}
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}
64EOF
65)
66
67eval "$functions"
68
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}
77
78set_ident () {
79 parse_ident_from_commit author AUTHOR committer COMMITTER
80 finish_ident AUTHOR
81 finish_ident COMMITTER
82}
83
84USAGE="[--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>...]"
91
92OPTIONS_SPEC=
93. git-sh-setup
94
95if [ "$(is_bare_repository)" = false ]; then
96 require_clean_work_tree 'rewrite branches'
97fi
98
99tempdir=.git-rewrite
100filter_setup=
101filter_env=
102filter_tree=
103filter_index=
104filter_parent=
105filter_msg=cat
106filter_commit=
107filter_tag_name=
108filter_subdir=
109state_branch=
110orig_namespace=refs/original/
111force=
112prune_empty=
113remap_to_ancestor=
114while :
115do
116 case "$1" in
117 --)
118 shift
119 break
120 ;;
121 --force|-f)
122 shift
123 force=t
124 continue
125 ;;
126 --remap-to-ancestor)
127 # deprecated ($remap_to_ancestor is set now automatically)
128 shift
129 remap_to_ancestor=t
130 continue
131 ;;
132 --prune-empty)
133 shift
134 prune_empty=t
135 continue
136 ;;
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 ;;
154 --setup)
155 filter_setup="$OPTARG"
156 ;;
157 --subdirectory-filter)
158 filter_subdir="$OPTARG"
159 remap_to_ancestor=t
160 ;;
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)
177 filter_commit="$functions; $OPTARG"
178 ;;
179 --tag-name-filter)
180 filter_tag_name="$OPTARG"
181 ;;
182 --original)
183 orig_namespace=$(expr "$OPTARG/" : '\(.*[^/]\)/*$')/
184 ;;
185 --state-branch)
186 state_branch="$OPTARG"
187 ;;
188 *)
189 usage
190 ;;
191 esac
192done
193
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*)
202 die "Cannot set --prune-empty and --commit-filter at the same time"
203esac
204
205case "$force" in
206t)
207 rm -rf "$tempdir"
208;;
209'')
210 test -d "$tempdir" &&
211 die "$tempdir already exists, please remove it"
212esac
213orig_dir=$(pwd)
214mkdir -p "$tempdir/t" &&
215tempdir="$(cd "$tempdir"; pwd)" &&
216cd "$tempdir/t" &&
217workdir="$(pwd)" ||
218die ""
219
220# Remove tempdir on exit
221trap 'cd "$orig_dir"; rm -rf "$tempdir"' 0
222
223ORIG_GIT_DIR="$GIT_DIR"
224ORIG_GIT_WORK_TREE="$GIT_WORK_TREE"
225ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE"
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
233GIT_WORK_TREE=.
234export GIT_DIR GIT_WORK_TREE
235
236# Make sure refs/original is empty
237git for-each-ref > "$tempdir"/backup-refs || exit
238while read sha1 type name
239do
240 case "$force,$name" in
241 ,$orig_namespace*)
242 die "Cannot create a new backup.
243A previous backup already exists in $orig_namespace
244Force overwriting the backup with -f"
245 ;;
246 t,$orig_namespace*)
247 git update-ref -d "$name" $sha1
248 ;;
249 esac
250done < "$tempdir"/backup-refs
251
252# The refs should be updated if their heads were rewritten
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
256
257test -s "$tempdir"/heads ||
258 die "You must specify a ref to rewrite."
259
260GIT_INDEX_FILE="$(pwd)/../index"
261export GIT_INDEX_FILE
262
263# map old->new commit ids for rewriting parents
264mkdir ../map || die "Could not create map/ directory"
265
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
286# we need "--" only if there are no path arguments in $@
287nonrevs=$(git rev-parse --no-revs "$@") || exit
288if test -z "$nonrevs"
289then
290 dashdash=--
291else
292 dashdash=
293 remap_to_ancestor=t
294fi
295
296git rev-parse --revs-only "$@" >../parse
297
298case "$filter_subdir" in
299"")
300 eval set -- "$(git rev-parse --sq --no-revs "$@")"
301 ;;
302*)
303 eval set -- "$(git rev-parse --sq --no-revs "$@" $dashdash \
304 "$filter_subdir")"
305 ;;
306esac
307
308git rev-list --reverse --topo-order --default HEAD \
309 --parents --simplify-merges --stdin "$@" <../parse >../revs ||
310 die "Could not get the commits"
311commits=$(wc -l <../revs | tr -d " ")
312
313test $commits -eq 0 && die "Found nothing to rewrite"
314
315# Rewrite the commits
316report_progress ()
317{
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
322
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))
331 fi
332 progress=" ($elapsed seconds passed, remaining $remaining predicted)"
333 fi
334 printf "\rRewrite $commit ($count/$commits)$progress "
335}
336
337git_filter_branch__commit_count=0
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
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
356eval "$filter_setup" < /dev/null ||
357 die "filter setup failed: $filter_setup"
358
359while read commit parents; do
360 git_filter_branch__commit_count=$(($git_filter_branch__commit_count+1))
361
362 report_progress
363 test -f "$workdir"/../map/$commit && continue
364
365 case "$filter_subdir" in
366 "")
367 if test -n "$need_index"
368 then
369 GIT_ALLOW_NULL_SHA1=1 git read-tree -i -m $commit
370 fi
371 ;;
372 *)
373 # The commit may not have the subdirectory at all
374 err=$(GIT_ALLOW_NULL_SHA1=1 \
375 git read-tree -i -m $commit:"$filter_subdir" 2>&1) || {
376 if ! git rev-parse -q --verify $commit:"$filter_subdir"
377 then
378 rm -f "$GIT_INDEX_FILE"
379 else
380 echo >&2 "$err"
381 false
382 fi
383 }
384 esac || die "Could not initialize the index"
385
386 GIT_COMMIT=$commit
387 export GIT_COMMIT
388 git cat-file commit "$commit" >../commit ||
389 die "Cannot read commit $commit"
390
391 eval "$(set_ident <../commit)" ||
392 die "setting author/committer failed for commit $commit"
393 eval "$filter_env" < /dev/null ||
394 die "env filter failed: $filter_env"
395
396 if [ "$filter_tree" ]; then
397 git checkout-index -f -u -a ||
398 die "Could not checkout the index"
399 # files that $commit removed are now still in the working tree;
400 # remove them, else they would be added again
401 git clean -d -q -f -x
402 eval "$filter_tree" < /dev/null ||
403 die "tree filter failed: $filter_tree"
404
405 (
406 git diff-index -r --name-only --ignore-submodules $commit -- &&
407 git ls-files --others
408 ) > "$tempdir"/tree-state || exit
409 git update-index --add --replace --remove --stdin \
410 < "$tempdir"/tree-state || exit
411 fi
412
413 eval "$filter_index" < /dev/null ||
414 die "index filter failed: $filter_index"
415
416 parentstr=
417 for parent in $parents; do
418 for reparent in $(map "$parent"); do
419 case "$parentstr " in
420 *" -p $reparent "*)
421 ;;
422 *)
423 parentstr="$parentstr -p $reparent"
424 ;;
425 esac
426 done
427 done
428 if [ "$filter_parent" ]; then
429 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
430 die "parent filter failed: $filter_parent"
431 fi
432
433 {
434 while IFS='' read -r header_line && test -n "$header_line"
435 do
436 # skip header lines...
437 :;
438 done
439 # and output the actual commit message
440 cat
441 } <../commit |
442 eval "$filter_msg" > ../message ||
443 die "msg filter failed: $filter_msg"
444
445 if test -n "$need_index"
446 then
447 tree=$(git write-tree)
448 else
449 tree=$(git rev-parse "$commit^{tree}")
450 fi
451 workdir=$workdir @SHELL_PATH@ -c "$filter_commit" "git commit-tree" \
452 "$tree" $parentstr < ../message > ../map/$commit ||
453 die "could not write rewritten commit"
454done <../revs
455
456# If we are filtering for paths, as in the case of a subdirectory
457# filter, it is possible that a specified head is not in the set of
458# rewritten commits, because it was pruned by the revision walker.
459# Ancestor remapping fixes this by mapping these heads to the unique
460# nearest ancestor that survived the pruning.
461
462if test "$remap_to_ancestor" = t
463then
464 while read ref
465 do
466 sha1=$(git rev-parse "$ref"^0)
467 test -f "$workdir"/../map/$sha1 && continue
468 ancestor=$(git rev-list --simplify-merges -1 "$ref" "$@")
469 test "$ancestor" && echo $(map $ancestor) >> "$workdir"/../map/$sha1
470 done < "$tempdir"/heads
471fi
472
473# Finally update the refs
474
475_x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
476_x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
477echo
478while read ref
479do
480 # avoid rewriting a ref twice
481 test -f "$orig_namespace$ref" && continue
482
483 sha1=$(git rev-parse "$ref"^0)
484 rewritten=$(map $sha1)
485
486 test $sha1 = "$rewritten" &&
487 warn "WARNING: Ref '$ref' is unchanged" &&
488 continue
489
490 case "$rewritten" in
491 '')
492 echo "Ref '$ref' was deleted"
493 git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
494 die "Could not delete $ref"
495 ;;
496 $_x40)
497 echo "Ref '$ref' was rewritten"
498 if ! git update-ref -m "filter-branch: rewrite" \
499 "$ref" $rewritten $sha1 2>/dev/null; then
500 if test $(git cat-file -t "$ref") = tag; then
501 if test -z "$filter_tag_name"; then
502 warn "WARNING: You said to rewrite tagged commits, but not the corresponding tag."
503 warn "WARNING: Perhaps use '--tag-name-filter cat' to rewrite the tag."
504 fi
505 else
506 die "Could not rewrite $ref"
507 fi
508 fi
509 ;;
510 *)
511 # NEEDSWORK: possibly add -Werror, making this an error
512 warn "WARNING: '$ref' was rewritten into multiple commits:"
513 warn "$rewritten"
514 warn "WARNING: Ref '$ref' points to the first one now."
515 rewritten=$(echo "$rewritten" | head -n 1)
516 git update-ref -m "filter-branch: rewrite to first" \
517 "$ref" $rewritten $sha1 ||
518 die "Could not rewrite $ref"
519 ;;
520 esac
521 git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1 ||
522 exit
523done < "$tempdir"/heads
524
525# TODO: This should possibly go, with the semantics that all positive given
526# refs are updated, and their original heads stored in refs/original/
527# Filter tags
528
529if [ "$filter_tag_name" ]; then
530 git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags |
531 while read sha1 type ref; do
532 ref="${ref#refs/tags/}"
533 # XXX: Rewrite tagged trees as well?
534 if [ "$type" != "commit" -a "$type" != "tag" ]; then
535 continue;
536 fi
537
538 if [ "$type" = "tag" ]; then
539 # Dereference to a commit
540 sha1t="$sha1"
541 sha1="$(git rev-parse -q "$sha1"^{commit})" || continue
542 fi
543
544 [ -f "../map/$sha1" ] || continue
545 new_sha1="$(cat "../map/$sha1")"
546 GIT_COMMIT="$sha1"
547 export GIT_COMMIT
548 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
549 die "tag name filter failed: $filter_tag_name"
550
551 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
552
553 if [ "$type" = "tag" ]; then
554 new_sha1=$( ( printf 'object %s\ntype commit\ntag %s\n' \
555 "$new_sha1" "$new_ref"
556 git cat-file tag "$ref" |
557 sed -n \
558 -e '1,/^$/{
559 /^object /d
560 /^type /d
561 /^tag /d
562 }' \
563 -e '/^-----BEGIN PGP SIGNATURE-----/q' \
564 -e 'p' ) |
565 git hash-object -t tag -w --stdin) ||
566 die "Could not create new tag object for $ref"
567 if git cat-file tag "$ref" | \
568 sane_grep '^-----BEGIN PGP SIGNATURE-----' >/dev/null 2>&1
569 then
570 warn "gpg signature stripped from tag object $sha1t"
571 fi
572 fi
573
574 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
575 die "Could not write tag $new_ref"
576 done
577fi
578
579unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
580unset GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
581unset GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL GIT_COMMITTER_DATE
582test -z "$ORIG_GIT_DIR" || {
583 GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR
584}
585test -z "$ORIG_GIT_WORK_TREE" || {
586 GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" &&
587 export GIT_WORK_TREE
588}
589test -z "$ORIG_GIT_INDEX_FILE" || {
590 GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" &&
591 export GIT_INDEX_FILE
592}
593test -z "$ORIG_GIT_AUTHOR_NAME" || {
594 GIT_AUTHOR_NAME="$ORIG_GIT_AUTHOR_NAME" &&
595 export GIT_AUTHOR_NAME
596}
597test -z "$ORIG_GIT_AUTHOR_EMAIL" || {
598 GIT_AUTHOR_EMAIL="$ORIG_GIT_AUTHOR_EMAIL" &&
599 export GIT_AUTHOR_EMAIL
600}
601test -z "$ORIG_GIT_AUTHOR_DATE" || {
602 GIT_AUTHOR_DATE="$ORIG_GIT_AUTHOR_DATE" &&
603 export GIT_AUTHOR_DATE
604}
605test -z "$ORIG_GIT_COMMITTER_NAME" || {
606 GIT_COMMITTER_NAME="$ORIG_GIT_COMMITTER_NAME" &&
607 export GIT_COMMITTER_NAME
608}
609test -z "$ORIG_GIT_COMMITTER_EMAIL" || {
610 GIT_COMMITTER_EMAIL="$ORIG_GIT_COMMITTER_EMAIL" &&
611 export GIT_COMMITTER_EMAIL
612}
613test -z "$ORIG_GIT_COMMITTER_DATE" || {
614 GIT_COMMITTER_DATE="$ORIG_GIT_COMMITTER_DATE" &&
615 export GIT_COMMITTER_DATE
616}
617
618if test -n "$state_branch"
619then
620 echo "Saving rewrite state to $state_branch" 1>&2
621 state_blob=$(
622 perl -e'opendir D, "../map" or die;
623 open H, "|-", "git hash-object -w --stdin" or die;
624 foreach (sort readdir(D)) {
625 next if m/^\.\.?$/;
626 open F, "<../map/$_" or die;
627 chomp($f = <F>);
628 print H "$_:$f\n" or die;
629 }
630 close(H) or die;' || die "Unable to save state")
631 state_tree=$(printf '100644 blob %s\tfilter.map\n' "$state_blob" | git mktree)
632 if test -n "$state_commit"
633 then
634 state_commit=$(echo "Sync" | git commit-tree "$state_tree" -p "$state_commit")
635 else
636 state_commit=$(echo "Sync" | git commit-tree "$state_tree" )
637 fi
638 git update-ref "$state_branch" "$state_commit"
639fi
640
641cd "$orig_dir"
642rm -rf "$tempdir"
643
644trap - 0
645
646if [ "$(is_bare_repository)" = false ]; then
647 git read-tree -u -m HEAD || exit
648fi
649
650exit 0