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
95OPTIONS_SPEC=
96. git-sh-setup
97
98git diff-files --quiet &&
99 git diff-index --cached --quiet HEAD ||
100 die "Cannot rewrite branch(es) with a dirty working directory."
101
102tempdir=.git-rewrite
103filter_env=
104filter_tree=
105filter_index=
106filter_parent=
107filter_msg=cat
108filter_commit='git commit-tree "$@"'
109filter_tag_name=
110filter_subdir=
111orig_namespace=refs/original/
112force=
113while :
114do
115 test $# = 0 && usage
116 case "$1" in
117 --)
118 shift
119 break
120 ;;
121 --force|-f)
122 shift
123 force=t
124 continue
125 ;;
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 ;;
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)
159 filter_commit='SOURCE_FUNCTIONS=1 . "$this_script";'" $OPTARG"
160 ;;
161 --tag-name-filter)
162 filter_tag_name="$OPTARG"
163 ;;
164 --subdirectory-filter)
165 filter_subdir="$OPTARG"
166 ;;
167 --original)
168 orig_namespace=$(expr "$OPTARG/" : '\(.*[^/]\)/*$')/
169 ;;
170 *)
171 usage
172 ;;
173 esac
174done
175
176case "$force" in
177t)
178 rm -rf "$tempdir"
179;;
180'')
181 test -d "$tempdir" &&
182 die "$tempdir already exists, please remove it"
183esac
184mkdir -p "$tempdir/t" &&
185tempdir="$(cd "$tempdir"; pwd)" &&
186cd "$tempdir/t" &&
187workdir="$(pwd)" ||
188die ""
189
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
204ORIG_GIT_DIR="$GIT_DIR"
205ORIG_GIT_WORK_TREE="$GIT_WORK_TREE"
206ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE"
207export GIT_DIR GIT_WORK_TREE=.
208
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
232export GIT_INDEX_FILE="$(pwd)/../index"
233git read-tree || die "Could not seed the index"
234
235ret=0
236
237# map old->new commit ids for rewriting parents
238mkdir ../map || die "Could not create map/ directory"
239
240case "$filter_subdir" in
241"")
242 git rev-list --reverse --topo-order --default HEAD \
243 --parents "$@"
244 ;;
245*)
246 git rev-list --reverse --topo-order --default HEAD \
247 --parents --full-history "$@" -- "$filter_subdir"
248esac > ../revs || die "Could not get the commits"
249commits=$(wc -l <../revs | tr -d " ")
250
251test $commits -eq 0 && die "Found nothing to rewrite"
252
253# Rewrite the commits
254
255i=0
256while read commit parents; do
257 i=$(($i+1))
258 printf "\rRewrite $commit ($i/$commits)"
259
260 case "$filter_subdir" in
261 "")
262 git read-tree -i -m $commit
263 ;;
264 *)
265 git read-tree -i -m $commit:"$filter_subdir"
266 esac || die "Could not initialize the index"
267
268 export GIT_COMMIT=$commit
269 git cat-file commit "$commit" >../commit ||
270 die "Cannot read commit $commit"
271
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"
278
279 if [ "$filter_tree" ]; then
280 git checkout-index -f -u -a ||
281 die "Could not checkout the index"
282 # files that $commit removed are now still in the working tree;
283 # remove them, else they would be added again
284 git ls-files -z --others | xargs -0 rm -f
285 eval "$filter_tree" < /dev/null ||
286 die "tree filter failed: $filter_tree"
287
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
292 fi
293
294 eval "$filter_index" < /dev/null ||
295 die "index filter failed: $filter_index"
296
297 parentstr=
298 for parent in $parents; do
299 for reparent in $(map "$parent"); do
300 parentstr="$parentstr -p $reparent"
301 done
302 done
303 if [ "$filter_parent" ]; then
304 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
305 die "parent filter failed: $filter_parent"
306 fi
307
308 sed -e '1,/^$/d' <../commit | \
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
313done <../revs
314
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.)
328 for p in $( (cd "$workdir"/../map; ls | sed "s/^/^/") |
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"
359 ;;
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"
365 ;;
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
384
385if [ "$filter_tag_name" ]; then
386 git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags |
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"
397 sha1="$(git rev-parse "$sha1"^{commit} 2>/dev/null)" || continue
398 fi
399
400 [ -f "../map/$sha1" ] || continue
401 new_sha1="$(cat "../map/$sha1")"
402 export GIT_COMMIT="$sha1"
403 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
404 die "tag name filter failed: $filter_tag_name"
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
413 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
414 die "Could not write tag $new_ref"
415 done
416fi
417
418cd ../..
419rm -rf "$tempdir"
420echo
421test $count -gt 0 && echo "These refs were rewritten:"
422git show-ref | grep ^"$orig_namespace"
423
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
432exit $ret