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