Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/git-filter-branch.sh # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / git-filter-branch.sh` 667 lines ยท 15850 bytes Raw Blame History 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 11 # The following functions will also be available in the commit filter: 12 13 functions=$(cat << \EOF 14 EMPTY_TREE=$(git hash-object -t tree /dev/null) 15 16 warn () { 17 echo "$*" >&2 18 } 19 20 map() 21 { 22 # if it was not rewritten, take the original 23 if test -r "$workdir/../map/$1" 24 then 25 cat "$workdir/../map/$1" 26 else 27 echo "$1" 28 fi 29 } 30 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 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" 51 elif test $# = 1 && test "$1" = $EMPTY_TREE; then 52 : 53 else 54 git commit-tree "$@" 55 fi 56 } 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 } 66 EOF 67 ) 68 69 eval "$functions" 70 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 } 79 80 set_ident () { 81 parse_ident_from_commit author AUTHOR committer COMMITTER 82 finish_ident AUTHOR 83 finish_ident COMMITTER 84 } 85 86 if test -z "$FILTER_BRANCH_SQUELCH_WARNING$GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS" 87 then 88 cat < "$tempdir"/backup-refs || exit 254 while read sha1 type name 255 do 256 case "$force,$name" in 257 ,$orig_namespace*) 258 die "Cannot create a new backup. 259 A previous backup already exists in $orig_namespace 260 Force overwriting the backup with -f" 261 ;; 262 t,$orig_namespace*) 263 git update-ref -d "$name" $sha1 264 ;; 265 esac 266 done < "$tempdir"/backup-refs 267 268 # The refs should be updated if their heads were rewritten 269 git rev-parse --no-flags --revs-only --symbolic-full-name \ 270 --default HEAD "$@" > "$tempdir"/raw-refs || exit 271 while read ref 272 do 273 case "$ref" in ^?*) continue ;; esac 274 275 if git rev-parse --verify "$ref"^0 >/dev/null 2>&1 276 then 277 echo "$ref" 278 else 279 warn "WARNING: not rewriting '$ref' (not a committish)" 280 fi 281 done >"$tempdir"/heads <"$tempdir"/raw-refs 282 283 test -s "$tempdir"/heads || 284 die "You must specify a ref to rewrite." 285 286 GIT_INDEX_FILE="$(pwd)/../index" 287 export GIT_INDEX_FILE 288 289 # map old->new commit ids for rewriting parents 290 mkdir ../map || die "Could not create map/ directory" 291 292 if test -n "$state_branch" 293 then 294 state_commit=$(git rev-parse --no-flags --revs-only "$state_branch") 295 if test -n "$state_commit" 296 then 297 echo "Populating map from $state_branch ($state_commit)" 1>&2 298 299 git show "$state_commit:filter.map" >"$tempdir"/filter-map || 300 die "Unable to load state from $state_branch:filter.map" 301 while read line 302 do 303 case "$line" in 304 *:*) 305 from_commit=${line%:*} 306 to_commit=${line#*:} 307 echo "$to_commit" >../map/"$from_commit";; 308 *) 309 die "Unable to load state from $state_branch:filter.map";; 310 esac 311 done <"$tempdir"/filter-map 312 else 313 echo "Branch $state_branch does not exist. Will create" 1>&2 314 fi 315 fi 316 317 # we need "--" only if there are no path arguments in $@ 318 nonrevs=$(git rev-parse --no-revs "$@") || exit 319 if test -z "$nonrevs" 320 then 321 dashdash=-- 322 else 323 dashdash= 324 remap_to_ancestor=t 325 fi 326 327 git rev-parse --revs-only "$@" >../parse 328 329 case "$filter_subdir" in 330 "") 331 eval set -- "$(git rev-parse --sq --no-revs "$@")" 332 ;; 333 *) 334 eval set -- "$(git rev-parse --sq --no-revs "$@" $dashdash \ 335 "$filter_subdir")" 336 ;; 337 esac 338 339 git rev-list --reverse --topo-order --default HEAD \ 340 --parents --simplify-merges --stdin "$@" <../parse >../revs || 341 die "Could not get the commits" 342 commits=$(wc -l <../revs | tr -d " ") 343 344 test $commits -eq 0 && die_with_status 2 "Found nothing to rewrite" 345 346 # Rewrite the commits 347 report_progress () 348 { 349 if test -n "$progress" && 350 test $git_filter_branch__commit_count -gt $next_sample_at 351 then 352 count=$git_filter_branch__commit_count 353 354 now=$(date +%s) 355 elapsed=$(($now - $start_timestamp)) 356 remaining=$(( ($commits - $count) * $elapsed / $count )) 357 if test $elapsed -gt 0 358 then 359 next_sample_at=$(( ($elapsed + 1) * $count / $elapsed )) 360 else 361 next_sample_at=$(($next_sample_at + 1)) 362 fi 363 progress=" ($elapsed seconds passed, remaining $remaining predicted)" 364 fi 365 printf "\rRewrite $commit ($count/$commits)$progress " 366 } 367 368 git_filter_branch__commit_count=0 369 370 progress= start_timestamp= 371 if date '+%s' 2>/dev/null | grep -q '^[0-9][0-9]*$' 372 then 373 next_sample_at=0 374 progress="dummy to ensure this is not empty" 375 start_timestamp=$(date '+%s') 376 fi 377 378 if test -n "$filter_index" || 379 test -n "$filter_tree" || 380 test -n "$filter_subdir" 381 then 382 need_index=t 383 else 384 need_index= 385 fi 386 387 eval "$filter_setup" < /dev/null || 388 die "filter setup failed: $filter_setup" 389 390 while read commit parents; do 391 git_filter_branch__commit_count=$(($git_filter_branch__commit_count+1)) 392 393 report_progress 394 test -f "$workdir"/../map/$commit && continue 395 396 case "$filter_subdir" in 397 "") 398 if test -n "$need_index" 399 then 400 GIT_ALLOW_NULL_SHA1=1 git read-tree -i -m $commit 401 fi 402 ;; 403 *) 404 # The commit may not have the subdirectory at all 405 err=$(GIT_ALLOW_NULL_SHA1=1 \ 406 git read-tree -i -m $commit:"$filter_subdir" 2>&1) || { 407 if ! git rev-parse -q --verify $commit:"$filter_subdir" 408 then 409 rm -f "$GIT_INDEX_FILE" 410 else 411 echo >&2 "$err" 412 false 413 fi 414 } 415 esac || die "Could not initialize the index" 416 417 GIT_COMMIT=$commit 418 export GIT_COMMIT 419 git cat-file commit "$commit" >../commit || 420 die "Cannot read commit $commit" 421 422 eval "$(set_ident <../commit)" || 423 die "setting author/committer failed for commit $commit" 424 eval "$filter_env" < /dev/null || 425 die "env filter failed: $filter_env" 426 427 if [ "$filter_tree" ]; then 428 git checkout-index -f -u -a || 429 die "Could not checkout the index" 430 # files that $commit removed are now still in the working tree; 431 # remove them, else they would be added again 432 git clean -d -q -f -x 433 eval "$filter_tree" < /dev/null || 434 die "tree filter failed: $filter_tree" 435 436 ( 437 git diff-index -r --name-only --ignore-submodules $commit -- && 438 git ls-files --others 439 ) > "$tempdir"/tree-state || exit 440 git update-index --add --replace --remove --stdin \ 441 < "$tempdir"/tree-state || exit 442 fi 443 444 eval "$filter_index" < /dev/null || 445 die "index filter failed: $filter_index" 446 447 parentstr= 448 for parent in $parents; do 449 for reparent in $(map "$parent"); do 450 case "$parentstr " in 451 *" -p $reparent "*) 452 ;; 453 *) 454 parentstr="$parentstr -p $reparent" 455 ;; 456 esac 457 done 458 done 459 if [ "$filter_parent" ]; then 460 parentstr="$(echo "$parentstr" | eval "$filter_parent")" || 461 die "parent filter failed: $filter_parent" 462 fi 463 464 { 465 while IFS='' read -r header_line && test -n "$header_line" 466 do 467 # skip header lines... 468 :; 469 done 470 # and output the actual commit message 471 cat 472 } <../commit | 473 eval "$filter_msg" > ../message || 474 die "msg filter failed: $filter_msg" 475 476 if test -n "$need_index" 477 then 478 tree=$(git write-tree) 479 else 480 tree=$(git rev-parse "$commit^{tree}") 481 fi 482 workdir=$workdir @SHELL_PATH@ -c "$filter_commit" "git commit-tree" \ 483 "$tree" $parentstr < ../message > ../map/$commit || 484 die "could not write rewritten commit" 485 done <../revs 486 487 # If we are filtering for paths, as in the case of a subdirectory 488 # filter, it is possible that a specified head is not in the set of 489 # rewritten commits, because it was pruned by the revision walker. 490 # Ancestor remapping fixes this by mapping these heads to the unique 491 # nearest ancestor that survived the pruning. 492 493 if test "$remap_to_ancestor" = t 494 then 495 while read ref 496 do 497 sha1=$(git rev-parse "$ref"^0) 498 test -f "$workdir"/../map/$sha1 && continue 499 ancestor=$(git rev-list --simplify-merges -1 "$ref" "$@") 500 test "$ancestor" && echo $(map $ancestor) >"$workdir"/../map/$sha1 501 done < "$tempdir"/heads 502 fi 503 504 # Finally update the refs 505 506 echo 507 while read ref 508 do 509 # avoid rewriting a ref twice 510 test -f "$orig_namespace$ref" && continue 511 512 sha1=$(git rev-parse "$ref"^0) 513 rewritten=$(map $sha1) 514 515 test $sha1 = "$rewritten" && 516 warn "WARNING: Ref '$ref' is unchanged" && 517 continue 518 519 case "$rewritten" in 520 '') 521 echo "Ref '$ref' was deleted" 522 git update-ref -m "filter-branch: delete" -d "$ref" $sha1 || 523 die "Could not delete $ref" 524 ;; 525 *) 526 echo "Ref '$ref' was rewritten" 527 if ! git update-ref -m "filter-branch: rewrite" \ 528 "$ref" $rewritten $sha1 2>/dev/null; then 529 if test $(git cat-file -t "$ref") = tag; then 530 if test -z "$filter_tag_name"; then 531 warn "WARNING: You said to rewrite tagged commits, but not the corresponding tag." 532 warn "WARNING: Perhaps use '--tag-name-filter cat' to rewrite the tag." 533 fi 534 else 535 die "Could not rewrite $ref" 536 fi 537 fi 538 ;; 539 esac 540 git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1 || 541 exit 542 done < "$tempdir"/heads 543 544 # TODO: This should possibly go, with the semantics that all positive given 545 # refs are updated, and their original heads stored in refs/original/ 546 # Filter tags 547 548 if [ "$filter_tag_name" ]; then 549 git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags | 550 while read sha1 type ref; do 551 ref="${ref#refs/tags/}" 552 # XXX: Rewrite tagged trees as well? 553 if [ "$type" != "commit" -a "$type" != "tag" ]; then 554 continue; 555 fi 556 557 if [ "$type" = "tag" ]; then 558 # Dereference to a commit 559 sha1t="$sha1" 560 sha1="$(git rev-parse -q "$sha1"^{commit})" || continue 561 fi 562 563 [ -f "../map/$sha1" ] || continue 564 new_sha1="$(cat "../map/$sha1")" 565 GIT_COMMIT="$sha1" 566 export GIT_COMMIT 567 new_ref="$(echo "$ref" | eval "$filter_tag_name")" || 568 die "tag name filter failed: $filter_tag_name" 569 570 echo "$ref -> $new_ref ($sha1 -> $new_sha1)" 571 572 if [ "$type" = "tag" ]; then 573 new_sha1=$( ( printf 'object %s\ntype commit\ntag %s\n' \ 574 "$new_sha1" "$new_ref" 575 git cat-file tag "$ref" | 576 sed -n \ 577 -e '1,/^$/{ 578 /^object /d 579 /^type /d 580 /^tag /d 581 }' \ 582 -e '/^-----BEGIN PGP SIGNATURE-----/q' \ 583 -e 'p' ) | 584 git hash-object -t tag -w --stdin) || 585 die "Could not create new tag object for $ref" 586 if git cat-file tag "$ref" | \ 587 grep '^-----BEGIN PGP SIGNATURE-----' >/dev/null 2>&1 588 then 589 warn "gpg signature stripped from tag object $sha1t" 590 fi 591 fi 592 593 git update-ref "refs/tags/$new_ref" "$new_sha1" || 594 die "Could not write tag $new_ref" 595 done 596 fi 597 598 unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE 599 unset GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE 600 unset GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL GIT_COMMITTER_DATE 601 test -z "$ORIG_GIT_DIR" || { 602 GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR 603 } 604 test -z "$ORIG_GIT_WORK_TREE" || { 605 GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" && 606 export GIT_WORK_TREE 607 } 608 test -z "$ORIG_GIT_INDEX_FILE" || { 609 GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" && 610 export GIT_INDEX_FILE 611 } 612 test -z "$ORIG_GIT_AUTHOR_NAME" || { 613 GIT_AUTHOR_NAME="$ORIG_GIT_AUTHOR_NAME" && 614 export GIT_AUTHOR_NAME 615 } 616 test -z "$ORIG_GIT_AUTHOR_EMAIL" || { 617 GIT_AUTHOR_EMAIL="$ORIG_GIT_AUTHOR_EMAIL" && 618 export GIT_AUTHOR_EMAIL 619 } 620 test -z "$ORIG_GIT_AUTHOR_DATE" || { 621 GIT_AUTHOR_DATE="$ORIG_GIT_AUTHOR_DATE" && 622 export GIT_AUTHOR_DATE 623 } 624 test -z "$ORIG_GIT_COMMITTER_NAME" || { 625 GIT_COMMITTER_NAME="$ORIG_GIT_COMMITTER_NAME" && 626 export GIT_COMMITTER_NAME 627 } 628 test -z "$ORIG_GIT_COMMITTER_EMAIL" || { 629 GIT_COMMITTER_EMAIL="$ORIG_GIT_COMMITTER_EMAIL" && 630 export GIT_COMMITTER_EMAIL 631 } 632 test -z "$ORIG_GIT_COMMITTER_DATE" || { 633 GIT_COMMITTER_DATE="$ORIG_GIT_COMMITTER_DATE" && 634 export GIT_COMMITTER_DATE 635 } 636 637 if test -n "$state_branch" 638 then 639 echo "Saving rewrite state to $state_branch" 1>&2 640 state_blob=$( 641 for file in ../map/* 642 do 643 from_commit=$(basename "$file") 644 to_commit=$(cat "$file") 645 echo "$from_commit:$to_commit" 646 done | git hash-object -w --stdin || die "Unable to save state" 647 ) 648 state_tree=$(printf '100644 blob %s\tfilter.map\n' "$state_blob" | git mktree) 649 if test -n "$state_commit" 650 then 651 state_commit=$(echo "Sync" | git commit-tree "$state_tree" -p "$state_commit") 652 else 653 state_commit=$(echo "Sync" | git commit-tree "$state_tree" ) 654 fi 655 git update-ref "$state_branch" "$state_commit" 656 fi 657 658 cd "$orig_dir" 659 rm -rf "$tempdir" 660 661 trap - 0 662 663 if [ "$(is_bare_repository)" = false ]; then 664 git read-tree -u -m HEAD || exit 665 fi 666 667 exit 0