Make builtin-mv.c use parse-options
[git/dkf.git] / git-filter-branch.sh
blobffcc408ee5217d7a8f6179a0404ffe387e0dab0f
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 warn () {
12 echo "$*" >&2
15 map()
17 # if it was not rewritten, take the original
18 if test -r "$workdir/../map/$1"
19 then
20 cat "$workdir/../map/$1"
21 else
22 echo "$1"
26 # if you run 'skip_commit "$@"' in a commit filter, it will print
27 # the (mapped) parents, effectively skipping the commit.
29 skip_commit()
31 shift;
32 while [ -n "$1" ];
34 shift;
35 map "$1";
36 shift;
37 done;
40 # override die(): this version puts in an extra line break, so that
41 # the progress is still visible
43 die()
45 echo >&2
46 echo "$*" >&2
47 exit 1
50 # When piped a commit, output a script to set the ident of either
51 # "author" or "committer
53 set_ident () {
54 lid="$(echo "$1" | tr "A-Z" "a-z")"
55 uid="$(echo "$1" | tr "a-z" "A-Z")"
56 pick_id_script='
57 /^'$lid' /{
58 s/'\''/'\''\\'\'\''/g
60 s/^'$lid' \([^<]*\) <[^>]*> .*$/\1/
61 s/'\''/'\''\'\'\''/g
62 s/.*/export GIT_'$uid'_NAME='\''&'\''/p
65 s/^'$lid' [^<]* <\([^>]*\)> .*$/\1/
66 s/'\''/'\''\'\'\''/g
67 s/.*/export GIT_'$uid'_EMAIL='\''&'\''/p
70 s/^'$lid' [^<]* <[^>]*> \(.*\)$/\1/
71 s/'\''/'\''\'\'\''/g
72 s/.*/export GIT_'$uid'_DATE='\''&'\''/p
78 LANG=C LC_ALL=C sed -ne "$pick_id_script"
79 # Ensure non-empty id name.
80 echo "[ -n \"\$GIT_${uid}_NAME\" ] || export GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\""
83 # This script can be sourced by the commit filter to get the functions
84 test "a$SOURCE_FUNCTIONS" = a1 && return
85 this_script="$(cd "$(dirname "$0")"; pwd)"/$(basename "$0")
86 export this_script
88 USAGE="[--env-filter <command>] [--tree-filter <command>] \
89 [--index-filter <command>] [--parent-filter <command>] \
90 [--msg-filter <command>] [--commit-filter <command>] \
91 [--tag-name-filter <command>] [--subdirectory-filter <directory>] \
92 [--original <namespace>] [-d <directory>] [-f | --force] \
93 [<rev-list options>...]"
95 . git-sh-setup
97 git diff-files --quiet &&
98 git diff-index --cached --quiet HEAD ||
99 die "Cannot rewrite branch(es) with a dirty working directory."
101 tempdir=.git-rewrite
102 filter_env=
103 filter_tree=
104 filter_index=
105 filter_parent=
106 filter_msg=cat
107 filter_commit='git commit-tree "$@"'
108 filter_tag_name=
109 filter_subdir=
110 orig_namespace=refs/original/
111 force=
112 while :
114 test $# = 0 && usage
115 case "$1" in
117 shift
118 break
120 --force|-f)
121 shift
122 force=t
123 continue
128 break;
129 esac
131 # all switches take one argument
132 ARG="$1"
133 case "$#" in 1) usage ;; esac
134 shift
135 OPTARG="$1"
136 shift
138 case "$ARG" in
140 tempdir="$OPTARG"
142 --env-filter)
143 filter_env="$OPTARG"
145 --tree-filter)
146 filter_tree="$OPTARG"
148 --index-filter)
149 filter_index="$OPTARG"
151 --parent-filter)
152 filter_parent="$OPTARG"
154 --msg-filter)
155 filter_msg="$OPTARG"
157 --commit-filter)
158 filter_commit='SOURCE_FUNCTIONS=1 . "$this_script";'" $OPTARG"
160 --tag-name-filter)
161 filter_tag_name="$OPTARG"
163 --subdirectory-filter)
164 filter_subdir="$OPTARG"
166 --original)
167 orig_namespace=$(expr "$OPTARG/" : '\(.*[^/]\)/*$')/
170 usage
172 esac
173 done
175 case "$force" in
177 rm -rf "$tempdir"
180 test -d "$tempdir" &&
181 die "$tempdir already exists, please remove it"
182 esac
183 mkdir -p "$tempdir/t" &&
184 tempdir="$(cd "$tempdir"; pwd)" &&
185 cd "$tempdir/t" &&
186 workdir="$(pwd)" ||
187 die ""
189 # Make sure refs/original is empty
190 git for-each-ref > "$tempdir"/backup-refs
191 while read sha1 type name
193 case "$force,$name" in
194 ,$orig_namespace*)
195 die "Namespace $orig_namespace not empty"
197 t,$orig_namespace*)
198 git update-ref -d "$name" $sha1
200 esac
201 done < "$tempdir"/backup-refs
203 ORIG_GIT_DIR="$GIT_DIR"
204 ORIG_GIT_WORK_TREE="$GIT_WORK_TREE"
205 ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE"
206 export GIT_DIR GIT_WORK_TREE=.
208 # These refs should be updated if their heads were rewritten
210 git rev-parse --revs-only --symbolic "$@" |
211 while read ref
213 # normalize ref
214 case "$ref" in
215 HEAD)
216 ref="$(git symbolic-ref "$ref")"
218 refs/*)
221 ref="$(git for-each-ref --format='%(refname)' |
222 grep /"$ref")"
223 esac
225 git check-ref-format "$ref" && echo "$ref"
226 done > "$tempdir"/heads
228 test -s "$tempdir"/heads ||
229 die "Which ref do you want to rewrite?"
231 export GIT_INDEX_FILE="$(pwd)/../index"
232 git read-tree || die "Could not seed the index"
234 ret=0
236 # map old->new commit ids for rewriting parents
237 mkdir ../map || die "Could not create map/ directory"
239 case "$filter_subdir" in
241 git rev-list --reverse --topo-order --default HEAD \
242 --parents "$@"
245 git rev-list --reverse --topo-order --default HEAD \
246 --parents --full-history "$@" -- "$filter_subdir"
247 esac > ../revs || die "Could not get the commits"
248 commits=$(wc -l <../revs | tr -d " ")
250 test $commits -eq 0 && die "Found nothing to rewrite"
252 # Rewrite the commits
255 while read commit parents; do
256 i=$(($i+1))
257 printf "\rRewrite $commit ($i/$commits)"
259 case "$filter_subdir" in
261 git read-tree -i -m $commit
264 git read-tree -i -m $commit:"$filter_subdir"
265 esac || die "Could not initialize the index"
267 export GIT_COMMIT=$commit
268 git cat-file commit "$commit" >../commit ||
269 die "Cannot read commit $commit"
271 eval "$(set_ident AUTHOR <../commit)" ||
272 die "setting author failed for commit $commit"
273 eval "$(set_ident COMMITTER <../commit)" ||
274 die "setting committer failed for commit $commit"
275 eval "$filter_env" < /dev/null ||
276 die "env filter failed: $filter_env"
278 if [ "$filter_tree" ]; then
279 git checkout-index -f -u -a ||
280 die "Could not checkout the index"
281 # files that $commit removed are now still in the working tree;
282 # remove them, else they would be added again
283 git ls-files -z --others | xargs -0 rm -f
284 eval "$filter_tree" < /dev/null ||
285 die "tree filter failed: $filter_tree"
287 git diff-index -r $commit | cut -f 2- | tr '\n' '\0' | \
288 xargs -0 git update-index --add --replace --remove
289 git ls-files -z --others | \
290 xargs -0 git update-index --add --replace --remove
293 eval "$filter_index" < /dev/null ||
294 die "index filter failed: $filter_index"
296 parentstr=
297 for parent in $parents; do
298 for reparent in $(map "$parent"); do
299 parentstr="$parentstr -p $reparent"
300 done
301 done
302 if [ "$filter_parent" ]; then
303 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
304 die "parent filter failed: $filter_parent"
307 sed -e '1,/^$/d' <../commit | \
308 eval "$filter_msg" > ../message ||
309 die "msg filter failed: $filter_msg"
310 sh -c "$filter_commit" "git commit-tree" \
311 $(git write-tree) $parentstr < ../message > ../map/$commit
312 done <../revs
314 # In case of a subdirectory filter, it is possible that a specified head
315 # is not in the set of rewritten commits, because it was pruned by the
316 # revision walker. Fix it by mapping these heads to the next rewritten
317 # ancestor(s), i.e. the boundaries in the set of rewritten commits.
319 # NEEDSWORK: we should sort the unmapped refs topologically first
320 while read ref
322 sha1=$(git rev-parse "$ref"^0)
323 test -f "$workdir"/../map/$sha1 && continue
324 # Assign the boundarie(s) in the set of rewritten commits
325 # as the replacement commit(s).
326 # (This would look a bit nicer if --not --stdin worked.)
327 for p in $( (cd "$workdir"/../map; ls | sed "s/^/^/") |
328 git rev-list $ref --boundary --stdin |
329 sed -n "s/^-//p")
331 map $p >> "$workdir"/../map/$sha1
332 done
333 done < "$tempdir"/heads
335 # Finally update the refs
337 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
338 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
339 count=0
340 echo
341 while read ref
343 # avoid rewriting a ref twice
344 test -f "$orig_namespace$ref" && continue
346 sha1=$(git rev-parse "$ref"^0)
347 rewritten=$(map $sha1)
349 test $sha1 = "$rewritten" &&
350 warn "WARNING: Ref '$ref' is unchanged" &&
351 continue
353 case "$rewritten" in
355 echo "Ref '$ref' was deleted"
356 git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
357 die "Could not delete $ref"
359 $_x40)
360 echo "Ref '$ref' was rewritten"
361 git update-ref -m "filter-branch: rewrite" \
362 "$ref" $rewritten $sha1 ||
363 die "Could not rewrite $ref"
366 # NEEDSWORK: possibly add -Werror, making this an error
367 warn "WARNING: '$ref' was rewritten into multiple commits:"
368 warn "$rewritten"
369 warn "WARNING: Ref '$ref' points to the first one now."
370 rewritten=$(echo "$rewritten" | head -n 1)
371 git update-ref -m "filter-branch: rewrite to first" \
372 "$ref" $rewritten $sha1 ||
373 die "Could not rewrite $ref"
375 esac
376 git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1
377 count=$(($count+1))
378 done < "$tempdir"/heads
380 # TODO: This should possibly go, with the semantics that all positive given
381 # refs are updated, and their original heads stored in refs/original/
382 # Filter tags
384 if [ "$filter_tag_name" ]; then
385 git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags |
386 while read sha1 type ref; do
387 ref="${ref#refs/tags/}"
388 # XXX: Rewrite tagged trees as well?
389 if [ "$type" != "commit" -a "$type" != "tag" ]; then
390 continue;
393 if [ "$type" = "tag" ]; then
394 # Dereference to a commit
395 sha1t="$sha1"
396 sha1="$(git rev-parse "$sha1"^{commit} 2>/dev/null)" || continue
399 [ -f "../map/$sha1" ] || continue
400 new_sha1="$(cat "../map/$sha1")"
401 export GIT_COMMIT="$sha1"
402 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
403 die "tag name filter failed: $filter_tag_name"
405 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
407 if [ "$type" = "tag" ]; then
408 # Warn that we are not rewriting the tag object itself.
409 warn "unreferencing tag object $sha1t"
412 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
413 die "Could not write tag $new_ref"
414 done
417 cd ../..
418 rm -rf "$tempdir"
419 echo
420 test $count -gt 0 && echo "These refs were rewritten:"
421 git show-ref | grep ^"$orig_namespace"
423 unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
424 test -z "$ORIG_GIT_DIR" || GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR
425 test -z "$ORIG_GIT_WORK_TREE" || GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" &&
426 export GIT_WORK_TREE
427 test -z "$ORIG_GIT_INDEX_FILE" || GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" &&
428 export GIT_INDEX_FILE
429 git read-tree -u -m HEAD
431 exit $ret