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