each_ref_fn: change to take an object_id parameter

Change typedef each_ref_fn to take a "const struct object_id *oid"
parameter instead of "const unsigned char *sha1".

To aid this transition, implement an adapter that can be used to wrap
old-style functions matching the old typedef, which is now called
"each_ref_sha1_fn"), and make such functions callable via the new
interface. This requires the old function and its cb_data to be
wrapped in a "struct each_ref_fn_sha1_adapter", and that object to be
used as the cb_data for an adapter function, each_ref_fn_adapter().

This is an enormous diff, but most of it consists of simple,
mechanical changes to the sites that call any of the "for_each_ref"
family of functions. Subsequent to this change, the call sites can be
rewritten one by one to use the new interface.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Michael Haggerty
2015-05-25 18:38:28 +00:00
committed by Junio C Hamano
parent 8353847e85
commit 2b2a5be394
36 changed files with 275 additions and 97 deletions

42
refs.c
View File

@ -722,7 +722,7 @@ static int do_one_ref(struct ref_entry *entry, void *cb_data)
/* Store the old value, in case this is a recursive call: */
old_current_ref = current_ref;
current_ref = entry;
retval = data->fn(entry->name + data->trim, entry->u.value.oid.hash,
retval = data->fn(entry->name + data->trim, &entry->u.value.oid,
entry->flag, data->cb_data);
current_ref = old_current_ref;
return retval;
@ -1756,13 +1756,14 @@ int ref_exists(const char *refname)
return !!resolve_ref_unsafe(refname, RESOLVE_REF_READING, sha1, NULL);
}
static int filter_refs(const char *refname, const unsigned char *sha1, int flags,
void *data)
static int filter_refs(const char *refname, const struct object_id *oid,
int flags, void *data)
{
struct ref_filter *filter = (struct ref_filter *)data;
if (wildmatch(filter->pattern, refname, 0, NULL))
return 0;
return filter->fn(refname, sha1, flags, filter->cb_data);
return filter->fn(refname, oid, flags, filter->cb_data);
}
enum peel_status {
@ -1897,7 +1898,7 @@ struct warn_if_dangling_data {
const char *msg_fmt;
};
static int warn_if_dangling_symref(const char *refname, const unsigned char *sha1,
static int warn_if_dangling_symref(const char *refname, const struct object_id *oid,
int flags, void *cb_data)
{
struct warn_if_dangling_data *d = cb_data;
@ -2027,18 +2028,18 @@ static int do_for_each_ref(struct ref_cache *refs, const char *base,
static int do_head_ref(const char *submodule, each_ref_fn fn, void *cb_data)
{
unsigned char sha1[20];
struct object_id oid;
int flag;
if (submodule) {
if (resolve_gitlink_ref(submodule, "HEAD", sha1) == 0)
return fn("HEAD", sha1, 0, cb_data);
if (resolve_gitlink_ref(submodule, "HEAD", oid.hash) == 0)
return fn("HEAD", &oid, 0, cb_data);
return 0;
}
if (!read_ref_full("HEAD", RESOLVE_REF_READING, sha1, &flag))
return fn("HEAD", sha1, flag, cb_data);
if (!read_ref_full("HEAD", RESOLVE_REF_READING, oid.hash, &flag))
return fn("HEAD", &oid, flag, cb_data);
return 0;
}
@ -2113,12 +2114,12 @@ int head_ref_namespaced(each_ref_fn fn, void *cb_data)
{
struct strbuf buf = STRBUF_INIT;
int ret = 0;
unsigned char sha1[20];
struct object_id oid;
int flag;
strbuf_addf(&buf, "%sHEAD", get_git_namespace());
if (!read_ref_full(buf.buf, RESOLVE_REF_READING, sha1, &flag))
ret = fn(buf.buf, sha1, flag, cb_data);
if (!read_ref_full(buf.buf, RESOLVE_REF_READING, oid.hash, &flag))
ret = fn(buf.buf, &oid, flag, cb_data);
strbuf_release(&buf);
return ret;
@ -2175,6 +2176,14 @@ int for_each_rawref(each_ref_fn fn, void *cb_data)
DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
}
int each_ref_fn_adapter(const char *refname,
const struct object_id *oid, int flags, void *cb_data)
{
struct each_ref_fn_sha1_adapter *cb = cb_data;
return cb->original_fn(refname, oid->hash, flags, cb->original_cb_data);
}
const char *prettify_refname(const char *name)
{
return name + (
@ -3616,11 +3625,12 @@ static int do_for_each_reflog(struct strbuf *name, each_ref_fn fn, void *cb_data
strbuf_addch(name, '/');
retval = do_for_each_reflog(name, fn, cb_data);
} else {
unsigned char sha1[20];
if (read_ref_full(name->buf, 0, sha1, NULL))
struct object_id oid;
if (read_ref_full(name->buf, 0, oid.hash, NULL))
retval = error("bad ref for %s", name->buf);
else
retval = fn(name->buf, sha1, 0, cb_data);
retval = fn(name->buf, &oid, 0, cb_data);
}
if (retval)
break;