handle_revision_arg: add handle_dotdot() helper
[git/debian.git] / git-filter-branch.sh
blob2b8cdba157d9cd822acc88b7ec58cecda0149b85
1 #!/bin/sh
3 # Rewrite revision history
4 # Copyright (c) Petr Baudis, 2006
5 # Minimal changes to "port" it to core-git (c) Johannes Schindelin, 2007
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.
11 # The following functions will also be available in the commit filter:
13 functions=$(cat << \EOF
14 warn () {
15 echo "$*" >&2
18 map()
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"
29 # if you run 'skip_commit "$@"' in a commit filter, it will print
30 # the (mapped) parents, effectively skipping the commit.
32 skip_commit()
34 shift;
35 while [ -n "$1" ];
37 shift;
38 map "$1";
39 shift;
40 done;
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.
45 git_commit_non_empty_tree()
47 if test $# = 3 && test "$1" = $(git rev-parse "$3^{tree}"); then
48 map "$3"
49 elif test $# = 1 && test "$1" = 4b825dc642cb6eb9a060e54bf8d69288fbee4904; then
51 else
52 git commit-tree "$@"
55 # override die(): this version puts in an extra line break, so that
56 # the progress is still visible
58 die()
60 echo >&2
61 echo "$*" >&2
62 exit 1
64 EOF
67 eval "$functions"
69 finish_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"
78 set_ident () {
79 parse_ident_from_commit author AUTHOR committer COMMITTER
80 finish_ident AUTHOR
81 finish_ident COMMITTER
84 USAGE="[--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>...]"
91 OPTIONS_SPEC=
92 . git-sh-setup
94 if [ "$(is_bare_repository)" = false ]; then
95 require_clean_work_tree 'rewrite branches'
98 tempdir=.git-rewrite
99 filter_env=
100 filter_tree=
101 filter_index=
102 filter_parent=
103 filter_msg=cat
104 filter_commit=
105 filter_tag_name=
106 filter_subdir=
107 orig_namespace=refs/original/
108 force=
109 prune_empty=
110 remap_to_ancestor=
111 while :
113 case "$1" in
115 shift
116 break
118 --force|-f)
119 shift
120 force=t
121 continue
123 --remap-to-ancestor)
124 # deprecated ($remap_to_ancestor is set now automatically)
125 shift
126 remap_to_ancestor=t
127 continue
129 --prune-empty)
130 shift
131 prune_empty=t
132 continue
137 break;
138 esac
140 # all switches take one argument
141 ARG="$1"
142 case "$#" in 1) usage ;; esac
143 shift
144 OPTARG="$1"
145 shift
147 case "$ARG" in
149 tempdir="$OPTARG"
151 --env-filter)
152 filter_env="$OPTARG"
154 --tree-filter)
155 filter_tree="$OPTARG"
157 --index-filter)
158 filter_index="$OPTARG"
160 --parent-filter)
161 filter_parent="$OPTARG"
163 --msg-filter)
164 filter_msg="$OPTARG"
166 --commit-filter)
167 filter_commit="$functions; $OPTARG"
169 --tag-name-filter)
170 filter_tag_name="$OPTARG"
172 --subdirectory-filter)
173 filter_subdir="$OPTARG"
174 remap_to_ancestor=t
176 --original)
177 orig_namespace=$(expr "$OPTARG/" : '\(.*[^/]\)/*$')/
180 usage
182 esac
183 done
185 case "$prune_empty,$filter_commit" in
187 filter_commit='git commit-tree "$@"';;
189 filter_commit="$functions;"' git_commit_non_empty_tree "$@"';;
193 die "Cannot set --prune-empty and --commit-filter at the same time"
194 esac
196 case "$force" in
198 rm -rf "$tempdir"
201 test -d "$tempdir" &&
202 die "$tempdir already exists, please remove it"
203 esac
204 orig_dir=$(pwd)
205 mkdir -p "$tempdir/t" &&
206 tempdir="$(cd "$tempdir"; pwd)" &&
207 cd "$tempdir/t" &&
208 workdir="$(pwd)" ||
209 die ""
211 # Remove tempdir on exit
212 trap 'cd "$orig_dir"; rm -rf "$tempdir"' 0
214 ORIG_GIT_DIR="$GIT_DIR"
215 ORIG_GIT_WORK_TREE="$GIT_WORK_TREE"
216 ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE"
217 GIT_WORK_TREE=.
218 export GIT_DIR GIT_WORK_TREE
220 # Make sure refs/original is empty
221 git for-each-ref > "$tempdir"/backup-refs || exit
222 while read sha1 type name
224 case "$force,$name" in
225 ,$orig_namespace*)
226 die "Cannot create a new backup.
227 A previous backup already exists in $orig_namespace
228 Force overwriting the backup with -f"
230 t,$orig_namespace*)
231 git update-ref -d "$name" $sha1
233 esac
234 done < "$tempdir"/backup-refs
236 # The refs should be updated if their heads were rewritten
237 git rev-parse --no-flags --revs-only --symbolic-full-name \
238 --default HEAD "$@" > "$tempdir"/raw-heads || exit
239 sed -e '/^^/d' "$tempdir"/raw-heads >"$tempdir"/heads
241 test -s "$tempdir"/heads ||
242 die "Which ref do you want to rewrite?"
244 GIT_INDEX_FILE="$(pwd)/../index"
245 export GIT_INDEX_FILE
247 # map old->new commit ids for rewriting parents
248 mkdir ../map || die "Could not create map/ directory"
250 # we need "--" only if there are no path arguments in $@
251 nonrevs=$(git rev-parse --no-revs "$@") || exit
252 if test -z "$nonrevs"
253 then
254 dashdash=--
255 else
256 dashdash=
257 remap_to_ancestor=t
260 git rev-parse --revs-only "$@" >../parse
262 case "$filter_subdir" in
264 eval set -- "$(git rev-parse --sq --no-revs "$@")"
267 eval set -- "$(git rev-parse --sq --no-revs "$@" $dashdash \
268 "$filter_subdir")"
270 esac
272 git rev-list --reverse --topo-order --default HEAD \
273 --parents --simplify-merges --stdin "$@" <../parse >../revs ||
274 die "Could not get the commits"
275 commits=$(wc -l <../revs | tr -d " ")
277 test $commits -eq 0 && die "Found nothing to rewrite"
279 # Rewrite the commits
280 report_progress ()
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
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))
296 progress=" ($elapsed seconds passed, remaining $remaining predicted)"
298 printf "\rRewrite $commit ($count/$commits)$progress "
301 git_filter_branch__commit_count=0
303 progress= start_timestamp=
304 if date '+%s' 2>/dev/null | grep -q '^[0-9][0-9]*$'
305 then
306 next_sample_at=0
307 progress="dummy to ensure this is not empty"
308 start_timestamp=$(date '+%s')
311 if test -n "$filter_index" ||
312 test -n "$filter_tree" ||
313 test -n "$filter_subdir"
314 then
315 need_index=t
316 else
317 need_index=
320 while read commit parents; do
321 git_filter_branch__commit_count=$(($git_filter_branch__commit_count+1))
323 report_progress
325 case "$filter_subdir" in
327 if test -n "$need_index"
328 then
329 GIT_ALLOW_NULL_SHA1=1 git read-tree -i -m $commit
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
344 esac || die "Could not initialize the index"
346 GIT_COMMIT=$commit
347 export GIT_COMMIT
348 git cat-file commit "$commit" >../commit ||
349 die "Cannot read commit $commit"
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"
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"
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
373 eval "$filter_index" < /dev/null ||
374 die "index filter failed: $filter_index"
376 parentstr=
377 for parent in $parents; do
378 for reparent in $(map "$parent"); do
379 case "$parentstr " in
380 *" -p $reparent "*)
383 parentstr="$parentstr -p $reparent"
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"
394 while IFS='' read -r header_line && test -n "$header_line"
396 # skip header lines...
398 done
399 # and output the actual commit message
401 } <../commit |
402 eval "$filter_msg" > ../message ||
403 die "msg filter failed: $filter_msg"
405 if test -n "$need_index"
406 then
407 tree=$(git write-tree)
408 else
409 tree=$(git rev-parse "$commit^{tree}")
411 workdir=$workdir @SHELL_PATH@ -c "$filter_commit" "git commit-tree" \
412 "$tree" $parentstr < ../message > ../map/$commit ||
413 die "could not write rewritten commit"
414 done <../revs
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.
422 if test "$remap_to_ancestor" = t
423 then
424 while read ref
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
433 # Finally update the refs
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"
437 echo
438 while read ref
440 # avoid rewriting a ref twice
441 test -f "$orig_namespace$ref" && continue
443 sha1=$(git rev-parse "$ref"^0)
444 rewritten=$(map $sha1)
446 test $sha1 = "$rewritten" &&
447 warn "WARNING: Ref '$ref' is unchanged" &&
448 continue
450 case "$rewritten" in
452 echo "Ref '$ref' was deleted"
453 git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
454 die "Could not delete $ref"
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."
465 else
466 die "Could not rewrite $ref"
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"
480 esac
481 git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1 ||
482 exit
483 done < "$tempdir"/heads
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
489 if [ "$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;
498 if [ "$type" = "tag" ]; then
499 # Dereference to a commit
500 sha1t="$sha1"
501 sha1="$(git rev-parse -q "$sha1"^{commit})" || continue
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"
511 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
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"
534 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
535 die "Could not write tag $new_ref"
536 done
539 cd "$orig_dir"
540 rm -rf "$tempdir"
542 trap - 0
544 unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
545 test -z "$ORIG_GIT_DIR" || {
546 GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR
548 test -z "$ORIG_GIT_WORK_TREE" || {
549 GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" &&
550 export GIT_WORK_TREE
552 test -z "$ORIG_GIT_INDEX_FILE" || {
553 GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" &&
554 export GIT_INDEX_FILE
557 if [ "$(is_bare_repository)" = false ]; then
558 git read-tree -u -m HEAD || exit
561 exit 0