]>
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 | |
03a7f388 | 14 | EMPTY_TREE=$(git hash-object -t tree /dev/null) |
15 | ||
b5669a05 | 16 | warn () { |
285c6cbf | 17 | echo "$*" >&2 |
b5669a05 SP |
18 | } |
19 | ||
6f6826c5 JS |
20 | map() |
21 | { | |
3520e1e8 | 22 | # if it was not rewritten, take the original |
c57a3494 JS |
23 | if test -r "$workdir/../map/$1" |
24 | then | |
25 | cat "$workdir/../map/$1" | |
26 | else | |
27 | echo "$1" | |
28 | fi | |
6f6826c5 JS |
29 | } |
30 | ||
f95eef15 JS |
31 | # if you run 'skip_commit "$@"' in a commit filter, it will print |
32 | # the (mapped) parents, effectively skipping the commit. | |
33 | ||
34 | skip_commit() | |
35 | { | |
36 | shift; | |
37 | while [ -n "$1" ]; | |
38 | do | |
39 | shift; | |
40 | map "$1"; | |
41 | shift; | |
42 | done; | |
43 | } | |
44 | ||
d3240d93 PH |
45 | # if you run 'git_commit_non_empty_tree "$@"' in a commit filter, |
46 | # it will skip commits that leave the tree untouched, commit the other. | |
47 | git_commit_non_empty_tree() | |
48 | { | |
49 | if test $# = 3 && test "$1" = $(git rev-parse "$3^{tree}"); then | |
50 | map "$3" | |
03a7f388 | 51 | elif test $# = 1 && test "$1" = $EMPTY_TREE; then |
a582a82d | 52 | : |
d3240d93 PH |
53 | else |
54 | git commit-tree "$@" | |
55 | fi | |
56 | } | |
8c1ce0f4 JS |
57 | # override die(): this version puts in an extra line break, so that |
58 | # the progress is still visible | |
59 | ||
60 | die() | |
61 | { | |
62 | echo >&2 | |
63 | echo "$*" >&2 | |
64 | exit 1 | |
65 | } | |
16ed34ad JS |
66 | EOF |
67 | ) | |
68 | ||
69 | eval "$functions" | |
8c1ce0f4 | 70 | |
3c730fab JK |
71 | finish_ident() { |
72 | # Ensure non-empty id name. | |
73 | echo "case \"\$GIT_$1_NAME\" in \"\") GIT_$1_NAME=\"\${GIT_$1_EMAIL%%@*}\" && export GIT_$1_NAME;; esac" | |
74 | # And make sure everything is exported. | |
75 | echo "export GIT_$1_NAME" | |
76 | echo "export GIT_$1_EMAIL" | |
77 | echo "export GIT_$1_DATE" | |
78 | } | |
6f6826c5 JS |
79 | |
80 | set_ident () { | |
3c730fab JK |
81 | parse_ident_from_commit author AUTHOR committer COMMITTER |
82 | finish_ident AUTHOR | |
83 | finish_ident COMMITTER | |
6f6826c5 JS |
84 | } |
85 | ||
e336afdf | 86 | USAGE="[--setup <command>] [--subdirectory-filter <directory>] [--env-filter <command>] |
3b117f73 AH |
87 | [--tree-filter <command>] [--index-filter <command>] |
88 | [--parent-filter <command>] [--msg-filter <command>] | |
89 | [--commit-filter <command>] [--tag-name-filter <command>] | |
e336afdf | 90 | [--original <namespace>] |
bd2c79fb | 91 | [-d <directory>] [-f | --force] [--state-branch <branch>] |
d612975e | 92 | [--] [<rev-list options>...]" |
7e0f1704 | 93 | |
8f321a39 | 94 | OPTIONS_SPEC= |
7e0f1704 JS |
95 | . git-sh-setup |
96 | ||
a4661b01 | 97 | if [ "$(is_bare_repository)" = false ]; then |
5347a50f | 98 | require_clean_work_tree 'rewrite branches' |
a4661b01 | 99 | fi |
46eb449c | 100 | |
6f6826c5 | 101 | tempdir=.git-rewrite |
3b117f73 | 102 | filter_setup= |
6f6826c5 JS |
103 | filter_env= |
104 | filter_tree= | |
105 | filter_index= | |
106 | filter_parent= | |
107 | filter_msg=cat | |
d3240d93 | 108 | filter_commit= |
6f6826c5 | 109 | filter_tag_name= |
685ef546 | 110 | filter_subdir= |
bd2c79fb | 111 | state_branch= |
dfd05e38 JS |
112 | orig_namespace=refs/original/ |
113 | force= | |
d3240d93 | 114 | prune_empty= |
f2f3a6b8 | 115 | remap_to_ancestor= |
822f7c73 | 116 | while : |
6f6826c5 JS |
117 | do |
118 | case "$1" in | |
119 | --) | |
120 | shift | |
121 | break | |
122 | ;; | |
dfd05e38 JS |
123 | --force|-f) |
124 | shift | |
125 | force=t | |
126 | continue | |
127 | ;; | |
f2f3a6b8 | 128 | --remap-to-ancestor) |
7ec344d8 | 129 | # deprecated ($remap_to_ancestor is set now automatically) |
f2f3a6b8 TR |
130 | shift |
131 | remap_to_ancestor=t | |
132 | continue | |
133 | ;; | |
d3240d93 PH |
134 | --prune-empty) |
135 | shift | |
136 | prune_empty=t | |
137 | continue | |
138 | ;; | |
6f6826c5 JS |
139 | -*) |
140 | ;; | |
141 | *) | |
142 | break; | |
143 | esac | |
144 | ||
145 | # all switches take one argument | |
146 | ARG="$1" | |
147 | case "$#" in 1) usage ;; esac | |
148 | shift | |
149 | OPTARG="$1" | |
150 | shift | |
151 | ||
152 | case "$ARG" in | |
153 | -d) | |
154 | tempdir="$OPTARG" | |
155 | ;; | |
3b117f73 AH |
156 | --setup) |
157 | filter_setup="$OPTARG" | |
158 | ;; | |
07c49845 DG |
159 | --subdirectory-filter) |
160 | filter_subdir="$OPTARG" | |
161 | remap_to_ancestor=t | |
162 | ;; | |
6f6826c5 JS |
163 | --env-filter) |
164 | filter_env="$OPTARG" | |
165 | ;; | |
166 | --tree-filter) | |
167 | filter_tree="$OPTARG" | |
168 | ;; | |
169 | --index-filter) | |
170 | filter_index="$OPTARG" | |
171 | ;; | |
172 | --parent-filter) | |
173 | filter_parent="$OPTARG" | |
174 | ;; | |
175 | --msg-filter) | |
176 | filter_msg="$OPTARG" | |
177 | ;; | |
178 | --commit-filter) | |
16ed34ad | 179 | filter_commit="$functions; $OPTARG" |
6f6826c5 JS |
180 | ;; |
181 | --tag-name-filter) | |
182 | filter_tag_name="$OPTARG" | |
183 | ;; | |
dfd05e38 | 184 | --original) |
55ced83d | 185 | orig_namespace=$(expr "$OPTARG/" : '\(.*[^/]\)/*$')/ |
dfd05e38 | 186 | ;; |
bd2c79fb IC |
187 | --state-branch) |
188 | state_branch="$OPTARG" | |
189 | ;; | |
6f6826c5 JS |
190 | *) |
191 | usage | |
192 | ;; | |
193 | esac | |
194 | done | |
195 | ||
d3240d93 PH |
196 | case "$prune_empty,$filter_commit" in |
197 | ,) | |
198 | filter_commit='git commit-tree "$@"';; | |
199 | t,) | |
200 | filter_commit="$functions;"' git_commit_non_empty_tree "$@"';; | |
201 | ,*) | |
202 | ;; | |
203 | *) | |
5da81713 | 204 | die "Cannot set --prune-empty and --commit-filter at the same time" |
d3240d93 PH |
205 | esac |
206 | ||
dfd05e38 JS |
207 | case "$force" in |
208 | t) | |
209 | rm -rf "$tempdir" | |
210 | ;; | |
211 | '') | |
212 | test -d "$tempdir" && | |
213 | die "$tempdir already exists, please remove it" | |
214 | esac | |
97276019 | 215 | orig_dir=$(pwd) |
af580e9c | 216 | mkdir -p "$tempdir/t" && |
dfd05e38 | 217 | tempdir="$(cd "$tempdir"; pwd)" && |
af580e9c JS |
218 | cd "$tempdir/t" && |
219 | workdir="$(pwd)" || | |
220 | die "" | |
6f6826c5 | 221 | |
def16e71 | 222 | # Remove tempdir on exit |
97276019 | 223 | trap 'cd "$orig_dir"; rm -rf "$tempdir"' 0 |
def16e71 | 224 | |
88e38808 LN |
225 | ORIG_GIT_DIR="$GIT_DIR" |
226 | ORIG_GIT_WORK_TREE="$GIT_WORK_TREE" | |
227 | ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE" | |
7b1378bd IC |
228 | ORIG_GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" |
229 | ORIG_GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" | |
230 | ORIG_GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" | |
231 | ORIG_GIT_COMMITTER_NAME="$GIT_COMMITTER_NAME" | |
232 | ORIG_GIT_COMMITTER_EMAIL="$GIT_COMMITTER_EMAIL" | |
233 | ORIG_GIT_COMMITTER_DATE="$GIT_COMMITTER_DATE" | |
234 | ||
88e38808 LN |
235 | GIT_WORK_TREE=. |
236 | export GIT_DIR GIT_WORK_TREE | |
237 | ||
dfd05e38 | 238 | # Make sure refs/original is empty |
0ea29cce | 239 | git for-each-ref > "$tempdir"/backup-refs || exit |
dfd05e38 JS |
240 | while read sha1 type name |
241 | do | |
242 | case "$force,$name" in | |
243 | ,$orig_namespace*) | |
734cd572 JT |
244 | die "Cannot create a new backup. |
245 | A previous backup already exists in $orig_namespace | |
246 | Force overwriting the backup with -f" | |
dfd05e38 JS |
247 | ;; |
248 | t,$orig_namespace*) | |
249 | git update-ref -d "$name" $sha1 | |
250 | ;; | |
251 | esac | |
252 | done < "$tempdir"/backup-refs | |
253 | ||
418fa3a5 | 254 | # The refs should be updated if their heads were rewritten |
0ea29cce | 255 | git rev-parse --no-flags --revs-only --symbolic-full-name \ |
f78ab355 YK |
256 | --default HEAD "$@" > "$tempdir"/raw-refs || exit |
257 | while read ref | |
258 | do | |
259 | case "$ref" in ^?*) continue ;; esac | |
260 | ||
261 | if git rev-parse --verify "$ref"^0 >/dev/null 2>&1 | |
262 | then | |
263 | echo "$ref" | |
264 | else | |
265 | warn "WARNING: not rewriting '$ref' (not a committish)" | |
266 | fi | |
267 | done >"$tempdir"/heads <"$tempdir"/raw-refs | |
dfd05e38 JS |
268 | |
269 | test -s "$tempdir"/heads || | |
69638939 | 270 | die "You must specify a ref to rewrite." |
dfd05e38 | 271 | |
3addc94a JS |
272 | GIT_INDEX_FILE="$(pwd)/../index" |
273 | export GIT_INDEX_FILE | |
6f6826c5 | 274 | |
af580e9c JS |
275 | # map old->new commit ids for rewriting parents |
276 | mkdir ../map || die "Could not create map/ directory" | |
6f6826c5 | 277 | |
bd2c79fb IC |
278 | if test -n "$state_branch" |
279 | then | |
280 | state_commit=$(git rev-parse --no-flags --revs-only "$state_branch") | |
281 | if test -n "$state_commit" | |
282 | then | |
283 | echo "Populating map from $state_branch ($state_commit)" 1>&2 | |
284 | perl -e'open(MAP, "-|", "git show $ARGV[0]:filter.map") or die; | |
285 | while (<MAP>) { | |
286 | m/(.*):(.*)/ or die; | |
287 | open F, ">../map/$1" or die; | |
288 | print F "$2" or die; | |
289 | close(F) or die; | |
290 | } | |
291 | close(MAP) or die;' "$state_commit" \ | |
292 | || die "Unable to load state from $state_branch:filter.map" | |
293 | else | |
294 | echo "Branch $state_branch does not exist. Will create" 1>&2 | |
295 | fi | |
296 | fi | |
297 | ||
2c1d2d81 TR |
298 | # we need "--" only if there are no path arguments in $@ |
299 | nonrevs=$(git rev-parse --no-revs "$@") || exit | |
7ec344d8 CH |
300 | if test -z "$nonrevs" |
301 | then | |
302 | dashdash=-- | |
303 | else | |
304 | dashdash= | |
305 | remap_to_ancestor=t | |
306 | fi | |
307 | ||
3361a548 | 308 | git rev-parse --revs-only "$@" >../parse |
2c1d2d81 | 309 | |
685ef546 JS |
310 | case "$filter_subdir" in |
311 | "") | |
2c1d2d81 | 312 | eval set -- "$(git rev-parse --sq --no-revs "$@")" |
685ef546 JS |
313 | ;; |
314 | *) | |
2c1d2d81 TR |
315 | eval set -- "$(git rev-parse --sq --no-revs "$@" $dashdash \ |
316 | "$filter_subdir")" | |
317 | ;; | |
318 | esac | |
319 | ||
320 | git rev-list --reverse --topo-order --default HEAD \ | |
3361a548 | 321 | --parents --simplify-merges --stdin "$@" <../parse >../revs || |
2c1d2d81 | 322 | die "Could not get the commits" |
9d6f220c | 323 | commits=$(wc -l <../revs | tr -d " ") |
6f6826c5 | 324 | |
0a0eb2e5 | 325 | test $commits -eq 0 && die_with_status 2 "Found nothing to rewrite" |
6f6826c5 | 326 | |
dfd05e38 | 327 | # Rewrite the commits |
6a9d16a0 GB |
328 | report_progress () |
329 | { | |
330 | if test -n "$progress" && | |
331 | test $git_filter_branch__commit_count -gt $next_sample_at | |
332 | then | |
71400d97 JH |
333 | count=$git_filter_branch__commit_count |
334 | ||
335 | now=$(date +%s) | |
336 | elapsed=$(($now - $start_timestamp)) | |
337 | remaining=$(( ($commits - $count) * $elapsed / $count )) | |
338 | if test $elapsed -gt 0 | |
6a9d16a0 | 339 | then |
71400d97 | 340 | next_sample_at=$(( ($elapsed + 1) * $count / $elapsed )) |
6a9d16a0 GB |
341 | else |
342 | next_sample_at=$(($next_sample_at + 1)) | |
343 | fi | |
71400d97 | 344 | progress=" ($elapsed seconds passed, remaining $remaining predicted)" |
6a9d16a0 | 345 | fi |
71400d97 | 346 | printf "\rRewrite $commit ($count/$commits)$progress " |
6a9d16a0 | 347 | } |
dfd05e38 | 348 | |
d5b0c97d | 349 | git_filter_branch__commit_count=0 |
6a9d16a0 GB |
350 | |
351 | progress= start_timestamp= | |
352 | if date '+%s' 2>/dev/null | grep -q '^[0-9][0-9]*$' | |
353 | then | |
354 | next_sample_at=0 | |
355 | progress="dummy to ensure this is not empty" | |
356 | start_timestamp=$(date '+%s') | |
357 | fi | |
358 | ||
348d4f2f JK |
359 | if test -n "$filter_index" || |
360 | test -n "$filter_tree" || | |
361 | test -n "$filter_subdir" | |
362 | then | |
363 | need_index=t | |
364 | else | |
365 | need_index= | |
366 | fi | |
367 | ||
3b117f73 AH |
368 | eval "$filter_setup" < /dev/null || |
369 | die "filter setup failed: $filter_setup" | |
370 | ||
813b4734 | 371 | while read commit parents; do |
d5b0c97d | 372 | git_filter_branch__commit_count=$(($git_filter_branch__commit_count+1)) |
6a9d16a0 GB |
373 | |
374 | report_progress | |
709cfe84 | 375 | test -f "$workdir"/../map/$commit && continue |
6f6826c5 | 376 | |
685ef546 JS |
377 | case "$filter_subdir" in |
378 | "") | |
348d4f2f JK |
379 | if test -n "$need_index" |
380 | then | |
381 | GIT_ALLOW_NULL_SHA1=1 git read-tree -i -m $commit | |
382 | fi | |
685ef546 JS |
383 | ;; |
384 | *) | |
5b044ac3 | 385 | # The commit may not have the subdirectory at all |
83bd7437 JK |
386 | err=$(GIT_ALLOW_NULL_SHA1=1 \ |
387 | git read-tree -i -m $commit:"$filter_subdir" 2>&1) || { | |
d69da76d | 388 | if ! git rev-parse -q --verify $commit:"$filter_subdir" |
5b044ac3 JH |
389 | then |
390 | rm -f "$GIT_INDEX_FILE" | |
391 | else | |
392 | echo >&2 "$err" | |
393 | false | |
394 | fi | |
395 | } | |
af580e9c | 396 | esac || die "Could not initialize the index" |
6f6826c5 | 397 | |
3addc94a JS |
398 | GIT_COMMIT=$commit |
399 | export GIT_COMMIT | |
af580e9c JS |
400 | git cat-file commit "$commit" >../commit || |
401 | die "Cannot read commit $commit" | |
6f6826c5 | 402 | |
3c730fab JK |
403 | eval "$(set_ident <../commit)" || |
404 | die "setting author/committer failed for commit $commit" | |
8c1ce0f4 JS |
405 | eval "$filter_env" < /dev/null || |
406 | die "env filter failed: $filter_env" | |
6f6826c5 JS |
407 | |
408 | if [ "$filter_tree" ]; then | |
af580e9c JS |
409 | git checkout-index -f -u -a || |
410 | die "Could not checkout the index" | |
6f6826c5 JS |
411 | # files that $commit removed are now still in the working tree; |
412 | # remove them, else they would be added again | |
6a589fda | 413 | git clean -d -q -f -x |
8c1ce0f4 JS |
414 | eval "$filter_tree" < /dev/null || |
415 | die "tree filter failed: $filter_tree" | |
416 | ||
1fe32cb9 | 417 | ( |
4d2a3646 | 418 | git diff-index -r --name-only --ignore-submodules $commit -- && |
1fe32cb9 | 419 | git ls-files --others |
0ea29cce EK |
420 | ) > "$tempdir"/tree-state || exit |
421 | git update-index --add --replace --remove --stdin \ | |
422 | < "$tempdir"/tree-state || exit | |
6f6826c5 JS |
423 | fi |
424 | ||
8c1ce0f4 JS |
425 | eval "$filter_index" < /dev/null || |
426 | die "index filter failed: $filter_index" | |
6f6826c5 JS |
427 | |
428 | parentstr= | |
813b4734 | 429 | for parent in $parents; do |
3520e1e8 | 430 | for reparent in $(map "$parent"); do |
79bc4ef3 CB |
431 | case "$parentstr " in |
432 | *" -p $reparent "*) | |
433 | ;; | |
434 | *) | |
435 | parentstr="$parentstr -p $reparent" | |
436 | ;; | |
437 | esac | |
3520e1e8 | 438 | done |
6f6826c5 JS |
439 | done |
440 | if [ "$filter_parent" ]; then | |
8c1ce0f4 JS |
441 | parentstr="$(echo "$parentstr" | eval "$filter_parent")" || |
442 | die "parent filter failed: $filter_parent" | |
6f6826c5 JS |
443 | fi |
444 | ||
df062010 | 445 | { |
a5a4b3ff | 446 | while IFS='' read -r header_line && test -n "$header_line" |
df062010 JK |
447 | do |
448 | # skip header lines... | |
449 | :; | |
450 | done | |
451 | # and output the actual commit message | |
452 | cat | |
453 | } <../commit | | |
8c1ce0f4 JS |
454 | eval "$filter_msg" > ../message || |
455 | die "msg filter failed: $filter_msg" | |
348d4f2f JK |
456 | |
457 | if test -n "$need_index" | |
458 | then | |
459 | tree=$(git write-tree) | |
460 | else | |
1dc413eb | 461 | tree=$(git rev-parse "$commit^{tree}") |
348d4f2f | 462 | fi |
0906f6e1 | 463 | workdir=$workdir @SHELL_PATH@ -c "$filter_commit" "git commit-tree" \ |
348d4f2f | 464 | "$tree" $parentstr < ../message > ../map/$commit || |
0ea29cce | 465 | die "could not write rewritten commit" |
6f6826c5 JS |
466 | done <../revs |
467 | ||
f2f3a6b8 TR |
468 | # If we are filtering for paths, as in the case of a subdirectory |
469 | # filter, it is possible that a specified head is not in the set of | |
470 | # rewritten commits, because it was pruned by the revision walker. | |
471 | # Ancestor remapping fixes this by mapping these heads to the unique | |
472 | # nearest ancestor that survived the pruning. | |
dfd05e38 | 473 | |
f2f3a6b8 | 474 | if test "$remap_to_ancestor" = t |
a0e46390 TR |
475 | then |
476 | while read ref | |
dfd05e38 | 477 | do |
a0e46390 TR |
478 | sha1=$(git rev-parse "$ref"^0) |
479 | test -f "$workdir"/../map/$sha1 && continue | |
2c1d2d81 | 480 | ancestor=$(git rev-list --simplify-merges -1 "$ref" "$@") |
a0e46390 TR |
481 | test "$ancestor" && echo $(map $ancestor) >> "$workdir"/../map/$sha1 |
482 | done < "$tempdir"/heads | |
483 | fi | |
dfd05e38 JS |
484 | |
485 | # Finally update the refs | |
486 | ||
487 | _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]' | |
488 | _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40" | |
dfd05e38 JS |
489 | echo |
490 | while read ref | |
491 | do | |
492 | # avoid rewriting a ref twice | |
493 | test -f "$orig_namespace$ref" && continue | |
494 | ||
495 | sha1=$(git rev-parse "$ref"^0) | |
496 | rewritten=$(map $sha1) | |
497 | ||
498 | test $sha1 = "$rewritten" && | |
499 | warn "WARNING: Ref '$ref' is unchanged" && | |
500 | continue | |
501 | ||
502 | case "$rewritten" in | |
503 | '') | |
504 | echo "Ref '$ref' was deleted" | |
505 | git update-ref -m "filter-branch: delete" -d "$ref" $sha1 || | |
506 | die "Could not delete $ref" | |
98409060 | 507 | ;; |
dfd05e38 JS |
508 | $_x40) |
509 | echo "Ref '$ref' was rewritten" | |
261044e8 TR |
510 | if ! git update-ref -m "filter-branch: rewrite" \ |
511 | "$ref" $rewritten $sha1 2>/dev/null; then | |
512 | if test $(git cat-file -t "$ref") = tag; then | |
513 | if test -z "$filter_tag_name"; then | |
514 | warn "WARNING: You said to rewrite tagged commits, but not the corresponding tag." | |
515 | warn "WARNING: Perhaps use '--tag-name-filter cat' to rewrite the tag." | |
516 | fi | |
517 | else | |
518 | die "Could not rewrite $ref" | |
519 | fi | |
520 | fi | |
98409060 | 521 | ;; |
dfd05e38 JS |
522 | *) |
523 | # NEEDSWORK: possibly add -Werror, making this an error | |
524 | warn "WARNING: '$ref' was rewritten into multiple commits:" | |
525 | warn "$rewritten" | |
526 | warn "WARNING: Ref '$ref' points to the first one now." | |
527 | rewritten=$(echo "$rewritten" | head -n 1) | |
528 | git update-ref -m "filter-branch: rewrite to first" \ | |
529 | "$ref" $rewritten $sha1 || | |
530 | die "Could not rewrite $ref" | |
531 | ;; | |
532 | esac | |
0ea29cce EK |
533 | git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1 || |
534 | exit | |
dfd05e38 JS |
535 | done < "$tempdir"/heads |
536 | ||
537 | # TODO: This should possibly go, with the semantics that all positive given | |
538 | # refs are updated, and their original heads stored in refs/original/ | |
539 | # Filter tags | |
6f6826c5 JS |
540 | |
541 | if [ "$filter_tag_name" ]; then | |
5be60078 | 542 | git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags | |
6f6826c5 JS |
543 | while read sha1 type ref; do |
544 | ref="${ref#refs/tags/}" | |
545 | # XXX: Rewrite tagged trees as well? | |
546 | if [ "$type" != "commit" -a "$type" != "tag" ]; then | |
547 | continue; | |
548 | fi | |
549 | ||
550 | if [ "$type" = "tag" ]; then | |
551 | # Dereference to a commit | |
552 | sha1t="$sha1" | |
7bd93c1c | 553 | sha1="$(git rev-parse -q "$sha1"^{commit})" || continue |
6f6826c5 JS |
554 | fi |
555 | ||
556 | [ -f "../map/$sha1" ] || continue | |
557 | new_sha1="$(cat "../map/$sha1")" | |
3addc94a JS |
558 | GIT_COMMIT="$sha1" |
559 | export GIT_COMMIT | |
8c1ce0f4 JS |
560 | new_ref="$(echo "$ref" | eval "$filter_tag_name")" || |
561 | die "tag name filter failed: $filter_tag_name" | |
6f6826c5 JS |
562 | |
563 | echo "$ref -> $new_ref ($sha1 -> $new_sha1)" | |
564 | ||
565 | if [ "$type" = "tag" ]; then | |
a9da1663 JS |
566 | new_sha1=$( ( printf 'object %s\ntype commit\ntag %s\n' \ |
567 | "$new_sha1" "$new_ref" | |
568 | git cat-file tag "$ref" | | |
1bf6551e | 569 | sed -n \ |
9524cf29 | 570 | -e '1,/^$/{ |
a9da1663 JS |
571 | /^object /d |
572 | /^type /d | |
573 | /^tag /d | |
9524cf29 | 574 | }' \ |
1bf6551e | 575 | -e '/^-----BEGIN PGP SIGNATURE-----/q' \ |
a9da1663 | 576 | -e 'p' ) | |
b2c1ca6b | 577 | git hash-object -t tag -w --stdin) || |
1bf6551e BC |
578 | die "Could not create new tag object for $ref" |
579 | if git cat-file tag "$ref" | \ | |
e1622bfc | 580 | sane_grep '^-----BEGIN PGP SIGNATURE-----' >/dev/null 2>&1 |
1bf6551e BC |
581 | then |
582 | warn "gpg signature stripped from tag object $sha1t" | |
583 | fi | |
6f6826c5 JS |
584 | fi |
585 | ||
af580e9c JS |
586 | git update-ref "refs/tags/$new_ref" "$new_sha1" || |
587 | die "Could not write tag $new_ref" | |
6f6826c5 JS |
588 | done |
589 | fi | |
590 | ||
9273b562 | 591 | unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE |
7b1378bd IC |
592 | unset GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE |
593 | unset GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL GIT_COMMITTER_DATE | |
9273b562 EK |
594 | test -z "$ORIG_GIT_DIR" || { |
595 | GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR | |
596 | } | |
597 | test -z "$ORIG_GIT_WORK_TREE" || { | |
598 | GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" && | |
599 | export GIT_WORK_TREE | |
600 | } | |
601 | test -z "$ORIG_GIT_INDEX_FILE" || { | |
602 | GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" && | |
603 | export GIT_INDEX_FILE | |
604 | } | |
7b1378bd IC |
605 | test -z "$ORIG_GIT_AUTHOR_NAME" || { |
606 | GIT_AUTHOR_NAME="$ORIG_GIT_AUTHOR_NAME" && | |
607 | export GIT_AUTHOR_NAME | |
608 | } | |
609 | test -z "$ORIG_GIT_AUTHOR_EMAIL" || { | |
610 | GIT_AUTHOR_EMAIL="$ORIG_GIT_AUTHOR_EMAIL" && | |
611 | export GIT_AUTHOR_EMAIL | |
612 | } | |
613 | test -z "$ORIG_GIT_AUTHOR_DATE" || { | |
614 | GIT_AUTHOR_DATE="$ORIG_GIT_AUTHOR_DATE" && | |
615 | export GIT_AUTHOR_DATE | |
616 | } | |
617 | test -z "$ORIG_GIT_COMMITTER_NAME" || { | |
618 | GIT_COMMITTER_NAME="$ORIG_GIT_COMMITTER_NAME" && | |
619 | export GIT_COMMITTER_NAME | |
620 | } | |
621 | test -z "$ORIG_GIT_COMMITTER_EMAIL" || { | |
622 | GIT_COMMITTER_EMAIL="$ORIG_GIT_COMMITTER_EMAIL" && | |
623 | export GIT_COMMITTER_EMAIL | |
624 | } | |
625 | test -z "$ORIG_GIT_COMMITTER_DATE" || { | |
626 | GIT_COMMITTER_DATE="$ORIG_GIT_COMMITTER_DATE" && | |
627 | export GIT_COMMITTER_DATE | |
628 | } | |
9273b562 | 629 | |
bd2c79fb IC |
630 | if test -n "$state_branch" |
631 | then | |
632 | echo "Saving rewrite state to $state_branch" 1>&2 | |
633 | state_blob=$( | |
634 | perl -e'opendir D, "../map" or die; | |
635 | open H, "|-", "git hash-object -w --stdin" or die; | |
636 | foreach (sort readdir(D)) { | |
637 | next if m/^\.\.?$/; | |
638 | open F, "<../map/$_" or die; | |
639 | chomp($f = <F>); | |
640 | print H "$_:$f\n" or die; | |
641 | } | |
642 | close(H) or die;' || die "Unable to save state") | |
206a6ae0 | 643 | state_tree=$(printf '100644 blob %s\tfilter.map\n' "$state_blob" | git mktree) |
bd2c79fb IC |
644 | if test -n "$state_commit" |
645 | then | |
206a6ae0 | 646 | state_commit=$(echo "Sync" | git commit-tree "$state_tree" -p "$state_commit") |
bd2c79fb | 647 | else |
206a6ae0 | 648 | state_commit=$(echo "Sync" | git commit-tree "$state_tree" ) |
bd2c79fb IC |
649 | fi |
650 | git update-ref "$state_branch" "$state_commit" | |
651 | fi | |
652 | ||
d24813c4 IC |
653 | cd "$orig_dir" |
654 | rm -rf "$tempdir" | |
655 | ||
656 | trap - 0 | |
657 | ||
a4661b01 | 658 | if [ "$(is_bare_repository)" = false ]; then |
0ea29cce | 659 | git read-tree -u -m HEAD || exit |
a4661b01 | 660 | fi |
46eb449c | 661 | |
0ea29cce | 662 | exit 0 |