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="[--env-filter <command>] [--tree-filter <command>]
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>...]"
90
91OPTIONS_SPEC=
92. git-sh-setup
93
94if [ "$(is_bare_repository)" = false ]; then
95 require_clean_work_tree 'rewrite branches'
96fi
97
98tempdir=.git-rewrite
99filter_env=
100filter_tree=
101filter_index=
102filter_parent=
103filter_msg=cat
104filter_commit=
105filter_tag_name=
106filter_subdir=
107orig_namespace=refs/original/
108force=
109prune_empty=
110remap_to_ancestor=
111while :
112do
113 case "$1" in
114 --)
115 shift
116 break
117 ;;
118 --force|-f)
119 shift
120 force=t
121 continue
122 ;;
123 --remap-to-ancestor)
124 # deprecated ($remap_to_ancestor is set now automatically)
125 shift
126 remap_to_ancestor=t
127 continue
128 ;;
129 --prune-empty)
130 shift
131 prune_empty=t
132 continue
133 ;;
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 ;;
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)
167 filter_commit="$functions; $OPTARG"
168 ;;
169 --tag-name-filter)
170 filter_tag_name="$OPTARG"
171 ;;
172 --subdirectory-filter)
173 filter_subdir="$OPTARG"
174 remap_to_ancestor=t
175 ;;
176 --original)
177 orig_namespace=$(expr "$OPTARG/" : '\(.*[^/]\)/*$')/
178 ;;
179 *)
180 usage
181 ;;
182 esac
183done
184
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*)
193 die "Cannot set --prune-empty and --commit-filter at the same time"
194esac
195
196case "$force" in
197t)
198 rm -rf "$tempdir"
199;;
200'')
201 test -d "$tempdir" &&
202 die "$tempdir already exists, please remove it"
203esac
204orig_dir=$(pwd)
205mkdir -p "$tempdir/t" &&
206tempdir="$(cd "$tempdir"; pwd)" &&
207cd "$tempdir/t" &&
208workdir="$(pwd)" ||
209die ""
210
211# Remove tempdir on exit
212trap 'cd "$orig_dir"; rm -rf "$tempdir"' 0
213
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
220# Make sure refs/original is empty
221git for-each-ref > "$tempdir"/backup-refs || exit
222while read sha1 type name
223do
224 case "$force,$name" in
225 ,$orig_namespace*)
226 die "Cannot create a new backup.
227A previous backup already exists in $orig_namespace
228Force overwriting the backup with -f"
229 ;;
230 t,$orig_namespace*)
231 git update-ref -d "$name" $sha1
232 ;;
233 esac
234done < "$tempdir"/backup-refs
235
236# The refs should be updated if their heads were rewritten
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
240
241test -s "$tempdir"/heads ||
242 die "Which ref do you want to rewrite?"
243
244GIT_INDEX_FILE="$(pwd)/../index"
245export GIT_INDEX_FILE
246
247# map old->new commit ids for rewriting parents
248mkdir ../map || die "Could not create map/ directory"
249
250# we need "--" only if there are no path arguments in $@
251nonrevs=$(git rev-parse --no-revs "$@") || exit
252if test -z "$nonrevs"
253then
254 dashdash=--
255else
256 dashdash=
257 remap_to_ancestor=t
258fi
259
260git rev-parse --revs-only "$@" >../parse
261
262case "$filter_subdir" in
263"")
264 eval set -- "$(git rev-parse --sq --no-revs "$@")"
265 ;;
266*)
267 eval set -- "$(git rev-parse --sq --no-revs "$@" $dashdash \
268 "$filter_subdir")"
269 ;;
270esac
271
272git rev-list --reverse --topo-order --default HEAD \
273 --parents --simplify-merges --stdin "$@" <../parse >../revs ||
274 die "Could not get the commits"
275commits=$(wc -l <../revs | tr -d " ")
276
277test $commits -eq 0 && die "Found nothing to rewrite"
278
279# Rewrite the commits
280report_progress ()
281{
282 if test -n "$progress" &&
283 test $git_filter_branch__commit_count -gt $next_sample_at
284 then
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
291 then
292 next_sample_at=$(( ($elapsed + 1) * $count / $elapsed ))
293 else
294 next_sample_at=$(($next_sample_at + 1))
295 fi
296 progress=" ($elapsed seconds passed, remaining $remaining predicted)"
297 fi
298 printf "\rRewrite $commit ($count/$commits)$progress "
299}
300
301git_filter_branch__commit_count=0
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
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
320while read commit parents; do
321 git_filter_branch__commit_count=$(($git_filter_branch__commit_count+1))
322
323 report_progress
324
325 case "$filter_subdir" in
326 "")
327 if test -n "$need_index"
328 then
329 GIT_ALLOW_NULL_SHA1=1 git read-tree -i -m $commit
330 fi
331 ;;
332 *)
333 # The commit may not have the subdirectory at all
334 err=$(GIT_ALLOW_NULL_SHA1=1 \
335 git read-tree -i -m $commit:"$filter_subdir" 2>&1) || {
336 if ! git rev-parse -q --verify $commit:"$filter_subdir"
337 then
338 rm -f "$GIT_INDEX_FILE"
339 else
340 echo >&2 "$err"
341 false
342 fi
343 }
344 esac || die "Could not initialize the index"
345
346 GIT_COMMIT=$commit
347 export GIT_COMMIT
348 git cat-file commit "$commit" >../commit ||
349 die "Cannot read commit $commit"
350
351 eval "$(set_ident <../commit)" ||
352 die "setting author/committer failed for commit $commit"
353 eval "$filter_env" < /dev/null ||
354 die "env filter failed: $filter_env"
355
356 if [ "$filter_tree" ]; then
357 git checkout-index -f -u -a ||
358 die "Could not checkout the index"
359 # files that $commit removed are now still in the working tree;
360 # remove them, else they would be added again
361 git clean -d -q -f -x
362 eval "$filter_tree" < /dev/null ||
363 die "tree filter failed: $filter_tree"
364
365 (
366 git diff-index -r --name-only --ignore-submodules $commit -- &&
367 git ls-files --others
368 ) > "$tempdir"/tree-state || exit
369 git update-index --add --replace --remove --stdin \
370 < "$tempdir"/tree-state || exit
371 fi
372
373 eval "$filter_index" < /dev/null ||
374 die "index filter failed: $filter_index"
375
376 parentstr=
377 for parent in $parents; do
378 for reparent in $(map "$parent"); do
379 case "$parentstr " in
380 *" -p $reparent "*)
381 ;;
382 *)
383 parentstr="$parentstr -p $reparent"
384 ;;
385 esac
386 done
387 done
388 if [ "$filter_parent" ]; then
389 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
390 die "parent filter failed: $filter_parent"
391 fi
392
393 {
394 while IFS='' read -r header_line && test -n "$header_line"
395 do
396 # skip header lines...
397 :;
398 done
399 # and output the actual commit message
400 cat
401 } <../commit |
402 eval "$filter_msg" > ../message ||
403 die "msg filter failed: $filter_msg"
404
405 if test -n "$need_index"
406 then
407 tree=$(git write-tree)
408 else
409 tree=$(git rev-parse "$commit^{tree}")
410 fi
411 workdir=$workdir @SHELL_PATH@ -c "$filter_commit" "git commit-tree" \
412 "$tree" $parentstr < ../message > ../map/$commit ||
413 die "could not write rewritten commit"
414done <../revs
415
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.
421
422if test "$remap_to_ancestor" = t
423then
424 while read ref
425 do
426 sha1=$(git rev-parse "$ref"^0)
427 test -f "$workdir"/../map/$sha1 && continue
428 ancestor=$(git rev-list --simplify-merges -1 "$ref" "$@")
429 test "$ancestor" && echo $(map $ancestor) >> "$workdir"/../map/$sha1
430 done < "$tempdir"/heads
431fi
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"
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"
455 ;;
456 $_x40)
457 echo "Ref '$ref' was rewritten"
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
469 ;;
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
481 git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1 ||
482 exit
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
488
489if [ "$filter_tag_name" ]; then
490 git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags |
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"
501 sha1="$(git rev-parse -q "$sha1"^{commit})" || continue
502 fi
503
504 [ -f "../map/$sha1" ] || continue
505 new_sha1="$(cat "../map/$sha1")"
506 GIT_COMMIT="$sha1"
507 export GIT_COMMIT
508 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
509 die "tag name filter failed: $filter_tag_name"
510
511 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
512
513 if [ "$type" = "tag" ]; then
514 new_sha1=$( ( printf 'object %s\ntype commit\ntag %s\n' \
515 "$new_sha1" "$new_ref"
516 git cat-file tag "$ref" |
517 sed -n \
518 -e '1,/^$/{
519 /^object /d
520 /^type /d
521 /^tag /d
522 }' \
523 -e '/^-----BEGIN PGP SIGNATURE-----/q' \
524 -e 'p' ) |
525 git mktag) ||
526 die "Could not create new tag object for $ref"
527 if git cat-file tag "$ref" | \
528 sane_grep '^-----BEGIN PGP SIGNATURE-----' >/dev/null 2>&1
529 then
530 warn "gpg signature stripped from tag object $sha1t"
531 fi
532 fi
533
534 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
535 die "Could not write tag $new_ref"
536 done
537fi
538
539cd "$orig_dir"
540rm -rf "$tempdir"
541
542trap - 0
543
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
557if [ "$(is_bare_repository)" = false ]; then
558 git read-tree -u -m HEAD || exit
559fi
560
561exit 0