diff options
Diffstat (limited to 'refs')
-rw-r--r-- | refs/debug.c | 398 | ||||
-rw-r--r-- | refs/files-backend.c | 56 | ||||
-rw-r--r-- | refs/packed-backend.c | 1 | ||||
-rw-r--r-- | refs/refs-internal.h | 23 |
4 files changed, 446 insertions, 32 deletions
diff --git a/refs/debug.c b/refs/debug.c new file mode 100644 index 0000000000..922e64fa6a --- /dev/null +++ b/refs/debug.c @@ -0,0 +1,398 @@ + +#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) +{ + 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); + + 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\n", refname, res); + } + 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"; + 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, 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, + int force_create, struct strbuf *err) +{ + struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store; + int res = drefs->refs->be->create_reflog(drefs->refs, refname, + force_create, 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; +} + +static int debug_reflog_expire(struct ref_store *ref_store, const char *refname, + const struct object_id *oid, 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; + int res = drefs->refs->be->reflog_expire(drefs->refs, refname, oid, + flags, prepare_fn, + should_prune_fn, cleanup_fn, + policy_cb_data); + 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, +}; diff --git a/refs/files-backend.c b/refs/files-backend.c index 985631f33e..04e85e7002 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -39,13 +39,6 @@ #define REF_NEEDS_COMMIT (1 << 6) /* - * Used as a flag in ref_update::flags when we want to log a ref - * update but not actually perform it. This is used when a symbolic - * ref update is split up. - */ -#define REF_LOG_ONLY (1 << 7) - -/* * Used as a flag in ref_update::flags when the ref_update was via an * update to HEAD. */ @@ -67,7 +60,6 @@ struct files_ref_store { struct ref_store base; unsigned int store_flags; - char *gitdir; char *gitcommondir; struct ref_cache *loose; @@ -94,18 +86,17 @@ static struct ref_store *files_ref_store_create(const char *gitdir, struct ref_store *ref_store = (struct ref_store *)refs; struct strbuf sb = STRBUF_INIT; + ref_store->gitdir = xstrdup(gitdir); base_ref_store_init(ref_store, &refs_be_files); refs->store_flags = flags; - refs->gitdir = xstrdup(gitdir); get_common_dir_noenv(&sb, gitdir); refs->gitcommondir = strbuf_detach(&sb, NULL); strbuf_addf(&sb, "%s/packed-refs", refs->gitcommondir); refs->packed_ref_store = packed_ref_store_create(sb.buf, flags); strbuf_release(&sb); - chdir_notify_reparent("files-backend $GIT_DIR", - &refs->gitdir); + chdir_notify_reparent("files-backend $GIT_DIR", &refs->base.gitdir); chdir_notify_reparent("files-backend $GIT_COMMONDIR", &refs->gitcommondir); @@ -176,7 +167,7 @@ static void files_reflog_path(struct files_ref_store *refs, switch (ref_type(refname)) { case REF_TYPE_PER_WORKTREE: case REF_TYPE_PSEUDOREF: - strbuf_addf(sb, "%s/logs/%s", refs->gitdir, refname); + strbuf_addf(sb, "%s/logs/%s", refs->base.gitdir, refname); break; case REF_TYPE_OTHER_PSEUDOREF: case REF_TYPE_MAIN_PSEUDOREF: @@ -198,7 +189,7 @@ static void files_ref_path(struct files_ref_store *refs, switch (ref_type(refname)) { case REF_TYPE_PER_WORKTREE: case REF_TYPE_PSEUDOREF: - strbuf_addf(sb, "%s/%s", refs->gitdir, refname); + strbuf_addf(sb, "%s/%s", refs->base.gitdir, refname); break; case REF_TYPE_MAIN_PSEUDOREF: if (!skip_prefix(refname, "main-worktree/", &refname)) @@ -360,7 +351,6 @@ static int files_read_raw_ref(struct ref_store *ref_store, struct strbuf sb_path = STRBUF_INIT; const char *path; const char *buf; - const char *p; struct stat st; int fd; int ret = -1; @@ -465,6 +455,21 @@ stat_ref: close(fd); strbuf_rtrim(&sb_contents); buf = sb_contents.buf; + + ret = parse_loose_ref_contents(buf, oid, referent, type); + +out: + save_errno = errno; + strbuf_release(&sb_path); + strbuf_release(&sb_contents); + errno = save_errno; + return ret; +} + +int parse_loose_ref_contents(const char *buf, struct object_id *oid, + struct strbuf *referent, unsigned int *type) +{ + const char *p; if (skip_prefix(buf, "ref:", &buf)) { while (isspace(*buf)) buf++; @@ -472,29 +477,19 @@ stat_ref: strbuf_reset(referent); strbuf_addstr(referent, buf); *type |= REF_ISSYMREF; - ret = 0; - goto out; + return 0; } /* - * Please note that FETCH_HEAD has additional - * data after the sha. + * FETCH_HEAD has additional data after the sha. */ if (parse_oid_hex(buf, oid, &p) || (*p != '\0' && !isspace(*p))) { *type |= REF_ISBROKEN; errno = EINVAL; - goto out; + return -1; } - - ret = 0; - -out: - save_errno = errno; - strbuf_release(&sb_path); - strbuf_release(&sb_contents); - errno = save_errno; - return ret; + return 0; } static void unlock_ref(struct ref_lock *lock) @@ -2199,12 +2194,11 @@ static struct ref_iterator *files_reflog_iterator_begin(struct ref_store *ref_st files_downcast(ref_store, REF_STORE_READ, "reflog_iterator_begin"); - if (!strcmp(refs->gitdir, refs->gitcommondir)) { + if (!strcmp(refs->base.gitdir, refs->gitcommondir)) { return reflog_iterator_begin(ref_store, refs->gitcommondir); } else { return merge_ref_iterator_begin( - 0, - reflog_iterator_begin(ref_store, refs->gitdir), + 0, reflog_iterator_begin(ref_store, refs->base.gitdir), reflog_iterator_begin(ref_store, refs->gitcommondir), reflog_iterator_select, refs); } diff --git a/refs/packed-backend.c b/refs/packed-backend.c index 4458a0f69c..b912f2505f 100644 --- a/refs/packed-backend.c +++ b/refs/packed-backend.c @@ -200,6 +200,7 @@ struct ref_store *packed_ref_store_create(const char *path, struct ref_store *ref_store = (struct ref_store *)refs; base_ref_store_init(ref_store, &refs_be_packed); + ref_store->gitdir = xstrdup(path); refs->store_flags = store_flags; refs->path = xstrdup(path); diff --git a/refs/refs-internal.h b/refs/refs-internal.h index 357359a0be..467f4b3c93 100644 --- a/refs/refs-internal.h +++ b/refs/refs-internal.h @@ -32,6 +32,13 @@ struct ref_transaction; #define REF_HAVE_OLD (1 << 3) /* + * Used as a flag in ref_update::flags when we want to log a ref + * update but not actually perform it. This is used when a symbolic + * ref update is split up. + */ +#define REF_LOG_ONLY (1 << 7) + +/* * Return the length of time to retry acquiring a loose reference lock * before giving up, in milliseconds: */ @@ -667,18 +674,32 @@ extern struct ref_storage_be refs_be_packed; /* * A representation of the reference store for the main repository or * a submodule. The ref_store instances for submodules are kept in a - * linked list. + * hash map; see get_submodule_ref_store() for more info. */ struct ref_store { /* The backend describing this ref_store's storage scheme: */ const struct ref_storage_be *be; + + /* The gitdir that this ref_store applies to: */ + char *gitdir; }; /* + * Parse contents of a loose ref file. + */ +int parse_loose_ref_contents(const char *buf, struct object_id *oid, + struct strbuf *referent, unsigned int *type); + +/* * Fill in the generic part of refs and add it to our collection of * reference stores. */ void base_ref_store_init(struct ref_store *refs, const struct ref_storage_be *be); +/* + * Support GIT_TRACE_REFS by optionally wrapping the given ref_store instance. + */ +struct ref_store *maybe_debug_wrap_ref_store(const char *gitdir, struct ref_store *store); + #endif /* REFS_REFS_INTERNAL_H */ |