]>
Commit | Line | Data |
---|---|---|
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 | |
16ed34ad JS |
11 | # The following functions will also be available in the commit filter: |
12 | ||
13 | functions=$(cat << \EOF | |
b5669a05 SP |
14 | warn () { |
15 | echo "$*" >&2 | |
16 | } | |
17 | ||
6f6826c5 JS |
18 | map() |
19 | { | |
3520e1e8 | 20 | # if it was not rewritten, take the original |
c57a3494 JS |
21 | if test -r "$workdir/../map/$1" |
22 | then | |
23 | cat "$workdir/../map/$1" | |
24 | else | |
25 | echo "$1" | |
26 | fi | |
6f6826c5 JS |
27 | } |
28 | ||
f95eef15 JS |
29 | # if you run 'skip_commit "$@"' in a commit filter, it will print |
30 | # the (mapped) parents, effectively skipping the commit. | |
31 | ||
32 | skip_commit() | |
33 | { | |
34 | shift; | |
35 | while [ -n "$1" ]; | |
36 | do | |
37 | shift; | |
38 | map "$1"; | |
39 | shift; | |
40 | done; | |
41 | } | |
42 | ||
8c1ce0f4 JS |
43 | # override die(): this version puts in an extra line break, so that |
44 | # the progress is still visible | |
45 | ||
46 | die() | |
47 | { | |
48 | echo >&2 | |
49 | echo "$*" >&2 | |
50 | exit 1 | |
51 | } | |
16ed34ad JS |
52 | EOF |
53 | ) | |
54 | ||
55 | eval "$functions" | |
8c1ce0f4 | 56 | |
6f6826c5 JS |
57 | # When piped a commit, output a script to set the ident of either |
58 | # "author" or "committer | |
59 | ||
60 | set_ident () { | |
40a7ce64 JK |
61 | lid="$(echo "$1" | tr "[A-Z]" "[a-z]")" |
62 | uid="$(echo "$1" | tr "[a-z]" "[A-Z]")" | |
6f6826c5 JS |
63 | pick_id_script=' |
64 | /^'$lid' /{ | |
65 | s/'\''/'\''\\'\'\''/g | |
66 | h | |
67 | s/^'$lid' \([^<]*\) <[^>]*> .*$/\1/ | |
68 | s/'\''/'\''\'\'\''/g | |
3addc94a | 69 | s/.*/GIT_'$uid'_NAME='\''&'\''; export GIT_'$uid'_NAME/p |
6f6826c5 JS |
70 | |
71 | g | |
72 | s/^'$lid' [^<]* <\([^>]*\)> .*$/\1/ | |
73 | s/'\''/'\''\'\'\''/g | |
3addc94a | 74 | s/.*/GIT_'$uid'_EMAIL='\''&'\''; export GIT_'$uid'_EMAIL/p |
6f6826c5 JS |
75 | |
76 | g | |
77 | s/^'$lid' [^<]* <[^>]*> \(.*\)$/\1/ | |
78 | s/'\''/'\''\'\'\''/g | |
3addc94a | 79 | s/.*/GIT_'$uid'_DATE='\''&'\''; export GIT_'$uid'_DATE/p |
6f6826c5 JS |
80 | |
81 | q | |
82 | } | |
83 | ' | |
84 | ||
85 | LANG=C LC_ALL=C sed -ne "$pick_id_script" | |
86 | # Ensure non-empty id name. | |
3addc94a | 87 | echo "case \"\$GIT_${uid}_NAME\" in \"\") GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\" && export GIT_${uid}_NAME;; esac" |
6f6826c5 JS |
88 | } |
89 | ||
7e0f1704 JS |
90 | USAGE="[--env-filter <command>] [--tree-filter <command>] \ |
91 | [--index-filter <command>] [--parent-filter <command>] \ | |
92 | [--msg-filter <command>] [--commit-filter <command>] \ | |
93 | [--tag-name-filter <command>] [--subdirectory-filter <directory>] \ | |
94 | [--original <namespace>] [-d <directory>] [-f | --force] \ | |
bb8eebb9 | 95 | [<rev-list options>...]" |
7e0f1704 | 96 | |
8f321a39 | 97 | OPTIONS_SPEC= |
7e0f1704 JS |
98 | . git-sh-setup |
99 | ||
a4661b01 PB |
100 | if [ "$(is_bare_repository)" = false ]; then |
101 | git diff-files --quiet && | |
38762c47 | 102 | git diff-index --cached --quiet HEAD -- || |
46eb449c | 103 | die "Cannot rewrite branch(es) with a dirty working directory." |
a4661b01 | 104 | fi |
46eb449c | 105 | |
6f6826c5 | 106 | tempdir=.git-rewrite |
6f6826c5 JS |
107 | filter_env= |
108 | filter_tree= | |
109 | filter_index= | |
110 | filter_parent= | |
111 | filter_msg=cat | |
5be60078 | 112 | filter_commit='git commit-tree "$@"' |
6f6826c5 | 113 | filter_tag_name= |
685ef546 | 114 | filter_subdir= |
dfd05e38 JS |
115 | orig_namespace=refs/original/ |
116 | force= | |
822f7c73 | 117 | while : |
6f6826c5 JS |
118 | do |
119 | case "$1" in | |
120 | --) | |
121 | shift | |
122 | break | |
123 | ;; | |
dfd05e38 JS |
124 | --force|-f) |
125 | shift | |
126 | force=t | |
127 | continue | |
128 | ;; | |
6f6826c5 JS |
129 | -*) |
130 | ;; | |
131 | *) | |
132 | break; | |
133 | esac | |
134 | ||
135 | # all switches take one argument | |
136 | ARG="$1" | |
137 | case "$#" in 1) usage ;; esac | |
138 | shift | |
139 | OPTARG="$1" | |
140 | shift | |
141 | ||
142 | case "$ARG" in | |
143 | -d) | |
144 | tempdir="$OPTARG" | |
145 | ;; | |
6f6826c5 JS |
146 | --env-filter) |
147 | filter_env="$OPTARG" | |
148 | ;; | |
149 | --tree-filter) | |
150 | filter_tree="$OPTARG" | |
151 | ;; | |
152 | --index-filter) | |
153 | filter_index="$OPTARG" | |
154 | ;; | |
155 | --parent-filter) | |
156 | filter_parent="$OPTARG" | |
157 | ;; | |
158 | --msg-filter) | |
159 | filter_msg="$OPTARG" | |
160 | ;; | |
161 | --commit-filter) | |
16ed34ad | 162 | filter_commit="$functions; $OPTARG" |
6f6826c5 JS |
163 | ;; |
164 | --tag-name-filter) | |
165 | filter_tag_name="$OPTARG" | |
166 | ;; | |
685ef546 JS |
167 | --subdirectory-filter) |
168 | filter_subdir="$OPTARG" | |
169 | ;; | |
dfd05e38 | 170 | --original) |
55ced83d | 171 | orig_namespace=$(expr "$OPTARG/" : '\(.*[^/]\)/*$')/ |
dfd05e38 | 172 | ;; |
6f6826c5 JS |
173 | *) |
174 | usage | |
175 | ;; | |
176 | esac | |
177 | done | |
178 | ||
dfd05e38 JS |
179 | case "$force" in |
180 | t) | |
181 | rm -rf "$tempdir" | |
182 | ;; | |
183 | '') | |
184 | test -d "$tempdir" && | |
185 | die "$tempdir already exists, please remove it" | |
186 | esac | |
af580e9c | 187 | mkdir -p "$tempdir/t" && |
dfd05e38 | 188 | tempdir="$(cd "$tempdir"; pwd)" && |
af580e9c JS |
189 | cd "$tempdir/t" && |
190 | workdir="$(pwd)" || | |
191 | die "" | |
6f6826c5 | 192 | |
def16e71 BC |
193 | # Remove tempdir on exit |
194 | trap 'cd ../..; rm -rf "$tempdir"' 0 | |
195 | ||
dfd05e38 JS |
196 | # Make sure refs/original is empty |
197 | git for-each-ref > "$tempdir"/backup-refs | |
198 | while read sha1 type name | |
199 | do | |
200 | case "$force,$name" in | |
201 | ,$orig_namespace*) | |
202 | die "Namespace $orig_namespace not empty" | |
203 | ;; | |
204 | t,$orig_namespace*) | |
205 | git update-ref -d "$name" $sha1 | |
206 | ;; | |
207 | esac | |
208 | done < "$tempdir"/backup-refs | |
209 | ||
46eb449c JS |
210 | ORIG_GIT_DIR="$GIT_DIR" |
211 | ORIG_GIT_WORK_TREE="$GIT_WORK_TREE" | |
212 | ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE" | |
3addc94a JS |
213 | GIT_WORK_TREE=. |
214 | export GIT_DIR GIT_WORK_TREE | |
6f6826c5 | 215 | |
418fa3a5 | 216 | # The refs should be updated if their heads were rewritten |
0f047f3b | 217 | git rev-parse --no-flags --revs-only --symbolic-full-name --default HEAD "$@" | |
418fa3a5 | 218 | sed -e '/^^/d' >"$tempdir"/heads |
dfd05e38 JS |
219 | |
220 | test -s "$tempdir"/heads || | |
221 | die "Which ref do you want to rewrite?" | |
222 | ||
3addc94a JS |
223 | GIT_INDEX_FILE="$(pwd)/../index" |
224 | export GIT_INDEX_FILE | |
af580e9c | 225 | git read-tree || die "Could not seed the index" |
6f6826c5 JS |
226 | |
227 | ret=0 | |
228 | ||
af580e9c JS |
229 | # map old->new commit ids for rewriting parents |
230 | mkdir ../map || die "Could not create map/ directory" | |
6f6826c5 | 231 | |
685ef546 JS |
232 | case "$filter_subdir" in |
233 | "") | |
5be60078 | 234 | git rev-list --reverse --topo-order --default HEAD \ |
813b4734 | 235 | --parents "$@" |
685ef546 JS |
236 | ;; |
237 | *) | |
5be60078 | 238 | git rev-list --reverse --topo-order --default HEAD \ |
a17171b4 | 239 | --parents "$@" -- "$filter_subdir" |
af580e9c | 240 | esac > ../revs || die "Could not get the commits" |
9d6f220c | 241 | commits=$(wc -l <../revs | tr -d " ") |
6f6826c5 JS |
242 | |
243 | test $commits -eq 0 && die "Found nothing to rewrite" | |
244 | ||
dfd05e38 JS |
245 | # Rewrite the commits |
246 | ||
6f6826c5 | 247 | i=0 |
813b4734 | 248 | while read commit parents; do |
c12764b8 | 249 | i=$(($i+1)) |
5efb48b5 | 250 | printf "\rRewrite $commit ($i/$commits)" |
6f6826c5 | 251 | |
685ef546 JS |
252 | case "$filter_subdir" in |
253 | "") | |
5be60078 | 254 | git read-tree -i -m $commit |
685ef546 JS |
255 | ;; |
256 | *) | |
5b044ac3 JH |
257 | # The commit may not have the subdirectory at all |
258 | err=$(git read-tree -i -m $commit:"$filter_subdir" 2>&1) || { | |
259 | if ! git rev-parse --verify $commit:"$filter_subdir" 2>/dev/null | |
260 | then | |
261 | rm -f "$GIT_INDEX_FILE" | |
262 | else | |
263 | echo >&2 "$err" | |
264 | false | |
265 | fi | |
266 | } | |
af580e9c | 267 | esac || die "Could not initialize the index" |
6f6826c5 | 268 | |
3addc94a JS |
269 | GIT_COMMIT=$commit |
270 | export GIT_COMMIT | |
af580e9c JS |
271 | git cat-file commit "$commit" >../commit || |
272 | die "Cannot read commit $commit" | |
6f6826c5 | 273 | |
8c1ce0f4 JS |
274 | eval "$(set_ident AUTHOR <../commit)" || |
275 | die "setting author failed for commit $commit" | |
276 | eval "$(set_ident COMMITTER <../commit)" || | |
277 | die "setting committer failed for commit $commit" | |
278 | eval "$filter_env" < /dev/null || | |
279 | die "env filter failed: $filter_env" | |
6f6826c5 JS |
280 | |
281 | if [ "$filter_tree" ]; then | |
af580e9c JS |
282 | git checkout-index -f -u -a || |
283 | die "Could not checkout the index" | |
6f6826c5 JS |
284 | # files that $commit removed are now still in the working tree; |
285 | # remove them, else they would be added again | |
6a589fda | 286 | git clean -d -q -f -x |
8c1ce0f4 JS |
287 | eval "$filter_tree" < /dev/null || |
288 | die "tree filter failed: $filter_tree" | |
289 | ||
1fe32cb9 JH |
290 | ( |
291 | git diff-index -r --name-only $commit | |
292 | git ls-files --others | |
293 | ) | | |
294 | git update-index --add --replace --remove --stdin | |
6f6826c5 JS |
295 | fi |
296 | ||
8c1ce0f4 JS |
297 | eval "$filter_index" < /dev/null || |
298 | die "index filter failed: $filter_index" | |
6f6826c5 JS |
299 | |
300 | parentstr= | |
813b4734 | 301 | for parent in $parents; do |
3520e1e8 JS |
302 | for reparent in $(map "$parent"); do |
303 | parentstr="$parentstr -p $reparent" | |
304 | done | |
6f6826c5 JS |
305 | done |
306 | if [ "$filter_parent" ]; then | |
8c1ce0f4 JS |
307 | parentstr="$(echo "$parentstr" | eval "$filter_parent")" || |
308 | die "parent filter failed: $filter_parent" | |
6f6826c5 JS |
309 | fi |
310 | ||
311 | sed -e '1,/^$/d' <../commit | \ | |
8c1ce0f4 JS |
312 | eval "$filter_msg" > ../message || |
313 | die "msg filter failed: $filter_msg" | |
4bf9f27d | 314 | @SHELL_PATH@ -c "$filter_commit" "git commit-tree" \ |
8c1ce0f4 | 315 | $(git write-tree) $parentstr < ../message > ../map/$commit |
6f6826c5 JS |
316 | done <../revs |
317 | ||
dfd05e38 JS |
318 | # In case of a subdirectory filter, it is possible that a specified head |
319 | # is not in the set of rewritten commits, because it was pruned by the | |
320 | # revision walker. Fix it by mapping these heads to the next rewritten | |
321 | # ancestor(s), i.e. the boundaries in the set of rewritten commits. | |
322 | ||
323 | # NEEDSWORK: we should sort the unmapped refs topologically first | |
324 | while read ref | |
325 | do | |
326 | sha1=$(git rev-parse "$ref"^0) | |
327 | test -f "$workdir"/../map/$sha1 && continue | |
328 | # Assign the boundarie(s) in the set of rewritten commits | |
329 | # as the replacement commit(s). | |
330 | # (This would look a bit nicer if --not --stdin worked.) | |
24d00634 | 331 | for p in $( (cd "$workdir"/../map; ls | sed "s/^/^/") | |
dfd05e38 JS |
332 | git rev-list $ref --boundary --stdin | |
333 | sed -n "s/^-//p") | |
334 | do | |
335 | map $p >> "$workdir"/../map/$sha1 | |
336 | done | |
337 | done < "$tempdir"/heads | |
338 | ||
339 | # Finally update the refs | |
340 | ||
341 | _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]' | |
342 | _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40" | |
dfd05e38 JS |
343 | echo |
344 | while read ref | |
345 | do | |
346 | # avoid rewriting a ref twice | |
347 | test -f "$orig_namespace$ref" && continue | |
348 | ||
349 | sha1=$(git rev-parse "$ref"^0) | |
350 | rewritten=$(map $sha1) | |
351 | ||
352 | test $sha1 = "$rewritten" && | |
353 | warn "WARNING: Ref '$ref' is unchanged" && | |
354 | continue | |
355 | ||
356 | case "$rewritten" in | |
357 | '') | |
358 | echo "Ref '$ref' was deleted" | |
359 | git update-ref -m "filter-branch: delete" -d "$ref" $sha1 || | |
360 | die "Could not delete $ref" | |
98409060 | 361 | ;; |
dfd05e38 JS |
362 | $_x40) |
363 | echo "Ref '$ref' was rewritten" | |
261044e8 TR |
364 | if ! git update-ref -m "filter-branch: rewrite" \ |
365 | "$ref" $rewritten $sha1 2>/dev/null; then | |
366 | if test $(git cat-file -t "$ref") = tag; then | |
367 | if test -z "$filter_tag_name"; then | |
368 | warn "WARNING: You said to rewrite tagged commits, but not the corresponding tag." | |
369 | warn "WARNING: Perhaps use '--tag-name-filter cat' to rewrite the tag." | |
370 | fi | |
371 | else | |
372 | die "Could not rewrite $ref" | |
373 | fi | |
374 | fi | |
98409060 | 375 | ;; |
dfd05e38 JS |
376 | *) |
377 | # NEEDSWORK: possibly add -Werror, making this an error | |
378 | warn "WARNING: '$ref' was rewritten into multiple commits:" | |
379 | warn "$rewritten" | |
380 | warn "WARNING: Ref '$ref' points to the first one now." | |
381 | rewritten=$(echo "$rewritten" | head -n 1) | |
382 | git update-ref -m "filter-branch: rewrite to first" \ | |
383 | "$ref" $rewritten $sha1 || | |
384 | die "Could not rewrite $ref" | |
385 | ;; | |
386 | esac | |
387 | git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1 | |
dfd05e38 JS |
388 | done < "$tempdir"/heads |
389 | ||
390 | # TODO: This should possibly go, with the semantics that all positive given | |
391 | # refs are updated, and their original heads stored in refs/original/ | |
392 | # Filter tags | |
6f6826c5 JS |
393 | |
394 | if [ "$filter_tag_name" ]; then | |
5be60078 | 395 | git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags | |
6f6826c5 JS |
396 | while read sha1 type ref; do |
397 | ref="${ref#refs/tags/}" | |
398 | # XXX: Rewrite tagged trees as well? | |
399 | if [ "$type" != "commit" -a "$type" != "tag" ]; then | |
400 | continue; | |
401 | fi | |
402 | ||
403 | if [ "$type" = "tag" ]; then | |
404 | # Dereference to a commit | |
405 | sha1t="$sha1" | |
5be60078 | 406 | sha1="$(git rev-parse "$sha1"^{commit} 2>/dev/null)" || continue |
6f6826c5 JS |
407 | fi |
408 | ||
409 | [ -f "../map/$sha1" ] || continue | |
410 | new_sha1="$(cat "../map/$sha1")" | |
3addc94a JS |
411 | GIT_COMMIT="$sha1" |
412 | export GIT_COMMIT | |
8c1ce0f4 JS |
413 | new_ref="$(echo "$ref" | eval "$filter_tag_name")" || |
414 | die "tag name filter failed: $filter_tag_name" | |
6f6826c5 JS |
415 | |
416 | echo "$ref -> $new_ref ($sha1 -> $new_sha1)" | |
417 | ||
418 | if [ "$type" = "tag" ]; then | |
1bf6551e BC |
419 | new_sha1=$(git cat-file tag "$ref" | |
420 | sed -n \ | |
421 | -e "1,/^$/{ | |
422 | s/^object .*/object $new_sha1/ | |
423 | s/^type .*/type commit/ | |
424 | s/^tag .*/tag $new_ref/ | |
425 | }" \ | |
426 | -e '/^-----BEGIN PGP SIGNATURE-----/q' \ | |
427 | -e 'p' | | |
428 | git mktag) || | |
429 | die "Could not create new tag object for $ref" | |
430 | if git cat-file tag "$ref" | \ | |
431 | grep '^-----BEGIN PGP SIGNATURE-----' >/dev/null 2>&1 | |
432 | then | |
433 | warn "gpg signature stripped from tag object $sha1t" | |
434 | fi | |
6f6826c5 JS |
435 | fi |
436 | ||
af580e9c JS |
437 | git update-ref "refs/tags/$new_ref" "$new_sha1" || |
438 | die "Could not write tag $new_ref" | |
6f6826c5 JS |
439 | done |
440 | fi | |
441 | ||
442 | cd ../.. | |
443 | rm -rf "$tempdir" | |
6f6826c5 | 444 | |
def16e71 BC |
445 | trap - 0 |
446 | ||
a4661b01 PB |
447 | if [ "$(is_bare_repository)" = false ]; then |
448 | unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE | |
449 | test -z "$ORIG_GIT_DIR" || { | |
450 | GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR | |
451 | } | |
452 | test -z "$ORIG_GIT_WORK_TREE" || { | |
453 | GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" && | |
454 | export GIT_WORK_TREE | |
455 | } | |
456 | test -z "$ORIG_GIT_INDEX_FILE" || { | |
457 | GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" && | |
458 | export GIT_INDEX_FILE | |
459 | } | |
460 | git read-tree -u -m HEAD | |
461 | fi | |
46eb449c | 462 | |
6f6826c5 | 463 | exit $ret |