2 #include "refs-internal.h"
5 static struct trace_key trace_refs = TRACE_KEY_INIT(REFS);
7 struct debug_ref_store {
9 struct ref_store *refs;
12 extern struct ref_storage_be refs_be_debug;
14 struct ref_store *maybe_debug_wrap_ref_store(const char *gitdir, struct ref_store *store)
16 struct debug_ref_store *res;
17 struct ref_storage_be *be_copy;
19 if (!trace_want(&trace_refs)) {
22 res = xmalloc(sizeof(struct debug_ref_store));
23 be_copy = xmalloc(sizeof(*be_copy));
24 *be_copy = refs_be_debug;
25 /* we never deallocate backends, so safe to copy the pointer. */
26 be_copy->name = store->be->name;
27 trace_printf_key(&trace_refs, "ref_store for %s\n", gitdir);
29 base_ref_store_init((struct ref_store *)res, be_copy);
30 return (struct ref_store *)res;
33 static int debug_init_db(struct ref_store *refs, struct strbuf *err)
35 struct debug_ref_store *drefs = (struct debug_ref_store *)refs;
36 int res = drefs->refs->be->init_db(drefs->refs, err);
37 trace_printf_key(&trace_refs, "init_db: %d\n", res);
41 static int debug_transaction_prepare(struct ref_store *refs,
42 struct ref_transaction *transaction,
45 struct debug_ref_store *drefs = (struct debug_ref_store *)refs;
47 transaction->ref_store = drefs->refs;
48 res = drefs->refs->be->transaction_prepare(drefs->refs, transaction,
50 trace_printf_key(&trace_refs, "transaction_prepare: %d\n", res);
54 static void print_update(int i, const char *refname,
55 const struct object_id *old_oid,
56 const struct object_id *new_oid, unsigned int flags,
57 unsigned int type, const char *msg)
59 char o[GIT_MAX_HEXSZ + 1] = "null";
60 char n[GIT_MAX_HEXSZ + 1] = "null";
62 oid_to_hex_r(o, old_oid);
64 oid_to_hex_r(n, new_oid);
66 type &= 0xf; /* see refs.h REF_* */
67 flags &= REF_HAVE_NEW | REF_HAVE_OLD | REF_NO_DEREF |
68 REF_FORCE_CREATE_REFLOG;
69 trace_printf_key(&trace_refs, "%d: %s %s -> %s (F=0x%x, T=0x%x) \"%s\"\n", i, refname,
70 o, n, flags, type, msg);
73 static void print_transaction(struct ref_transaction *transaction)
76 trace_printf_key(&trace_refs, "transaction {\n");
77 for (i = 0; i < transaction->nr; i++) {
78 struct ref_update *u = transaction->updates[i];
79 print_update(i, u->refname, &u->old_oid, &u->new_oid, u->flags,
82 trace_printf_key(&trace_refs, "}\n");
85 static int debug_transaction_finish(struct ref_store *refs,
86 struct ref_transaction *transaction,
89 struct debug_ref_store *drefs = (struct debug_ref_store *)refs;
91 transaction->ref_store = drefs->refs;
92 res = drefs->refs->be->transaction_finish(drefs->refs, transaction,
94 print_transaction(transaction);
95 trace_printf_key(&trace_refs, "finish: %d\n", res);
99 static int debug_transaction_abort(struct ref_store *refs,
100 struct ref_transaction *transaction,
103 struct debug_ref_store *drefs = (struct debug_ref_store *)refs;
105 transaction->ref_store = drefs->refs;
106 res = drefs->refs->be->transaction_abort(drefs->refs, transaction, err);
110 static int debug_initial_transaction_commit(struct ref_store *refs,
111 struct ref_transaction *transaction,
114 struct debug_ref_store *drefs = (struct debug_ref_store *)refs;
116 transaction->ref_store = drefs->refs;
117 res = drefs->refs->be->initial_transaction_commit(drefs->refs,
122 static int debug_pack_refs(struct ref_store *ref_store, unsigned int flags)
124 struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
125 int res = drefs->refs->be->pack_refs(drefs->refs, flags);
126 trace_printf_key(&trace_refs, "pack_refs: %d\n", res);
130 static int debug_create_symref(struct ref_store *ref_store,
131 const char *ref_name, const char *target,
134 struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
135 int res = drefs->refs->be->create_symref(drefs->refs, ref_name, target,
137 trace_printf_key(&trace_refs, "create_symref: %s -> %s \"%s\": %d\n", ref_name,
138 target, logmsg, res);
142 static int debug_delete_refs(struct ref_store *ref_store, const char *msg,
143 struct string_list *refnames, unsigned int flags)
145 struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
147 drefs->refs->be->delete_refs(drefs->refs, msg, refnames, flags);
149 trace_printf_key(&trace_refs, "delete_refs {\n");
150 for (i = 0; i < refnames->nr; i++)
151 trace_printf_key(&trace_refs, "%s\n", refnames->items[i].string);
152 trace_printf_key(&trace_refs, "}: %d\n", res);
156 static int debug_rename_ref(struct ref_store *ref_store, const char *oldref,
157 const char *newref, const char *logmsg)
159 struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
160 int res = drefs->refs->be->rename_ref(drefs->refs, oldref, newref,
162 trace_printf_key(&trace_refs, "rename_ref: %s -> %s \"%s\": %d\n", oldref, newref,
167 static int debug_copy_ref(struct ref_store *ref_store, const char *oldref,
168 const char *newref, const char *logmsg)
170 struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
172 drefs->refs->be->copy_ref(drefs->refs, oldref, newref, logmsg);
173 trace_printf_key(&trace_refs, "copy_ref: %s -> %s \"%s\": %d\n", oldref, newref,
178 struct debug_ref_iterator {
179 struct ref_iterator base;
180 struct ref_iterator *iter;
183 static int debug_ref_iterator_advance(struct ref_iterator *ref_iterator)
185 struct debug_ref_iterator *diter =
186 (struct debug_ref_iterator *)ref_iterator;
187 int res = diter->iter->vtable->advance(diter->iter);
189 trace_printf_key(&trace_refs, "iterator_advance: (%d)\n", res);
191 trace_printf_key(&trace_refs, "iterator_advance: %s (0)\n",
192 diter->iter->refname);
194 diter->base.ordered = diter->iter->ordered;
195 diter->base.refname = diter->iter->refname;
196 diter->base.oid = diter->iter->oid;
197 diter->base.flags = diter->iter->flags;
201 static int debug_ref_iterator_peel(struct ref_iterator *ref_iterator,
202 struct object_id *peeled)
204 struct debug_ref_iterator *diter =
205 (struct debug_ref_iterator *)ref_iterator;
206 int res = diter->iter->vtable->peel(diter->iter, peeled);
207 trace_printf_key(&trace_refs, "iterator_peel: %s: %d\n", diter->iter->refname, res);
211 static int debug_ref_iterator_abort(struct ref_iterator *ref_iterator)
213 struct debug_ref_iterator *diter =
214 (struct debug_ref_iterator *)ref_iterator;
215 int res = diter->iter->vtable->abort(diter->iter);
216 trace_printf_key(&trace_refs, "iterator_abort: %d\n", res);
220 static struct ref_iterator_vtable debug_ref_iterator_vtable = {
221 debug_ref_iterator_advance, debug_ref_iterator_peel,
222 debug_ref_iterator_abort
225 static struct ref_iterator *
226 debug_ref_iterator_begin(struct ref_store *ref_store, const char *prefix,
229 struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
230 struct ref_iterator *res =
231 drefs->refs->be->iterator_begin(drefs->refs, prefix, flags);
232 struct debug_ref_iterator *diter = xcalloc(1, sizeof(*diter));
233 base_ref_iterator_init(&diter->base, &debug_ref_iterator_vtable, 1);
235 trace_printf_key(&trace_refs, "ref_iterator_begin: %s (0x%x)\n", prefix, flags);
239 static int debug_read_raw_ref(struct ref_store *ref_store, const char *refname,
240 struct object_id *oid, struct strbuf *referent,
243 struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
246 oidcpy(oid, &null_oid);
247 res = drefs->refs->be->read_raw_ref(drefs->refs, refname, oid, referent,
251 trace_printf_key(&trace_refs, "read_raw_ref: %s: %s (=> %s) type %x: %d\n",
252 refname, oid_to_hex(oid), referent->buf, *type, res);
254 trace_printf_key(&trace_refs, "read_raw_ref: %s: %d\n", refname, res);
259 static struct ref_iterator *
260 debug_reflog_iterator_begin(struct ref_store *ref_store)
262 struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
263 struct ref_iterator *res =
264 drefs->refs->be->reflog_iterator_begin(drefs->refs);
265 trace_printf_key(&trace_refs, "for_each_reflog_iterator_begin\n");
269 struct debug_reflog {
271 each_reflog_ent_fn *fn;
275 static int debug_print_reflog_ent(struct object_id *old_oid,
276 struct object_id *new_oid,
277 const char *committer, timestamp_t timestamp,
278 int tz, const char *msg, void *cb_data)
280 struct debug_reflog *dbg = (struct debug_reflog *)cb_data;
282 char o[GIT_MAX_HEXSZ + 1] = "null";
283 char n[GIT_MAX_HEXSZ + 1] = "null";
285 oid_to_hex_r(o, old_oid);
287 oid_to_hex_r(n, new_oid);
289 ret = dbg->fn(old_oid, new_oid, committer, timestamp, tz, msg,
291 trace_printf_key(&trace_refs, "reflog_ent %s (ret %d): %s -> %s, %s %ld \"%s\"\n",
292 dbg->refname, ret, o, n, committer, (long int)timestamp, msg);
296 static int debug_for_each_reflog_ent(struct ref_store *ref_store,
297 const char *refname, each_reflog_ent_fn fn,
300 struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
301 struct debug_reflog dbg = {
307 int res = drefs->refs->be->for_each_reflog_ent(
308 drefs->refs, refname, &debug_print_reflog_ent, &dbg);
309 trace_printf_key(&trace_refs, "for_each_reflog: %s: %d\n", refname, res);
313 static int debug_for_each_reflog_ent_reverse(struct ref_store *ref_store,
315 each_reflog_ent_fn fn,
318 struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
319 struct debug_reflog dbg = {
324 int res = drefs->refs->be->for_each_reflog_ent_reverse(
325 drefs->refs, refname, &debug_print_reflog_ent, &dbg);
326 trace_printf_key(&trace_refs, "for_each_reflog_reverse: %s: %d\n", refname, res);
330 static int debug_reflog_exists(struct ref_store *ref_store, const char *refname)
332 struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
333 int res = drefs->refs->be->reflog_exists(drefs->refs, refname);
334 trace_printf_key(&trace_refs, "reflog_exists: %s: %d\n", refname, res);
338 static int debug_create_reflog(struct ref_store *ref_store, const char *refname,
339 int force_create, struct strbuf *err)
341 struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
342 int res = drefs->refs->be->create_reflog(drefs->refs, refname,
344 trace_printf_key(&trace_refs, "create_reflog: %s: %d\n", refname, res);
348 static int debug_delete_reflog(struct ref_store *ref_store, const char *refname)
350 struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
351 int res = drefs->refs->be->delete_reflog(drefs->refs, refname);
352 trace_printf_key(&trace_refs, "delete_reflog: %s: %d\n", refname, res);
356 static int debug_reflog_expire(struct ref_store *ref_store, const char *refname,
357 const struct object_id *oid, unsigned int flags,
358 reflog_expiry_prepare_fn prepare_fn,
359 reflog_expiry_should_prune_fn should_prune_fn,
360 reflog_expiry_cleanup_fn cleanup_fn,
361 void *policy_cb_data)
363 struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
364 int res = drefs->refs->be->reflog_expire(drefs->refs, refname, oid,
366 should_prune_fn, cleanup_fn,
368 trace_printf_key(&trace_refs, "reflog_expire: %s: %d\n", refname, res);
372 struct ref_storage_be refs_be_debug = {
377 debug_transaction_prepare,
378 debug_transaction_finish,
379 debug_transaction_abort,
380 debug_initial_transaction_commit,
388 debug_ref_iterator_begin,
391 debug_reflog_iterator_begin,
392 debug_for_each_reflog_ent,
393 debug_for_each_reflog_ent_reverse,