]> git.ipfire.org Git - thirdparty/git.git/blame - git-filter-branch.sh
disallow providing multiple upstream branches to rebase, pull --rebase
[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
7e0f1704
JS
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] \
bb8eebb9 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=
822f7c73 128while :
6f6826c5
JS
129do
130 case "$1" in
131 --)
132 shift
133 break
134 ;;
dfd05e38
JS
135 --force|-f)
136 shift
137 force=t
138 continue
139 ;;
d3240d93
PH
140 --prune-empty)
141 shift
142 prune_empty=t
143 continue
144 ;;
6f6826c5
JS
145 -*)
146 ;;
147 *)
148 break;
149 esac
150
151 # all switches take one argument
152 ARG="$1"
153 case "$#" in 1) usage ;; esac
154 shift
155 OPTARG="$1"
156 shift
157
158 case "$ARG" in
159 -d)
160 tempdir="$OPTARG"
161 ;;
6f6826c5
JS
162 --env-filter)
163 filter_env="$OPTARG"
164 ;;
165 --tree-filter)
166 filter_tree="$OPTARG"
167 ;;
168 --index-filter)
169 filter_index="$OPTARG"
170 ;;
171 --parent-filter)
172 filter_parent="$OPTARG"
173 ;;
174 --msg-filter)
175 filter_msg="$OPTARG"
176 ;;
177 --commit-filter)
16ed34ad 178 filter_commit="$functions; $OPTARG"
6f6826c5
JS
179 ;;
180 --tag-name-filter)
181 filter_tag_name="$OPTARG"
182 ;;
685ef546
JS
183 --subdirectory-filter)
184 filter_subdir="$OPTARG"
185 ;;
dfd05e38 186 --original)
55ced83d 187 orig_namespace=$(expr "$OPTARG/" : '\(.*[^/]\)/*$')/
dfd05e38 188 ;;
6f6826c5
JS
189 *)
190 usage
191 ;;
192 esac
193done
194
d3240d93
PH
195case "$prune_empty,$filter_commit" in
196,)
197 filter_commit='git commit-tree "$@"';;
198t,)
199 filter_commit="$functions;"' git_commit_non_empty_tree "$@"';;
200,*)
201 ;;
202*)
203 die "Cannot set --prune-empty and --filter-commit at the same time"
204esac
205
dfd05e38
JS
206case "$force" in
207t)
208 rm -rf "$tempdir"
209;;
210'')
211 test -d "$tempdir" &&
212 die "$tempdir already exists, please remove it"
213esac
af580e9c 214mkdir -p "$tempdir/t" &&
dfd05e38 215tempdir="$(cd "$tempdir"; pwd)" &&
af580e9c
JS
216cd "$tempdir/t" &&
217workdir="$(pwd)" ||
218die ""
6f6826c5 219
def16e71
BC
220# Remove tempdir on exit
221trap 'cd ../..; rm -rf "$tempdir"' 0
222
dfd05e38 223# Make sure refs/original is empty
0ea29cce 224git for-each-ref > "$tempdir"/backup-refs || exit
dfd05e38
JS
225while read sha1 type name
226do
227 case "$force,$name" in
228 ,$orig_namespace*)
229 die "Namespace $orig_namespace not empty"
230 ;;
231 t,$orig_namespace*)
232 git update-ref -d "$name" $sha1
233 ;;
234 esac
235done < "$tempdir"/backup-refs
236
46eb449c
JS
237ORIG_GIT_DIR="$GIT_DIR"
238ORIG_GIT_WORK_TREE="$GIT_WORK_TREE"
239ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE"
3addc94a
JS
240GIT_WORK_TREE=.
241export GIT_DIR GIT_WORK_TREE
6f6826c5 242
418fa3a5 243# The refs should be updated if their heads were rewritten
0ea29cce
EK
244git rev-parse --no-flags --revs-only --symbolic-full-name \
245 --default HEAD "$@" > "$tempdir"/raw-heads || exit
246sed -e '/^^/d' "$tempdir"/raw-heads >"$tempdir"/heads
dfd05e38
JS
247
248test -s "$tempdir"/heads ||
249 die "Which ref do you want to rewrite?"
250
3addc94a
JS
251GIT_INDEX_FILE="$(pwd)/../index"
252export GIT_INDEX_FILE
af580e9c 253git read-tree || die "Could not seed the index"
6f6826c5 254
af580e9c
JS
255# map old->new commit ids for rewriting parents
256mkdir ../map || die "Could not create map/ directory"
6f6826c5 257
685ef546
JS
258case "$filter_subdir" in
259"")
5be60078 260 git rev-list --reverse --topo-order --default HEAD \
f34a9416 261 --parents --simplify-merges "$@"
685ef546
JS
262 ;;
263*)
5be60078 264 git rev-list --reverse --topo-order --default HEAD \
f34a9416 265 --parents --simplify-merges "$@" -- "$filter_subdir"
af580e9c 266esac > ../revs || die "Could not get the commits"
9d6f220c 267commits=$(wc -l <../revs | tr -d " ")
6f6826c5
JS
268
269test $commits -eq 0 && die "Found nothing to rewrite"
270
dfd05e38
JS
271# Rewrite the commits
272
6f6826c5 273i=0
813b4734 274while read commit parents; do
c12764b8 275 i=$(($i+1))
5efb48b5 276 printf "\rRewrite $commit ($i/$commits)"
6f6826c5 277
685ef546
JS
278 case "$filter_subdir" in
279 "")
5be60078 280 git read-tree -i -m $commit
685ef546
JS
281 ;;
282 *)
5b044ac3
JH
283 # The commit may not have the subdirectory at all
284 err=$(git read-tree -i -m $commit:"$filter_subdir" 2>&1) || {
d69da76d 285 if ! git rev-parse -q --verify $commit:"$filter_subdir"
5b044ac3
JH
286 then
287 rm -f "$GIT_INDEX_FILE"
288 else
289 echo >&2 "$err"
290 false
291 fi
292 }
af580e9c 293 esac || die "Could not initialize the index"
6f6826c5 294
3addc94a
JS
295 GIT_COMMIT=$commit
296 export GIT_COMMIT
af580e9c
JS
297 git cat-file commit "$commit" >../commit ||
298 die "Cannot read commit $commit"
6f6826c5 299
8c1ce0f4
JS
300 eval "$(set_ident AUTHOR <../commit)" ||
301 die "setting author failed for commit $commit"
302 eval "$(set_ident COMMITTER <../commit)" ||
303 die "setting committer failed for commit $commit"
304 eval "$filter_env" < /dev/null ||
305 die "env filter failed: $filter_env"
6f6826c5
JS
306
307 if [ "$filter_tree" ]; then
af580e9c
JS
308 git checkout-index -f -u -a ||
309 die "Could not checkout the index"
6f6826c5
JS
310 # files that $commit removed are now still in the working tree;
311 # remove them, else they would be added again
6a589fda 312 git clean -d -q -f -x
8c1ce0f4
JS
313 eval "$filter_tree" < /dev/null ||
314 die "tree filter failed: $filter_tree"
315
1fe32cb9 316 (
0ea29cce 317 git diff-index -r --name-only $commit &&
1fe32cb9 318 git ls-files --others
0ea29cce
EK
319 ) > "$tempdir"/tree-state || exit
320 git update-index --add --replace --remove --stdin \
321 < "$tempdir"/tree-state || exit
6f6826c5
JS
322 fi
323
8c1ce0f4
JS
324 eval "$filter_index" < /dev/null ||
325 die "index filter failed: $filter_index"
6f6826c5
JS
326
327 parentstr=
813b4734 328 for parent in $parents; do
3520e1e8
JS
329 for reparent in $(map "$parent"); do
330 parentstr="$parentstr -p $reparent"
331 done
6f6826c5
JS
332 done
333 if [ "$filter_parent" ]; then
8c1ce0f4
JS
334 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
335 die "parent filter failed: $filter_parent"
6f6826c5
JS
336 fi
337
338 sed -e '1,/^$/d' <../commit | \
8c1ce0f4
JS
339 eval "$filter_msg" > ../message ||
340 die "msg filter failed: $filter_msg"
4bf9f27d 341 @SHELL_PATH@ -c "$filter_commit" "git commit-tree" \
0ea29cce
EK
342 $(git write-tree) $parentstr < ../message > ../map/$commit ||
343 die "could not write rewritten commit"
6f6826c5
JS
344done <../revs
345
dfd05e38
JS
346# In case of a subdirectory filter, it is possible that a specified head
347# is not in the set of rewritten commits, because it was pruned by the
a0e46390
TR
348# revision walker. Fix it by mapping these heads to the unique nearest
349# ancestor that survived the pruning.
dfd05e38 350
a0e46390
TR
351if test "$filter_subdir"
352then
353 while read ref
dfd05e38 354 do
a0e46390
TR
355 sha1=$(git rev-parse "$ref"^0)
356 test -f "$workdir"/../map/$sha1 && continue
f34a9416
TR
357 ancestor=$(git rev-list --simplify-merges -1 \
358 $ref -- "$filter_subdir")
a0e46390
TR
359 test "$ancestor" && echo $(map $ancestor) >> "$workdir"/../map/$sha1
360 done < "$tempdir"/heads
361fi
dfd05e38
JS
362
363# Finally update the refs
364
365_x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
366_x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
dfd05e38
JS
367echo
368while read ref
369do
370 # avoid rewriting a ref twice
371 test -f "$orig_namespace$ref" && continue
372
373 sha1=$(git rev-parse "$ref"^0)
374 rewritten=$(map $sha1)
375
376 test $sha1 = "$rewritten" &&
377 warn "WARNING: Ref '$ref' is unchanged" &&
378 continue
379
380 case "$rewritten" in
381 '')
382 echo "Ref '$ref' was deleted"
383 git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
384 die "Could not delete $ref"
98409060 385 ;;
dfd05e38
JS
386 $_x40)
387 echo "Ref '$ref' was rewritten"
261044e8
TR
388 if ! git update-ref -m "filter-branch: rewrite" \
389 "$ref" $rewritten $sha1 2>/dev/null; then
390 if test $(git cat-file -t "$ref") = tag; then
391 if test -z "$filter_tag_name"; then
392 warn "WARNING: You said to rewrite tagged commits, but not the corresponding tag."
393 warn "WARNING: Perhaps use '--tag-name-filter cat' to rewrite the tag."
394 fi
395 else
396 die "Could not rewrite $ref"
397 fi
398 fi
98409060 399 ;;
dfd05e38
JS
400 *)
401 # NEEDSWORK: possibly add -Werror, making this an error
402 warn "WARNING: '$ref' was rewritten into multiple commits:"
403 warn "$rewritten"
404 warn "WARNING: Ref '$ref' points to the first one now."
405 rewritten=$(echo "$rewritten" | head -n 1)
406 git update-ref -m "filter-branch: rewrite to first" \
407 "$ref" $rewritten $sha1 ||
408 die "Could not rewrite $ref"
409 ;;
410 esac
0ea29cce
EK
411 git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1 ||
412 exit
dfd05e38
JS
413done < "$tempdir"/heads
414
415# TODO: This should possibly go, with the semantics that all positive given
416# refs are updated, and their original heads stored in refs/original/
417# Filter tags
6f6826c5
JS
418
419if [ "$filter_tag_name" ]; then
5be60078 420 git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags |
6f6826c5
JS
421 while read sha1 type ref; do
422 ref="${ref#refs/tags/}"
423 # XXX: Rewrite tagged trees as well?
424 if [ "$type" != "commit" -a "$type" != "tag" ]; then
425 continue;
426 fi
427
428 if [ "$type" = "tag" ]; then
429 # Dereference to a commit
430 sha1t="$sha1"
5be60078 431 sha1="$(git rev-parse "$sha1"^{commit} 2>/dev/null)" || continue
6f6826c5
JS
432 fi
433
434 [ -f "../map/$sha1" ] || continue
435 new_sha1="$(cat "../map/$sha1")"
3addc94a
JS
436 GIT_COMMIT="$sha1"
437 export GIT_COMMIT
8c1ce0f4
JS
438 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
439 die "tag name filter failed: $filter_tag_name"
6f6826c5
JS
440
441 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
442
443 if [ "$type" = "tag" ]; then
a9da1663
JS
444 new_sha1=$( ( printf 'object %s\ntype commit\ntag %s\n' \
445 "$new_sha1" "$new_ref"
446 git cat-file tag "$ref" |
1bf6551e
BC
447 sed -n \
448 -e "1,/^$/{
a9da1663
JS
449 /^object /d
450 /^type /d
451 /^tag /d
1bf6551e
BC
452 }" \
453 -e '/^-----BEGIN PGP SIGNATURE-----/q' \
a9da1663 454 -e 'p' ) |
1bf6551e
BC
455 git mktag) ||
456 die "Could not create new tag object for $ref"
457 if git cat-file tag "$ref" | \
458 grep '^-----BEGIN PGP SIGNATURE-----' >/dev/null 2>&1
459 then
460 warn "gpg signature stripped from tag object $sha1t"
461 fi
6f6826c5
JS
462 fi
463
af580e9c
JS
464 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
465 die "Could not write tag $new_ref"
6f6826c5
JS
466 done
467fi
468
469cd ../..
470rm -rf "$tempdir"
6f6826c5 471
def16e71
BC
472trap - 0
473
9273b562
EK
474unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
475test -z "$ORIG_GIT_DIR" || {
476 GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR
477}
478test -z "$ORIG_GIT_WORK_TREE" || {
479 GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" &&
480 export GIT_WORK_TREE
481}
482test -z "$ORIG_GIT_INDEX_FILE" || {
483 GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" &&
484 export GIT_INDEX_FILE
485}
486
a4661b01 487if [ "$(is_bare_repository)" = false ]; then
0ea29cce 488 git read-tree -u -m HEAD || exit
a4661b01 489fi
46eb449c 490
0ea29cce 491exit 0