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/t/unit-tests/lib-reftable.c # 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 / t / unit-tests / lib-reftable.c` 103 lines ยท 2447 bytes Raw Blame History 1 #include "unit-test.h" 2 #include "lib-reftable.h" 3 #include "hex.h" 4 #include "parse-options.h" 5 #include "reftable/constants.h" 6 #include "reftable/writer.h" 7 #include "strbuf.h" 8 #include "string-list.h" 9 #include "strvec.h" 10 11 void cl_reftable_set_hash(uint8_t *p, int i, enum reftable_hash id) 12 { 13 memset(p, (uint8_t)i, hash_size(id)); 14 } 15 16 static ssize_t strbuf_writer_write(void *b, const void *data, size_t sz) 17 { 18 strbuf_add(b, data, sz); 19 return sz; 20 } 21 22 static int strbuf_writer_flush(void *arg UNUSED) 23 { 24 return 0; 25 } 26 27 struct reftable_writer *cl_reftable_strbuf_writer(struct reftable_buf *buf, 28 enum reftable_hash hash_id, 29 struct reftable_write_options *opts) 30 { 31 struct reftable_writer *writer; 32 int ret = reftable_writer_new(&writer, &strbuf_writer_write, &strbuf_writer_flush, 33 buf, hash_id, opts); 34 cl_assert(!ret); 35 return writer; 36 } 37 38 void cl_reftable_write_to_buf(struct reftable_buf *buf, 39 struct reftable_ref_record *refs, 40 size_t nrefs, 41 struct reftable_log_record *logs, 42 size_t nlogs, 43 enum reftable_hash hash_id, 44 struct reftable_write_options *_opts) 45 { 46 struct reftable_write_options opts = { 0 }; 47 const struct reftable_stats *stats; 48 struct reftable_writer *writer; 49 uint64_t min = 0xffffffff; 50 uint64_t max = 0; 51 int ret; 52 53 if (_opts) 54 opts = *_opts; 55 56 for (size_t i = 0; i < nrefs; i++) { 57 uint64_t ui = refs[i].update_index; 58 if (ui > max) 59 max = ui; 60 if (ui < min) 61 min = ui; 62 } 63 for (size_t i = 0; i < nlogs; i++) { 64 uint64_t ui = logs[i].update_index; 65 if (ui > max) 66 max = ui; 67 if (ui < min) 68 min = ui; 69 } 70 71 writer = cl_reftable_strbuf_writer(buf, hash_id, &opts); 72 ret = reftable_writer_set_limits(writer, min, max); 73 cl_assert(!ret); 74 75 if (nrefs) { 76 ret = reftable_writer_add_refs(writer, refs, nrefs); 77 cl_assert_equal_i(ret, 0); 78 } 79 80 if (nlogs) { 81 ret = reftable_writer_add_logs(writer, logs, nlogs); 82 cl_assert_equal_i(ret, 0); 83 } 84 85 ret = reftable_writer_close(writer); 86 cl_assert_equal_i(ret, 0); 87 88 stats = reftable_writer_stats(writer); 89 for (size_t i = 0; i < (size_t)stats->ref_stats.blocks; i++) { 90 size_t off = i * (opts.block_size ? opts.block_size 91 : DEFAULT_BLOCK_SIZE); 92 if (!off) 93 off = header_size(hash_id == REFTABLE_HASH_SHA256 ? 2 : 1); 94 cl_assert(buf->buf[off] == 'r'); 95 } 96 97 if (nrefs) 98 cl_assert(stats->ref_stats.blocks > 0); 99 if (nlogs) 100 cl_assert(stats->log_stats.blocks > 0); 101 102 reftable_writer_free(writer); 103 }