Add callback data to for_each_ref() family.

This is a long overdue fix to the API for for_each_ref() family
of functions.  It allows the callers to specify a callback data
pointer, so that the caller does not have to use static
variables to communicate with the callback funciton.

The updated for_each_ref() family takes a function of type

	int (*fn)(const char *, const unsigned char *, void *)

and a void pointer as parameters, and calls the function with
the name of the ref and its SHA-1 with the caller-supplied void
pointer as parameters.

The commit updates two callers, builtin-name-rev.c and
builtin-pack-refs.c as an example.

Signed-off-by: Junio C Hamano <junkio@cox.net>
This commit is contained in:
Junio C Hamano
2006-09-20 21:47:42 -07:00
parent cc4c4f0ce2
commit cb5d709ff8
18 changed files with 79 additions and 71 deletions

View File

@ -346,7 +346,7 @@ static void sort_ref_range(int bottom, int top)
compare_ref_name);
}
static int append_ref(const char *refname, const unsigned char *sha1)
static int append_ref(const char *refname, const unsigned char *sha1, void *cb_data)
{
struct commit *commit = lookup_commit_reference_gently(sha1, 1);
int i;
@ -369,7 +369,7 @@ static int append_ref(const char *refname, const unsigned char *sha1)
return 0;
}
static int append_head_ref(const char *refname, const unsigned char *sha1)
static int append_head_ref(const char *refname, const unsigned char *sha1, void *cb_data)
{
unsigned char tmp[20];
int ofs = 11;
@ -380,14 +380,14 @@ static int append_head_ref(const char *refname, const unsigned char *sha1)
*/
if (get_sha1(refname + ofs, tmp) || hashcmp(tmp, sha1))
ofs = 5;
return append_ref(refname + ofs, sha1);
return append_ref(refname + ofs, sha1, cb_data);
}
static int append_tag_ref(const char *refname, const unsigned char *sha1)
static int append_tag_ref(const char *refname, const unsigned char *sha1, void *cb_data)
{
if (strncmp(refname, "refs/tags/", 10))
return 0;
return append_ref(refname + 5, sha1);
return append_ref(refname + 5, sha1, cb_data);
}
static const char *match_ref_pattern = NULL;
@ -401,7 +401,7 @@ static int count_slash(const char *s)
return cnt;
}
static int append_matching_ref(const char *refname, const unsigned char *sha1)
static int append_matching_ref(const char *refname, const unsigned char *sha1, void *cb_data)
{
/* we want to allow pattern hold/<asterisk> to show all
* branches under refs/heads/hold/, and v0.99.9? to show
@ -417,22 +417,22 @@ static int append_matching_ref(const char *refname, const unsigned char *sha1)
if (fnmatch(match_ref_pattern, tail, 0))
return 0;
if (!strncmp("refs/heads/", refname, 11))
return append_head_ref(refname, sha1);
return append_head_ref(refname, sha1, cb_data);
if (!strncmp("refs/tags/", refname, 10))
return append_tag_ref(refname, sha1);
return append_ref(refname, sha1);
return append_tag_ref(refname, sha1, cb_data);
return append_ref(refname, sha1, cb_data);
}
static void snarf_refs(int head, int tag)
{
if (head) {
int orig_cnt = ref_name_cnt;
for_each_ref(append_head_ref);
for_each_ref(append_head_ref, NULL);
sort_ref_range(orig_cnt, ref_name_cnt);
}
if (tag) {
int orig_cnt = ref_name_cnt;
for_each_ref(append_tag_ref);
for_each_ref(append_tag_ref, NULL);
sort_ref_range(orig_cnt, ref_name_cnt);
}
}
@ -487,7 +487,7 @@ static void append_one_rev(const char *av)
{
unsigned char revkey[20];
if (!get_sha1(av, revkey)) {
append_ref(av, revkey);
append_ref(av, revkey, NULL);
return;
}
if (strchr(av, '*') || strchr(av, '?') || strchr(av, '[')) {
@ -495,7 +495,7 @@ static void append_one_rev(const char *av)
int saved_matches = ref_name_cnt;
match_ref_pattern = av;
match_ref_slash = count_slash(av);
for_each_ref(append_matching_ref);
for_each_ref(append_matching_ref, NULL);
if (saved_matches == ref_name_cnt &&
ref_name_cnt < MAX_REVS)
error("no matching refs with %s", av);