sha1_array: let callbacks interrupt iteration

The callbacks for iterating a sha1_array must have a void return. This is unlike our usual for_each semantics, where a callback may interrupt iteration and have its value propagated. Let's switch it to the usual form, which will enable its use in more places (e.g., where we are replacing an existing iteration with a different data structure). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Sep 26, 2016 at 08:00 UTC 16ddcd403bdd74f47f3ae1a7e58a01e36e54a7d7
7 files changed +24 -12
Documentation/technical/api-sha1-array.txt
+6 -2
@@ -38,16 +38,20 @@ Functions
38 `sha1_array_for_each_unique`::
39 Efficiently iterate over each unique element of the list,
40 executing the callback function for each one. If the array is
41 - not sorted, this function has the side effect of sorting it.
41 + not sorted, this function has the side effect of sorting it. If
42 + the callback returns a non-zero value, the iteration ends
43 + immediately and the callback's return is propagated; otherwise,
44 + 0 is returned.
45
46 Examples
47 --------
48
49 -----------------------------------------
47 -void print_callback(const unsigned char sha1[20],
50 +int print_callback(const unsigned char sha1[20],
51 void *data)
52 {
53 printf("%s\n", sha1_to_hex(sha1));
54 + return 0; /* always continue */
55 }
56
57 void some_func(void)
builtin/cat-file.c
+2 -1
@@ -401,11 +401,12 @@ struct object_cb_data {
401 struct expand_data *expand;
402 };
403
404 -static void batch_object_cb(const unsigned char sha1[20], void *vdata)
404 +static int batch_object_cb(const unsigned char sha1[20], void *vdata)
405 {
406 struct object_cb_data *data = vdata;
407 hashcpy(data->expand->oid.hash, sha1);
408 batch_object_write(NULL, data->opt, data->expand);
409 + return 0;
410 }
411
412 static int batch_loose_object(const unsigned char *sha1,
builtin/receive-pack.c
+2 -1
@@ -268,9 +268,10 @@ static int show_ref_cb(const char *path_full, const struct object_id *oid,
268 return 0;
269 }
270
271 -static void show_one_alternate_sha1(const unsigned char sha1[20], void *unused)
271 +static int show_one_alternate_sha1(const unsigned char sha1[20], void *unused)
272 {
273 show_ref(".have", sha1);
274 + return 0;
275 }
276
277 static void collect_one_alternate_ref(const struct ref *ref, void *data)
sha1-array.c
+6 -2
@@ -42,7 +42,7 @@ void sha1_array_clear(struct sha1_array *array)
42 array->sorted = 0;
43 }
44
45 -void sha1_array_for_each_unique(struct sha1_array *array,
45 +int sha1_array_for_each_unique(struct sha1_array *array,
46 for_each_sha1_fn fn,
47 void *data)
48 {
@@ -52,8 +52,12 @@ void sha1_array_for_each_unique(struct sha1_array *array,
52 sha1_array_sort(array);
53
54 for (i = 0; i < array->nr; i++) {
55 + int ret;
56 if (i > 0 && !hashcmp(array->sha1[i], array->sha1[i-1]))
57 continue;
57 - fn(array->sha1[i], data);
58 + ret = fn(array->sha1[i], data);
59 + if (ret)
60 + return ret;
61 }
62 + return 0;
63 }
sha1-array.h
+4 -4
@@ -14,10 +14,10 @@ void sha1_array_append(struct sha1_array *array, const unsigned char *sha1);
14 int sha1_array_lookup(struct sha1_array *array, const unsigned char *sha1);
15 void sha1_array_clear(struct sha1_array *array);
16
17 -typedef void (*for_each_sha1_fn)(const unsigned char sha1[20],
18 - void *data);
19 -void sha1_array_for_each_unique(struct sha1_array *array,
20 - for_each_sha1_fn fn,
17 +typedef int (*for_each_sha1_fn)(const unsigned char sha1[20],
18 void *data);
19 +int sha1_array_for_each_unique(struct sha1_array *array,
20 + for_each_sha1_fn fn,
21 + void *data);
22
23 #endif /* SHA1_ARRAY_H */
submodule.c
+2 -1
@@ -728,9 +728,10 @@ void check_for_new_submodule_commits(unsigned char new_sha1[20])
728 sha1_array_append(&ref_tips_after_fetch, new_sha1);
729 }
730
731 -static void add_sha1_to_argv(const unsigned char sha1[20], void *data)
731 +static int add_sha1_to_argv(const unsigned char sha1[20], void *data)
732 {
733 argv_array_push(data, sha1_to_hex(sha1));
734 + return 0;
735 }
736
737 static void calculate_changed_submodule_paths(void)
t/helper/test-sha1-array.c
+2 -1
@@ -1,9 +1,10 @@
1 #include "cache.h"
2 #include "sha1-array.h"
3
4 -static void print_sha1(const unsigned char sha1[20], void *data)
4 +static int print_sha1(const unsigned char sha1[20], void *data)
5 {
6 puts(sha1_to_hex(sha1));
7 + return 0;
8 }
9
10 int cmd_main(int argc, const char **argv)