| 1 | #!/bin/sh |
| 2 | # |
| 3 | # Copyright (c) 2022 Han Xin |
| 4 | # |
| 5 | |
| 6 | test_description='git unpack-objects with large objects' |
| 7 | |
| 8 | . ./test-lib.sh |
| 9 | |
| 10 | prepare_dest () { |
| 11 | test_when_finished "rm -rf dest.git" && |
| 12 | git init --bare dest.git && |
| 13 | git -C dest.git config core.bigFileThreshold "$1" |
| 14 | } |
| 15 | |
| 16 | test_expect_success "create large objects (1.5 MB) and PACK" ' |
| 17 | test-tool genrandom foo 1500000 >big-blob && |
| 18 | test_commit --append foo big-blob && |
| 19 | test-tool genrandom bar 1500000 >big-blob && |
| 20 | test_commit --append bar big-blob && |
| 21 | PACK=$(echo HEAD | git pack-objects --revs pack) && |
| 22 | git verify-pack -v pack-$PACK.pack >out && |
| 23 | sed -n -e "s/^\([0-9a-f][0-9a-f]*\).*\(commit\|tree\|blob\).*/\1/p" \ |
| 24 | <out >obj-list |
| 25 | ' |
| 26 | |
| 27 | test_expect_success 'set memory limitation to 1MB' ' |
| 28 | GIT_ALLOC_LIMIT=1m && |
| 29 | export GIT_ALLOC_LIMIT |
| 30 | ' |
| 31 | |
| 32 | test_expect_success 'unpack-objects failed under memory limitation' ' |
| 33 | prepare_dest 2m && |
| 34 | test_must_fail git -C dest.git unpack-objects <pack-$PACK.pack 2>err && |
| 35 | grep "fatal: attempting to allocate" err |
| 36 | ' |
| 37 | |
| 38 | test_expect_success 'unpack-objects works with memory limitation in dry-run mode' ' |
| 39 | prepare_dest 2m && |
| 40 | git -C dest.git unpack-objects -n <pack-$PACK.pack && |
| 41 | test_stdout_line_count = 0 find dest.git/objects -type f && |
| 42 | test_dir_is_empty dest.git/objects/pack |
| 43 | ' |
| 44 | |
| 45 | test_expect_success 'unpack big object in stream' ' |
| 46 | prepare_dest 1m && |
| 47 | git -C dest.git unpack-objects <pack-$PACK.pack && |
| 48 | test_dir_is_empty dest.git/objects/pack |
| 49 | ' |
| 50 | |
| 51 | check_fsync_events () { |
| 52 | local trace="$1" && |
| 53 | shift && |
| 54 | |
| 55 | cat >expect && |
| 56 | sed -n \ |
| 57 | -e '/^{"event":"counter",.*"category":"fsync",/ { |
| 58 | s/.*"category":"fsync",//; |
| 59 | s/}$//; |
| 60 | p; |
| 61 | }' \ |
| 62 | <"$trace" >actual && |
| 63 | test_cmp expect actual |
| 64 | } |
| 65 | |
| 66 | BATCH_CONFIGURATION='-c core.fsync=loose-object -c core.fsyncmethod=batch' |
| 67 | |
| 68 | test_expect_success 'unpack big object in stream (core.fsyncmethod=batch)' ' |
| 69 | prepare_dest 1m && |
| 70 | GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \ |
| 71 | GIT_TEST_FSYNC=true \ |
| 72 | git -C dest.git $BATCH_CONFIGURATION unpack-objects <pack-$PACK.pack && |
| 73 | if grep "core.fsyncMethod = batch is unsupported" trace2.txt |
| 74 | then |
| 75 | flush_count=7 |
| 76 | else |
| 77 | flush_count=1 |
| 78 | fi && |
| 79 | check_fsync_events trace2.txt <<-EOF && |
| 80 | "name":"writeout-only","count":6 |
| 81 | "name":"hardware-flush","count":$flush_count |
| 82 | EOF |
| 83 | |
| 84 | test_dir_is_empty dest.git/objects/pack && |
| 85 | git -C dest.git cat-file --batch-check="%(objectname)" <obj-list >current && |
| 86 | cmp obj-list current |
| 87 | ' |
| 88 | |
| 89 | test_expect_success 'do not unpack existing large objects' ' |
| 90 | prepare_dest 1m && |
| 91 | git -C dest.git index-pack --stdin <pack-$PACK.pack && |
| 92 | git -C dest.git unpack-objects <pack-$PACK.pack && |
| 93 | |
| 94 | # The destination came up with the exact same pack... |
| 95 | DEST_PACK=$(echo dest.git/objects/pack/pack-*.pack) && |
| 96 | cmp pack-$PACK.pack $DEST_PACK && |
| 97 | |
| 98 | # ...and wrote no loose objects |
| 99 | test_stdout_line_count = 0 find dest.git/objects -type f ! -name "pack-*" |
| 100 | ' |
| 101 | |
| 102 | test_done |