git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 08/10] sha1_array: let callbacks interrupt iteration

From
Jeff King <peff@peff.net>
Date
Sep 26, 2016, 12:00 UTC
Message-ID
<20160926120029.zuyasa3hph372beb@sigill.intra.peff.net>
In-Reply-To
<20160926115720.p2yb22lcq37gboon@sigill.intra.peff.net>

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>
---
 Documentation/technical/api-sha1-array.txt | 8 ++++++--
 builtin/cat-file.c                         | 3 ++-
 builtin/receive-pack.c                     | 3 ++-
 sha1-array.c                               | 8 ++++++--
 sha1-array.h                               | 8 ++++----
 submodule.c                                | 3 ++-
 t/helper/test-sha1-array.c                 | 3 ++-
 7 files changed, 24 insertions(+), 12 deletions(-)
diff --git a/Documentation/technical/api-sha1-array.txt b/Documentation/technical/api-sha1-array.txt
index 3e75497..dcc5294 100644
--- a/Documentation/technical/api-sha1-array.txt
+++ b/Documentation/technical/api-sha1-array.txt
@@ -38,16 +38,20 @@ Functions
 `sha1_array_for_each_unique`::
 	Efficiently iterate over each unique element of the list,
 	executing the callback function for each one. If the array is
-	not sorted, this function has the side effect of sorting it.
+	not sorted, this function has the side effect of sorting it. If
+	the callback returns a non-zero value, the iteration ends
+	immediately and the callback's return is propagated; otherwise,
+	0 is returned.
 
 Examples
 --------
 
 -----------------------------------------
-void print_callback(const unsigned char sha1[20],
+int print_callback(const unsigned char sha1[20],
 		    void *data)
 {
 	printf("%s\n", sha1_to_hex(sha1));
+	return 0; /* always continue */
 }
 
 void some_func(void)
diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index 94e67eb..cca97a8 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -401,11 +401,12 @@ struct object_cb_data {
 	struct expand_data *expand;
 };
 
-static void batch_object_cb(const unsigned char sha1[20], void *vdata)
+static int batch_object_cb(const unsigned char sha1[20], void *vdata)
 {
 	struct object_cb_data *data = vdata;
 	hashcpy(data->expand->oid.hash, sha1);
 	batch_object_write(NULL, data->opt, data->expand);
+	return 0;
 }
 
 static int batch_loose_object(const unsigned char *sha1,
diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index 896b16f..f7cd180 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -268,9 +268,10 @@ static int show_ref_cb(const char *path_full, const struct object_id *oid,
 	return 0;
 }
 
-static void show_one_alternate_sha1(const unsigned char sha1[20], void *unused)
+static int show_one_alternate_sha1(const unsigned char sha1[20], void *unused)
 {
 	show_ref(".have", sha1);
+	return 0;
 }
 
 static void collect_one_alternate_ref(const struct ref *ref, void *data)
diff --git a/sha1-array.c b/sha1-array.c
index 6f4a224..af1d7d5 100644
--- a/sha1-array.c
+++ b/sha1-array.c
@@ -42,7 +42,7 @@ void sha1_array_clear(struct sha1_array *array)
 	array->sorted = 0;
 }
 
-void sha1_array_for_each_unique(struct sha1_array *array,
+int sha1_array_for_each_unique(struct sha1_array *array,
 				for_each_sha1_fn fn,
 				void *data)
 {
@@ -52,8 +52,12 @@ void sha1_array_for_each_unique(struct sha1_array *array,
 		sha1_array_sort(array);
 
 	for (i = 0; i < array->nr; i++) {
+		int ret;
 		if (i > 0 && !hashcmp(array->sha1[i], array->sha1[i-1]))
 			continue;
-		fn(array->sha1[i], data);
+		ret = fn(array->sha1[i], data);
+		if (ret)
+			return ret;
 	}
+	return 0;
 }
diff --git a/sha1-array.h b/sha1-array.h
index 72bb33b..b3230be 100644
--- a/sha1-array.h
+++ b/sha1-array.h
@@ -14,10 +14,10 @@ void sha1_array_append(struct sha1_array *array, const unsigned char *sha1);
 int sha1_array_lookup(struct sha1_array *array, const unsigned char *sha1);
 void sha1_array_clear(struct sha1_array *array);
 
-typedef void (*for_each_sha1_fn)(const unsigned char sha1[20],
-				 void *data);
-void sha1_array_for_each_unique(struct sha1_array *array,
-				for_each_sha1_fn fn,
+typedef int (*for_each_sha1_fn)(const unsigned char sha1[20],
 				void *data);
+int sha1_array_for_each_unique(struct sha1_array *array,
+			       for_each_sha1_fn fn,
+			       void *data);
 
 #endif /* SHA1_ARRAY_H */
diff --git a/submodule.c b/submodule.c
index 0ef2ff4..aba94dd 100644
--- a/submodule.c
+++ b/submodule.c
@@ -728,9 +728,10 @@ void check_for_new_submodule_commits(unsigned char new_sha1[20])
 	sha1_array_append(&ref_tips_after_fetch, new_sha1);
 }
 
-static void add_sha1_to_argv(const unsigned char sha1[20], void *data)
+static int add_sha1_to_argv(const unsigned char sha1[20], void *data)
 {
 	argv_array_push(data, sha1_to_hex(sha1));
+	return 0;
 }
 
 static void calculate_changed_submodule_paths(void)
diff --git a/t/helper/test-sha1-array.c b/t/helper/test-sha1-array.c
index 09f7790..f7a53c4 100644
--- a/t/helper/test-sha1-array.c
+++ b/t/helper/test-sha1-array.c
@@ -1,9 +1,10 @@
 #include "cache.h"
 #include "sha1-array.h"
 
-static void print_sha1(const unsigned char sha1[20], void *data)
+static int print_sha1(const unsigned char sha1[20], void *data)
 {
 	puts(sha1_to_hex(sha1));
+	return 0;
 }
 
 int cmd_main(int argc, const char **argv)
-- 
2.10.0.492.g14f803f
Previous: Jeff KingNext: Jeff King
Message 22 of 111 in “Changing the default for "core.abbrev"?”
  1. Linus TorvaldsSep 26, 2016
  2. Junio C HamanoSep 26, 2016
  3. Jeff KingSep 26, 2016
  4. Junio C HamanoSep 26, 2016
  5. 0/10 helping people resolve ambiguous sha1sJeff King, Sep 26, 2016
  6. 01/10 get_sha1: detect buggy calls with multiple disambiguatorsJeff King, Sep 26, 2016
  7. Junio C HamanoSep 26, 2016
  8. Jeff KingSep 26, 2016
  9. Junio C HamanoSep 26, 2016
  10. 02/10 get_sha1: avoid repeating ourselves via ONLY_TO_DIEJeff King, Sep 26, 2016
  11. 03/10 get_sha1: propagate flags to child functionsJeff King, Sep 26, 2016
  12. 04/10 get_short_sha1: peel tags when looking for treeishJeff King, Sep 26, 2016
  13. Jeff KingSep 26, 2016
  14. Junio C HamanoSep 26, 2016
  15. Jeff KingSep 26, 2016
  16. 05/10 get_short_sha1: refactor init of disambiguation codeJeff King, Sep 26, 2016
  17. 06/10 get_short_sha1: NUL-terminate hex prefixJeff King, Sep 26, 2016
  18. Junio C HamanoSep 26, 2016
  19. Jeff KingSep 26, 2016
  20. Junio C HamanoSep 26, 2016
  21. 07/10 get_short_sha1: mark ambiguity error for translationJeff King, Sep 26, 2016
  22. 08/10 sha1_array: let callbacks interrupt iterationJeff King, Sep 26, 2016
  23. 09/10 for_each_abbrev: drop duplicate objectsJeff King, Sep 26, 2016
  24. 10/10 get_short_sha1: list ambiguous objects on errorJeff King, Sep 26, 2016
  25. Linus TorvaldsSep 26, 2016
  26. Jacob KellerSep 27, 2016
  27. Jeff KingSep 27, 2016
  28. Kyle J. McKaySep 29, 2016
  29. Jeff KingSep 29, 2016
  30. Kyle J. McKaySep 29, 2016
  31. Jeff KingSep 29, 2016
  32. Junio C HamanoSep 26, 2016
  33. Jeff KingSep 26, 2016
  34. Junio C HamanoSep 26, 2016
  35. Kyle J. McKaySep 29, 2016
  36. Jeff KingSep 29, 2016
  37. Junio C HamanoSep 29, 2016
  38. Jacob KellerSep 30, 2016
  39. core.abbrev doc: document and test the abbreviation lengthÆvar Arnfjörð Bjarmason, Feb 4, 2019
  40. Junio C HamanoFeb 4, 2019
  41. Junio C HamanoFeb 4, 2019
  42. Ævar Arnfjörð BjarmasonFeb 4, 2019
  43. Jeff KingFeb 4, 2019
  44. Ævar Arnfjörð BjarmasonFeb 4, 2019
  45. Jeff KingFeb 6, 2019
  46. Ævar Arnfjörð BjarmasonFeb 6, 2019
  47. Matthieu MoySep 26, 2016
  48. Jeff KingSep 26, 2016
  49. Kyle J. McKaySep 29, 2016
  50. Christian CouderSep 26, 2016
  51. 0/4 raising core.abbrev default to 12 hexdigitsJunio C Hamano, Sep 28, 2016
  52. 3/4 worktree: honor configuration variablesJunio C Hamano, Sep 28, 2016
  53. 4/4 core.abbrev: raise the default abbreviation to 12 hexdigitsJunio C Hamano, Sep 28, 2016
  54. SZEDER GáborSep 29, 2016
  55. Lukas FleischerSep 29, 2016
  56. Jeff KingSep 29, 2016
  57. Jeff KingSep 29, 2016
  58. Matthieu MoySep 29, 2016
  59. SZEDER GáborSep 29, 2016
  60. Johannes SixtSep 29, 2016
  61. Junio C HamanoSep 29, 2016
  62. Linus TorvaldsSep 29, 2016
  63. Linus TorvaldsSep 29, 2016
  64. Linus TorvaldsSep 29, 2016
  65. Junio C HamanoSep 29, 2016
  66. Mike HommeySep 30, 2016
  67. Linus TorvaldsSep 30, 2016
  68. Ævar Arnfjörð BjarmasonSep 30, 2016
  69. Jeff KingSep 29, 2016
  70. Linus TorvaldsSep 29, 2016
  71. Junio C HamanoSep 29, 2016
  72. Linus TorvaldsSep 29, 2016
  73. Junio C HamanoSep 29, 2016
  74. Junio C HamanoSep 29, 2016
  75. Linus TorvaldsSep 30, 2016
  76. Linus TorvaldsSep 30, 2016
  77. Linus TorvaldsSep 30, 2016
  78. Linus TorvaldsSep 30, 2016
  79. Junio C HamanoSep 30, 2016
  80. Junio C HamanoSep 30, 2016
  81. Linus TorvaldsSep 30, 2016
  82. Linus TorvaldsSep 30, 2016
  83. Junio C HamanoSep 30, 2016
  84. Junio C HamanoSep 30, 2016
  85. Junio C HamanoSep 30, 2016
  86. Linus TorvaldsSep 30, 2016
  87. Junio C HamanoSep 30, 2016
  88. Linus TorvaldsSep 30, 2016
  89. Jeff KingSep 30, 2016
  90. Linus TorvaldsSep 30, 2016
  91. Jeff KingSep 30, 2016
  92. Linus TorvaldsSep 30, 2016
  93. Junio C HamanoSep 30, 2016
  94. Junio C HamanoSep 30, 2016
  95. Jeff KingSep 30, 2016
  96. Jeff KingSep 29, 2016
  97. 2/4 t13xx: do not assume system config is emptyJunio C Hamano, Sep 28, 2016
  98. Jeff KingSep 29, 2016
  99. Junio C HamanoSep 29, 2016
  100. Jeff KingSep 29, 2016
  101. Junio C HamanoSep 29, 2016
  102. Jeff KingSep 29, 2016
  103. Junio C HamanoSep 29, 2016
  104. Junio C HamanoSep 29, 2016
  105. Jeff KingSep 29, 2016
  106. Junio C HamanoSep 29, 2016
  107. Jeff KingSep 29, 2016
  108. 1/4 config: allow customizing /etc/gitconfig locationJunio C Hamano, Sep 28, 2016
  109. Jakub NarębskiSep 29, 2016
  110. Junio C HamanoSep 29, 2016
  111. Matthieu MoySep 29, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.