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