summaryrefslogtreecommitdiff
path: root/refs/debug.c
blob: 791423c6a7dc1e6f849068d689fd289b0063a89c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444

#include "refs-internal.h"
#include "trace.h"

static struct trace_key trace_refs = TRACE_KEY_INIT(REFS);

struct debug_ref_store {
	struct ref_store base;
	struct ref_store *refs;
};

extern struct ref_storage_be refs_be_debug;

struct ref_store *maybe_debug_wrap_ref_store(const char *gitdir, struct ref_store *store)
{
	struct debug_ref_store *res;
	struct ref_storage_be *be_copy;

	if (!trace_want(&trace_refs)) {
		return store;
	}
	res = xmalloc(sizeof(struct debug_ref_store));
	be_copy = xmalloc(sizeof(*be_copy));
	*be_copy = refs_be_debug;
	/* we never deallocate backends, so safe to copy the pointer. */
	be_copy->name = store->be->name;
	trace_printf_key(&trace_refs, "ref_store for %s\n", gitdir);
	res->refs = store;
	base_ref_store_init((struct ref_store *)res, be_copy);
	return (struct ref_store *)res;
}

static int debug_init_db(struct ref_store *refs, struct strbuf *err)
{
	struct debug_ref_store *drefs = (struct debug_ref_store *)refs;
	int res = drefs->refs->be->init_db(drefs->refs, err);
	trace_printf_key(&trace_refs, "init_db: %d\n", res);
	return res;
}

static int debug_transaction_prepare(struct ref_store *refs,
				     struct ref_transaction *transaction,
				     struct strbuf *err)
{
	struct debug_ref_store *drefs = (struct debug_ref_store *)refs;
	int res;
	transaction->ref_store = drefs->refs;
	res = drefs->refs->be->transaction_prepare(drefs->refs, transaction,
						   err);
	trace_printf_key(&trace_refs, "transaction_prepare: %d\n", res);
	return res;
}

static void print_update(int i, const char *refname,
			 const struct object_id *old_oid,
			 const struct object_id *new_oid, unsigned int flags,
			 unsigned int type, const char *msg)
{
	char o[GIT_MAX_HEXSZ + 1] = "null";
	char n[GIT_MAX_HEXSZ + 1] = "null";
	if (old_oid)
		oid_to_hex_r(o, old_oid);
	if (new_oid)
		oid_to_hex_r(n, new_oid);

	type &= 0xf; /* see refs.h REF_* */
	flags &= REF_HAVE_NEW | REF_HAVE_OLD | REF_NO_DEREF |
		REF_FORCE_CREATE_REFLOG;
	trace_printf_key(&trace_refs, "%d: %s %s -> %s (F=0x%x, T=0x%x) \"%s\"\n", i, refname,
		o, n, flags, type, msg);
}

static void print_transaction(struct ref_transaction *transaction)
{
	int i;
	trace_printf_key(&trace_refs, "transaction {\n");
	for (i = 0; i < transaction->nr; i++) {
		struct ref_update *u = transaction->updates[i];
		print_update(i, u->refname, &u->old_oid, &u->new_oid, u->flags,
			     u->type, u->msg);
	}
	trace_printf_key(&trace_refs, "}\n");
}

static int debug_transaction_finish(struct ref_store *refs,
				    struct ref_transaction *transaction,
				    struct strbuf *err)
{
	struct debug_ref_store *drefs = (struct debug_ref_store *)refs;
	int res;
	transaction->ref_store = drefs->refs;
	res = drefs->refs->be->transaction_finish(drefs->refs, transaction,
						  err);
	print_transaction(transaction);
	trace_printf_key(&trace_refs, "finish: %d\n", res);
	return res;
}

static int debug_transaction_abort(struct ref_store *refs,
				   struct ref_transaction *transaction,
				   struct strbuf *err)
{
	struct debug_ref_store *drefs = (struct debug_ref_store *)refs;
	int res;
	transaction->ref_store = drefs->refs;
	res = drefs->refs->be->transaction_abort(drefs->refs, transaction, err);
	return res;
}

static int debug_initial_transaction_commit(struct ref_store *refs,
					    struct ref_transaction *transaction,
					    struct strbuf *err)
{
	struct debug_ref_store *drefs = (struct debug_ref_store *)refs;
	int res;
	transaction->ref_store = drefs->refs;
	res = drefs->refs->be->initial_transaction_commit(drefs->refs,
							  transaction, err);
	return res;
}

static int debug_pack_refs(struct ref_store *ref_store, unsigned int flags)
{
	struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
	int res = drefs->refs->be->pack_refs(drefs->refs, flags);
	trace_printf_key(&trace_refs, "pack_refs: %d\n", res);
	return res;
}

static int debug_create_symref(struct ref_store *ref_store,
			       const char *ref_name, const char *target,
			       const char *logmsg)
{
	struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
	int res = drefs->refs->be->create_symref(drefs->refs, ref_name, target,
						 logmsg);
	trace_printf_key(&trace_refs, "create_symref: %s -> %s \"%s\": %d\n", ref_name,
		target, logmsg, res);
	return res;
}

static int debug_delete_refs(struct ref_store *ref_store, const char *msg,
			     struct string_list *refnames, unsigned int flags)
{
	struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
	int res =
		drefs->refs->be->delete_refs(drefs->refs, msg, refnames, flags);
	int i;
	trace_printf_key(&trace_refs, "delete_refs {\n");
	for (i = 0; i < refnames->nr; i++)
		trace_printf_key(&trace_refs, "%s\n", refnames->items[i].string);
	trace_printf_key(&trace_refs, "}: %d\n", res);
	return res;
}

static int debug_rename_ref(struct ref_store *ref_store, const char *oldref,
			    const char *newref, const char *logmsg)
{
	struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
	int res = drefs->refs->be->rename_ref(drefs->refs, oldref, newref,
					      logmsg);
	trace_printf_key(&trace_refs, "rename_ref: %s -> %s \"%s\": %d\n", oldref, newref,
		logmsg, res);
	return res;
}

static int debug_copy_ref(struct ref_store *ref_store, const char *oldref,
			  const char *newref, const char *logmsg)
{
	struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
	int res =
		drefs->refs->be->copy_ref(drefs->refs, oldref, newref, logmsg);
	trace_printf_key(&trace_refs, "copy_ref: %s -> %s \"%s\": %d\n", oldref, newref,
		logmsg, res);
	return res;
}

struct debug_ref_iterator {
	struct ref_iterator base;
	struct ref_iterator *iter;
};

static int debug_ref_iterator_advance(struct ref_iterator *ref_iterator)
{
	struct debug_ref_iterator *diter =
		(struct debug_ref_iterator *)ref_iterator;
	int res = diter->iter->vtable->advance(diter->iter);
	if (res)
		trace_printf_key(&trace_refs, "iterator_advance: (%d)\n", res);
	else
		trace_printf_key(&trace_refs, "iterator_advance: %s (0)\n",
			diter->iter->refname);

	diter->base.ordered = diter->iter->ordered;
	diter->base.refname = diter->iter->refname;
	diter->base.oid = diter->iter->oid;
	diter->base.flags = diter->iter->flags;
	return res;
}

static int debug_ref_iterator_peel(struct ref_iterator *ref_iterator,
				   struct object_id *peeled)
{
	struct debug_ref_iterator *diter =
		(struct debug_ref_iterator *)ref_iterator;
	int res = diter->iter->vtable->peel(diter->iter, peeled);
	trace_printf_key(&trace_refs, "iterator_peel: %s: %d\n", diter->iter->refname, res);
	return res;
}

static int debug_ref_iterator_abort(struct ref_iterator *ref_iterator)
{
	struct debug_ref_iterator *diter =
		(struct debug_ref_iterator *)ref_iterator;
	int res = diter->iter->vtable->abort(diter->iter);
	trace_printf_key(&trace_refs, "iterator_abort: %d\n", res);
	return res;
}

static struct ref_iterator_vtable debug_ref_iterator_vtable = {
	debug_ref_iterator_advance, debug_ref_iterator_peel,
	debug_ref_iterator_abort
};

static struct ref_iterator *
debug_ref_iterator_begin(struct ref_store *ref_store, const char *prefix,
			 unsigned int flags)
{
	struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
	struct ref_iterator *res =
		drefs->refs->be->iterator_begin(drefs->refs, prefix, flags);
	struct debug_ref_iterator *diter = xcalloc(1, sizeof(*diter));
	base_ref_iterator_init(&diter->base, &debug_ref_iterator_vtable, 1);
	diter->iter = res;
	trace_printf_key(&trace_refs, "ref_iterator_begin: \"%s\" (0x%x)\n",
			 prefix, flags);
	return &diter->base;
}

static int debug_read_raw_ref(struct ref_store *ref_store, const char *refname,
			      struct object_id *oid, struct strbuf *referent,
			      unsigned int *type, int *failure_errno)
{
	struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
	int res = 0;

	oidcpy(oid, null_oid());
	res = drefs->refs->be->read_raw_ref(drefs->refs, refname, oid, referent,
					    type, failure_errno);

	if (res == 0) {
		trace_printf_key(&trace_refs, "read_raw_ref: %s: %s (=> %s) type %x: %d\n",
			refname, oid_to_hex(oid), referent->buf, *type, res);
	} else {
		trace_printf_key(&trace_refs,
				 "read_raw_ref: %s: %d (errno %d)\n", refname,
				 res, *failure_errno);
	}
	return res;
}

static struct ref_iterator *
debug_reflog_iterator_begin(struct ref_store *ref_store)
{
	struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
	struct ref_iterator *res =
		drefs->refs->be->reflog_iterator_begin(drefs->refs);
	trace_printf_key(&trace_refs, "for_each_reflog_iterator_begin\n");
	return res;
}

struct debug_reflog {
	const char *refname;
	each_reflog_ent_fn *fn;
	void *cb_data;
};

static int debug_print_reflog_ent(struct object_id *old_oid,
				  struct object_id *new_oid,
				  const char *committer, timestamp_t timestamp,
				  int tz, const char *msg, void *cb_data)
{
	struct debug_reflog *dbg = (struct debug_reflog *)cb_data;
	int ret;
	char o[GIT_MAX_HEXSZ + 1] = "null";
	char n[GIT_MAX_HEXSZ + 1] = "null";
	char *msgend = strchrnul(msg, '\n');
	if (old_oid)
		oid_to_hex_r(o, old_oid);
	if (new_oid)
		oid_to_hex_r(n, new_oid);

	ret = dbg->fn(old_oid, new_oid, committer, timestamp, tz, msg,
		      dbg->cb_data);
	trace_printf_key(&trace_refs,
			 "reflog_ent %s (ret %d): %s -> %s, %s %ld \"%.*s\"\n",
			 dbg->refname, ret, o, n, committer,
			 (long int)timestamp, (int)(msgend - msg), msg);
	return ret;
}

static int debug_for_each_reflog_ent(struct ref_store *ref_store,
				     const char *refname, each_reflog_ent_fn fn,
				     void *cb_data)
{
	struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
	struct debug_reflog dbg = {
		.refname = refname,
		.fn = fn,
		.cb_data = cb_data,
	};

	int res = drefs->refs->be->for_each_reflog_ent(
		drefs->refs, refname, &debug_print_reflog_ent, &dbg);
	trace_printf_key(&trace_refs, "for_each_reflog: %s: %d\n", refname, res);
	return res;
}

static int debug_for_each_reflog_ent_reverse(struct ref_store *ref_store,
					     const char *refname,
					     each_reflog_ent_fn fn,
					     void *cb_data)
{
	struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
	struct debug_reflog dbg = {
		.refname = refname,
		.fn = fn,
		.cb_data = cb_data,
	};
	int res = drefs->refs->be->for_each_reflog_ent_reverse(
		drefs->refs, refname, &debug_print_reflog_ent, &dbg);
	trace_printf_key(&trace_refs, "for_each_reflog_reverse: %s: %d\n", refname, res);
	return res;
}

static int debug_reflog_exists(struct ref_store *ref_store, const char *refname)
{
	struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
	int res = drefs->refs->be->reflog_exists(drefs->refs, refname);
	trace_printf_key(&trace_refs, "reflog_exists: %s: %d\n", refname, res);
	return res;
}

static int debug_create_reflog(struct ref_store *ref_store, const char *refname,
			       struct strbuf *err)
{
	struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
	int res = drefs->refs->be->create_reflog(drefs->refs, refname, err);
	trace_printf_key(&trace_refs, "create_reflog: %s: %d\n", refname, res);
	return res;
}

static int debug_delete_reflog(struct ref_store *ref_store, const char *refname)
{
	struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
	int res = drefs->refs->be->delete_reflog(drefs->refs, refname);
	trace_printf_key(&trace_refs, "delete_reflog: %s: %d\n", refname, res);
	return res;
}

struct debug_reflog_expiry_should_prune {
	reflog_expiry_prepare_fn *prepare;
	reflog_expiry_should_prune_fn *should_prune;
	reflog_expiry_cleanup_fn *cleanup;
	void *cb_data;
};

static void debug_reflog_expiry_prepare(const char *refname,
					const struct object_id *oid,
					void *cb_data)
{
	struct debug_reflog_expiry_should_prune *prune = cb_data;
	trace_printf_key(&trace_refs, "reflog_expire_prepare: %s\n", refname);
	prune->prepare(refname, oid, prune->cb_data);
}

static int debug_reflog_expiry_should_prune_fn(struct object_id *ooid,
					       struct object_id *noid,
					       const char *email,
					       timestamp_t timestamp, int tz,
					       const char *message, void *cb_data) {
	struct debug_reflog_expiry_should_prune *prune = cb_data;

	int result = prune->should_prune(ooid, noid, email, timestamp, tz, message, prune->cb_data);
	trace_printf_key(&trace_refs, "reflog_expire_should_prune: %s %ld: %d\n", message, (long int) timestamp, result);
	return result;
}

static void debug_reflog_expiry_cleanup(void *cb_data)
{
	struct debug_reflog_expiry_should_prune *prune = cb_data;
	prune->cleanup(prune->cb_data);
}

static int debug_reflog_expire(struct ref_store *ref_store, const char *refname,
			       unsigned int flags,
			       reflog_expiry_prepare_fn prepare_fn,
			       reflog_expiry_should_prune_fn should_prune_fn,
			       reflog_expiry_cleanup_fn cleanup_fn,
			       void *policy_cb_data)
{
	struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
	struct debug_reflog_expiry_should_prune prune = {
		.prepare = prepare_fn,
		.cleanup = cleanup_fn,
		.should_prune = should_prune_fn,
		.cb_data = policy_cb_data,
	};
	int res = drefs->refs->be->reflog_expire(drefs->refs, refname,
						 flags, &debug_reflog_expiry_prepare,
						 &debug_reflog_expiry_should_prune_fn,
						 &debug_reflog_expiry_cleanup,
						 &prune);
	trace_printf_key(&trace_refs, "reflog_expire: %s: %d\n", refname, res);
	return res;
}

struct ref_storage_be refs_be_debug = {
	NULL,
	"debug",
	NULL,
	debug_init_db,
	debug_transaction_prepare,
	debug_transaction_finish,
	debug_transaction_abort,
	debug_initial_transaction_commit,

	debug_pack_refs,
	debug_create_symref,
	debug_delete_refs,
	debug_rename_ref,
	debug_copy_ref,

	debug_ref_iterator_begin,
	debug_read_raw_ref,

	debug_reflog_iterator_begin,
	debug_for_each_reflog_ent,
	debug_for_each_reflog_ent_reverse,
	debug_reflog_exists,
	debug_create_reflog,
	debug_delete_reflog,
	debug_reflog_expire,
};