]> git.ipfire.org Git - thirdparty/git.git/blame - git-filter-branch.sh
rebase -i: mention the option to split commits in the man page
[thirdparty/git.git] / git-filter-branch.sh
CommitLineData
6f6826c5
JS
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#
c401b33c
JS
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.
6f6826c5 10
26a65dea
GB
11USAGE="[--env-filter <command>] [--tree-filter <command>] \
12[--index-filter <command>] [--parent-filter <command>] \
13[--msg-filter <command>] [--commit-filter <command>] \
14[--tag-name-filter <command>] [--subdirectory-filter <directory>] \
15[--original <namespace>] [-d <directory>] [-f | --force] \
16[<rev-list options>...]"
17
6f6826c5
JS
18. git-sh-setup
19
b5669a05
SP
20warn () {
21 echo "$*" >&2
22}
23
6f6826c5
JS
24map()
25{
3520e1e8 26 # if it was not rewritten, take the original
c57a3494
JS
27 if test -r "$workdir/../map/$1"
28 then
29 cat "$workdir/../map/$1"
30 else
31 echo "$1"
32 fi
6f6826c5
JS
33}
34
8c1ce0f4
JS
35# override die(): this version puts in an extra line break, so that
36# the progress is still visible
37
38die()
39{
40 echo >&2
41 echo "$*" >&2
42 exit 1
43}
44
6f6826c5
JS
45# When piped a commit, output a script to set the ident of either
46# "author" or "committer
47
48set_ident () {
49 lid="$(echo "$1" | tr "A-Z" "a-z")"
50 uid="$(echo "$1" | tr "a-z" "A-Z")"
51 pick_id_script='
52 /^'$lid' /{
53 s/'\''/'\''\\'\'\''/g
54 h
55 s/^'$lid' \([^<]*\) <[^>]*> .*$/\1/
56 s/'\''/'\''\'\'\''/g
57 s/.*/export GIT_'$uid'_NAME='\''&'\''/p
58
59 g
60 s/^'$lid' [^<]* <\([^>]*\)> .*$/\1/
61 s/'\''/'\''\'\'\''/g
62 s/.*/export GIT_'$uid'_EMAIL='\''&'\''/p
63
64 g
65 s/^'$lid' [^<]* <[^>]*> \(.*\)$/\1/
66 s/'\''/'\''\'\'\''/g
67 s/.*/export GIT_'$uid'_DATE='\''&'\''/p
68
69 q
70 }
71 '
72
73 LANG=C LC_ALL=C sed -ne "$pick_id_script"
74 # Ensure non-empty id name.
75 echo "[ -n \"\$GIT_${uid}_NAME\" ] || export GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\""
76}
77
6f6826c5 78tempdir=.git-rewrite
6f6826c5
JS
79filter_env=
80filter_tree=
81filter_index=
82filter_parent=
83filter_msg=cat
5be60078 84filter_commit='git commit-tree "$@"'
6f6826c5 85filter_tag_name=
685ef546 86filter_subdir=
dfd05e38
JS
87orig_namespace=refs/original/
88force=
6f6826c5
JS
89while case "$#" in 0) usage;; esac
90do
91 case "$1" in
92 --)
93 shift
94 break
95 ;;
dfd05e38
JS
96 --force|-f)
97 shift
98 force=t
99 continue
100 ;;
6f6826c5
JS
101 -*)
102 ;;
103 *)
104 break;
105 esac
106
107 # all switches take one argument
108 ARG="$1"
109 case "$#" in 1) usage ;; esac
110 shift
111 OPTARG="$1"
112 shift
113
114 case "$ARG" in
115 -d)
116 tempdir="$OPTARG"
117 ;;
6f6826c5
JS
118 --env-filter)
119 filter_env="$OPTARG"
120 ;;
121 --tree-filter)
122 filter_tree="$OPTARG"
123 ;;
124 --index-filter)
125 filter_index="$OPTARG"
126 ;;
127 --parent-filter)
128 filter_parent="$OPTARG"
129 ;;
130 --msg-filter)
131 filter_msg="$OPTARG"
132 ;;
133 --commit-filter)
134 filter_commit="$OPTARG"
135 ;;
136 --tag-name-filter)
137 filter_tag_name="$OPTARG"
138 ;;
685ef546
JS
139 --subdirectory-filter)
140 filter_subdir="$OPTARG"
141 ;;
dfd05e38 142 --original)
55ced83d 143 orig_namespace=$(expr "$OPTARG/" : '\(.*[^/]\)/*$')/
dfd05e38 144 ;;
6f6826c5
JS
145 *)
146 usage
147 ;;
148 esac
149done
150
dfd05e38
JS
151case "$force" in
152t)
153 rm -rf "$tempdir"
154;;
155'')
156 test -d "$tempdir" &&
157 die "$tempdir already exists, please remove it"
158esac
af580e9c 159mkdir -p "$tempdir/t" &&
dfd05e38 160tempdir="$(cd "$tempdir"; pwd)" &&
af580e9c
JS
161cd "$tempdir/t" &&
162workdir="$(pwd)" ||
163die ""
6f6826c5 164
dfd05e38
JS
165# Make sure refs/original is empty
166git for-each-ref > "$tempdir"/backup-refs
167while read sha1 type name
168do
169 case "$force,$name" in
170 ,$orig_namespace*)
171 die "Namespace $orig_namespace not empty"
172 ;;
173 t,$orig_namespace*)
174 git update-ref -d "$name" $sha1
175 ;;
176 esac
177done < "$tempdir"/backup-refs
178
9489d0f1 179export GIT_DIR GIT_WORK_TREE=.
6f6826c5 180
dfd05e38
JS
181# These refs should be updated if their heads were rewritten
182
183git rev-parse --revs-only --symbolic "$@" |
184while read ref
185do
186 # normalize ref
187 case "$ref" in
188 HEAD)
189 ref="$(git symbolic-ref "$ref")"
190 ;;
191 refs/*)
192 ;;
193 *)
194 ref="$(git for-each-ref --format='%(refname)' |
195 grep /"$ref")"
196 esac
197
198 git check-ref-format "$ref" && echo "$ref"
199done > "$tempdir"/heads
200
201test -s "$tempdir"/heads ||
202 die "Which ref do you want to rewrite?"
203
6f6826c5 204export GIT_INDEX_FILE="$(pwd)/../index"
af580e9c 205git read-tree || die "Could not seed the index"
6f6826c5
JS
206
207ret=0
208
af580e9c
JS
209# map old->new commit ids for rewriting parents
210mkdir ../map || die "Could not create map/ directory"
6f6826c5 211
685ef546
JS
212case "$filter_subdir" in
213"")
5be60078 214 git rev-list --reverse --topo-order --default HEAD \
813b4734 215 --parents "$@"
685ef546
JS
216 ;;
217*)
5be60078 218 git rev-list --reverse --topo-order --default HEAD \
cfabd6ee 219 --parents --full-history "$@" -- "$filter_subdir"
af580e9c 220esac > ../revs || die "Could not get the commits"
9d6f220c 221commits=$(wc -l <../revs | tr -d " ")
6f6826c5
JS
222
223test $commits -eq 0 && die "Found nothing to rewrite"
224
dfd05e38
JS
225# Rewrite the commits
226
6f6826c5 227i=0
813b4734 228while read commit parents; do
c12764b8 229 i=$(($i+1))
5efb48b5 230 printf "\rRewrite $commit ($i/$commits)"
6f6826c5 231
685ef546
JS
232 case "$filter_subdir" in
233 "")
5be60078 234 git read-tree -i -m $commit
685ef546
JS
235 ;;
236 *)
5be60078 237 git read-tree -i -m $commit:"$filter_subdir"
af580e9c 238 esac || die "Could not initialize the index"
6f6826c5
JS
239
240 export GIT_COMMIT=$commit
af580e9c
JS
241 git cat-file commit "$commit" >../commit ||
242 die "Cannot read commit $commit"
6f6826c5 243
8c1ce0f4
JS
244 eval "$(set_ident AUTHOR <../commit)" ||
245 die "setting author failed for commit $commit"
246 eval "$(set_ident COMMITTER <../commit)" ||
247 die "setting committer failed for commit $commit"
248 eval "$filter_env" < /dev/null ||
249 die "env filter failed: $filter_env"
6f6826c5
JS
250
251 if [ "$filter_tree" ]; then
af580e9c
JS
252 git checkout-index -f -u -a ||
253 die "Could not checkout the index"
6f6826c5
JS
254 # files that $commit removed are now still in the working tree;
255 # remove them, else they would be added again
5be60078 256 git ls-files -z --others | xargs -0 rm -f
8c1ce0f4
JS
257 eval "$filter_tree" < /dev/null ||
258 die "tree filter failed: $filter_tree"
259
5be60078
JH
260 git diff-index -r $commit | cut -f 2- | tr '\n' '\0' | \
261 xargs -0 git update-index --add --replace --remove
262 git ls-files -z --others | \
263 xargs -0 git update-index --add --replace --remove
6f6826c5
JS
264 fi
265
8c1ce0f4
JS
266 eval "$filter_index" < /dev/null ||
267 die "index filter failed: $filter_index"
6f6826c5
JS
268
269 parentstr=
813b4734 270 for parent in $parents; do
3520e1e8
JS
271 for reparent in $(map "$parent"); do
272 parentstr="$parentstr -p $reparent"
273 done
6f6826c5
JS
274 done
275 if [ "$filter_parent" ]; then
8c1ce0f4
JS
276 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
277 die "parent filter failed: $filter_parent"
6f6826c5
JS
278 fi
279
280 sed -e '1,/^$/d' <../commit | \
8c1ce0f4
JS
281 eval "$filter_msg" > ../message ||
282 die "msg filter failed: $filter_msg"
283 sh -c "$filter_commit" "git commit-tree" \
284 $(git write-tree) $parentstr < ../message > ../map/$commit
6f6826c5
JS
285done <../revs
286
dfd05e38
JS
287# In case of a subdirectory filter, it is possible that a specified head
288# is not in the set of rewritten commits, because it was pruned by the
289# revision walker. Fix it by mapping these heads to the next rewritten
290# ancestor(s), i.e. the boundaries in the set of rewritten commits.
291
292# NEEDSWORK: we should sort the unmapped refs topologically first
293while read ref
294do
295 sha1=$(git rev-parse "$ref"^0)
296 test -f "$workdir"/../map/$sha1 && continue
297 # Assign the boundarie(s) in the set of rewritten commits
298 # as the replacement commit(s).
299 # (This would look a bit nicer if --not --stdin worked.)
24d00634 300 for p in $( (cd "$workdir"/../map; ls | sed "s/^/^/") |
dfd05e38
JS
301 git rev-list $ref --boundary --stdin |
302 sed -n "s/^-//p")
303 do
304 map $p >> "$workdir"/../map/$sha1
305 done
306done < "$tempdir"/heads
307
308# Finally update the refs
309
310_x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
311_x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
312count=0
313echo
314while read ref
315do
316 # avoid rewriting a ref twice
317 test -f "$orig_namespace$ref" && continue
318
319 sha1=$(git rev-parse "$ref"^0)
320 rewritten=$(map $sha1)
321
322 test $sha1 = "$rewritten" &&
323 warn "WARNING: Ref '$ref' is unchanged" &&
324 continue
325
326 case "$rewritten" in
327 '')
328 echo "Ref '$ref' was deleted"
329 git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
330 die "Could not delete $ref"
98409060 331 ;;
dfd05e38
JS
332 $_x40)
333 echo "Ref '$ref' was rewritten"
334 git update-ref -m "filter-branch: rewrite" \
335 "$ref" $rewritten $sha1 ||
336 die "Could not rewrite $ref"
98409060 337 ;;
dfd05e38
JS
338 *)
339 # NEEDSWORK: possibly add -Werror, making this an error
340 warn "WARNING: '$ref' was rewritten into multiple commits:"
341 warn "$rewritten"
342 warn "WARNING: Ref '$ref' points to the first one now."
343 rewritten=$(echo "$rewritten" | head -n 1)
344 git update-ref -m "filter-branch: rewrite to first" \
345 "$ref" $rewritten $sha1 ||
346 die "Could not rewrite $ref"
347 ;;
348 esac
349 git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1
350 count=$(($count+1))
351done < "$tempdir"/heads
352
353# TODO: This should possibly go, with the semantics that all positive given
354# refs are updated, and their original heads stored in refs/original/
355# Filter tags
6f6826c5
JS
356
357if [ "$filter_tag_name" ]; then
5be60078 358 git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags |
6f6826c5
JS
359 while read sha1 type ref; do
360 ref="${ref#refs/tags/}"
361 # XXX: Rewrite tagged trees as well?
362 if [ "$type" != "commit" -a "$type" != "tag" ]; then
363 continue;
364 fi
365
366 if [ "$type" = "tag" ]; then
367 # Dereference to a commit
368 sha1t="$sha1"
5be60078 369 sha1="$(git rev-parse "$sha1"^{commit} 2>/dev/null)" || continue
6f6826c5
JS
370 fi
371
372 [ -f "../map/$sha1" ] || continue
373 new_sha1="$(cat "../map/$sha1")"
374 export GIT_COMMIT="$sha1"
8c1ce0f4
JS
375 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
376 die "tag name filter failed: $filter_tag_name"
6f6826c5
JS
377
378 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
379
380 if [ "$type" = "tag" ]; then
381 # Warn that we are not rewriting the tag object itself.
382 warn "unreferencing tag object $sha1t"
383 fi
384
af580e9c
JS
385 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
386 die "Could not write tag $new_ref"
6f6826c5
JS
387 done
388fi
389
390cd ../..
391rm -rf "$tempdir"
dfd05e38
JS
392echo
393test $count -gt 0 && echo "These refs were rewritten:"
394git show-ref | grep ^"$orig_namespace"
6f6826c5
JS
395
396exit $ret