Documentation: describe -i/--info option to "git-help"
[git/gitweb.git] / git-filter-branch.sh
blob29d35fd27c52abf208f57e2a99d3de6e6ff2f977
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 # override die(): this version puts in an extra line break, so that
44 # the progress is still visible
46 die()
48 echo >&2
49 echo "$*" >&2
50 exit 1
52 EOF
55 eval "$functions"
57 # When piped a commit, output a script to set the ident of either
58 # "author" or "committer
60 set_ident () {
61 lid="$(echo "$1" | tr "A-Z" "a-z")"
62 uid="$(echo "$1" | tr "a-z" "A-Z")"
63 pick_id_script='
64 /^'$lid' /{
65 s/'\''/'\''\\'\'\''/g
67 s/^'$lid' \([^<]*\) <[^>]*> .*$/\1/
68 s/'\''/'\''\'\'\''/g
69 s/.*/GIT_'$uid'_NAME='\''&'\''; export GIT_'$uid'_NAME/p
72 s/^'$lid' [^<]* <\([^>]*\)> .*$/\1/
73 s/'\''/'\''\'\'\''/g
74 s/.*/GIT_'$uid'_EMAIL='\''&'\''; export GIT_'$uid'_EMAIL/p
77 s/^'$lid' [^<]* <[^>]*> \(.*\)$/\1/
78 s/'\''/'\''\'\'\''/g
79 s/.*/GIT_'$uid'_DATE='\''&'\''; export GIT_'$uid'_DATE/p
85 LANG=C LC_ALL=C sed -ne "$pick_id_script"
86 # Ensure non-empty id name.
87 echo "case \"\$GIT_${uid}_NAME\" in \"\") GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\" && export GIT_${uid}_NAME;; esac"
90 USAGE="[--env-filter <command>] [--tree-filter <command>] \
91 [--index-filter <command>] [--parent-filter <command>] \
92 [--msg-filter <command>] [--commit-filter <command>] \
93 [--tag-name-filter <command>] [--subdirectory-filter <directory>] \
94 [--original <namespace>] [-d <directory>] [-f | --force] \
95 [<rev-list options>...]"
97 OPTIONS_SPEC=
98 . git-sh-setup
100 git diff-files --quiet &&
101 git diff-index --cached --quiet HEAD -- ||
102 die "Cannot rewrite branch(es) with a dirty working directory."
104 tempdir=.git-rewrite
105 filter_env=
106 filter_tree=
107 filter_index=
108 filter_parent=
109 filter_msg=cat
110 filter_commit='git commit-tree "$@"'
111 filter_tag_name=
112 filter_subdir=
113 orig_namespace=refs/original/
114 force=
115 while :
117 test $# = 0 && usage
118 case "$1" in
120 shift
121 break
123 --force|-f)
124 shift
125 force=t
126 continue
131 break;
132 esac
134 # all switches take one argument
135 ARG="$1"
136 case "$#" in 1) usage ;; esac
137 shift
138 OPTARG="$1"
139 shift
141 case "$ARG" in
143 tempdir="$OPTARG"
145 --env-filter)
146 filter_env="$OPTARG"
148 --tree-filter)
149 filter_tree="$OPTARG"
151 --index-filter)
152 filter_index="$OPTARG"
154 --parent-filter)
155 filter_parent="$OPTARG"
157 --msg-filter)
158 filter_msg="$OPTARG"
160 --commit-filter)
161 filter_commit="$functions; $OPTARG"
163 --tag-name-filter)
164 filter_tag_name="$OPTARG"
166 --subdirectory-filter)
167 filter_subdir="$OPTARG"
169 --original)
170 orig_namespace=$(expr "$OPTARG/" : '\(.*[^/]\)/*$')/
173 usage
175 esac
176 done
178 case "$force" in
180 rm -rf "$tempdir"
183 test -d "$tempdir" &&
184 die "$tempdir already exists, please remove it"
185 esac
186 mkdir -p "$tempdir/t" &&
187 tempdir="$(cd "$tempdir"; pwd)" &&
188 cd "$tempdir/t" &&
189 workdir="$(pwd)" ||
190 die ""
192 # Make sure refs/original is empty
193 git for-each-ref > "$tempdir"/backup-refs
194 while read sha1 type name
196 case "$force,$name" in
197 ,$orig_namespace*)
198 die "Namespace $orig_namespace not empty"
200 t,$orig_namespace*)
201 git update-ref -d "$name" $sha1
203 esac
204 done < "$tempdir"/backup-refs
206 ORIG_GIT_DIR="$GIT_DIR"
207 ORIG_GIT_WORK_TREE="$GIT_WORK_TREE"
208 ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE"
209 GIT_WORK_TREE=.
210 export GIT_DIR GIT_WORK_TREE
212 # These refs should be updated if their heads were rewritten
214 git rev-parse --revs-only --symbolic "$@" |
215 while read ref
217 # normalize ref
218 case "$ref" in
219 HEAD)
220 ref="$(git symbolic-ref "$ref")"
222 refs/*)
225 ref="$(git for-each-ref --format='%(refname)' |
226 grep /"$ref")"
227 esac
229 git check-ref-format "$ref" && echo "$ref"
230 done > "$tempdir"/heads
232 test -s "$tempdir"/heads ||
233 die "Which ref do you want to rewrite?"
235 GIT_INDEX_FILE="$(pwd)/../index"
236 export GIT_INDEX_FILE
237 git read-tree || die "Could not seed the index"
239 ret=0
241 # map old->new commit ids for rewriting parents
242 mkdir ../map || die "Could not create map/ directory"
244 case "$filter_subdir" in
246 git rev-list --reverse --topo-order --default HEAD \
247 --parents "$@"
250 git rev-list --reverse --topo-order --default HEAD \
251 --parents --full-history "$@" -- "$filter_subdir"
252 esac > ../revs || die "Could not get the commits"
253 commits=$(wc -l <../revs | tr -d " ")
255 test $commits -eq 0 && die "Found nothing to rewrite"
257 # Rewrite the commits
260 while read commit parents; do
261 i=$(($i+1))
262 printf "\rRewrite $commit ($i/$commits)"
264 case "$filter_subdir" in
266 git read-tree -i -m $commit
269 git read-tree -i -m $commit:"$filter_subdir"
270 esac || die "Could not initialize the index"
272 GIT_COMMIT=$commit
273 export GIT_COMMIT
274 git cat-file commit "$commit" >../commit ||
275 die "Cannot read commit $commit"
277 eval "$(set_ident AUTHOR <../commit)" ||
278 die "setting author failed for commit $commit"
279 eval "$(set_ident COMMITTER <../commit)" ||
280 die "setting committer failed for commit $commit"
281 eval "$filter_env" < /dev/null ||
282 die "env filter failed: $filter_env"
284 if [ "$filter_tree" ]; then
285 git checkout-index -f -u -a ||
286 die "Could not checkout the index"
287 # files that $commit removed are now still in the working tree;
288 # remove them, else they would be added again
289 git ls-files -z --others | xargs -0 rm -f
290 eval "$filter_tree" < /dev/null ||
291 die "tree filter failed: $filter_tree"
293 git diff-index -r $commit | cut -f 2- | tr '\n' '\0' | \
294 xargs -0 git update-index --add --replace --remove
295 git ls-files -z --others | \
296 xargs -0 git update-index --add --replace --remove
299 eval "$filter_index" < /dev/null ||
300 die "index filter failed: $filter_index"
302 parentstr=
303 for parent in $parents; do
304 for reparent in $(map "$parent"); do
305 parentstr="$parentstr -p $reparent"
306 done
307 done
308 if [ "$filter_parent" ]; then
309 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
310 die "parent filter failed: $filter_parent"
313 sed -e '1,/^$/d' <../commit | \
314 eval "$filter_msg" > ../message ||
315 die "msg filter failed: $filter_msg"
316 sh -c "$filter_commit" "git commit-tree" \
317 $(git write-tree) $parentstr < ../message > ../map/$commit
318 done <../revs
320 # In case of a subdirectory filter, it is possible that a specified head
321 # is not in the set of rewritten commits, because it was pruned by the
322 # revision walker. Fix it by mapping these heads to the next rewritten
323 # ancestor(s), i.e. the boundaries in the set of rewritten commits.
325 # NEEDSWORK: we should sort the unmapped refs topologically first
326 while read ref
328 sha1=$(git rev-parse "$ref"^0)
329 test -f "$workdir"/../map/$sha1 && continue
330 # Assign the boundarie(s) in the set of rewritten commits
331 # as the replacement commit(s).
332 # (This would look a bit nicer if --not --stdin worked.)
333 for p in $( (cd "$workdir"/../map; ls | sed "s/^/^/") |
334 git rev-list $ref --boundary --stdin |
335 sed -n "s/^-//p")
337 map $p >> "$workdir"/../map/$sha1
338 done
339 done < "$tempdir"/heads
341 # Finally update the refs
343 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
344 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
345 count=0
346 echo
347 while read ref
349 # avoid rewriting a ref twice
350 test -f "$orig_namespace$ref" && continue
352 sha1=$(git rev-parse "$ref"^0)
353 rewritten=$(map $sha1)
355 test $sha1 = "$rewritten" &&
356 warn "WARNING: Ref '$ref' is unchanged" &&
357 continue
359 case "$rewritten" in
361 echo "Ref '$ref' was deleted"
362 git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
363 die "Could not delete $ref"
365 $_x40)
366 echo "Ref '$ref' was rewritten"
367 git update-ref -m "filter-branch: rewrite" \
368 "$ref" $rewritten $sha1 ||
369 die "Could not rewrite $ref"
372 # NEEDSWORK: possibly add -Werror, making this an error
373 warn "WARNING: '$ref' was rewritten into multiple commits:"
374 warn "$rewritten"
375 warn "WARNING: Ref '$ref' points to the first one now."
376 rewritten=$(echo "$rewritten" | head -n 1)
377 git update-ref -m "filter-branch: rewrite to first" \
378 "$ref" $rewritten $sha1 ||
379 die "Could not rewrite $ref"
381 esac
382 git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1
383 count=$(($count+1))
384 done < "$tempdir"/heads
386 # TODO: This should possibly go, with the semantics that all positive given
387 # refs are updated, and their original heads stored in refs/original/
388 # Filter tags
390 if [ "$filter_tag_name" ]; then
391 git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags |
392 while read sha1 type ref; do
393 ref="${ref#refs/tags/}"
394 # XXX: Rewrite tagged trees as well?
395 if [ "$type" != "commit" -a "$type" != "tag" ]; then
396 continue;
399 if [ "$type" = "tag" ]; then
400 # Dereference to a commit
401 sha1t="$sha1"
402 sha1="$(git rev-parse "$sha1"^{commit} 2>/dev/null)" || continue
405 [ -f "../map/$sha1" ] || continue
406 new_sha1="$(cat "../map/$sha1")"
407 GIT_COMMIT="$sha1"
408 export GIT_COMMIT
409 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
410 die "tag name filter failed: $filter_tag_name"
412 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
414 if [ "$type" = "tag" ]; then
415 # Warn that we are not rewriting the tag object itself.
416 warn "unreferencing tag object $sha1t"
419 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
420 die "Could not write tag $new_ref"
421 done
424 cd ../..
425 rm -rf "$tempdir"
426 echo
427 test $count -gt 0 && echo "These refs were rewritten:"
428 git show-ref | grep ^"$orig_namespace"
430 unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
431 test -z "$ORIG_GIT_DIR" || GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR
432 test -z "$ORIG_GIT_WORK_TREE" || GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" &&
433 export GIT_WORK_TREE
434 test -z "$ORIG_GIT_INDEX_FILE" || GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" &&
435 export GIT_INDEX_FILE
436 git read-tree -u -m HEAD
438 exit $ret