3b041d81efd7e96a49c40eba0e23fc0b18508c82
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#
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.
10
11warn () {
12 echo "$*" >&2
13}
14
15map()
16{
17 # if it was not rewritten, take the original
18 if test -r "$workdir/../map/$1"
19 then
20 cat "$workdir/../map/$1"
21 else
22 echo "$1"
23 fi
24}
25
26# override die(): this version puts in an extra line break, so that
27# the progress is still visible
28
29die()
30{
31 echo >&2
32 echo "$*" >&2
33 exit 1
34}
35
36# When piped a commit, output a script to set the ident of either
37# "author" or "committer
38
39set_ident () {
40 lid="$(echo "$1" | tr "A-Z" "a-z")"
41 uid="$(echo "$1" | tr "a-z" "A-Z")"
42 pick_id_script='
43 /^'$lid' /{
44 s/'\''/'\''\\'\'\''/g
45 h
46 s/^'$lid' \([^<]*\) <[^>]*> .*$/\1/
47 s/'\''/'\''\'\'\''/g
48 s/.*/export GIT_'$uid'_NAME='\''&'\''/p
49
50 g
51 s/^'$lid' [^<]* <\([^>]*\)> .*$/\1/
52 s/'\''/'\''\'\'\''/g
53 s/.*/export GIT_'$uid'_EMAIL='\''&'\''/p
54
55 g
56 s/^'$lid' [^<]* <[^>]*> \(.*\)$/\1/
57 s/'\''/'\''\'\'\''/g
58 s/.*/export GIT_'$uid'_DATE='\''&'\''/p
59
60 q
61 }
62 '
63
64 LANG=C LC_ALL=C sed -ne "$pick_id_script"
65 # Ensure non-empty id name.
66 echo "[ -n \"\$GIT_${uid}_NAME\" ] || export GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\""
67}
68
69# This script can be sourced by the commit filter to get the functions
70test "a$SOURCE_FUNCTIONS" = a1 && return
71this_script="$(cd "$(dirname "$0")"; pwd)"/$(basename "$0")
72export this_script
73
74USAGE="[--env-filter <command>] [--tree-filter <command>] \
75[--index-filter <command>] [--parent-filter <command>] \
76[--msg-filter <command>] [--commit-filter <command>] \
77[--tag-name-filter <command>] [--subdirectory-filter <directory>] \
78[--original <namespace>] [-d <directory>] [-f | --force] \
79[<rev-list options>...]"
80
81. git-sh-setup
82
83tempdir=.git-rewrite
84filter_env=
85filter_tree=
86filter_index=
87filter_parent=
88filter_msg=cat
89filter_commit='git commit-tree "$@"'
90filter_tag_name=
91filter_subdir=
92orig_namespace=refs/original/
93force=
94while case "$#" in 0) usage;; esac
95do
96 case "$1" in
97 --)
98 shift
99 break
100 ;;
101 --force|-f)
102 shift
103 force=t
104 continue
105 ;;
106 -*)
107 ;;
108 *)
109 break;
110 esac
111
112 # all switches take one argument
113 ARG="$1"
114 case "$#" in 1) usage ;; esac
115 shift
116 OPTARG="$1"
117 shift
118
119 case "$ARG" in
120 -d)
121 tempdir="$OPTARG"
122 ;;
123 --env-filter)
124 filter_env="$OPTARG"
125 ;;
126 --tree-filter)
127 filter_tree="$OPTARG"
128 ;;
129 --index-filter)
130 filter_index="$OPTARG"
131 ;;
132 --parent-filter)
133 filter_parent="$OPTARG"
134 ;;
135 --msg-filter)
136 filter_msg="$OPTARG"
137 ;;
138 --commit-filter)
139 filter_commit='SOURCE_FUNCTIONS=1 . "$this_script";'" $OPTARG"
140 ;;
141 --tag-name-filter)
142 filter_tag_name="$OPTARG"
143 ;;
144 --subdirectory-filter)
145 filter_subdir="$OPTARG"
146 ;;
147 --original)
148 orig_namespace=$(expr "$OPTARG/" : '\(.*[^/]\)/*$')/
149 ;;
150 *)
151 usage
152 ;;
153 esac
154done
155
156case "$force" in
157t)
158 rm -rf "$tempdir"
159;;
160'')
161 test -d "$tempdir" &&
162 die "$tempdir already exists, please remove it"
163esac
164mkdir -p "$tempdir/t" &&
165tempdir="$(cd "$tempdir"; pwd)" &&
166cd "$tempdir/t" &&
167workdir="$(pwd)" ||
168die ""
169
170# Make sure refs/original is empty
171git for-each-ref > "$tempdir"/backup-refs
172while read sha1 type name
173do
174 case "$force,$name" in
175 ,$orig_namespace*)
176 die "Namespace $orig_namespace not empty"
177 ;;
178 t,$orig_namespace*)
179 git update-ref -d "$name" $sha1
180 ;;
181 esac
182done < "$tempdir"/backup-refs
183
184export GIT_DIR GIT_WORK_TREE=.
185
186# These refs should be updated if their heads were rewritten
187
188git rev-parse --revs-only --symbolic "$@" |
189while read ref
190do
191 # normalize ref
192 case "$ref" in
193 HEAD)
194 ref="$(git symbolic-ref "$ref")"
195 ;;
196 refs/*)
197 ;;
198 *)
199 ref="$(git for-each-ref --format='%(refname)' |
200 grep /"$ref")"
201 esac
202
203 git check-ref-format "$ref" && echo "$ref"
204done > "$tempdir"/heads
205
206test -s "$tempdir"/heads ||
207 die "Which ref do you want to rewrite?"
208
209export GIT_INDEX_FILE="$(pwd)/../index"
210git read-tree || die "Could not seed the index"
211
212ret=0
213
214# map old->new commit ids for rewriting parents
215mkdir ../map || die "Could not create map/ directory"
216
217case "$filter_subdir" in
218"")
219 git rev-list --reverse --topo-order --default HEAD \
220 --parents "$@"
221 ;;
222*)
223 git rev-list --reverse --topo-order --default HEAD \
224 --parents --full-history "$@" -- "$filter_subdir"
225esac > ../revs || die "Could not get the commits"
226commits=$(wc -l <../revs | tr -d " ")
227
228test $commits -eq 0 && die "Found nothing to rewrite"
229
230# Rewrite the commits
231
232i=0
233while read commit parents; do
234 i=$(($i+1))
235 printf "\rRewrite $commit ($i/$commits)"
236
237 case "$filter_subdir" in
238 "")
239 git read-tree -i -m $commit
240 ;;
241 *)
242 git read-tree -i -m $commit:"$filter_subdir"
243 esac || die "Could not initialize the index"
244
245 export GIT_COMMIT=$commit
246 git cat-file commit "$commit" >../commit ||
247 die "Cannot read commit $commit"
248
249 eval "$(set_ident AUTHOR <../commit)" ||
250 die "setting author failed for commit $commit"
251 eval "$(set_ident COMMITTER <../commit)" ||
252 die "setting committer failed for commit $commit"
253 eval "$filter_env" < /dev/null ||
254 die "env filter failed: $filter_env"
255
256 if [ "$filter_tree" ]; then
257 git checkout-index -f -u -a ||
258 die "Could not checkout the index"
259 # files that $commit removed are now still in the working tree;
260 # remove them, else they would be added again
261 git ls-files -z --others | xargs -0 rm -f
262 eval "$filter_tree" < /dev/null ||
263 die "tree filter failed: $filter_tree"
264
265 git diff-index -r $commit | cut -f 2- | tr '\n' '\0' | \
266 xargs -0 git update-index --add --replace --remove
267 git ls-files -z --others | \
268 xargs -0 git update-index --add --replace --remove
269 fi
270
271 eval "$filter_index" < /dev/null ||
272 die "index filter failed: $filter_index"
273
274 parentstr=
275 for parent in $parents; do
276 for reparent in $(map "$parent"); do
277 parentstr="$parentstr -p $reparent"
278 done
279 done
280 if [ "$filter_parent" ]; then
281 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
282 die "parent filter failed: $filter_parent"
283 fi
284
285 sed -e '1,/^$/d' <../commit | \
286 eval "$filter_msg" > ../message ||
287 die "msg filter failed: $filter_msg"
288 sh -c "$filter_commit" "git commit-tree" \
289 $(git write-tree) $parentstr < ../message > ../map/$commit
290done <../revs
291
292# In case of a subdirectory filter, it is possible that a specified head
293# is not in the set of rewritten commits, because it was pruned by the
294# revision walker. Fix it by mapping these heads to the next rewritten
295# ancestor(s), i.e. the boundaries in the set of rewritten commits.
296
297# NEEDSWORK: we should sort the unmapped refs topologically first
298while read ref
299do
300 sha1=$(git rev-parse "$ref"^0)
301 test -f "$workdir"/../map/$sha1 && continue
302 # Assign the boundarie(s) in the set of rewritten commits
303 # as the replacement commit(s).
304 # (This would look a bit nicer if --not --stdin worked.)
305 for p in $( (cd "$workdir"/../map; ls | sed "s/^/^/") |
306 git rev-list $ref --boundary --stdin |
307 sed -n "s/^-//p")
308 do
309 map $p >> "$workdir"/../map/$sha1
310 done
311done < "$tempdir"/heads
312
313# Finally update the refs
314
315_x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
316_x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
317count=0
318echo
319while read ref
320do
321 # avoid rewriting a ref twice
322 test -f "$orig_namespace$ref" && continue
323
324 sha1=$(git rev-parse "$ref"^0)
325 rewritten=$(map $sha1)
326
327 test $sha1 = "$rewritten" &&
328 warn "WARNING: Ref '$ref' is unchanged" &&
329 continue
330
331 case "$rewritten" in
332 '')
333 echo "Ref '$ref' was deleted"
334 git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
335 die "Could not delete $ref"
336 ;;
337 $_x40)
338 echo "Ref '$ref' was rewritten"
339 git update-ref -m "filter-branch: rewrite" \
340 "$ref" $rewritten $sha1 ||
341 die "Could not rewrite $ref"
342 ;;
343 *)
344 # NEEDSWORK: possibly add -Werror, making this an error
345 warn "WARNING: '$ref' was rewritten into multiple commits:"
346 warn "$rewritten"
347 warn "WARNING: Ref '$ref' points to the first one now."
348 rewritten=$(echo "$rewritten" | head -n 1)
349 git update-ref -m "filter-branch: rewrite to first" \
350 "$ref" $rewritten $sha1 ||
351 die "Could not rewrite $ref"
352 ;;
353 esac
354 git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1
355 count=$(($count+1))
356done < "$tempdir"/heads
357
358# TODO: This should possibly go, with the semantics that all positive given
359# refs are updated, and their original heads stored in refs/original/
360# Filter tags
361
362if [ "$filter_tag_name" ]; then
363 git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags |
364 while read sha1 type ref; do
365 ref="${ref#refs/tags/}"
366 # XXX: Rewrite tagged trees as well?
367 if [ "$type" != "commit" -a "$type" != "tag" ]; then
368 continue;
369 fi
370
371 if [ "$type" = "tag" ]; then
372 # Dereference to a commit
373 sha1t="$sha1"
374 sha1="$(git rev-parse "$sha1"^{commit} 2>/dev/null)" || continue
375 fi
376
377 [ -f "../map/$sha1" ] || continue
378 new_sha1="$(cat "../map/$sha1")"
379 export GIT_COMMIT="$sha1"
380 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
381 die "tag name filter failed: $filter_tag_name"
382
383 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
384
385 if [ "$type" = "tag" ]; then
386 # Warn that we are not rewriting the tag object itself.
387 warn "unreferencing tag object $sha1t"
388 fi
389
390 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
391 die "Could not write tag $new_ref"
392 done
393fi
394
395cd ../..
396rm -rf "$tempdir"
397echo
398test $count -gt 0 && echo "These refs were rewritten:"
399git show-ref | grep ^"$orig_namespace"
400
401exit $ret