@matheusbsbs / matheus-bagliano / commits / cb5d709ff8
Fork 0

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>

Junio C Hamano committed Sep 20, 2006 at 21:47 UTC cb5d709ff8a4bae19d57a470ba2b137c25938a44
18 files changed +79 -71
builtin-name-rev.c
+4 -4
@@ -75,11 +75,10 @@ copy_data:
75 }
76 }
77
78 -static int tags_only;
79 -
80 -static int name_ref(const char *path, const unsigned char *sha1)
78 +static int name_ref(const char *path, const unsigned char *sha1, void *cb_data)
79 {
80 struct object *o = parse_object(sha1);
81 + int tags_only = *(int*)cb_data;
82 int deref = 0;
83
84 if (tags_only && strncmp(path, "refs/tags/", 10))
@@ -131,6 +130,7 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
130 {
131 struct object_array revs = { 0, 0, NULL };
132 int as_is = 0, all = 0, transform_stdin = 0;
133 + int tags_only = 0;
134
135 git_config(git_default_config);
136
@@ -186,7 +186,7 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
186 add_object_array((struct object *)commit, *argv, &revs);
187 }
188
189 - for_each_ref(name_ref);
189 + for_each_ref(name_ref, &tags_only);
190
191 if (transform_stdin) {
192 char buffer[2048];
builtin-pack-refs.c
+5 -3
@@ -1,7 +1,6 @@
1 #include "cache.h"
2 #include "refs.h"
3
4 -static FILE *refs_file;
4 static const char *result_path, *lock_path;
5
6 static void remove_lock_file(void)
@@ -10,8 +9,10 @@ static void remove_lock_file(void)
9 unlink(lock_path);
10 }
11
13 -static int handle_one_ref(const char *path, const unsigned char *sha1)
12 +static int handle_one_ref(const char *path, const unsigned char *sha1, void *cb_data)
13 {
14 + FILE *refs_file = cb_data;
15 +
16 fprintf(refs_file, "%s %s\n", sha1_to_hex(sha1), path);
17 return 0;
18 }
@@ -19,6 +20,7 @@ static int handle_one_ref(const char *path, const unsigned char *sha1)
20 int cmd_pack_refs(int argc, const char **argv, const char *prefix)
21 {
22 int fd;
23 + FILE *refs_file;
24
25 result_path = xstrdup(git_path("packed-refs"));
26 lock_path = xstrdup(mkpath("%s.lock", result_path));
@@ -31,7 +33,7 @@ int cmd_pack_refs(int argc, const char **argv, const char *prefix)
33 refs_file = fdopen(fd, "w");
34 if (!refs_file)
35 die("unable to create ref-pack file structure (%s)", strerror(errno));
34 - for_each_ref(handle_one_ref);
36 + for_each_ref(handle_one_ref, refs_file);
37 fsync(fd);
38 fclose(refs_file);
39 if (rename(lock_path, result_path) < 0)
builtin-prune.c
+2 -2
@@ -174,7 +174,7 @@ static void walk_commit_list(struct rev_info *revs)
174 }
175 }
176
177 -static int add_one_ref(const char *path, const unsigned char *sha1)
177 +static int add_one_ref(const char *path, const unsigned char *sha1, void *cb_data)
178 {
179 struct object *object = parse_object(sha1);
180 if (!object)
@@ -240,7 +240,7 @@ int cmd_prune(int argc, const char **argv, const char *prefix)
240 revs.tree_objects = 1;
241
242 /* Add all external refs */
243 - for_each_ref(add_one_ref);
243 + for_each_ref(add_one_ref, NULL);
244
245 /* Add all refs from the index file */
246 add_cache_refs();
builtin-push.c
+2 -2
@@ -27,7 +27,7 @@ static void add_refspec(const char *ref)
27 refspec_nr = nr;
28 }
29
30 -static int expand_one_ref(const char *ref, const unsigned char *sha1)
30 +static int expand_one_ref(const char *ref, const unsigned char *sha1, void *cb_data)
31 {
32 /* Ignore the "refs/" at the beginning of the refname */
33 ref += 5;
@@ -51,7 +51,7 @@ static void expand_refspecs(void)
51 }
52 if (!tags)
53 return;
54 - for_each_ref(expand_one_ref);
54 + for_each_ref(expand_one_ref, NULL);
55 }
56
57 static void set_refspecs(const char **refs, int nr)
builtin-rev-parse.c
+5 -5
@@ -137,7 +137,7 @@ static void show_default(void)
137 }
138 }
139
140 -static int show_reference(const char *refname, const unsigned char *sha1)
140 +static int show_reference(const char *refname, const unsigned char *sha1, void *cb_data)
141 {
142 show_rev(NORMAL, sha1, refname);
143 return 0;
@@ -299,19 +299,19 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
299 continue;
300 }
301 if (!strcmp(arg, "--all")) {
302 - for_each_ref(show_reference);
302 + for_each_ref(show_reference, NULL);
303 continue;
304 }
305 if (!strcmp(arg, "--branches")) {
306 - for_each_branch_ref(show_reference);
306 + for_each_branch_ref(show_reference, NULL);
307 continue;
308 }
309 if (!strcmp(arg, "--tags")) {
310 - for_each_tag_ref(show_reference);
310 + for_each_tag_ref(show_reference, NULL);
311 continue;
312 }
313 if (!strcmp(arg, "--remotes")) {
314 - for_each_remote_ref(show_reference);
314 + for_each_remote_ref(show_reference, NULL);
315 continue;
316 }
317 if (!strcmp(arg, "--show-prefix")) {
builtin-show-branch.c
+13 -13
@@ -346,7 +346,7 @@ static void sort_ref_range(int bottom, int top)
346 compare_ref_name);
347 }
348
349 -static int append_ref(const char *refname, const unsigned char *sha1)
349 +static int append_ref(const char *refname, const unsigned char *sha1, void *cb_data)
350 {
351 struct commit *commit = lookup_commit_reference_gently(sha1, 1);
352 int i;
@@ -369,7 +369,7 @@ static int append_ref(const char *refname, const unsigned char *sha1)
369 return 0;
370 }
371
372 -static int append_head_ref(const char *refname, const unsigned char *sha1)
372 +static int append_head_ref(const char *refname, const unsigned char *sha1, void *cb_data)
373 {
374 unsigned char tmp[20];
375 int ofs = 11;
@@ -380,14 +380,14 @@ static int append_head_ref(const char *refname, const unsigned char *sha1)
380 */
381 if (get_sha1(refname + ofs, tmp) || hashcmp(tmp, sha1))
382 ofs = 5;
383 - return append_ref(refname + ofs, sha1);
383 + return append_ref(refname + ofs, sha1, cb_data);
384 }
385
386 -static int append_tag_ref(const char *refname, const unsigned char *sha1)
386 +static int append_tag_ref(const char *refname, const unsigned char *sha1, void *cb_data)
387 {
388 if (strncmp(refname, "refs/tags/", 10))
389 return 0;
390 - return append_ref(refname + 5, sha1);
390 + return append_ref(refname + 5, sha1, cb_data);
391 }
392
393 static const char *match_ref_pattern = NULL;
@@ -401,7 +401,7 @@ static int count_slash(const char *s)
401 return cnt;
402 }
403
404 -static int append_matching_ref(const char *refname, const unsigned char *sha1)
404 +static int append_matching_ref(const char *refname, const unsigned char *sha1, void *cb_data)
405 {
406 /* we want to allow pattern hold/<asterisk> to show all
407 * 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)
417 if (fnmatch(match_ref_pattern, tail, 0))
418 return 0;
419 if (!strncmp("refs/heads/", refname, 11))
420 - return append_head_ref(refname, sha1);
420 + return append_head_ref(refname, sha1, cb_data);
421 if (!strncmp("refs/tags/", refname, 10))
422 - return append_tag_ref(refname, sha1);
423 - return append_ref(refname, sha1);
422 + return append_tag_ref(refname, sha1, cb_data);
423 + return append_ref(refname, sha1, cb_data);
424 }
425
426 static void snarf_refs(int head, int tag)
427 {
428 if (head) {
429 int orig_cnt = ref_name_cnt;
430 - for_each_ref(append_head_ref);
430 + for_each_ref(append_head_ref, NULL);
431 sort_ref_range(orig_cnt, ref_name_cnt);
432 }
433 if (tag) {
434 int orig_cnt = ref_name_cnt;
435 - for_each_ref(append_tag_ref);
435 + for_each_ref(append_tag_ref, NULL);
436 sort_ref_range(orig_cnt, ref_name_cnt);
437 }
438 }
@@ -487,7 +487,7 @@ static void append_one_rev(const char *av)
487 {
488 unsigned char revkey[20];
489 if (!get_sha1(av, revkey)) {
490 - append_ref(av, revkey);
490 + append_ref(av, revkey, NULL);
491 return;
492 }
493 if (strchr(av, '*') || strchr(av, '?') || strchr(av, '[')) {
@@ -495,7 +495,7 @@ static void append_one_rev(const char *av)
495 int saved_matches = ref_name_cnt;
496 match_ref_pattern = av;
497 match_ref_slash = count_slash(av);
498 - for_each_ref(append_matching_ref);
498 + for_each_ref(append_matching_ref, NULL);
499 if (saved_matches == ref_name_cnt &&
500 ref_name_cnt < MAX_REVS)
501 error("no matching refs with %s", av);
describe.c
+2 -2
@@ -53,7 +53,7 @@ static void add_to_known_names(const char *path,
53 names = ++idx;
54 }
55
56 -static int get_name(const char *path, const unsigned char *sha1)
56 +static int get_name(const char *path, const unsigned char *sha1, void *cb_data)
57 {
58 struct commit *commit = lookup_commit_reference_gently(sha1, 1);
59 struct object *object;
@@ -113,7 +113,7 @@ static void describe(const char *arg, int last_one)
113
114 if (!initialized) {
115 initialized = 1;
116 - for_each_ref(get_name);
116 + for_each_ref(get_name, NULL);
117 qsort(name_array, names, sizeof(*name_array), compare_names);
118 }
119
fetch-pack.c
+4 -4
@@ -42,7 +42,7 @@ static void rev_list_push(struct commit *commit, int mark)
42 }
43 }
44
45 -static int rev_list_insert_ref(const char *path, const unsigned char *sha1)
45 +static int rev_list_insert_ref(const char *path, const unsigned char *sha1, void *cb_data)
46 {
47 struct object *o = deref_tag(parse_object(sha1), path, 0);
48
@@ -143,7 +143,7 @@ static int find_common(int fd[2], unsigned char *result_sha1,
143 unsigned in_vain = 0;
144 int got_continue = 0;
145
146 - for_each_ref(rev_list_insert_ref);
146 + for_each_ref(rev_list_insert_ref, NULL);
147
148 fetching = 0;
149 for ( ; refs ; refs = refs->next) {
@@ -253,7 +253,7 @@ done:
253
254 static struct commit_list *complete;
255
256 -static int mark_complete(const char *path, const unsigned char *sha1)
256 +static int mark_complete(const char *path, const unsigned char *sha1, void *cb_data)
257 {
258 struct object *o = parse_object(sha1);
259
@@ -365,7 +365,7 @@ static int everything_local(struct ref **refs, int nr_match, char **match)
365 }
366 }
367
368 - for_each_ref(mark_complete);
368 + for_each_ref(mark_complete, NULL);
369 if (cutoff)
370 mark_recent_complete_commits(cutoff);
371
fetch.c
+2 -2
@@ -201,7 +201,7 @@ static int interpret_target(char *target, unsigned char *sha1)
201 return -1;
202 }
203
204 -static int mark_complete(const char *path, const unsigned char *sha1)
204 +static int mark_complete(const char *path, const unsigned char *sha1, void *cb_data)
205 {
206 struct commit *commit = lookup_commit_reference_gently(sha1, 1);
207 if (commit) {
@@ -274,7 +274,7 @@ int pull(int targets, char **target, const char **write_ref,
274 }
275
276 if (!get_recover)
277 - for_each_ref(mark_complete);
277 + for_each_ref(mark_complete, NULL);
278
279 for (i = 0; i < targets; i++) {
280 if (interpret_target(target[i], &sha1[20 * i])) {
fsck-objects.c
+2 -2
@@ -402,7 +402,7 @@ static void fsck_dir(int i, char *path)
402
403 static int default_refs;
404
405 -static int fsck_handle_ref(const char *refname, const unsigned char *sha1)
405 +static int fsck_handle_ref(const char *refname, const unsigned char *sha1, void *cb_data)
406 {
407 struct object *obj;
408
@@ -424,7 +424,7 @@ static int fsck_handle_ref(const char *refname, const unsigned char *sha1)
424
425 static void get_default_heads(void)
426 {
427 - for_each_ref(fsck_handle_ref);
427 + for_each_ref(fsck_handle_ref, NULL);
428
429 /*
430 * Not having any default heads isn't really fatal, but
http-push.c
+2 -2
@@ -1864,7 +1864,7 @@ static int update_remote(unsigned char *sha1, struct remote_lock *lock)
1864 static struct ref *local_refs, **local_tail;
1865 static struct ref *remote_refs, **remote_tail;
1866
1867 -static int one_local_ref(const char *refname, const unsigned char *sha1)
1867 +static int one_local_ref(const char *refname, const unsigned char *sha1, void *cb_data)
1868 {
1869 struct ref *ref;
1870 int len = strlen(refname) + 1;
@@ -1913,7 +1913,7 @@ static void one_remote_ref(char *refname)
1913 static void get_local_heads(void)
1914 {
1915 local_tail = &local_refs;
1916 - for_each_ref(one_local_ref);
1916 + for_each_ref(one_local_ref, NULL);
1917 }
1918
1919 static void get_dav_remote_heads(void)
receive-pack.c
+3 -3
@@ -12,7 +12,7 @@ static int report_status;
12 static char capabilities[] = "report-status";
13 static int capabilities_sent;
14
15 -static int show_ref(const char *path, const unsigned char *sha1)
15 +static int show_ref(const char *path, const unsigned char *sha1, void *cb_data)
16 {
17 if (capabilities_sent)
18 packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
@@ -25,9 +25,9 @@ static int show_ref(const char *path, const unsigned char *sha1)
25
26 static void write_head_info(void)
27 {
28 - for_each_ref(show_ref);
28 + for_each_ref(show_ref, NULL);
29 if (!capabilities_sent)
30 - show_ref("capabilities^{}", null_sha1);
30 + show_ref("capabilities^{}", null_sha1, NULL);
31
32 }
33
refs.c
+18 -13
@@ -275,7 +275,7 @@ int read_ref(const char *ref, unsigned char *sha1)
275 return -1;
276 }
277
278 -static int do_for_each_ref(const char *base, int (*fn)(const char *path, const unsigned char *sha1), int trim)
278 +static int do_for_each_ref(const char *base, each_ref_fn fn, int trim, void *cb_data)
279 {
280 int retval;
281 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
303 error("%s does not point to a valid object!", entry->name);
304 continue;
305 }
306 - retval = fn(entry->name + trim, entry->sha1);
306 + retval = fn(entry->name + trim, entry->sha1, cb_data);
307 if (retval)
308 return retval;
309 }
@@ -311,7 +311,7 @@ static int do_for_each_ref(const char *base, int (*fn)(const char *path, const u
311 packed = packed ? packed : loose;
312 while (packed) {
313 if (!strncmp(base, packed->name, trim)) {
314 - retval = fn(packed->name + trim, packed->sha1);
314 + retval = fn(packed->name + trim, packed->sha1, cb_data);
315 if (retval)
316 return retval;
317 }
@@ -320,34 +320,39 @@ static int do_for_each_ref(const char *base, int (*fn)(const char *path, const u
320 return 0;
321 }
322
323 -int head_ref(int (*fn)(const char *path, const unsigned char *sha1))
323 +int head_ref(each_ref_fn fn, void *cb_data)
324 {
325 unsigned char sha1[20];
326 if (!read_ref("HEAD", sha1))
327 - return fn("HEAD", sha1);
327 + return fn("HEAD", sha1, cb_data);
328 return 0;
329 }
330
331 -int for_each_ref(int (*fn)(const char *path, const unsigned char *sha1))
331 +int for_each_ref(each_ref_fn fn, void *cb_data)
332 {
333 - return do_for_each_ref("refs/", fn, 0);
333 + return do_for_each_ref("refs/", fn, 0, cb_data);
334 }
335
336 -int for_each_tag_ref(int (*fn)(const char *path, const unsigned char *sha1))
336 +int for_each_tag_ref(each_ref_fn fn, void *cb_data)
337 {
338 - return do_for_each_ref("refs/tags/", fn, 10);
338 + return do_for_each_ref("refs/tags/", fn, 10, cb_data);
339 }
340
341 -int for_each_branch_ref(int (*fn)(const char *path, const unsigned char *sha1))
341 +int for_each_branch_ref(each_ref_fn fn, void *cb_data)
342 {
343 - return do_for_each_ref("refs/heads/", fn, 11);
343 + return do_for_each_ref("refs/heads/", fn, 11, cb_data);
344 }
345
346 -int for_each_remote_ref(int (*fn)(const char *path, const unsigned char *sha1))
346 +int for_each_remote_ref(each_ref_fn fn, void *cb_data)
347 {
348 - return do_for_each_ref("refs/remotes/", fn, 13);
348 + return do_for_each_ref("refs/remotes/", fn, 13, cb_data);
349 }
350
351 +/* NEEDSWORK: This is only used by ssh-upload and it should go; the
352 + * caller should do resolve_ref or read_ref like everybody else. Or
353 + * maybe everybody else should use get_ref_sha1() instead of doing
354 + * read_ref().
355 + */
356 int get_ref_sha1(const char *ref, unsigned char *sha1)
357 {
358 if (check_ref_format(ref))
refs.h
+6 -5
@@ -14,11 +14,12 @@ struct ref_lock {
14 * Calls the specified function for each ref file until it returns nonzero,
15 * and returns the value
16 */
17 -extern int head_ref(int (*fn)(const char *path, const unsigned char *sha1));
18 -extern int for_each_ref(int (*fn)(const char *path, const unsigned char *sha1));
19 -extern int for_each_tag_ref(int (*fn)(const char *path, const unsigned char *sha1));
20 -extern int for_each_branch_ref(int (*fn)(const char *path, const unsigned char *sha1));
21 -extern int for_each_remote_ref(int (*fn)(const char *path, const unsigned char *sha1));
17 +typedef int each_ref_fn(const char *refname, const unsigned char *sha1, void *cb_data);
18 +extern int head_ref(each_ref_fn, void *);
19 +extern int for_each_ref(each_ref_fn, void *);
20 +extern int for_each_tag_ref(each_ref_fn, void *);
21 +extern int for_each_branch_ref(each_ref_fn, void *);
22 +extern int for_each_remote_ref(each_ref_fn, void *);
23
24 /** Reads the refs file specified into sha1 **/
25 extern int get_ref_sha1(const char *ref, unsigned char *sha1);
revision.c
+2 -2
@@ -466,7 +466,7 @@ static void limit_list(struct rev_info *revs)
466 static int all_flags;
467 static struct rev_info *all_revs;
468
469 -static int handle_one_ref(const char *path, const unsigned char *sha1)
469 +static int handle_one_ref(const char *path, const unsigned char *sha1, void *cb_data)
470 {
471 struct object *object = get_reference(all_revs, path, sha1, all_flags);
472 add_pending_object(all_revs, object, "");
@@ -477,7 +477,7 @@ static void handle_all(struct rev_info *revs, unsigned flags)
477 {
478 all_revs = revs;
479 all_flags = flags;
480 - for_each_ref(handle_one_ref);
480 + for_each_ref(handle_one_ref, NULL);
481 }
482
483 static int add_parents_only(struct rev_info *revs, const char *arg, int flags)
send-pack.c
+2 -2
@@ -215,7 +215,7 @@ static int ref_newer(const unsigned char *new_sha1,
215 static struct ref *local_refs, **local_tail;
216 static struct ref *remote_refs, **remote_tail;
217
218 -static int one_local_ref(const char *refname, const unsigned char *sha1)
218 +static int one_local_ref(const char *refname, const unsigned char *sha1, void *cb_data)
219 {
220 struct ref *ref;
221 int len = strlen(refname) + 1;
@@ -230,7 +230,7 @@ static int one_local_ref(const char *refname, const unsigned char *sha1)
230 static void get_local_heads(void)
231 {
232 local_tail = &local_refs;
233 - for_each_ref(one_local_ref);
233 + for_each_ref(one_local_ref, NULL);
234 }
235
236 static int receive_status(int in)
server-info.c
+2 -2
@@ -7,7 +7,7 @@
7 /* refs */
8 static FILE *info_ref_fp;
9
10 -static int add_info_ref(const char *path, const unsigned char *sha1)
10 +static int add_info_ref(const char *path, const unsigned char *sha1, void *cb_data)
11 {
12 struct object *o = parse_object(sha1);
13
@@ -34,7 +34,7 @@ static int update_info_refs(int force)
34 info_ref_fp = fopen(path1, "w");
35 if (!info_ref_fp)
36 return error("unable to update %s", path0);
37 - for_each_ref(add_info_ref);
37 + for_each_ref(add_info_ref, NULL);
38 fclose(info_ref_fp);
39 rename(path1, path0);
40 free(path0);
upload-pack.c
+3 -3
@@ -416,7 +416,7 @@ static void receive_needs(void)
416 }
417 }
418
419 -static int send_ref(const char *refname, const unsigned char *sha1)
419 +static int send_ref(const char *refname, const unsigned char *sha1, void *cb_data)
420 {
421 static const char *capabilities = "multi_ack thin-pack side-band side-band-64k";
422 struct object *o = parse_object(sha1);
@@ -444,8 +444,8 @@ static int send_ref(const char *refname, const unsigned char *sha1)
444 static void upload_pack(void)
445 {
446 reset_timeout();
447 - head_ref(send_ref);
448 - for_each_ref(send_ref);
447 + head_ref(send_ref, NULL);
448 + for_each_ref(send_ref, NULL);
449 packet_flush(1);
450 receive_needs();
451 if (want_obj.nr) {