Skip to content

Commit 16ddcd4

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0c99171 commit 16ddcd4

File tree

7 files changed

+24
-12
lines changed

7 files changed

+24
-12
lines changed

Documentation/technical/api-sha1-array.txt

+6-2
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,20 @@ Functions
3838
`sha1_array_for_each_unique`::
3939
Efficiently iterate over each unique element of the list,
4040
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.
4245

4346
Examples
4447
--------
4548

4649
-----------------------------------------
47-
void print_callback(const unsigned char sha1[20],
50+
int print_callback(const unsigned char sha1[20],
4851
void *data)
4952
{
5053
printf("%s\n", sha1_to_hex(sha1));
54+
return 0; /* always continue */
5155
}
5256

5357
void some_func(void)

builtin/cat-file.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -401,11 +401,12 @@ struct object_cb_data {
401401
struct expand_data *expand;
402402
};
403403

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)
405405
{
406406
struct object_cb_data *data = vdata;
407407
hashcpy(data->expand->oid.hash, sha1);
408408
batch_object_write(NULL, data->opt, data->expand);
409+
return 0;
409410
}
410411

411412
static int batch_loose_object(const unsigned char *sha1,

builtin/receive-pack.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,10 @@ static int show_ref_cb(const char *path_full, const struct object_id *oid,
268268
return 0;
269269
}
270270

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)
272272
{
273273
show_ref(".have", sha1);
274+
return 0;
274275
}
275276

276277
static void collect_one_alternate_ref(const struct ref *ref, void *data)

sha1-array.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ void sha1_array_clear(struct sha1_array *array)
4242
array->sorted = 0;
4343
}
4444

45-
void sha1_array_for_each_unique(struct sha1_array *array,
45+
int sha1_array_for_each_unique(struct sha1_array *array,
4646
for_each_sha1_fn fn,
4747
void *data)
4848
{
@@ -52,8 +52,12 @@ void sha1_array_for_each_unique(struct sha1_array *array,
5252
sha1_array_sort(array);
5353

5454
for (i = 0; i < array->nr; i++) {
55+
int ret;
5556
if (i > 0 && !hashcmp(array->sha1[i], array->sha1[i-1]))
5657
continue;
57-
fn(array->sha1[i], data);
58+
ret = fn(array->sha1[i], data);
59+
if (ret)
60+
return ret;
5861
}
62+
return 0;
5963
}

sha1-array.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ void sha1_array_append(struct sha1_array *array, const unsigned char *sha1);
1414
int sha1_array_lookup(struct sha1_array *array, const unsigned char *sha1);
1515
void sha1_array_clear(struct sha1_array *array);
1616

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],
2118
void *data);
19+
int sha1_array_for_each_unique(struct sha1_array *array,
20+
for_each_sha1_fn fn,
21+
void *data);
2222

2323
#endif /* SHA1_ARRAY_H */

submodule.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -728,9 +728,10 @@ void check_for_new_submodule_commits(unsigned char new_sha1[20])
728728
sha1_array_append(&ref_tips_after_fetch, new_sha1);
729729
}
730730

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)
732732
{
733733
argv_array_push(data, sha1_to_hex(sha1));
734+
return 0;
734735
}
735736

736737
static void calculate_changed_submodule_paths(void)

t/helper/test-sha1-array.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#include "cache.h"
22
#include "sha1-array.h"
33

4-
static void print_sha1(const unsigned char sha1[20], void *data)
4+
static int print_sha1(const unsigned char sha1[20], void *data)
55
{
66
puts(sha1_to_hex(sha1));
7+
return 0;
78
}
89

910
int cmd_main(int argc, const char **argv)

0 commit comments

Comments
 (0)