git
[Ported from github.com/git/git] Sync with Git 2.56.1
Brought by drakestapleton
git / t / t5413-connectivity-check.sh
| 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='connectivity check (transfer.connectivityCheck)' |
| 4 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
| 5 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
| 6 | |
| 7 | . ./test-lib.sh |
| 8 | |
| 9 | test_oid_cache <<-\EOF |
| 10 | missing sha1:0000000000000000000000000000000000000001 |
| 11 | missing sha256:0000000000000000000000000000000000000000000000000000000000000001 |
| 12 | EOF |
| 13 | |
| 14 | set_connectivity_check () { |
| 15 | git -C "$1" config transfer.connectivityCheck "$2" |
| 16 | } |
| 17 | |
| 18 | # Run a connectivity check via rev-list directly. Uses $mode |
| 19 | # (set by the enclosing for-loop) to choose full or incremental. |
| 20 | check_connected () { |
| 21 | printf '%s\n' "$@" | |
| 22 | git rev-list --connectivity-check="$mode" \ |
| 23 | --objects --stdin --not --all --quiet \ |
| 24 | --exclude-promisor-objects |
| 25 | } |
| 26 | |
| 27 | # Run git with a temporary index, leaving the real index untouched. |
| 28 | tmpgit () { |
| 29 | GIT_INDEX_FILE=.git/tmp-idx git "$@" |
| 30 | } |
| 31 | |
| 32 | # Create a commit with one file changed, without modifying HEAD, |
| 33 | # index, or worktree. Prints the new commit OID on stdout. |
| 34 | # Usage: commit_with_change <parent> <path> <content> |
| 35 | commit_with_change () { |
| 36 | new_blob=$(echo "$3" | git hash-object -w --stdin) && |
| 37 | tmpgit read-tree "$1" && |
| 38 | tmpgit update-index --replace \ |
| 39 | --cacheinfo "100644,$new_blob,$2" && |
| 40 | new_tree=$(tmpgit write-tree) && |
| 41 | rm -f .git/tmp-idx && |
| 42 | git commit-tree "$new_tree" -p "$1" -m "modify $2" |
| 43 | } |
| 44 | |
| 45 | # Check OIDs and optionally verify trace2 counts. |
| 46 | # Usage: check_connected_trace <trace-file> <trees> <blobs> <oid>... |
| 47 | # An empty string for <trees> or <blobs> skips that assertion. |
| 48 | check_connected_trace () { |
| 49 | trace_file=$1 trees=$2 blobs=$3 && |
| 50 | shift 3 && |
| 51 | test_env GIT_TRACE2_EVENT="$(pwd)/$trace_file" \ |
| 52 | check_connected "$@" && |
| 53 | if test "$mode" != incremental |
| 54 | then |
| 55 | return |
| 56 | fi && |
| 57 | if test -n "$trees" |
| 58 | then |
| 59 | test_trace2_data_singular connectivity trees_loaded "$trees" \ |
| 60 | <"$trace_file" |
| 61 | fi && |
| 62 | if test -n "$blobs" |
| 63 | then |
| 64 | test_trace2_data_singular connectivity blobs_checked "$blobs" \ |
| 65 | <"$trace_file" |
| 66 | fi |
| 67 | } |
| 68 | |
| 69 | # Shared setup: a repo with several root-level files and nested dirs. |
| 70 | # The unchanged/ subtree (10 dirs x 10 files = 100 blobs, 11 trees) |
| 71 | # acts as a canary: any test asserting small tree/blob counts would |
| 72 | # fail dramatically if incremental accidentally walked into it. |
| 73 | |
| 74 | test_expect_success 'setup main repo' ' |
| 75 | git init main-repo && |
| 76 | ( |
| 77 | cd main-repo && |
| 78 | for i in $(test_seq 1 5) |
| 79 | do |
| 80 | echo "file $i" >"file-$i.txt" || return 1 |
| 81 | done && |
| 82 | git add file-*.txt && |
| 83 | git commit -m "initial" && |
| 84 | |
| 85 | mkdir -p a/b/c && |
| 86 | echo deep >a/b/c/deep.txt && |
| 87 | echo other >a/other.txt && |
| 88 | git add a/b/c/deep.txt a/other.txt && |
| 89 | git commit -m "add nested dirs" && |
| 90 | |
| 91 | for i in $(test_seq 1 10) |
| 92 | do |
| 93 | d="unchanged/dir-$i" && |
| 94 | mkdir -p "$d" && |
| 95 | for j in $(test_seq 1 10) |
| 96 | do |
| 97 | echo "$i $j" >"$d/file-$j.txt" || return 1 |
| 98 | done |
| 99 | done && |
| 100 | git add unchanged/ && |
| 101 | git commit -m "add unchanged canary subtree" |
| 102 | ) |
| 103 | ' |
| 104 | |
| 105 | test_expect_success 'setup replacement object repo' ' |
| 106 | git init replace-test && |
| 107 | ( |
| 108 | cd replace-test && |
| 109 | |
| 110 | test_commit --no-tag original file.txt && |
| 111 | original=$(git rev-parse HEAD) && |
| 112 | orig_blob=$(git rev-parse HEAD:file.txt) && |
| 113 | |
| 114 | # Orphan replacement commit with a different tree |
| 115 | replacement_tree=$(echo replaced | git hash-object -w --stdin | |
| 116 | xargs -I{} git mktree <<-EOF |
| 117 | 100644 blob {} file.txt |
| 118 | EOF |
| 119 | ) && |
| 120 | replacement=$(git commit-tree -m "replacement" \ |
| 121 | "$replacement_tree") && |
| 122 | |
| 123 | git replace "$original" "$replacement" && |
| 124 | |
| 125 | # Remove the original blob so only the replacement |
| 126 | # tree is complete. |
| 127 | rm .git/objects/$(test_oid_to_path "$orig_blob") && |
| 128 | |
| 129 | # Drop branch and HEAD so --not --all does not |
| 130 | # exclude the original commit. |
| 131 | git update-ref -d refs/heads/main && |
| 132 | git update-ref -d HEAD && |
| 133 | |
| 134 | echo "$original" >.git/test-oid && |
| 135 | echo "$orig_blob" >.git/test-blob |
| 136 | ) |
| 137 | ' |
| 138 | |
| 139 | missing_oid=$(test_oid missing) |
| 140 | original_oid=$(cat replace-test/.git/test-oid) |
| 141 | orig_blob_oid=$(cat replace-test/.git/test-blob) |
| 142 | |
| 143 | for mode in full incremental |
| 144 | do |
| 145 | |
| 146 | # The full traversal reports a missing tree as "bad tree object". |
| 147 | case "$mode" in |
| 148 | full) |
| 149 | missing_tree="bad tree object" |
| 150 | ;; |
| 151 | incremental) |
| 152 | missing_tree="missing tree object" |
| 153 | ;; |
| 154 | esac |
| 155 | |
| 156 | # Corruption detection: craft broken object graphs and verify detection. |
| 157 | # All tests use main-repo without modifying its refs or worktree. |
| 158 | |
| 159 | test_expect_success "$mode: rejects commit with missing blob" ' |
| 160 | ( |
| 161 | cd main-repo && |
| 162 | bad_tree=$(printf "100644 blob ${missing_oid}\tfile.txt\n" | |
| 163 | git mktree --missing) && |
| 164 | bad_commit=$(git commit-tree "$bad_tree" -p HEAD -m "bad") && |
| 165 | test_expect_code 128 check_connected "$bad_commit" 2>err && |
| 166 | test_grep "missing blob object.*$missing_oid" err |
| 167 | ) |
| 168 | ' |
| 169 | |
| 170 | test_expect_success "$mode: rejects commit with missing subtree" ' |
| 171 | ( |
| 172 | cd main-repo && |
| 173 | bad_tree=$(printf "40000 tree ${missing_oid}\tdir\n" | |
| 174 | git mktree --missing) && |
| 175 | bad_commit=$(git commit-tree "$bad_tree" -p HEAD -m "bad") && |
| 176 | test_expect_code 128 check_connected "$bad_commit" 2>err && |
| 177 | test_grep "$missing_tree $missing_oid" err |
| 178 | ) |
| 179 | ' |
| 180 | |
| 181 | test_expect_success "$mode: verifies direct tree tip" ' |
| 182 | ( |
| 183 | cd main-repo && |
| 184 | bad_tree=$(printf "100644 blob ${missing_oid}\tfile.txt\n" | |
| 185 | git mktree --missing) && |
| 186 | test_expect_code 128 check_connected "$bad_tree" 2>err && |
| 187 | test_grep "missing blob object.*$missing_oid" err |
| 188 | ) |
| 189 | ' |
| 190 | |
| 191 | test_expect_success "$mode: verifies direct blob tip" ' |
| 192 | ( |
| 193 | cd main-repo && |
| 194 | blob_oid=$(echo "hello" | git hash-object -w --stdin) && |
| 195 | check_connected "$blob_oid" |
| 196 | ) |
| 197 | ' |
| 198 | |
| 199 | test_expect_success "$mode: rejects missing direct blob tip" ' |
| 200 | ( |
| 201 | cd main-repo && |
| 202 | test_expect_code 128 check_connected \ |
| 203 | "$missing_oid" 2>err |
| 204 | ) |
| 205 | ' |
| 206 | |
| 207 | test_expect_success PERL_TEST_HELPERS \ |
| 208 | "$mode: rejects blob OID reused as tree entry" ' |
| 209 | ( |
| 210 | cd main-repo && |
| 211 | |
| 212 | blob_oid=$(git rev-parse HEAD:file-1.txt) && |
| 213 | bin_oid=$(echo "$blob_oid" | hex2oct) && |
| 214 | |
| 215 | bad_tree=$(printf "40000 subdir\0$bin_oid" | |
| 216 | git hash-object -t tree -w --stdin) && |
| 217 | bad_commit=$(git commit-tree -p HEAD -m "child" "$bad_tree") && |
| 218 | |
| 219 | test_expect_code 128 check_connected "$bad_commit" 2>err && |
| 220 | test_grep "not a tree" err |
| 221 | ) |
| 222 | ' |
| 223 | |
| 224 | test_expect_success PERL_TEST_HELPERS \ |
| 225 | "$mode: rejects tree OID reused as blob entry" ' |
| 226 | ( |
| 227 | cd main-repo && |
| 228 | |
| 229 | tree_oid=$(git rev-parse HEAD:a) && |
| 230 | bin_oid=$(echo "$tree_oid" | hex2oct) && |
| 231 | |
| 232 | bad_tree=$(printf "100644 fakefile\0$bin_oid" | |
| 233 | git hash-object -t tree -w --stdin) && |
| 234 | bad_commit=$(git commit-tree -p HEAD -m "child" "$bad_tree") && |
| 235 | |
| 236 | test_expect_code 128 check_connected "$bad_commit" 2>err && |
| 237 | test_grep "not a blob" err |
| 238 | ) |
| 239 | ' |
| 240 | |
| 241 | test_expect_success "$mode: checks multiple tips" ' |
| 242 | ( |
| 243 | cd main-repo && |
| 244 | c1=$(commit_with_change HEAD file-1.txt "tip-a") && |
| 245 | c2=$(commit_with_change HEAD file-2.txt "tip-b") && |
| 246 | git tag -a -m "tagged" multi-tag "$c1" && |
| 247 | tag_oid=$(git rev-parse multi-tag) && |
| 248 | git tag -d multi-tag && |
| 249 | check_connected "$c2" "$tag_oid" |
| 250 | ) |
| 251 | ' |
| 252 | |
| 253 | # Tree-diff optimization: verify trace2 counts. |
| 254 | |
| 255 | test_expect_success "$mode: handles root commit (no parents)" ' |
| 256 | ( |
| 257 | cd main-repo && |
| 258 | blob_oid=$(echo "root-content" | git hash-object -w --stdin) && |
| 259 | tree_oid=$(printf "100644 blob %s\tfile.txt\n" \ |
| 260 | "$blob_oid" | git mktree) && |
| 261 | root_oid=$(git commit-tree "$tree_oid" -m "root") && |
| 262 | |
| 263 | # No parent trees, so the full tree is verified. |
| 264 | # 1 tree loaded (root), 1 blob checked. |
| 265 | check_connected_trace trace-root.txt 1 1 "$root_oid" |
| 266 | ) |
| 267 | ' |
| 268 | |
| 269 | test_expect_success "$mode: handles single file change" ' |
| 270 | ( |
| 271 | cd main-repo && |
| 272 | oid=$(commit_with_change HEAD file-1.txt "changed") && |
| 273 | |
| 274 | # 2 trees loaded (new root + parent root), 1 blob checked. |
| 275 | check_connected_trace trace-flat.txt 2 1 "$oid" |
| 276 | ) |
| 277 | ' |
| 278 | |
| 279 | test_expect_success "$mode: handles nested change" ' |
| 280 | ( |
| 281 | cd main-repo && |
| 282 | oid=$(commit_with_change HEAD a/b/c/deep.txt "deep-changed") && |
| 283 | # 4 new trees + 4 parent trees = 8 loaded, 1 blob checked. |
| 284 | check_connected_trace trace-nested.txt 8 1 "$oid" |
| 285 | ) |
| 286 | ' |
| 287 | |
| 288 | test_expect_success "$mode: handles change-then-revert" ' |
| 289 | ( |
| 290 | cd main-repo && |
| 291 | c1=$(commit_with_change HEAD file-1.txt "revert-tmp") && |
| 292 | c2=$(commit_with_change "$c1" file-1.txt "file 1") && |
| 293 | c3=$(commit_with_change "$c2" file-1.txt "revert-final") && |
| 294 | |
| 295 | # c1: new root + parent root = 2 loads. c2: root matches |
| 296 | # HEAD (already trusted), skipped. c3: new root + parent |
| 297 | # already expanded = 1 load. Total: 3 trees, 2 blobs. |
| 298 | check_connected_trace trace-revert.txt 3 2 "$c3" |
| 299 | ) |
| 300 | ' |
| 301 | |
| 302 | test_expect_success "$mode: handles subtree moved to another path" ' |
| 303 | ( |
| 304 | cd main-repo && |
| 305 | moved_tree=$(git ls-tree HEAD | |
| 306 | sed "s/ a$/ moved/" | |
| 307 | git mktree) && |
| 308 | moved=$(git commit-tree "$moved_tree" -p HEAD -m move) && |
| 309 | # New root + parent root scanned, but the moved subtree |
| 310 | # (same OID) is trusted and not descended into. |
| 311 | check_connected_trace trace-move.txt 2 0 "$moved" |
| 312 | ) |
| 313 | ' |
| 314 | |
| 315 | test_expect_success "$mode: handles multi-parent merge" ' |
| 316 | ( |
| 317 | cd main-repo && |
| 318 | left=$(commit_with_change HEAD file-1.txt left) && |
| 319 | right=$(commit_with_change HEAD file-2.txt right) && |
| 320 | git update-ref refs/heads/left "$left" && |
| 321 | git update-ref refs/heads/right "$right" && |
| 322 | left_blob=$(git rev-parse "$left:file-1.txt") && |
| 323 | right_blob=$(git rev-parse "$right:file-2.txt") && |
| 324 | tmpgit read-tree HEAD && |
| 325 | tmpgit update-index --replace \ |
| 326 | --cacheinfo "100644,$left_blob,file-1.txt" && |
| 327 | tmpgit update-index --replace \ |
| 328 | --cacheinfo "100644,$right_blob,file-2.txt" && |
| 329 | merge_tree=$(tmpgit write-tree) && |
| 330 | rm -f .git/tmp-idx && |
| 331 | merge=$(git commit-tree "$merge_tree" \ |
| 332 | -p "$left" -p "$right" -m merge) && |
| 333 | # Both parent root trees are scanned as bases, so |
| 334 | # blobs from each parent are trusted without ODB checks. |
| 335 | check_connected_trace trace-merge.txt 3 0 "$merge" && |
| 336 | git update-ref -d refs/heads/left && |
| 337 | git update-ref -d refs/heads/right |
| 338 | ) |
| 339 | ' |
| 340 | |
| 341 | test_expect_success "$mode: handles merge with incoming and boundary parents" ' |
| 342 | ( |
| 343 | cd main-repo && |
| 344 | # parent1 is incoming (not a ref), HEAD is boundary. |
| 345 | parent1=$(commit_with_change HEAD file-1.txt merge-inc) && |
| 346 | merge=$(git commit-tree \ |
| 347 | "$(git rev-parse "$parent1^{tree}")" \ |
| 348 | -p "$parent1" -p HEAD -m merge-mixed) && |
| 349 | # parent1 verified first (topo order): 2 trees, 1 blob. |
| 350 | # merge tree = parent1 tree (already trusted): 0 extra. |
| 351 | check_connected_trace trace-merge-inc.txt 2 1 "$merge" |
| 352 | ) |
| 353 | ' |
| 354 | |
| 355 | test_expect_success "$mode: handles gitlink entries (submodules)" ' |
| 356 | ( |
| 357 | cd main-repo && |
| 358 | tmpgit read-tree HEAD && |
| 359 | tmpgit update-index --add \ |
| 360 | --cacheinfo "160000,$missing_oid,my-submodule" && |
| 361 | gitlink_tree=$(tmpgit write-tree) && |
| 362 | rm -f .git/tmp-idx && |
| 363 | gitlink_commit=$(git commit-tree "$gitlink_tree" -p HEAD \ |
| 364 | -m "add gitlink") && |
| 365 | |
| 366 | # Gitlink entries are skipped -- the missing submodule |
| 367 | # commit OID does not cause a failure. |
| 368 | check_connected_trace trace-gitlink.txt 2 0 "$gitlink_commit" |
| 369 | ) |
| 370 | ' |
| 371 | |
| 372 | test_expect_success "$mode: handles file-to-directory transition" ' |
| 373 | ( |
| 374 | cd main-repo && |
| 375 | |
| 376 | # Parent: "foo" is a blob at root. |
| 377 | blob_a=$(echo "file-content" | git hash-object -w --stdin) && |
| 378 | tmpgit read-tree HEAD && |
| 379 | tmpgit update-index --add \ |
| 380 | --cacheinfo "100644,$blob_a,foo" && |
| 381 | parent_tree=$(tmpgit write-tree) && |
| 382 | rm -f .git/tmp-idx && |
| 383 | parent=$(git commit-tree "$parent_tree" -p HEAD \ |
| 384 | -m "add foo as file") && |
| 385 | |
| 386 | # Child: "foo" becomes a directory (foo/bar.txt). |
| 387 | blob_b=$(echo "dir-content" | git hash-object -w --stdin) && |
| 388 | tmpgit read-tree "$parent" && |
| 389 | tmpgit update-index --remove foo && |
| 390 | tmpgit update-index --add \ |
| 391 | --cacheinfo "100644,$blob_b,foo/bar.txt" && |
| 392 | child_tree=$(tmpgit write-tree) && |
| 393 | rm -f .git/tmp-idx && |
| 394 | child=$(git commit-tree "$child_tree" -p "$parent" \ |
| 395 | -m "foo: file to directory") && |
| 396 | check_connected_trace trace-f2d.txt "" "" "$child" |
| 397 | ) |
| 398 | ' |
| 399 | |
| 400 | test_expect_success "$mode: handles directory-to-file transition" ' |
| 401 | ( |
| 402 | cd main-repo && |
| 403 | |
| 404 | # Parent: "bar/baz.txt" exists (bar is a directory). |
| 405 | blob_a=$(echo "nested" | git hash-object -w --stdin) && |
| 406 | tmpgit read-tree HEAD && |
| 407 | tmpgit update-index --add \ |
| 408 | --cacheinfo "100644,$blob_a,bar/baz.txt" && |
| 409 | parent_tree=$(tmpgit write-tree) && |
| 410 | rm -f .git/tmp-idx && |
| 411 | parent=$(git commit-tree "$parent_tree" -p HEAD \ |
| 412 | -m "add bar as directory") && |
| 413 | |
| 414 | # Child: "bar" becomes a plain file. |
| 415 | blob_b=$(echo "flat" | git hash-object -w --stdin) && |
| 416 | tmpgit read-tree "$parent" && |
| 417 | tmpgit update-index --remove bar/baz.txt && |
| 418 | tmpgit update-index --add \ |
| 419 | --cacheinfo "100644,$blob_b,bar" && |
| 420 | child_tree=$(tmpgit write-tree) && |
| 421 | rm -f .git/tmp-idx && |
| 422 | child=$(git commit-tree "$child_tree" -p "$parent" \ |
| 423 | -m "bar: directory to file") && |
| 424 | check_connected_trace trace-d2f.txt "" "" "$child" |
| 425 | ) |
| 426 | ' |
| 427 | |
| 428 | # Replacement objects. |
| 429 | |
| 430 | test_expect_success "$mode: accepts with replacement objects" ' |
| 431 | ( |
| 432 | cd replace-test && |
| 433 | check_connected "$original_oid" |
| 434 | ) |
| 435 | ' |
| 436 | |
| 437 | test_expect_success "$mode: rejects without replacement objects" ' |
| 438 | ( |
| 439 | cd replace-test && |
| 440 | GIT_NO_REPLACE_OBJECTS=1 && |
| 441 | export GIT_NO_REPLACE_OBJECTS && |
| 442 | test_expect_code 128 check_connected \ |
| 443 | "$original_oid" 2>err && |
| 444 | test_grep "missing blob object.*$orig_blob_oid" err |
| 445 | ) |
| 446 | ' |
| 447 | |
| 448 | test_expect_success "$mode: accepts missing promised blob" ' |
| 449 | test_when_finished "rm -rf prom-src prom-server.git prom-client" && |
| 450 | git init prom-src && |
| 451 | test_commit -C prom-src --no-tag base file.txt original && |
| 452 | test_commit -C prom-src --no-tag "add file2" file2.txt extra && |
| 453 | git clone --bare prom-src prom-server.git && |
| 454 | git -C prom-server.git config uploadpack.allowfilter true && |
| 455 | git -C prom-server.git config uploadpack.allowanysha1inwant true && |
| 456 | git clone --no-checkout --filter=blob:none \ |
| 457 | "file://$(pwd)/prom-server.git" prom-client && |
| 458 | ( |
| 459 | cd prom-client && |
| 460 | promised_blob=$(git rev-parse HEAD:file2.txt) && |
| 461 | test_must_fail env GIT_NO_LAZY_FETCH=1 \ |
| 462 | git cat-file -e "$promised_blob" && |
| 463 | new_tree=$(printf "100644 blob %s\tnewname.txt\n" \ |
| 464 | "$promised_blob" | |
| 465 | git mktree --missing) && |
| 466 | new_commit=$(git commit-tree "$new_tree" \ |
| 467 | -p HEAD -m "reuse promised blob") && |
| 468 | check_connected "$new_commit" && |
| 469 | test_must_fail env GIT_NO_LAZY_FETCH=1 \ |
| 470 | git cat-file -e "$promised_blob" |
| 471 | ) |
| 472 | ' |
| 473 | |
| 474 | test_expect_success "$mode: does not lazy-fetch promised blob" ' |
| 475 | test_when_finished "rm -rf nofetch-src nofetch-server.git nofetch-client" && |
| 476 | git init nofetch-src && |
| 477 | test_commit -C nofetch-src --no-tag base file.txt content && |
| 478 | git clone --bare nofetch-src nofetch-server.git && |
| 479 | git -C nofetch-server.git config uploadpack.allowfilter true && |
| 480 | git -C nofetch-server.git config uploadpack.allowanysha1inwant true && |
| 481 | git clone --no-checkout --filter=blob:none \ |
| 482 | "file://$(pwd)/nofetch-server.git" nofetch-client && |
| 483 | ( |
| 484 | cd nofetch-client && |
| 485 | promised_blob=$(git rev-parse HEAD:file.txt) && |
| 486 | test_must_fail env GIT_NO_LAZY_FETCH=1 \ |
| 487 | git cat-file -e "$promised_blob" && |
| 488 | new_tree=$(printf "100644 blob %s\tfile.txt\n" \ |
| 489 | "$promised_blob" | |
| 490 | git mktree --missing) && |
| 491 | new_commit=$(git commit-tree "$new_tree" \ |
| 492 | -m "root commit with promised blob") && |
| 493 | check_connected "$new_commit" && |
| 494 | test_must_fail env GIT_NO_LAZY_FETCH=1 \ |
| 495 | git cat-file -e "$promised_blob" |
| 496 | ) |
| 497 | ' |
| 498 | |
| 499 | test_expect_success "$mode: accepts missing promised tree" ' |
| 500 | test_when_finished "rm -rf prom-tree-src prom-tree-server.git prom-tree-client" && |
| 501 | git init prom-tree-src && |
| 502 | mkdir -p prom-tree-src/a/b && |
| 503 | test_commit -C prom-tree-src --no-tag "nested dirs" a/b/file.txt deep && |
| 504 | git clone --bare prom-tree-src prom-tree-server.git && |
| 505 | git -C prom-tree-server.git config uploadpack.allowfilter true && |
| 506 | git -C prom-tree-server.git config uploadpack.allowanysha1inwant true && |
| 507 | git clone --no-checkout --filter=tree:1 \ |
| 508 | "file://$(pwd)/prom-tree-server.git" prom-tree-client && |
| 509 | ( |
| 510 | cd prom-tree-client && |
| 511 | promised_tree=$(git ls-tree HEAD -- a | |
| 512 | awk "{print \$3}") && |
| 513 | test_must_fail env GIT_NO_LAZY_FETCH=1 \ |
| 514 | git cat-file -e "$promised_tree" && |
| 515 | new_tree=$(printf "40000 tree %s\trenamed\n" \ |
| 516 | "$promised_tree" | |
| 517 | git mktree --missing) && |
| 518 | new_commit=$(git commit-tree "$new_tree" \ |
| 519 | -p HEAD -m "reuse promised tree") && |
| 520 | check_connected "$new_commit" && |
| 521 | test_must_fail env GIT_NO_LAZY_FETCH=1 \ |
| 522 | git cat-file -e "$promised_tree" |
| 523 | ) |
| 524 | ' |
| 525 | |
| 526 | test_expect_success "$mode: verifies new subtree when parent subtree is promised" ' |
| 527 | test_when_finished "rm -rf prom-base-src prom-base-server.git prom-base-client" && |
| 528 | git init prom-base-src && |
| 529 | mkdir -p prom-base-src/a && |
| 530 | test_commit -C prom-base-src --no-tag "base" a/file.txt deep && |
| 531 | git clone --bare prom-base-src prom-base-server.git && |
| 532 | git -C prom-base-server.git config uploadpack.allowfilter true && |
| 533 | git -C prom-base-server.git config uploadpack.allowanysha1inwant true && |
| 534 | git clone --no-checkout --filter=tree:1 \ |
| 535 | "file://$(pwd)/prom-base-server.git" prom-base-client && |
| 536 | ( |
| 537 | cd prom-base-client && |
| 538 | parent_subtree=$(git ls-tree HEAD -- a | |
| 539 | awk "{print \$3}") && |
| 540 | test_must_fail env GIT_NO_LAZY_FETCH=1 \ |
| 541 | git cat-file -e "$parent_subtree" && |
| 542 | new_blob=$(echo "local-content" | git hash-object -w --stdin) && |
| 543 | new_subtree=$(printf "100644 blob %s\tfile.txt\n" \ |
| 544 | "$new_blob" | git mktree) && |
| 545 | new_root=$(printf "40000 tree %s\ta\n" \ |
| 546 | "$new_subtree" | git mktree) && |
| 547 | new_commit=$(git commit-tree "$new_root" \ |
| 548 | -p HEAD -m "replace promised subtree") && |
| 549 | check_connected "$new_commit" && |
| 550 | test_must_fail env GIT_NO_LAZY_FETCH=1 \ |
| 551 | git cat-file -e "$parent_subtree" |
| 552 | ) |
| 553 | ' |
| 554 | |
| 555 | test_expect_success "$mode: verifies local commit in partial clone" ' |
| 556 | test_when_finished "rm -rf pc-src pc-server.git pc-client" && |
| 557 | git init pc-src && |
| 558 | test_commit -C pc-src --no-tag base file.txt && |
| 559 | git clone --bare pc-src pc-server.git && |
| 560 | git -C pc-server.git config uploadpack.allowfilter true && |
| 561 | git -C pc-server.git config uploadpack.allowanysha1inwant true && |
| 562 | git clone --filter=blob:none \ |
| 563 | "file://$(pwd)/pc-server.git" pc-client && |
| 564 | ( |
| 565 | cd pc-client && |
| 566 | local_commit=$(commit_with_change HEAD file.txt local-content) && |
| 567 | check_connected "$local_commit" |
| 568 | ) |
| 569 | ' |
| 570 | |
| 571 | test_expect_success "$mode: tolerates missing promisor parent commit" ' |
| 572 | test_when_finished "rm -rf mp-src mp-server.git mp-client" && |
| 573 | git init mp-src && |
| 574 | test_commit -C mp-src --no-tag base file.txt && |
| 575 | git clone --bare mp-src mp-server.git && |
| 576 | git -C mp-server.git config uploadpack.allowfilter true && |
| 577 | git -C mp-server.git config uploadpack.allowanysha1inwant true && |
| 578 | git clone --filter=blob:none \ |
| 579 | "file://$(pwd)/mp-server.git" mp-client && |
| 580 | test_commit -C mp-src --no-tag c1 file.txt && |
| 581 | test_commit -C mp-src --no-tag c2 file.txt && |
| 582 | c1=$(git -C mp-src rev-parse HEAD~1) && |
| 583 | c2=$(git -C mp-src rev-parse HEAD) && |
| 584 | ( |
| 585 | cd mp-client && |
| 586 | # Add a promisor pack holding only c2, so its parent c1 |
| 587 | # is missing but promised (as after a lazy fetch that |
| 588 | # omits dependents). |
| 589 | echo "$c2" | |
| 590 | git -C ../mp-src pack-objects --stdout >c2.pack && |
| 591 | git index-pack --promisor --stdin <c2.pack && |
| 592 | test_must_fail env GIT_NO_LAZY_FETCH=1 \ |
| 593 | git cat-file -e "$c1" && |
| 594 | tree=$(git rev-parse HEAD^{tree}) && |
| 595 | local_commit=$(git hash-object -t commit -w --stdin <<-EOF |
| 596 | tree $tree |
| 597 | parent $c1 |
| 598 | author A U Thor <author@example.com> 1112912053 -0700 |
| 599 | committer C O Mitter <committer@example.com> 1112912053 -0700 |
| 600 | |
| 601 | on top of a missing promisor commit |
| 602 | EOF |
| 603 | ) && |
| 604 | check_connected "$local_commit" |
| 605 | ) |
| 606 | ' |
| 607 | |
| 608 | test_expect_success "$mode: respects shallow boundary" ' |
| 609 | test_when_finished "rm -rf shallow-src shallow" && |
| 610 | git init shallow-src && |
| 611 | test_commit -C shallow-src --no-tag base file content-1 && |
| 612 | mkdir shallow-src/sub && |
| 613 | test_commit -C shallow-src --no-tag change sub/other content-2 && |
| 614 | git clone --depth=1 "file://$(pwd)/shallow-src" shallow && |
| 615 | ( |
| 616 | cd shallow && |
| 617 | tip=$(git rev-parse HEAD) && |
| 618 | git for-each-ref --format="delete %(refname)" | |
| 619 | git update-ref --no-deref --stdin && |
| 620 | check_connected "$tip" |
| 621 | ) |
| 622 | ' |
| 623 | |
| 624 | test_expect_success "$mode: deepening fetch succeeds" ' |
| 625 | test_when_finished "rm -rf deepen-src deepen-server.git deepen-client" && |
| 626 | git init deepen-src && |
| 627 | test_commit -C deepen-src --no-tag c1 file.txt && |
| 628 | test_commit -C deepen-src --no-tag c2 file.txt && |
| 629 | test_commit -C deepen-src --no-tag c3 file.txt && |
| 630 | git clone --bare deepen-src deepen-server.git && |
| 631 | git clone --depth=1 "file://$(pwd)/deepen-server.git" deepen-client && |
| 632 | set_connectivity_check deepen-client $mode && |
| 633 | test -f deepen-client/.git/shallow && |
| 634 | GIT_TRACE2_EVENT="$(pwd)/deepen-trace.txt" \ |
| 635 | git -C deepen-client fetch --deepen=2 origin main && |
| 636 | # Incremental falls back to full for deepening fetches, |
| 637 | # so the trees_loaded event should not appear. |
| 638 | test_grep ! trees_loaded deepen-trace.txt |
| 639 | ' |
| 640 | |
| 641 | test_expect_success "$mode: malformed tree detected" ' |
| 642 | ( |
| 643 | cd main-repo && |
| 644 | echo abc >malformed-tree && |
| 645 | malformed_tree=$(git hash-object --literally -t tree -w \ |
| 646 | malformed-tree) && |
| 647 | malformed_commit=$(git commit-tree "$malformed_tree" \ |
| 648 | -p HEAD -m malformed) && |
| 649 | test_expect_code 128 check_connected \ |
| 650 | "$malformed_commit" 2>err |
| 651 | ) |
| 652 | ' |
| 653 | |
| 654 | test_expect_success PERL_TEST_HELPERS \ |
| 655 | "$mode: mid-tree corruption detected" ' |
| 656 | ( |
| 657 | cd main-repo && |
| 658 | # Build a tree with one valid entry followed by garbage. |
| 659 | blob_oid=$(echo "valid" | git hash-object -w --stdin) && |
| 660 | bin_oid=$(echo "$blob_oid" | hex2oct) && |
| 661 | printf "100644 good\0${bin_oid}GARBAGE" >corrupt-mid-tree && |
| 662 | corrupt_tree=$(git hash-object --literally -t tree -w \ |
| 663 | corrupt-mid-tree) && |
| 664 | corrupt_commit=$(git commit-tree "$corrupt_tree" \ |
| 665 | -p HEAD -m "mid-tree corruption") && |
| 666 | test_expect_code 128 check_connected \ |
| 667 | "$corrupt_commit" 2>err && |
| 668 | test_grep "too-short tree object" err |
| 669 | ) |
| 670 | ' |
| 671 | |
| 672 | done |
| 673 | |
| 674 | # The full check only checks that blobs exist, but the incremental |
| 675 | # check reads their headers and reports a corrupt blob. |
| 676 | test_expect_success 'incremental: rejects corrupt blob' ' |
| 677 | test_when_finished "rm -rf corrupt-repo" && |
| 678 | git init corrupt-repo && |
| 679 | ( |
| 680 | cd corrupt-repo && |
| 681 | test_commit base && |
| 682 | blob=$(echo corrupt | git hash-object -w --stdin) && |
| 683 | tree=$(printf "100644 blob $blob\tfile\n" | git mktree) && |
| 684 | commit=$(git commit-tree "$tree" -p HEAD -m corrupt) && |
| 685 | path=.git/objects/$(test_oid_to_path "$blob") && |
| 686 | chmod +w "$path" && |
| 687 | echo garbage >"$path" && |
| 688 | mode=incremental && |
| 689 | test_expect_code 128 check_connected "$commit" 2>err && |
| 690 | test_grep "unable to read blob object $blob" err |
| 691 | ) |
| 692 | ' |
| 693 | |
| 694 | # Algorithm selection. |
| 695 | |
| 696 | # Run a command with its trace2 events in "trace" and its stderr |
| 697 | # in "err". |
| 698 | run_traced () { |
| 699 | rm -f trace && |
| 700 | GIT_TRACE2_EVENT="$(pwd)/trace" "$@" 2>err |
| 701 | } |
| 702 | |
| 703 | # Check which connectivity check the last run_traced used. |
| 704 | expect_mode () { |
| 705 | case "$1" in |
| 706 | full) |
| 707 | test_grep ! trees_loaded trace |
| 708 | ;; |
| 709 | incremental) |
| 710 | test_grep trees_loaded trace |
| 711 | ;; |
| 712 | *) |
| 713 | BUG "unknown mode: $1" |
| 714 | ;; |
| 715 | esac |
| 716 | } |
| 717 | |
| 718 | test_expect_success 'setup source repository' ' |
| 719 | git init src && |
| 720 | test_commit -C src base file.txt && |
| 721 | test_commit -C src --no-tag update file.txt updated |
| 722 | ' |
| 723 | |
| 724 | test_expect_success 'unknown transfer.connectivityCheck falls back to full' ' |
| 725 | test_when_finished "rm -rf dst" && |
| 726 | git init --bare dst && |
| 727 | git -C dst fetch ../src base:refs/heads/main && |
| 728 | for value in bogus auto= auto=x |
| 729 | do |
| 730 | set_connectivity_check dst "$value" && |
| 731 | run_traced git -C dst fetch ../src main && |
| 732 | expect_mode full && |
| 733 | test_grep "unknown transfer.connectivityCheck algorithm .$value., using .full." err || |
| 734 | return 1 |
| 735 | done |
| 736 | ' |
| 737 | |
| 738 | test_expect_success 'push uses incremental when configured' ' |
| 739 | test_when_finished "rm -rf dst" && |
| 740 | git init --bare dst && |
| 741 | git -C src push ../dst base:refs/heads/main && |
| 742 | set_connectivity_check dst incremental && |
| 743 | run_traced git -C src push ../dst main && |
| 744 | expect_mode incremental |
| 745 | ' |
| 746 | |
| 747 | test_expect_success 'fetch uses incremental when configured' ' |
| 748 | test_when_finished "rm -rf dst" && |
| 749 | git init --bare dst && |
| 750 | git -C dst fetch ../src base:refs/heads/main && |
| 751 | set_connectivity_check dst incremental && |
| 752 | run_traced git -C dst fetch ../src main && |
| 753 | expect_mode incremental |
| 754 | ' |
| 755 | |
| 756 | for mode in incremental auto |
| 757 | do |
| 758 | test_expect_success "push with $mode into an empty repository uses full" ' |
| 759 | test_when_finished "rm -rf dst" && |
| 760 | git init --bare dst && |
| 761 | set_connectivity_check dst $mode && |
| 762 | run_traced git -C src push ../dst main && |
| 763 | expect_mode full |
| 764 | ' |
| 765 | |
| 766 | test_expect_success "clone with $mode uses full" ' |
| 767 | test_when_finished "rm -rf dst" && |
| 768 | run_traced git -c transfer.connectivityCheck=$mode \ |
| 769 | clone --no-local src dst && |
| 770 | expect_mode full |
| 771 | ' |
| 772 | done |
| 773 | |
| 774 | for value in auto auto=1 |
| 775 | do |
| 776 | test_expect_success "push with $value uses incremental up to the cutoff" ' |
| 777 | test_when_finished "rm -rf dst" && |
| 778 | git init --bare dst && |
| 779 | git -C src push ../dst base:refs/heads/main && |
| 780 | set_connectivity_check dst $value && |
| 781 | run_traced git -C src push ../dst main && |
| 782 | expect_mode incremental |
| 783 | ' |
| 784 | done |
| 785 | |
| 786 | test_expect_success 'push with auto uses full above the cutoff' ' |
| 787 | test_when_finished "rm -rf dst" && |
| 788 | git init --bare dst && |
| 789 | git -C src push ../dst base:refs/heads/main && |
| 790 | set_connectivity_check dst auto=0 && |
| 791 | run_traced git -C src push ../dst main && |
| 792 | expect_mode full |
| 793 | ' |
| 794 | |
| 795 | test_done |