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