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