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/helper/test-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 / helper / test-reftable.c` 276 lines ยท 6465 bytes Raw Blame History 1 #include "git-compat-util.h" 2 #include "hash.h" 3 #include "hex.h" 4 #include "reftable/system.h" 5 #include "reftable/reftable-constants.h" 6 #include "reftable/reftable-error.h" 7 #include "reftable/reftable-merged.h" 8 #include "reftable/reftable-stack.h" 9 #include "reftable/reftable-table.h" 10 #include "test-tool.h" 11 12 static void print_help(void) 13 { 14 printf("usage: dump [-st] arg\n\n" 15 "options: \n" 16 " -b dump blocks\n" 17 " -t dump table\n" 18 " -s dump stack\n" 19 " -6 sha256 hash format\n" 20 " -h this help\n" 21 "\n"); 22 } 23 24 static int dump_blocks(const char *tablename) 25 { 26 struct reftable_table_iterator ti = { 0 }; 27 struct reftable_block_source src = { 0 }; 28 struct reftable_table *table = NULL; 29 uint8_t section_type = 0; 30 int err; 31 32 err = reftable_block_source_from_file(&src, tablename); 33 if (err < 0) 34 goto done; 35 36 err = reftable_table_new(&table, &src, tablename); 37 if (err < 0) 38 goto done; 39 40 err = reftable_table_iterator_init(&ti, table); 41 if (err < 0) 42 goto done; 43 44 printf("header:\n"); 45 printf(" block_size: %d\n", table->block_size); 46 47 while (1) { 48 const struct reftable_block *block; 49 50 err = reftable_table_iterator_next(&ti, &block); 51 if (err < 0) 52 goto done; 53 if (err > 0) 54 break; 55 56 if (block->block_type != section_type) { 57 const char *section; 58 switch (block->block_type) { 59 case REFTABLE_BLOCK_TYPE_LOG: 60 section = "log"; 61 break; 62 case REFTABLE_BLOCK_TYPE_REF: 63 section = "ref"; 64 break; 65 case REFTABLE_BLOCK_TYPE_OBJ: 66 section = "obj"; 67 break; 68 case REFTABLE_BLOCK_TYPE_INDEX: 69 section = "idx"; 70 break; 71 default: 72 err = -1; 73 goto done; 74 } 75 76 section_type = block->block_type; 77 printf("%s:\n", section); 78 } 79 80 printf(" - length: %u\n", block->restart_off); 81 printf(" restarts: %u\n", block->restart_count); 82 } 83 84 done: 85 reftable_table_iterator_release(&ti); 86 reftable_table_decref(table); 87 return err; 88 } 89 90 static int dump_table(struct reftable_merged_table *mt) 91 { 92 struct reftable_iterator it = { NULL }; 93 struct reftable_ref_record ref = { NULL }; 94 struct reftable_log_record log = { NULL }; 95 const struct git_hash_algo *algop; 96 int err; 97 98 err = reftable_merged_table_init_ref_iterator(mt, &it); 99 if (err < 0) 100 return err; 101 102 err = reftable_iterator_seek_ref(&it, ""); 103 if (err < 0) 104 return err; 105 106 switch (reftable_merged_table_hash_id(mt)) { 107 case REFTABLE_HASH_SHA1: 108 algop = &hash_algos[GIT_HASH_SHA1]; 109 break; 110 case REFTABLE_HASH_SHA256: 111 algop = &hash_algos[GIT_HASH_SHA256]; 112 break; 113 default: 114 die("unknown reftable hash function: %d", reftable_merged_table_hash_id(mt)); 115 } 116 117 while (1) { 118 err = reftable_iterator_next_ref(&it, &ref); 119 if (err > 0) 120 break; 121 if (err < 0) 122 return err; 123 124 printf("ref{%s(%" PRIu64 ") ", ref.refname, ref.update_index); 125 switch (ref.value_type) { 126 case REFTABLE_REF_SYMREF: 127 printf("=> %s", ref.value.symref); 128 break; 129 case REFTABLE_REF_VAL2: 130 printf("val 2 %s", hash_to_hex_algop(ref.value.val2.value, algop)); 131 printf("(T %s)", hash_to_hex_algop(ref.value.val2.target_value, algop)); 132 break; 133 case REFTABLE_REF_VAL1: 134 printf("val 1 %s", hash_to_hex_algop(ref.value.val1, algop)); 135 break; 136 case REFTABLE_REF_DELETION: 137 printf("delete"); 138 break; 139 } 140 printf("}\n"); 141 } 142 reftable_iterator_destroy(&it); 143 reftable_ref_record_release(&ref); 144 145 err = reftable_merged_table_init_log_iterator(mt, &it); 146 if (err < 0) 147 return err; 148 149 err = reftable_iterator_seek_log(&it, ""); 150 if (err < 0) 151 return err; 152 153 while (1) { 154 err = reftable_iterator_next_log(&it, &log); 155 if (err > 0) 156 break; 157 if (err < 0) 158 return err; 159 160 switch (log.value_type) { 161 case REFTABLE_LOG_DELETION: 162 printf("log{%s(%" PRIu64 ") delete\n", log.refname, 163 log.update_index); 164 break; 165 case REFTABLE_LOG_UPDATE: 166 printf("log{%s(%" PRIu64 ") %s <%s> %" PRIu64 " %d\n", 167 log.refname, log.update_index, 168 log.value.update.name ? log.value.update.name : "", 169 log.value.update.email ? log.value.update.email : "", 170 log.value.update.time, 171 log.value.update.tz_offset); 172 printf("%s => ", hash_to_hex_algop(log.value.update.old_hash, algop)); 173 printf("%s\n\n%s\n}\n", hash_to_hex_algop(log.value.update.new_hash, algop), 174 log.value.update.message ? log.value.update.message : ""); 175 break; 176 } 177 } 178 reftable_iterator_destroy(&it); 179 reftable_log_record_release(&log); 180 return 0; 181 } 182 183 static int dump_stack(const char *stackdir, uint32_t hash_id) 184 { 185 struct reftable_stack *stack = NULL; 186 struct reftable_stack_options opts = { .hash_id = hash_id }; 187 struct reftable_merged_table *merged = NULL; 188 189 int err = reftable_new_stack(&stack, stackdir, &opts); 190 if (err < 0) 191 goto done; 192 193 merged = reftable_stack_merged_table(stack); 194 err = dump_table(merged); 195 done: 196 if (stack) 197 reftable_stack_destroy(stack); 198 return err; 199 } 200 201 static int dump_reftable(const char *tablename) 202 { 203 struct reftable_block_source src = { 0 }; 204 struct reftable_merged_table *mt = NULL; 205 struct reftable_table *table = NULL; 206 int err; 207 208 err = reftable_block_source_from_file(&src, tablename); 209 if (err < 0) 210 goto done; 211 212 err = reftable_table_new(&table, &src, tablename); 213 if (err < 0) 214 goto done; 215 216 err = reftable_merged_table_new(&mt, &table, 1, 217 reftable_table_hash_id(table)); 218 if (err < 0) 219 goto done; 220 221 err = dump_table(mt); 222 223 done: 224 reftable_merged_table_free(mt); 225 reftable_table_decref(table); 226 return err; 227 } 228 229 int cmd__dump_reftable(int argc, const char **argv) 230 { 231 int err = 0; 232 int opt_dump_blocks = 0; 233 int opt_dump_table = 0; 234 int opt_dump_stack = 0; 235 uint32_t opt_hash_id = REFTABLE_HASH_SHA1; 236 const char *arg = NULL, *argv0 = argv[0]; 237 238 for (; argc > 1; argv++, argc--) 239 if (*argv[1] != '-') 240 break; 241 else if (!strcmp("-b", argv[1])) 242 opt_dump_blocks = 1; 243 else if (!strcmp("-t", argv[1])) 244 opt_dump_table = 1; 245 else if (!strcmp("-6", argv[1])) 246 opt_hash_id = REFTABLE_HASH_SHA256; 247 else if (!strcmp("-s", argv[1])) 248 opt_dump_stack = 1; 249 else if (!strcmp("-?", argv[1]) || !strcmp("-h", argv[1])) { 250 print_help(); 251 return 2; 252 } 253 254 if (argc != 2) { 255 fprintf(stderr, "need argument\n"); 256 print_help(); 257 return 2; 258 } 259 260 arg = argv[1]; 261 262 if (opt_dump_blocks) { 263 err = dump_blocks(arg); 264 } else if (opt_dump_table) { 265 err = dump_reftable(arg); 266 } else if (opt_dump_stack) { 267 err = dump_stack(arg, opt_hash_id); 268 } 269 270 if (err < 0) { 271 fprintf(stderr, "%s: %s: %s\n", argv0, arg, 272 reftable_error_str(err)); 273 return 1; 274 } 275 return 0; 276 }