Merge branch 'master' of git://git.kernel.org/pub/scm/git/git
[git/jnareb-git.git] / git-filter-branch.sh
blobac2a005fdb23c48d8451188ffd7b1c8194b0295f
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 else
50 git commit-tree "$@"
53 # override die(): this version puts in an extra line break, so that
54 # the progress is still visible
56 die()
58 echo >&2
59 echo "$*" >&2
60 exit 1
62 EOF
65 eval "$functions"
67 finish_ident() {
68 # Ensure non-empty id name.
69 echo "case \"\$GIT_$1_NAME\" in \"\") GIT_$1_NAME=\"\${GIT_$1_EMAIL%%@*}\" && export GIT_$1_NAME;; esac"
70 # And make sure everything is exported.
71 echo "export GIT_$1_NAME"
72 echo "export GIT_$1_EMAIL"
73 echo "export GIT_$1_DATE"
76 set_ident () {
77 parse_ident_from_commit author AUTHOR committer COMMITTER
78 finish_ident AUTHOR
79 finish_ident COMMITTER
82 USAGE="[--env-filter <command>] [--tree-filter <command>]
83 [--index-filter <command>] [--parent-filter <command>]
84 [--msg-filter <command>] [--commit-filter <command>]
85 [--tag-name-filter <command>] [--subdirectory-filter <directory>]
86 [--original <namespace>] [-d <directory>] [-f | --force]
87 [<rev-list options>...]"
89 OPTIONS_SPEC=
90 . git-sh-setup
92 if [ "$(is_bare_repository)" = false ]; then
93 require_clean_work_tree 'rewrite branches'
96 tempdir=.git-rewrite
97 filter_env=
98 filter_tree=
99 filter_index=
100 filter_parent=
101 filter_msg=cat
102 filter_commit=
103 filter_tag_name=
104 filter_subdir=
105 orig_namespace=refs/original/
106 force=
107 prune_empty=
108 remap_to_ancestor=
109 while :
111 case "$1" in
113 shift
114 break
116 --force|-f)
117 shift
118 force=t
119 continue
121 --remap-to-ancestor)
122 # deprecated ($remap_to_ancestor is set now automatically)
123 shift
124 remap_to_ancestor=t
125 continue
127 --prune-empty)
128 shift
129 prune_empty=t
130 continue
135 break;
136 esac
138 # all switches take one argument
139 ARG="$1"
140 case "$#" in 1) usage ;; esac
141 shift
142 OPTARG="$1"
143 shift
145 case "$ARG" in
147 tempdir="$OPTARG"
149 --env-filter)
150 filter_env="$OPTARG"
152 --tree-filter)
153 filter_tree="$OPTARG"
155 --index-filter)
156 filter_index="$OPTARG"
158 --parent-filter)
159 filter_parent="$OPTARG"
161 --msg-filter)
162 filter_msg="$OPTARG"
164 --commit-filter)
165 filter_commit="$functions; $OPTARG"
167 --tag-name-filter)
168 filter_tag_name="$OPTARG"
170 --subdirectory-filter)
171 filter_subdir="$OPTARG"
172 remap_to_ancestor=t
174 --original)
175 orig_namespace=$(expr "$OPTARG/" : '\(.*[^/]\)/*$')/
178 usage
180 esac
181 done
183 case "$prune_empty,$filter_commit" in
185 filter_commit='git commit-tree "$@"';;
187 filter_commit="$functions;"' git_commit_non_empty_tree "$@"';;
191 die "Cannot set --prune-empty and --commit-filter at the same time"
192 esac
194 case "$force" in
196 rm -rf "$tempdir"
199 test -d "$tempdir" &&
200 die "$tempdir already exists, please remove it"
201 esac
202 orig_dir=$(pwd)
203 mkdir -p "$tempdir/t" &&
204 tempdir="$(cd "$tempdir"; pwd)" &&
205 cd "$tempdir/t" &&
206 workdir="$(pwd)" ||
207 die ""
209 # Remove tempdir on exit
210 trap 'cd "$orig_dir"; rm -rf "$tempdir"' 0
212 ORIG_GIT_DIR="$GIT_DIR"
213 ORIG_GIT_WORK_TREE="$GIT_WORK_TREE"
214 ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE"
215 GIT_WORK_TREE=.
216 export GIT_DIR GIT_WORK_TREE
218 # Make sure refs/original is empty
219 git for-each-ref > "$tempdir"/backup-refs || exit
220 while read sha1 type name
222 case "$force,$name" in
223 ,$orig_namespace*)
224 die "Cannot create a new backup.
225 A previous backup already exists in $orig_namespace
226 Force overwriting the backup with -f"
228 t,$orig_namespace*)
229 git update-ref -d "$name" $sha1
231 esac
232 done < "$tempdir"/backup-refs
234 # The refs should be updated if their heads were rewritten
235 git rev-parse --no-flags --revs-only --symbolic-full-name \
236 --default HEAD "$@" > "$tempdir"/raw-heads || exit
237 sed -e '/^^/d' "$tempdir"/raw-heads >"$tempdir"/heads
239 test -s "$tempdir"/heads ||
240 die "Which ref do you want to rewrite?"
242 GIT_INDEX_FILE="$(pwd)/../index"
243 export GIT_INDEX_FILE
245 # map old->new commit ids for rewriting parents
246 mkdir ../map || die "Could not create map/ directory"
248 # we need "--" only if there are no path arguments in $@
249 nonrevs=$(git rev-parse --no-revs "$@") || exit
250 if test -z "$nonrevs"
251 then
252 dashdash=--
253 else
254 dashdash=
255 remap_to_ancestor=t
258 rev_args=$(git rev-parse --revs-only "$@")
260 case "$filter_subdir" in
262 eval set -- "$(git rev-parse --sq --no-revs "$@")"
265 eval set -- "$(git rev-parse --sq --no-revs "$@" $dashdash \
266 "$filter_subdir")"
268 esac
270 git rev-list --reverse --topo-order --default HEAD \
271 --parents --simplify-merges $rev_args "$@" > ../revs ||
272 die "Could not get the commits"
273 commits=$(wc -l <../revs | tr -d " ")
275 test $commits -eq 0 && die "Found nothing to rewrite"
277 # Rewrite the commits
279 git_filter_branch__commit_count=0
280 while read commit parents; do
281 git_filter_branch__commit_count=$(($git_filter_branch__commit_count+1))
282 printf "\rRewrite $commit ($git_filter_branch__commit_count/$commits)"
284 case "$filter_subdir" in
286 git read-tree -i -m $commit
289 # The commit may not have the subdirectory at all
290 err=$(git read-tree -i -m $commit:"$filter_subdir" 2>&1) || {
291 if ! git rev-parse -q --verify $commit:"$filter_subdir"
292 then
293 rm -f "$GIT_INDEX_FILE"
294 else
295 echo >&2 "$err"
296 false
299 esac || die "Could not initialize the index"
301 GIT_COMMIT=$commit
302 export GIT_COMMIT
303 git cat-file commit "$commit" >../commit ||
304 die "Cannot read commit $commit"
306 eval "$(set_ident <../commit)" ||
307 die "setting author/committer failed for commit $commit"
308 eval "$filter_env" < /dev/null ||
309 die "env filter failed: $filter_env"
311 if [ "$filter_tree" ]; then
312 git checkout-index -f -u -a ||
313 die "Could not checkout the index"
314 # files that $commit removed are now still in the working tree;
315 # remove them, else they would be added again
316 git clean -d -q -f -x
317 eval "$filter_tree" < /dev/null ||
318 die "tree filter failed: $filter_tree"
321 git diff-index -r --name-only --ignore-submodules $commit &&
322 git ls-files --others
323 ) > "$tempdir"/tree-state || exit
324 git update-index --add --replace --remove --stdin \
325 < "$tempdir"/tree-state || exit
328 eval "$filter_index" < /dev/null ||
329 die "index filter failed: $filter_index"
331 parentstr=
332 for parent in $parents; do
333 for reparent in $(map "$parent"); do
334 parentstr="$parentstr -p $reparent"
335 done
336 done
337 if [ "$filter_parent" ]; then
338 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
339 die "parent filter failed: $filter_parent"
342 sed -e '1,/^$/d' <../commit | \
343 eval "$filter_msg" > ../message ||
344 die "msg filter failed: $filter_msg"
345 workdir=$workdir @SHELL_PATH@ -c "$filter_commit" "git commit-tree" \
346 $(git write-tree) $parentstr < ../message > ../map/$commit ||
347 die "could not write rewritten commit"
348 done <../revs
350 # If we are filtering for paths, as in the case of a subdirectory
351 # filter, it is possible that a specified head is not in the set of
352 # rewritten commits, because it was pruned by the revision walker.
353 # Ancestor remapping fixes this by mapping these heads to the unique
354 # nearest ancestor that survived the pruning.
356 if test "$remap_to_ancestor" = t
357 then
358 while read ref
360 sha1=$(git rev-parse "$ref"^0)
361 test -f "$workdir"/../map/$sha1 && continue
362 ancestor=$(git rev-list --simplify-merges -1 "$ref" "$@")
363 test "$ancestor" && echo $(map $ancestor) >> "$workdir"/../map/$sha1
364 done < "$tempdir"/heads
367 # Finally update the refs
369 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
370 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
371 echo
372 while read ref
374 # avoid rewriting a ref twice
375 test -f "$orig_namespace$ref" && continue
377 sha1=$(git rev-parse "$ref"^0)
378 rewritten=$(map $sha1)
380 test $sha1 = "$rewritten" &&
381 warn "WARNING: Ref '$ref' is unchanged" &&
382 continue
384 case "$rewritten" in
386 echo "Ref '$ref' was deleted"
387 git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
388 die "Could not delete $ref"
390 $_x40)
391 echo "Ref '$ref' was rewritten"
392 if ! git update-ref -m "filter-branch: rewrite" \
393 "$ref" $rewritten $sha1 2>/dev/null; then
394 if test $(git cat-file -t "$ref") = tag; then
395 if test -z "$filter_tag_name"; then
396 warn "WARNING: You said to rewrite tagged commits, but not the corresponding tag."
397 warn "WARNING: Perhaps use '--tag-name-filter cat' to rewrite the tag."
399 else
400 die "Could not rewrite $ref"
405 # NEEDSWORK: possibly add -Werror, making this an error
406 warn "WARNING: '$ref' was rewritten into multiple commits:"
407 warn "$rewritten"
408 warn "WARNING: Ref '$ref' points to the first one now."
409 rewritten=$(echo "$rewritten" | head -n 1)
410 git update-ref -m "filter-branch: rewrite to first" \
411 "$ref" $rewritten $sha1 ||
412 die "Could not rewrite $ref"
414 esac
415 git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1 ||
416 exit
417 done < "$tempdir"/heads
419 # TODO: This should possibly go, with the semantics that all positive given
420 # refs are updated, and their original heads stored in refs/original/
421 # Filter tags
423 if [ "$filter_tag_name" ]; then
424 git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags |
425 while read sha1 type ref; do
426 ref="${ref#refs/tags/}"
427 # XXX: Rewrite tagged trees as well?
428 if [ "$type" != "commit" -a "$type" != "tag" ]; then
429 continue;
432 if [ "$type" = "tag" ]; then
433 # Dereference to a commit
434 sha1t="$sha1"
435 sha1="$(git rev-parse -q "$sha1"^{commit})" || continue
438 [ -f "../map/$sha1" ] || continue
439 new_sha1="$(cat "../map/$sha1")"
440 GIT_COMMIT="$sha1"
441 export GIT_COMMIT
442 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
443 die "tag name filter failed: $filter_tag_name"
445 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
447 if [ "$type" = "tag" ]; then
448 new_sha1=$( ( printf 'object %s\ntype commit\ntag %s\n' \
449 "$new_sha1" "$new_ref"
450 git cat-file tag "$ref" |
451 sed -n \
452 -e '1,/^$/{
453 /^object /d
454 /^type /d
455 /^tag /d
456 }' \
457 -e '/^-----BEGIN PGP SIGNATURE-----/q' \
458 -e 'p' ) |
459 git mktag) ||
460 die "Could not create new tag object for $ref"
461 if git cat-file tag "$ref" | \
462 sane_grep '^-----BEGIN PGP SIGNATURE-----' >/dev/null 2>&1
463 then
464 warn "gpg signature stripped from tag object $sha1t"
468 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
469 die "Could not write tag $new_ref"
470 done
473 cd "$orig_dir"
474 rm -rf "$tempdir"
476 trap - 0
478 unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
479 test -z "$ORIG_GIT_DIR" || {
480 GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR
482 test -z "$ORIG_GIT_WORK_TREE" || {
483 GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" &&
484 export GIT_WORK_TREE
486 test -z "$ORIG_GIT_INDEX_FILE" || {
487 GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" &&
488 export GIT_INDEX_FILE
491 if [ "$(is_bare_repository)" = false ]; then
492 git read-tree -u -m HEAD || exit
495 exit 0