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/refs/debug.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 / refs / debug.c` 472 lines ยท 15166 bytes Raw Blame History 1 #include "git-compat-util.h" 2 #include "hex.h" 3 #include "refs-internal.h" 4 #include "trace.h" 5 6 static struct trace_key trace_refs = TRACE_KEY_INIT(REFS); 7 8 struct debug_ref_store { 9 struct ref_store base; 10 struct ref_store *refs; 11 }; 12 13 extern struct ref_storage_be refs_be_debug; 14 15 struct ref_store *maybe_debug_wrap_ref_store(const char *gitdir, struct ref_store *store) 16 { 17 struct debug_ref_store *res; 18 struct ref_storage_be *be_copy; 19 20 if (!trace_want(&trace_refs)) { 21 return store; 22 } 23 res = xmalloc(sizeof(struct debug_ref_store)); 24 be_copy = xmalloc(sizeof(*be_copy)); 25 *be_copy = refs_be_debug; 26 /* we never deallocate backends, so safe to copy the pointer. */ 27 be_copy->name = store->be->name; 28 trace_printf_key(&trace_refs, "ref_store for %s\n", gitdir); 29 res->refs = store; 30 base_ref_store_init((struct ref_store *)res, store->repo, gitdir, 31 be_copy); 32 return (struct ref_store *)res; 33 } 34 35 static void debug_release(struct ref_store *refs) 36 { 37 struct debug_ref_store *drefs = (struct debug_ref_store *)refs; 38 drefs->refs->be->release(drefs->refs); 39 trace_printf_key(&trace_refs, "release\n"); 40 } 41 42 static int debug_create_on_disk(struct ref_store *refs, int flags, struct strbuf *err) 43 { 44 struct debug_ref_store *drefs = (struct debug_ref_store *)refs; 45 int res = drefs->refs->be->create_on_disk(drefs->refs, flags, err); 46 trace_printf_key(&trace_refs, "create_on_disk: %d\n", res); 47 return res; 48 } 49 50 static int debug_remove_on_disk(struct ref_store *refs, struct strbuf *err) 51 { 52 struct debug_ref_store *drefs = (struct debug_ref_store *)refs; 53 int res = drefs->refs->be->remove_on_disk(drefs->refs, err); 54 trace_printf_key(&trace_refs, "remove_on_disk: %d\n", res); 55 return res; 56 } 57 58 static int debug_transaction_prepare(struct ref_store *refs, 59 struct ref_transaction *transaction, 60 struct strbuf *err) 61 { 62 struct debug_ref_store *drefs = (struct debug_ref_store *)refs; 63 int res; 64 transaction->ref_store = drefs->refs; 65 res = drefs->refs->be->transaction_prepare(drefs->refs, transaction, 66 err); 67 trace_printf_key(&trace_refs, "transaction_prepare: %d \"%s\"\n", res, 68 err->buf); 69 return res; 70 } 71 72 static void print_update(int i, const char *refname, 73 const struct object_id *old_oid, 74 const struct object_id *new_oid, unsigned int flags, 75 unsigned int type, const char *msg) 76 { 77 char o[GIT_MAX_HEXSZ + 1] = "null"; 78 char n[GIT_MAX_HEXSZ + 1] = "null"; 79 if (old_oid) 80 oid_to_hex_r(o, old_oid); 81 if (new_oid) 82 oid_to_hex_r(n, new_oid); 83 84 type &= 0xf; /* see refs.h REF_* */ 85 flags &= REF_HAVE_NEW | REF_HAVE_OLD | REF_NO_DEREF | 86 REF_FORCE_CREATE_REFLOG; 87 trace_printf_key(&trace_refs, "%d: %s %s -> %s (F=0x%x, T=0x%x) \"%s\"\n", i, refname, 88 o, n, flags, type, msg); 89 } 90 91 static void print_transaction(struct ref_transaction *transaction) 92 { 93 trace_printf_key(&trace_refs, "transaction {\n"); 94 for (size_t i = 0; i < transaction->nr; i++) { 95 struct ref_update *u = transaction->updates[i]; 96 print_update(i, u->refname, &u->old_oid, &u->new_oid, u->flags, 97 u->type, u->msg); 98 } 99 trace_printf_key(&trace_refs, "}\n"); 100 } 101 102 static int debug_transaction_finish(struct ref_store *refs, 103 struct ref_transaction *transaction, 104 struct strbuf *err) 105 { 106 struct debug_ref_store *drefs = (struct debug_ref_store *)refs; 107 int res; 108 transaction->ref_store = drefs->refs; 109 res = drefs->refs->be->transaction_finish(drefs->refs, transaction, 110 err); 111 print_transaction(transaction); 112 trace_printf_key(&trace_refs, "finish: %d\n", res); 113 return res; 114 } 115 116 static int debug_transaction_abort(struct ref_store *refs, 117 struct ref_transaction *transaction, 118 struct strbuf *err) 119 { 120 struct debug_ref_store *drefs = (struct debug_ref_store *)refs; 121 int res; 122 transaction->ref_store = drefs->refs; 123 res = drefs->refs->be->transaction_abort(drefs->refs, transaction, err); 124 return res; 125 } 126 127 static int debug_optimize(struct ref_store *ref_store, struct refs_optimize_opts *opts) 128 { 129 struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store; 130 int res = drefs->refs->be->optimize(drefs->refs, opts); 131 trace_printf_key(&trace_refs, "optimize: %d\n", res); 132 return res; 133 } 134 135 static int debug_optimize_required(struct ref_store *ref_store, 136 struct refs_optimize_opts *opts, 137 bool *required) 138 { 139 struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store; 140 int res = drefs->refs->be->optimize_required(drefs->refs, opts, required); 141 trace_printf_key(&trace_refs, "optimize_required: %s, res: %d\n", 142 *required ? "yes" : "no", res); 143 return res; 144 } 145 146 static int debug_rename_ref(struct ref_store *ref_store, const char *oldref, 147 const char *newref, const char *logmsg) 148 { 149 struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store; 150 int res = drefs->refs->be->rename_ref(drefs->refs, oldref, newref, 151 logmsg); 152 trace_printf_key(&trace_refs, "rename_ref: %s -> %s \"%s\": %d\n", oldref, newref, 153 logmsg, res); 154 return res; 155 } 156 157 static int debug_copy_ref(struct ref_store *ref_store, const char *oldref, 158 const char *newref, const char *logmsg) 159 { 160 struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store; 161 int res = 162 drefs->refs->be->copy_ref(drefs->refs, oldref, newref, logmsg); 163 trace_printf_key(&trace_refs, "copy_ref: %s -> %s \"%s\": %d\n", oldref, newref, 164 logmsg, res); 165 return res; 166 } 167 168 struct debug_ref_iterator { 169 struct ref_iterator base; 170 struct ref_iterator *iter; 171 }; 172 173 static int debug_ref_iterator_advance(struct ref_iterator *ref_iterator) 174 { 175 struct debug_ref_iterator *diter = 176 (struct debug_ref_iterator *)ref_iterator; 177 int res = diter->iter->vtable->advance(diter->iter); 178 if (res) 179 trace_printf_key(&trace_refs, "iterator_advance: (%d)\n", res); 180 else 181 trace_printf_key(&trace_refs, "iterator_advance: %s (0)\n", 182 diter->iter->ref.name); 183 184 diter->base.ref = diter->iter->ref; 185 return res; 186 } 187 188 static int debug_ref_iterator_seek(struct ref_iterator *ref_iterator, 189 const char *refname, unsigned int flags) 190 { 191 struct debug_ref_iterator *diter = 192 (struct debug_ref_iterator *)ref_iterator; 193 int res = diter->iter->vtable->seek(diter->iter, refname, flags); 194 trace_printf_key(&trace_refs, "iterator_seek: %s flags: %d: %d\n", 195 refname ? refname : "", flags, res); 196 return res; 197 } 198 199 static void debug_ref_iterator_release(struct ref_iterator *ref_iterator) 200 { 201 struct debug_ref_iterator *diter = 202 (struct debug_ref_iterator *)ref_iterator; 203 diter->iter->vtable->release(diter->iter); 204 trace_printf_key(&trace_refs, "iterator_abort\n"); 205 } 206 207 static struct ref_iterator_vtable debug_ref_iterator_vtable = { 208 .advance = debug_ref_iterator_advance, 209 .seek = debug_ref_iterator_seek, 210 .release = debug_ref_iterator_release, 211 }; 212 213 static struct ref_iterator * 214 debug_ref_iterator_begin(struct ref_store *ref_store, const char *prefix, 215 const char **exclude_patterns, unsigned int flags) 216 { 217 struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store; 218 struct ref_iterator *res = 219 drefs->refs->be->iterator_begin(drefs->refs, prefix, 220 exclude_patterns, flags); 221 struct debug_ref_iterator *diter = xcalloc(1, sizeof(*diter)); 222 base_ref_iterator_init(&diter->base, &debug_ref_iterator_vtable); 223 diter->iter = res; 224 trace_printf_key(&trace_refs, "ref_iterator_begin: \"%s\" (0x%x)\n", 225 prefix, flags); 226 return &diter->base; 227 } 228 229 static int debug_read_raw_ref(struct ref_store *ref_store, const char *refname, 230 struct object_id *oid, struct strbuf *referent, 231 unsigned int *type, int *failure_errno) 232 { 233 struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store; 234 int res = 0; 235 236 oidcpy(oid, null_oid(ref_store->repo->hash_algo)); 237 res = drefs->refs->be->read_raw_ref(drefs->refs, refname, oid, referent, 238 type, failure_errno); 239 240 if (res == 0) { 241 trace_printf_key(&trace_refs, "read_raw_ref: %s: %s (=> %s) type %x: %d\n", 242 refname, oid_to_hex(oid), referent->buf, *type, res); 243 } else { 244 trace_printf_key(&trace_refs, 245 "read_raw_ref: %s: %d (errno %d)\n", refname, 246 res, *failure_errno); 247 } 248 return res; 249 } 250 251 static int debug_read_symbolic_ref(struct ref_store *ref_store, const char *refname, 252 struct strbuf *referent) 253 { 254 struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store; 255 struct ref_store *refs = drefs->refs; 256 int res; 257 258 res = refs->be->read_symbolic_ref(refs, refname, referent); 259 if (!res) 260 trace_printf_key(&trace_refs, "read_symbolic_ref: %s: (%s)\n", 261 refname, referent->buf); 262 else 263 trace_printf_key(&trace_refs, 264 "read_symbolic_ref: %s: %d\n", refname, res); 265 return res; 266 267 } 268 269 static struct ref_iterator * 270 debug_reflog_iterator_begin(struct ref_store *ref_store) 271 { 272 struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store; 273 struct ref_iterator *res = 274 drefs->refs->be->reflog_iterator_begin(drefs->refs); 275 trace_printf_key(&trace_refs, "for_each_reflog_iterator_begin\n"); 276 return res; 277 } 278 279 struct debug_reflog { 280 const char *refname; 281 each_reflog_ent_fn *fn; 282 void *cb_data; 283 }; 284 285 static int debug_print_reflog_ent(const char *refname, 286 struct object_id *old_oid, 287 struct object_id *new_oid, 288 const char *committer, timestamp_t timestamp, 289 int tz, const char *msg, void *cb_data) 290 { 291 struct debug_reflog *dbg = (struct debug_reflog *)cb_data; 292 int ret; 293 char o[GIT_MAX_HEXSZ + 1] = "null"; 294 char n[GIT_MAX_HEXSZ + 1] = "null"; 295 char *msgend = strchrnul(msg, '\n'); 296 if (old_oid) 297 oid_to_hex_r(o, old_oid); 298 if (new_oid) 299 oid_to_hex_r(n, new_oid); 300 301 ret = dbg->fn(refname, old_oid, new_oid, committer, timestamp, tz, msg, 302 dbg->cb_data); 303 trace_printf_key(&trace_refs, 304 "reflog_ent %s (ret %d): %s -> %s, %s %ld \"%.*s\"\n", 305 dbg->refname, ret, o, n, committer, 306 (long int)timestamp, (int)(msgend - msg), msg); 307 return ret; 308 } 309 310 static int debug_for_each_reflog_ent(struct ref_store *ref_store, 311 const char *refname, each_reflog_ent_fn fn, 312 void *cb_data) 313 { 314 struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store; 315 struct debug_reflog dbg = { 316 .refname = refname, 317 .fn = fn, 318 .cb_data = cb_data, 319 }; 320 321 int res = drefs->refs->be->for_each_reflog_ent( 322 drefs->refs, refname, &debug_print_reflog_ent, &dbg); 323 trace_printf_key(&trace_refs, "for_each_reflog: %s: %d\n", refname, res); 324 return res; 325 } 326 327 static int debug_for_each_reflog_ent_reverse(struct ref_store *ref_store, 328 const char *refname, 329 each_reflog_ent_fn fn, 330 void *cb_data) 331 { 332 struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store; 333 struct debug_reflog dbg = { 334 .refname = refname, 335 .fn = fn, 336 .cb_data = cb_data, 337 }; 338 int res = drefs->refs->be->for_each_reflog_ent_reverse( 339 drefs->refs, refname, &debug_print_reflog_ent, &dbg); 340 trace_printf_key(&trace_refs, "for_each_reflog_reverse: %s: %d\n", refname, res); 341 return res; 342 } 343 344 static int debug_reflog_exists(struct ref_store *ref_store, const char *refname) 345 { 346 struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store; 347 int res = drefs->refs->be->reflog_exists(drefs->refs, refname); 348 trace_printf_key(&trace_refs, "reflog_exists: %s: %d\n", refname, res); 349 return res; 350 } 351 352 static int debug_create_reflog(struct ref_store *ref_store, const char *refname, 353 struct strbuf *err) 354 { 355 struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store; 356 int res = drefs->refs->be->create_reflog(drefs->refs, refname, err); 357 trace_printf_key(&trace_refs, "create_reflog: %s: %d\n", refname, res); 358 return res; 359 } 360 361 static int debug_delete_reflog(struct ref_store *ref_store, const char *refname) 362 { 363 struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store; 364 int res = drefs->refs->be->delete_reflog(drefs->refs, refname); 365 trace_printf_key(&trace_refs, "delete_reflog: %s: %d\n", refname, res); 366 return res; 367 } 368 369 struct debug_reflog_expiry_should_prune { 370 reflog_expiry_prepare_fn *prepare; 371 reflog_expiry_should_prune_fn *should_prune; 372 reflog_expiry_cleanup_fn *cleanup; 373 void *cb_data; 374 }; 375 376 static void debug_reflog_expiry_prepare(const char *refname, 377 const struct object_id *oid, 378 void *cb_data) 379 { 380 struct debug_reflog_expiry_should_prune *prune = cb_data; 381 trace_printf_key(&trace_refs, "reflog_expire_prepare: %s\n", refname); 382 prune->prepare(refname, oid, prune->cb_data); 383 } 384 385 static int debug_reflog_expiry_should_prune_fn(struct object_id *ooid, 386 struct object_id *noid, 387 const char *email, 388 timestamp_t timestamp, int tz, 389 const char *message, void *cb_data) { 390 struct debug_reflog_expiry_should_prune *prune = cb_data; 391 392 int result = prune->should_prune(ooid, noid, email, timestamp, tz, message, prune->cb_data); 393 trace_printf_key(&trace_refs, "reflog_expire_should_prune: %s %ld: %d\n", message, (long int) timestamp, result); 394 return result; 395 } 396 397 static void debug_reflog_expiry_cleanup(void *cb_data) 398 { 399 struct debug_reflog_expiry_should_prune *prune = cb_data; 400 prune->cleanup(prune->cb_data); 401 } 402 403 static int debug_reflog_expire(struct ref_store *ref_store, const char *refname, 404 unsigned int flags, 405 reflog_expiry_prepare_fn prepare_fn, 406 reflog_expiry_should_prune_fn should_prune_fn, 407 reflog_expiry_cleanup_fn cleanup_fn, 408 void *policy_cb_data) 409 { 410 struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store; 411 struct debug_reflog_expiry_should_prune prune = { 412 .prepare = prepare_fn, 413 .cleanup = cleanup_fn, 414 .should_prune = should_prune_fn, 415 .cb_data = policy_cb_data, 416 }; 417 int res = drefs->refs->be->reflog_expire(drefs->refs, refname, 418 flags, &debug_reflog_expiry_prepare, 419 &debug_reflog_expiry_should_prune_fn, 420 &debug_reflog_expiry_cleanup, 421 &prune); 422 trace_printf_key(&trace_refs, "reflog_expire: %s: %d\n", refname, res); 423 return res; 424 } 425 426 static int debug_fsck(struct ref_store *ref_store, 427 struct fsck_options *o, 428 struct worktree *wt) 429 { 430 struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store; 431 int res = drefs->refs->be->fsck(drefs->refs, o, wt); 432 trace_printf_key(&trace_refs, "fsck: %d\n", res); 433 return res; 434 } 435 436 struct ref_storage_be refs_be_debug = { 437 .name = "debug", 438 .init = NULL, 439 .release = debug_release, 440 .create_on_disk = debug_create_on_disk, 441 .remove_on_disk = debug_remove_on_disk, 442 443 /* 444 * None of these should be NULL. If the "files" backend (in 445 * "struct ref_storage_be refs_be_files" in files-backend.c) 446 * has a function we should also have a wrapper for it here. 447 * Test the output with "GIT_TRACE_REFS=1". 448 */ 449 .transaction_prepare = debug_transaction_prepare, 450 .transaction_finish = debug_transaction_finish, 451 .transaction_abort = debug_transaction_abort, 452 453 .optimize = debug_optimize, 454 .optimize_required = debug_optimize_required, 455 456 .rename_ref = debug_rename_ref, 457 .copy_ref = debug_copy_ref, 458 459 .iterator_begin = debug_ref_iterator_begin, 460 .read_raw_ref = debug_read_raw_ref, 461 .read_symbolic_ref = debug_read_symbolic_ref, 462 463 .reflog_iterator_begin = debug_reflog_iterator_begin, 464 .for_each_reflog_ent = debug_for_each_reflog_ent, 465 .for_each_reflog_ent_reverse = debug_for_each_reflog_ent_reverse, 466 .reflog_exists = debug_reflog_exists, 467 .create_reflog = debug_create_reflog, 468 .delete_reflog = debug_delete_reflog, 469 .reflog_expire = debug_reflog_expire, 470 471 .fsck = debug_fsck, 472 };