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