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