]> git.ipfire.org Git - thirdparty/git.git/blame - git-filter-branch.sh
Merge branch 'maint'
[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
d3240d93
PH
43# if you run 'git_commit_non_empty_tree "$@"' in a commit filter,
44# it will skip commits that leave the tree untouched, commit the other.
45git_commit_non_empty_tree()
46{
47 if test $# = 3 && test "$1" = $(git rev-parse "$3^{tree}"); then
48 map "$3"
49 else
50 git commit-tree "$@"
51 fi
52}
8c1ce0f4
JS
53# override die(): this version puts in an extra line break, so that
54# the progress is still visible
55
56die()
57{
58 echo >&2
59 echo "$*" >&2
60 exit 1
61}
16ed34ad
JS
62EOF
63)
64
65eval "$functions"
8c1ce0f4 66
6f6826c5
JS
67# When piped a commit, output a script to set the ident of either
68# "author" or "committer
69
70set_ident () {
40a7ce64
JK
71 lid="$(echo "$1" | tr "[A-Z]" "[a-z]")"
72 uid="$(echo "$1" | tr "[a-z]" "[A-Z]")"
6f6826c5
JS
73 pick_id_script='
74 /^'$lid' /{
75 s/'\''/'\''\\'\'\''/g
76 h
77 s/^'$lid' \([^<]*\) <[^>]*> .*$/\1/
78 s/'\''/'\''\'\'\''/g
3addc94a 79 s/.*/GIT_'$uid'_NAME='\''&'\''; export GIT_'$uid'_NAME/p
6f6826c5
JS
80
81 g
82 s/^'$lid' [^<]* <\([^>]*\)> .*$/\1/
83 s/'\''/'\''\'\'\''/g
3addc94a 84 s/.*/GIT_'$uid'_EMAIL='\''&'\''; export GIT_'$uid'_EMAIL/p
6f6826c5
JS
85
86 g
87 s/^'$lid' [^<]* <[^>]*> \(.*\)$/\1/
88 s/'\''/'\''\'\'\''/g
3addc94a 89 s/.*/GIT_'$uid'_DATE='\''&'\''; export GIT_'$uid'_DATE/p
6f6826c5
JS
90
91 q
92 }
93 '
94
95 LANG=C LC_ALL=C sed -ne "$pick_id_script"
96 # Ensure non-empty id name.
3addc94a 97 echo "case \"\$GIT_${uid}_NAME\" in \"\") GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\" && export GIT_${uid}_NAME;; esac"
6f6826c5
JS
98}
99
83e355a6
MM
100USAGE="[--env-filter <command>] [--tree-filter <command>]
101 [--index-filter <command>] [--parent-filter <command>]
102 [--msg-filter <command>] [--commit-filter <command>]
103 [--tag-name-filter <command>] [--subdirectory-filter <directory>]
104 [--original <namespace>] [-d <directory>] [-f | --force]
105 [<rev-list options>...]"
7e0f1704 106
8f321a39 107OPTIONS_SPEC=
7e0f1704
JS
108. git-sh-setup
109
a4661b01 110if [ "$(is_bare_repository)" = false ]; then
26be15f0 111 git diff-files --ignore-submodules --quiet &&
38762c47 112 git diff-index --cached --quiet HEAD -- ||
46eb449c 113 die "Cannot rewrite branch(es) with a dirty working directory."
a4661b01 114fi
46eb449c 115
6f6826c5 116tempdir=.git-rewrite
6f6826c5
JS
117filter_env=
118filter_tree=
119filter_index=
120filter_parent=
121filter_msg=cat
d3240d93 122filter_commit=
6f6826c5 123filter_tag_name=
685ef546 124filter_subdir=
dfd05e38
JS
125orig_namespace=refs/original/
126force=
d3240d93 127prune_empty=
f2f3a6b8 128remap_to_ancestor=
822f7c73 129while :
6f6826c5
JS
130do
131 case "$1" in
132 --)
133 shift
134 break
135 ;;
dfd05e38
JS
136 --force|-f)
137 shift
138 force=t
139 continue
140 ;;
f2f3a6b8
TR
141 --remap-to-ancestor)
142 shift
143 remap_to_ancestor=t
144 continue
145 ;;
d3240d93
PH
146 --prune-empty)
147 shift
148 prune_empty=t
149 continue
150 ;;
6f6826c5
JS
151 -*)
152 ;;
153 *)
154 break;
155 esac
156
157 # all switches take one argument
158 ARG="$1"
159 case "$#" in 1) usage ;; esac
160 shift
161 OPTARG="$1"
162 shift
163
164 case "$ARG" in
165 -d)
166 tempdir="$OPTARG"
167 ;;
6f6826c5
JS
168 --env-filter)
169 filter_env="$OPTARG"
170 ;;
171 --tree-filter)
172 filter_tree="$OPTARG"
173 ;;
174 --index-filter)
175 filter_index="$OPTARG"
176 ;;
177 --parent-filter)
178 filter_parent="$OPTARG"
179 ;;
180 --msg-filter)
181 filter_msg="$OPTARG"
182 ;;
183 --commit-filter)
16ed34ad 184 filter_commit="$functions; $OPTARG"
6f6826c5
JS
185 ;;
186 --tag-name-filter)
187 filter_tag_name="$OPTARG"
188 ;;
685ef546
JS
189 --subdirectory-filter)
190 filter_subdir="$OPTARG"
f2f3a6b8 191 remap_to_ancestor=t
685ef546 192 ;;
dfd05e38 193 --original)
55ced83d 194 orig_namespace=$(expr "$OPTARG/" : '\(.*[^/]\)/*$')/
dfd05e38 195 ;;
6f6826c5
JS
196 *)
197 usage
198 ;;
199 esac
200done
201
d3240d93
PH
202case "$prune_empty,$filter_commit" in
203,)
204 filter_commit='git commit-tree "$@"';;
205t,)
206 filter_commit="$functions;"' git_commit_non_empty_tree "$@"';;
207,*)
208 ;;
209*)
5da81713 210 die "Cannot set --prune-empty and --commit-filter at the same time"
d3240d93
PH
211esac
212
dfd05e38
JS
213case "$force" in
214t)
215 rm -rf "$tempdir"
216;;
217'')
218 test -d "$tempdir" &&
219 die "$tempdir already exists, please remove it"
220esac
af580e9c 221mkdir -p "$tempdir/t" &&
dfd05e38 222tempdir="$(cd "$tempdir"; pwd)" &&
af580e9c
JS
223cd "$tempdir/t" &&
224workdir="$(pwd)" ||
225die ""
6f6826c5 226
def16e71
BC
227# Remove tempdir on exit
228trap 'cd ../..; rm -rf "$tempdir"' 0
229
88e38808
LN
230ORIG_GIT_DIR="$GIT_DIR"
231ORIG_GIT_WORK_TREE="$GIT_WORK_TREE"
232ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE"
233GIT_WORK_TREE=.
234export GIT_DIR GIT_WORK_TREE
235
dfd05e38 236# Make sure refs/original is empty
0ea29cce 237git for-each-ref > "$tempdir"/backup-refs || exit
dfd05e38
JS
238while read sha1 type name
239do
240 case "$force,$name" in
241 ,$orig_namespace*)
734cd572
JT
242 die "Cannot create a new backup.
243A previous backup already exists in $orig_namespace
244Force overwriting the backup with -f"
dfd05e38
JS
245 ;;
246 t,$orig_namespace*)
247 git update-ref -d "$name" $sha1
248 ;;
249 esac
250done < "$tempdir"/backup-refs
251
418fa3a5 252# The refs should be updated if their heads were rewritten
0ea29cce
EK
253git rev-parse --no-flags --revs-only --symbolic-full-name \
254 --default HEAD "$@" > "$tempdir"/raw-heads || exit
255sed -e '/^^/d' "$tempdir"/raw-heads >"$tempdir"/heads
dfd05e38
JS
256
257test -s "$tempdir"/heads ||
258 die "Which ref do you want to rewrite?"
259
3addc94a
JS
260GIT_INDEX_FILE="$(pwd)/../index"
261export GIT_INDEX_FILE
6f6826c5 262
af580e9c
JS
263# map old->new commit ids for rewriting parents
264mkdir ../map || die "Could not create map/ directory"
6f6826c5 265
2c1d2d81
TR
266# we need "--" only if there are no path arguments in $@
267nonrevs=$(git rev-parse --no-revs "$@") || exit
268test -z "$nonrevs" && dashdash=-- || dashdash=
269rev_args=$(git rev-parse --revs-only "$@")
270
685ef546
JS
271case "$filter_subdir" in
272"")
2c1d2d81 273 eval set -- "$(git rev-parse --sq --no-revs "$@")"
685ef546
JS
274 ;;
275*)
2c1d2d81
TR
276 eval set -- "$(git rev-parse --sq --no-revs "$@" $dashdash \
277 "$filter_subdir")"
278 ;;
279esac
280
281git rev-list --reverse --topo-order --default HEAD \
282 --parents --simplify-merges $rev_args "$@" > ../revs ||
283 die "Could not get the commits"
9d6f220c 284commits=$(wc -l <../revs | tr -d " ")
6f6826c5
JS
285
286test $commits -eq 0 && die "Found nothing to rewrite"
287
dfd05e38
JS
288# Rewrite the commits
289
d5b0c97d 290git_filter_branch__commit_count=0
813b4734 291while read commit parents; do
d5b0c97d
EN
292 git_filter_branch__commit_count=$(($git_filter_branch__commit_count+1))
293 printf "\rRewrite $commit ($git_filter_branch__commit_count/$commits)"
6f6826c5 294
685ef546
JS
295 case "$filter_subdir" in
296 "")
5be60078 297 git read-tree -i -m $commit
685ef546
JS
298 ;;
299 *)
5b044ac3
JH
300 # The commit may not have the subdirectory at all
301 err=$(git read-tree -i -m $commit:"$filter_subdir" 2>&1) || {
d69da76d 302 if ! git rev-parse -q --verify $commit:"$filter_subdir"
5b044ac3
JH
303 then
304 rm -f "$GIT_INDEX_FILE"
305 else
306 echo >&2 "$err"
307 false
308 fi
309 }
af580e9c 310 esac || die "Could not initialize the index"
6f6826c5 311
3addc94a
JS
312 GIT_COMMIT=$commit
313 export GIT_COMMIT
af580e9c
JS
314 git cat-file commit "$commit" >../commit ||
315 die "Cannot read commit $commit"
6f6826c5 316
8c1ce0f4
JS
317 eval "$(set_ident AUTHOR <../commit)" ||
318 die "setting author failed for commit $commit"
319 eval "$(set_ident COMMITTER <../commit)" ||
320 die "setting committer failed for commit $commit"
321 eval "$filter_env" < /dev/null ||
322 die "env filter failed: $filter_env"
6f6826c5
JS
323
324 if [ "$filter_tree" ]; then
af580e9c
JS
325 git checkout-index -f -u -a ||
326 die "Could not checkout the index"
6f6826c5
JS
327 # files that $commit removed are now still in the working tree;
328 # remove them, else they would be added again
6a589fda 329 git clean -d -q -f -x
8c1ce0f4
JS
330 eval "$filter_tree" < /dev/null ||
331 die "tree filter failed: $filter_tree"
332
1fe32cb9 333 (
03ca8395 334 git diff-index -r --name-only --ignore-submodules $commit &&
1fe32cb9 335 git ls-files --others
0ea29cce
EK
336 ) > "$tempdir"/tree-state || exit
337 git update-index --add --replace --remove --stdin \
338 < "$tempdir"/tree-state || exit
6f6826c5
JS
339 fi
340
8c1ce0f4
JS
341 eval "$filter_index" < /dev/null ||
342 die "index filter failed: $filter_index"
6f6826c5
JS
343
344 parentstr=
813b4734 345 for parent in $parents; do
3520e1e8
JS
346 for reparent in $(map "$parent"); do
347 parentstr="$parentstr -p $reparent"
348 done
6f6826c5
JS
349 done
350 if [ "$filter_parent" ]; then
8c1ce0f4
JS
351 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
352 die "parent filter failed: $filter_parent"
6f6826c5
JS
353 fi
354
355 sed -e '1,/^$/d' <../commit | \
8c1ce0f4
JS
356 eval "$filter_msg" > ../message ||
357 die "msg filter failed: $filter_msg"
4bf9f27d 358 @SHELL_PATH@ -c "$filter_commit" "git commit-tree" \
0ea29cce
EK
359 $(git write-tree) $parentstr < ../message > ../map/$commit ||
360 die "could not write rewritten commit"
6f6826c5
JS
361done <../revs
362
f2f3a6b8
TR
363# If we are filtering for paths, as in the case of a subdirectory
364# filter, it is possible that a specified head is not in the set of
365# rewritten commits, because it was pruned by the revision walker.
366# Ancestor remapping fixes this by mapping these heads to the unique
367# nearest ancestor that survived the pruning.
dfd05e38 368
f2f3a6b8 369if test "$remap_to_ancestor" = t
a0e46390
TR
370then
371 while read ref
dfd05e38 372 do
a0e46390
TR
373 sha1=$(git rev-parse "$ref"^0)
374 test -f "$workdir"/../map/$sha1 && continue
2c1d2d81 375 ancestor=$(git rev-list --simplify-merges -1 "$ref" "$@")
a0e46390
TR
376 test "$ancestor" && echo $(map $ancestor) >> "$workdir"/../map/$sha1
377 done < "$tempdir"/heads
378fi
dfd05e38
JS
379
380# Finally update the refs
381
382_x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
383_x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
dfd05e38
JS
384echo
385while read ref
386do
387 # avoid rewriting a ref twice
388 test -f "$orig_namespace$ref" && continue
389
390 sha1=$(git rev-parse "$ref"^0)
391 rewritten=$(map $sha1)
392
393 test $sha1 = "$rewritten" &&
394 warn "WARNING: Ref '$ref' is unchanged" &&
395 continue
396
397 case "$rewritten" in
398 '')
399 echo "Ref '$ref' was deleted"
400 git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
401 die "Could not delete $ref"
98409060 402 ;;
dfd05e38
JS
403 $_x40)
404 echo "Ref '$ref' was rewritten"
261044e8
TR
405 if ! git update-ref -m "filter-branch: rewrite" \
406 "$ref" $rewritten $sha1 2>/dev/null; then
407 if test $(git cat-file -t "$ref") = tag; then
408 if test -z "$filter_tag_name"; then
409 warn "WARNING: You said to rewrite tagged commits, but not the corresponding tag."
410 warn "WARNING: Perhaps use '--tag-name-filter cat' to rewrite the tag."
411 fi
412 else
413 die "Could not rewrite $ref"
414 fi
415 fi
98409060 416 ;;
dfd05e38
JS
417 *)
418 # NEEDSWORK: possibly add -Werror, making this an error
419 warn "WARNING: '$ref' was rewritten into multiple commits:"
420 warn "$rewritten"
421 warn "WARNING: Ref '$ref' points to the first one now."
422 rewritten=$(echo "$rewritten" | head -n 1)
423 git update-ref -m "filter-branch: rewrite to first" \
424 "$ref" $rewritten $sha1 ||
425 die "Could not rewrite $ref"
426 ;;
427 esac
0ea29cce
EK
428 git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1 ||
429 exit
dfd05e38
JS
430done < "$tempdir"/heads
431
432# TODO: This should possibly go, with the semantics that all positive given
433# refs are updated, and their original heads stored in refs/original/
434# Filter tags
6f6826c5
JS
435
436if [ "$filter_tag_name" ]; then
5be60078 437 git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags |
6f6826c5
JS
438 while read sha1 type ref; do
439 ref="${ref#refs/tags/}"
440 # XXX: Rewrite tagged trees as well?
441 if [ "$type" != "commit" -a "$type" != "tag" ]; then
442 continue;
443 fi
444
445 if [ "$type" = "tag" ]; then
446 # Dereference to a commit
447 sha1t="$sha1"
7bd93c1c 448 sha1="$(git rev-parse -q "$sha1"^{commit})" || continue
6f6826c5
JS
449 fi
450
451 [ -f "../map/$sha1" ] || continue
452 new_sha1="$(cat "../map/$sha1")"
3addc94a
JS
453 GIT_COMMIT="$sha1"
454 export GIT_COMMIT
8c1ce0f4
JS
455 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
456 die "tag name filter failed: $filter_tag_name"
6f6826c5
JS
457
458 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
459
460 if [ "$type" = "tag" ]; then
a9da1663
JS
461 new_sha1=$( ( printf 'object %s\ntype commit\ntag %s\n' \
462 "$new_sha1" "$new_ref"
463 git cat-file tag "$ref" |
1bf6551e 464 sed -n \
9524cf29 465 -e '1,/^$/{
a9da1663
JS
466 /^object /d
467 /^type /d
468 /^tag /d
9524cf29 469 }' \
1bf6551e 470 -e '/^-----BEGIN PGP SIGNATURE-----/q' \
a9da1663 471 -e 'p' ) |
1bf6551e
BC
472 git mktag) ||
473 die "Could not create new tag object for $ref"
474 if git cat-file tag "$ref" | \
e1622bfc 475 sane_grep '^-----BEGIN PGP SIGNATURE-----' >/dev/null 2>&1
1bf6551e
BC
476 then
477 warn "gpg signature stripped from tag object $sha1t"
478 fi
6f6826c5
JS
479 fi
480
af580e9c
JS
481 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
482 die "Could not write tag $new_ref"
6f6826c5
JS
483 done
484fi
485
486cd ../..
487rm -rf "$tempdir"
6f6826c5 488
def16e71
BC
489trap - 0
490
9273b562
EK
491unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
492test -z "$ORIG_GIT_DIR" || {
493 GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR
494}
495test -z "$ORIG_GIT_WORK_TREE" || {
496 GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" &&
497 export GIT_WORK_TREE
498}
499test -z "$ORIG_GIT_INDEX_FILE" || {
500 GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" &&
501 export GIT_INDEX_FILE
502}
503
a4661b01 504if [ "$(is_bare_repository)" = false ]; then
0ea29cce 505 git read-tree -u -m HEAD || exit
a4661b01 506fi
46eb449c 507
0ea29cce 508exit 0