summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: cc4c4f0)
raw | patch | inline | side by side (parent: cc4c4f0)
author | Junio C Hamano <junkio@cox.net> | |
Thu, 21 Sep 2006 04:47:42 +0000 (21:47 -0700) | ||
committer | Junio C Hamano <junkio@cox.net> | |
Thu, 21 Sep 2006 04:47:42 +0000 (21:47 -0700) |
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>
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>
18 files changed:
builtin-name-rev.c | patch | blob | history | |
builtin-pack-refs.c | patch | blob | history | |
builtin-prune.c | patch | blob | history | |
builtin-push.c | patch | blob | history | |
builtin-rev-parse.c | patch | blob | history | |
builtin-show-branch.c | patch | blob | history | |
describe.c | patch | blob | history | |
fetch-pack.c | patch | blob | history | |
fetch.c | patch | blob | history | |
fsck-objects.c | patch | blob | history | |
http-push.c | patch | blob | history | |
receive-pack.c | patch | blob | history | |
refs.c | patch | blob | history | |
refs.h | patch | blob | history | |
revision.c | patch | blob | history | |
send-pack.c | patch | blob | history | |
server-info.c | patch | blob | history | |
upload-pack.c | patch | blob | history |
diff --git a/builtin-name-rev.c b/builtin-name-rev.c
index 52886b69b068eca899bea574c5d9790395eeb006..9e3e537f38ce18cdfbf64a54852351e4e7c258bf 100644 (file)
--- a/builtin-name-rev.c
+++ b/builtin-name-rev.c
}
}
-static int tags_only;
-
-static int name_ref(const char *path, const unsigned char *sha1)
+static int name_ref(const char *path, const unsigned char *sha1, void *cb_data)
{
struct object *o = parse_object(sha1);
+ int tags_only = *(int*)cb_data;
int deref = 0;
if (tags_only && strncmp(path, "refs/tags/", 10))
{
struct object_array revs = { 0, 0, NULL };
int as_is = 0, all = 0, transform_stdin = 0;
+ int tags_only = 0;
git_config(git_default_config);
add_object_array((struct object *)commit, *argv, &revs);
}
- for_each_ref(name_ref);
+ for_each_ref(name_ref, &tags_only);
if (transform_stdin) {
char buffer[2048];
diff --git a/builtin-pack-refs.c b/builtin-pack-refs.c
index 0f5d827c25ad08cac46a765468ea0a2336c1bd01..b3d5470cc09ad35f7baa79bc618c8dda3be2f324 100644 (file)
--- a/builtin-pack-refs.c
+++ b/builtin-pack-refs.c
#include "cache.h"
#include "refs.h"
-static FILE *refs_file;
static const char *result_path, *lock_path;
static void remove_lock_file(void)
unlink(lock_path);
}
-static int handle_one_ref(const char *path, const unsigned char *sha1)
+static int handle_one_ref(const char *path, const unsigned char *sha1, void *cb_data)
{
+ FILE *refs_file = cb_data;
+
fprintf(refs_file, "%s %s\n", sha1_to_hex(sha1), path);
return 0;
}
int cmd_pack_refs(int argc, const char **argv, const char *prefix)
{
int fd;
+ FILE *refs_file;
result_path = xstrdup(git_path("packed-refs"));
lock_path = xstrdup(mkpath("%s.lock", result_path));
refs_file = fdopen(fd, "w");
if (!refs_file)
die("unable to create ref-pack file structure (%s)", strerror(errno));
- for_each_ref(handle_one_ref);
+ for_each_ref(handle_one_ref, refs_file);
fsync(fd);
fclose(refs_file);
if (rename(lock_path, result_path) < 0)
diff --git a/builtin-prune.c b/builtin-prune.c
index 6228c7907b183fb686c9f4cc54347c3dc16f3ec4..e21c29baecfc38bee60253364efc1b173efc280d 100644 (file)
--- a/builtin-prune.c
+++ b/builtin-prune.c
}
}
-static int add_one_ref(const char *path, const unsigned char *sha1)
+static int add_one_ref(const char *path, const unsigned char *sha1, void *cb_data)
{
struct object *object = parse_object(sha1);
if (!object)
revs.tree_objects = 1;
/* Add all external refs */
- for_each_ref(add_one_ref);
+ for_each_ref(add_one_ref, NULL);
/* Add all refs from the index file */
add_cache_refs();
diff --git a/builtin-push.c b/builtin-push.c
index c43f2566d519ddb238ca70065ba901881dce372b..88fc8e2a3b66bb7b251a1055c040c6c7ca8d2be2 100644 (file)
--- a/builtin-push.c
+++ b/builtin-push.c
refspec_nr = nr;
}
-static int expand_one_ref(const char *ref, const unsigned char *sha1)
+static int expand_one_ref(const char *ref, const unsigned char *sha1, void *cb_data)
{
/* Ignore the "refs/" at the beginning of the refname */
ref += 5;
}
if (!tags)
return;
- for_each_ref(expand_one_ref);
+ for_each_ref(expand_one_ref, NULL);
}
static void set_refspecs(const char **refs, int nr)
diff --git a/builtin-rev-parse.c b/builtin-rev-parse.c
index fd3ccc8546455f60f56ceb59dbe98fb367a86d2a..c7712748bc034ba7ef39bc3b993093bea012d084 100644 (file)
--- a/builtin-rev-parse.c
+++ b/builtin-rev-parse.c
}
}
-static int show_reference(const char *refname, const unsigned char *sha1)
+static int show_reference(const char *refname, const unsigned char *sha1, void *cb_data)
{
show_rev(NORMAL, sha1, refname);
return 0;
continue;
}
if (!strcmp(arg, "--all")) {
- for_each_ref(show_reference);
+ for_each_ref(show_reference, NULL);
continue;
}
if (!strcmp(arg, "--branches")) {
- for_each_branch_ref(show_reference);
+ for_each_branch_ref(show_reference, NULL);
continue;
}
if (!strcmp(arg, "--tags")) {
- for_each_tag_ref(show_reference);
+ for_each_tag_ref(show_reference, NULL);
continue;
}
if (!strcmp(arg, "--remotes")) {
- for_each_remote_ref(show_reference);
+ for_each_remote_ref(show_reference, NULL);
continue;
}
if (!strcmp(arg, "--show-prefix")) {
diff --git a/builtin-show-branch.c b/builtin-show-branch.c
index 4d8db0c10255e7b8d89d75d4218ff5f7b5e805e3..b3548ae36fbc55ae6c3875978df2b2e96142a2bd 100644 (file)
--- a/builtin-show-branch.c
+++ b/builtin-show-branch.c
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;
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;
*/
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;
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);
}
}
{
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, '[')) {
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);
diff --git a/describe.c b/describe.c
index ab192f83ae27c5e0bf3f2a5f61684d0dc66deb32..ea0f2ce55bf7e20109f43e41908a580f7fdb6eac 100644 (file)
--- a/describe.c
+++ b/describe.c
names = ++idx;
}
-static int get_name(const char *path, const unsigned char *sha1)
+static int get_name(const char *path, const unsigned char *sha1, void *cb_data)
{
struct commit *commit = lookup_commit_reference_gently(sha1, 1);
struct object *object;
if (!initialized) {
initialized = 1;
- for_each_ref(get_name);
+ for_each_ref(get_name, NULL);
qsort(name_array, names, sizeof(*name_array), compare_names);
}
diff --git a/fetch-pack.c b/fetch-pack.c
index e8708aa802b8e09d8044bb99dbccb0fecdb14481..6264ea1af96c18f381063a4910507e69718cc99f 100644 (file)
--- a/fetch-pack.c
+++ b/fetch-pack.c
}
}
-static int rev_list_insert_ref(const char *path, const unsigned char *sha1)
+static int rev_list_insert_ref(const char *path, const unsigned char *sha1, void *cb_data)
{
struct object *o = deref_tag(parse_object(sha1), path, 0);
unsigned in_vain = 0;
int got_continue = 0;
- for_each_ref(rev_list_insert_ref);
+ for_each_ref(rev_list_insert_ref, NULL);
fetching = 0;
for ( ; refs ; refs = refs->next) {
static struct commit_list *complete;
-static int mark_complete(const char *path, const unsigned char *sha1)
+static int mark_complete(const char *path, const unsigned char *sha1, void *cb_data)
{
struct object *o = parse_object(sha1);
}
}
- for_each_ref(mark_complete);
+ for_each_ref(mark_complete, NULL);
if (cutoff)
mark_recent_complete_commits(cutoff);
index 34df8d37d7dc92f8b652c160c49f4ace6e44e19c..36d1e7668e15bb79dba428034f6cab55acceddd8 100644 (file)
--- a/fetch.c
+++ b/fetch.c
return -1;
}
-static int mark_complete(const char *path, const unsigned char *sha1)
+static int mark_complete(const char *path, const unsigned char *sha1, void *cb_data)
{
struct commit *commit = lookup_commit_reference_gently(sha1, 1);
if (commit) {
}
if (!get_recover)
- for_each_ref(mark_complete);
+ for_each_ref(mark_complete, NULL);
for (i = 0; i < targets; i++) {
if (interpret_target(target[i], &sha1[20 * i])) {
diff --git a/fsck-objects.c b/fsck-objects.c
index 456c17e2f6c2d226248156065b1a47dad487c402..bb0c94e9d3193d19fc687e2b8612a8af55fc55dc 100644 (file)
--- a/fsck-objects.c
+++ b/fsck-objects.c
static int default_refs;
-static int fsck_handle_ref(const char *refname, const unsigned char *sha1)
+static int fsck_handle_ref(const char *refname, const unsigned char *sha1, void *cb_data)
{
struct object *obj;
static void get_default_heads(void)
{
- for_each_ref(fsck_handle_ref);
+ for_each_ref(fsck_handle_ref, NULL);
/*
* Not having any default heads isn't really fatal, but
diff --git a/http-push.c b/http-push.c
index 670ff007beb8a5ba3043045a0b31399cd108c310..460c9bec100824abd2255d86d2b2255e97474826 100644 (file)
--- a/http-push.c
+++ b/http-push.c
static struct ref *local_refs, **local_tail;
static struct ref *remote_refs, **remote_tail;
-static int one_local_ref(const char *refname, const unsigned char *sha1)
+static int one_local_ref(const char *refname, const unsigned char *sha1, void *cb_data)
{
struct ref *ref;
int len = strlen(refname) + 1;
static void get_local_heads(void)
{
local_tail = &local_refs;
- for_each_ref(one_local_ref);
+ for_each_ref(one_local_ref, NULL);
}
static void get_dav_remote_heads(void)
diff --git a/receive-pack.c b/receive-pack.c
index 78f75da5ca99bd5457fd4e0f6e3af2737afc335a..7abc9210c5b316e58a097539a2724ef351091644 100644 (file)
--- a/receive-pack.c
+++ b/receive-pack.c
static char capabilities[] = "report-status";
static int capabilities_sent;
-static int show_ref(const char *path, const unsigned char *sha1)
+static int show_ref(const char *path, const unsigned char *sha1, void *cb_data)
{
if (capabilities_sent)
packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
static void write_head_info(void)
{
- for_each_ref(show_ref);
+ for_each_ref(show_ref, NULL);
if (!capabilities_sent)
- show_ref("capabilities^{}", null_sha1);
+ show_ref("capabilities^{}", null_sha1, NULL);
}
index 7bd36e4f633968c1ae24a0c3bc1c054b000c9191..85564f0dc70ffd298daf3a9f2bb84801b6564911 100644 (file)
--- a/refs.c
+++ b/refs.c
return -1;
}
-static int do_for_each_ref(const char *base, int (*fn)(const char *path, const unsigned char *sha1), int trim)
+static int do_for_each_ref(const char *base, each_ref_fn fn, int trim, void *cb_data)
{
int retval;
struct ref_list *packed = get_packed_refs();
@@ -303,7 +303,7 @@ static int do_for_each_ref(const char *base, int (*fn)(const char *path, const u
error("%s does not point to a valid object!", entry->name);
continue;
}
- retval = fn(entry->name + trim, entry->sha1);
+ retval = fn(entry->name + trim, entry->sha1, cb_data);
if (retval)
return retval;
}
@@ -311,7 +311,7 @@ static int do_for_each_ref(const char *base, int (*fn)(const char *path, const u
packed = packed ? packed : loose;
while (packed) {
if (!strncmp(base, packed->name, trim)) {
- retval = fn(packed->name + trim, packed->sha1);
+ retval = fn(packed->name + trim, packed->sha1, cb_data);
if (retval)
return retval;
}
@@ -320,34 +320,39 @@ static int do_for_each_ref(const char *base, int (*fn)(const char *path, const u
return 0;
}
-int head_ref(int (*fn)(const char *path, const unsigned char *sha1))
+int head_ref(each_ref_fn fn, void *cb_data)
{
unsigned char sha1[20];
if (!read_ref("HEAD", sha1))
- return fn("HEAD", sha1);
+ return fn("HEAD", sha1, cb_data);
return 0;
}
-int for_each_ref(int (*fn)(const char *path, const unsigned char *sha1))
+int for_each_ref(each_ref_fn fn, void *cb_data)
{
- return do_for_each_ref("refs/", fn, 0);
+ return do_for_each_ref("refs/", fn, 0, cb_data);
}
-int for_each_tag_ref(int (*fn)(const char *path, const unsigned char *sha1))
+int for_each_tag_ref(each_ref_fn fn, void *cb_data)
{
- return do_for_each_ref("refs/tags/", fn, 10);
+ return do_for_each_ref("refs/tags/", fn, 10, cb_data);
}
-int for_each_branch_ref(int (*fn)(const char *path, const unsigned char *sha1))
+int for_each_branch_ref(each_ref_fn fn, void *cb_data)
{
- return do_for_each_ref("refs/heads/", fn, 11);
+ return do_for_each_ref("refs/heads/", fn, 11, cb_data);
}
-int for_each_remote_ref(int (*fn)(const char *path, const unsigned char *sha1))
+int for_each_remote_ref(each_ref_fn fn, void *cb_data)
{
- return do_for_each_ref("refs/remotes/", fn, 13);
+ return do_for_each_ref("refs/remotes/", fn, 13, cb_data);
}
+/* NEEDSWORK: This is only used by ssh-upload and it should go; the
+ * caller should do resolve_ref or read_ref like everybody else. Or
+ * maybe everybody else should use get_ref_sha1() instead of doing
+ * read_ref().
+ */
int get_ref_sha1(const char *ref, unsigned char *sha1)
{
if (check_ref_format(ref))
index af347e6b19988ff38660fc3457302d8bd29873ce..886c857105aefff7a743b796f160b2bc7bb0c178 100644 (file)
--- a/refs.h
+++ b/refs.h
* Calls the specified function for each ref file until it returns nonzero,
* and returns the value
*/
-extern int head_ref(int (*fn)(const char *path, const unsigned char *sha1));
-extern int for_each_ref(int (*fn)(const char *path, const unsigned char *sha1));
-extern int for_each_tag_ref(int (*fn)(const char *path, const unsigned char *sha1));
-extern int for_each_branch_ref(int (*fn)(const char *path, const unsigned char *sha1));
-extern int for_each_remote_ref(int (*fn)(const char *path, const unsigned char *sha1));
+typedef int each_ref_fn(const char *refname, const unsigned char *sha1, void *cb_data);
+extern int head_ref(each_ref_fn, void *);
+extern int for_each_ref(each_ref_fn, void *);
+extern int for_each_tag_ref(each_ref_fn, void *);
+extern int for_each_branch_ref(each_ref_fn, void *);
+extern int for_each_remote_ref(each_ref_fn, void *);
/** Reads the refs file specified into sha1 **/
extern int get_ref_sha1(const char *ref, unsigned char *sha1);
diff --git a/revision.c b/revision.c
index 6a2539b623512917479be3e6601ee8b8bef4953d..0e84b8a0fbea67d8bc7fea37b94faa09ab964469 100644 (file)
--- a/revision.c
+++ b/revision.c
static int all_flags;
static struct rev_info *all_revs;
-static int handle_one_ref(const char *path, const unsigned char *sha1)
+static int handle_one_ref(const char *path, const unsigned char *sha1, void *cb_data)
{
struct object *object = get_reference(all_revs, path, sha1, all_flags);
add_pending_object(all_revs, object, "");
{
all_revs = revs;
all_flags = flags;
- for_each_ref(handle_one_ref);
+ for_each_ref(handle_one_ref, NULL);
}
static int add_parents_only(struct rev_info *revs, const char *arg, int flags)
diff --git a/send-pack.c b/send-pack.c
index 5bb123a37696384c5413dac128529d1c1f679940..ee1309313ff41e23b49380d87cd0077cc2e84a85 100644 (file)
--- a/send-pack.c
+++ b/send-pack.c
static struct ref *local_refs, **local_tail;
static struct ref *remote_refs, **remote_tail;
-static int one_local_ref(const char *refname, const unsigned char *sha1)
+static int one_local_ref(const char *refname, const unsigned char *sha1, void *cb_data)
{
struct ref *ref;
int len = strlen(refname) + 1;
static void get_local_heads(void)
{
local_tail = &local_refs;
- for_each_ref(one_local_ref);
+ for_each_ref(one_local_ref, NULL);
}
static int receive_status(int in)
diff --git a/server-info.c b/server-info.c
index 2fb8f571033918b870cde35979b377c98b9845b5..7667b412576644ee5f428658df6db32efc37f0c3 100644 (file)
--- a/server-info.c
+++ b/server-info.c
/* refs */
static FILE *info_ref_fp;
-static int add_info_ref(const char *path, const unsigned char *sha1)
+static int add_info_ref(const char *path, const unsigned char *sha1, void *cb_data)
{
struct object *o = parse_object(sha1);
info_ref_fp = fopen(path1, "w");
if (!info_ref_fp)
return error("unable to update %s", path0);
- for_each_ref(add_info_ref);
+ for_each_ref(add_info_ref, NULL);
fclose(info_ref_fp);
rename(path1, path0);
free(path0);
diff --git a/upload-pack.c b/upload-pack.c
index 189b239cc093ab1d585627a647c7c36bf65efe18..10237ebe548a134c52f77668bd74fa1039b2b739 100644 (file)
--- a/upload-pack.c
+++ b/upload-pack.c
}
}
-static int send_ref(const char *refname, const unsigned char *sha1)
+static int send_ref(const char *refname, const unsigned char *sha1, void *cb_data)
{
static const char *capabilities = "multi_ack thin-pack side-band side-band-64k";
struct object *o = parse_object(sha1);
static void upload_pack(void)
{
reset_timeout();
- head_ref(send_ref);
- for_each_ref(send_ref);
+ head_ref(send_ref, NULL);
+ for_each_ref(send_ref, NULL);
packet_flush(1);
receive_needs();
if (want_obj.nr) {