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

[PATCH v2 08/12] do_for_each_ref_in_arrays(): new function

From
mhagger@alum.mit.edu <mhagger@alum.mit.edu>
Date
Oct 28, 2011, 11:28 UTC
Message-ID
<1319801284-15625-9-git-send-email-mhagger@alum.mit.edu>
In-Reply-To
<1319801284-15625-1-git-send-email-mhagger@alum.mit.edu>
From: Michael Haggerty <mhagger@alum.mit.edu>
Extract function do_for_each_ref_in_arrays() from do_for_each_ref().
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 refs.c |   71 +++++++++++++++++++++++++++++++++++++--------------------------
 1 files changed, 42 insertions(+), 29 deletions(-)
diff --git a/refs.c b/refs.c
index 24a7a4d..ad7538a 100644
--- a/refs.c
+++ b/refs.c
@@ -713,45 +713,58 @@ static int do_for_each_ref_in_array(struct ref_array *array, int offset,
 	return 0;
 }
 
-static int do_for_each_ref(const char *submodule, const char *base, each_ref_fn fn,
-			   int trim, int flags, void *cb_data)
+static int do_for_each_ref_in_arrays(struct ref_array *array1,
+				     struct ref_array *array2,
+				     const char *base, each_ref_fn fn, int trim,
+				     int flags, void *cb_data)
 {
-	int retval = 0, p = 0, l = 0;
-	struct ref_cache *refs = get_ref_cache(submodule);
-	struct ref_array *packed = get_packed_refs(refs);
-	struct ref_array *loose = get_loose_refs(refs);
-
-	retval = do_for_each_ref_in_array(&extra_refs, 0,
-					  base, fn, trim, flags, cb_data);
-	if (retval)
-		goto end_each;
+	int retval;
+	int i1 = 0, i2 = 0;
 
-	while (p < packed->nr && l < loose->nr) {
-		struct ref_entry *entry;
-		int cmp = strcmp(packed->refs[p]->name, loose->refs[l]->name);
-		if (!cmp) {
-			p++;
+	while (1) {
+		struct ref_entry *e1, *e2;
+		int cmp;
+		if (i1 == array1->nr) {
+			return do_for_each_ref_in_array(array2, i2,
+							base, fn, trim, flags, cb_data);
+		}
+		if (i2 == array2->nr) {
+			return do_for_each_ref_in_array(array1, i1,
+							base, fn, trim, flags, cb_data);
+		}
+		e1 = array1->refs[i1];
+		e2 = array2->refs[i2];
+		cmp = strcmp(e1->name, e2->name);
+		if (cmp == 0) {
+			/* Two refs with the same name; ignore the one from array1. */
+			i1++;
 			continue;
 		}
-		if (cmp > 0) {
-			entry = loose->refs[l++];
+		if (cmp < 0) {
+			retval = do_one_ref(base, fn, trim, flags, cb_data, e1);
+			i1++;
 		} else {
-			entry = packed->refs[p++];
+			retval = do_one_ref(base, fn, trim, flags, cb_data, e2);
+			i2++;
 		}
-		retval = do_one_ref(base, fn, trim, flags, cb_data, entry);
 		if (retval)
-			goto end_each;
+			return retval;
 	}
+}
 
-	if (l < loose->nr) {
-		retval = do_for_each_ref_in_array(loose, l,
-						  base, fn, trim, flags, cb_data);
-	} else {
-		retval = do_for_each_ref_in_array(packed, p,
-						  base, fn, trim, flags, cb_data);
-	}
+static int do_for_each_ref(const char *submodule, const char *base, each_ref_fn fn,
+			   int trim, int flags, void *cb_data)
+{
+	int retval = 0;
+	struct ref_cache *refs = get_ref_cache(submodule);
+
+	retval = do_for_each_ref_in_array(&extra_refs, 0,
+					  base, fn, trim, flags, cb_data);
+	if (!retval)
+		retval = do_for_each_ref_in_arrays(get_packed_refs(refs),
+						   get_loose_refs(refs),
+						   base, fn, trim, flags, cb_data);
 
-end_each:
 	current_ref = NULL;
 	return retval;
 }
-- 
1.7.7
Previous: mhagger@alum.mit.eduNext: mhagger@alum.mit.edu
Message 9 of 13 in “Use refs API more consistently”
  1. 00/12 Use refs API more consistentlymhagger@alum.mit.edu, Oct 28, 2011
  2. 01/12 Rename another local variable name -> refnamemhagger@alum.mit.edu, Oct 28, 2011
  3. 02/12 repack_without_ref(): remove temporarymhagger@alum.mit.edu, Oct 28, 2011
  4. 03/12 parse_ref_line(): add a check that the refname is properly formattedmhagger@alum.mit.edu, Oct 28, 2011
  5. 04/12 create_ref_entry(): extract function from add_ref()mhagger@alum.mit.edu, Oct 28, 2011
  6. 05/12 add_ref(): take a (struct ref_entry *) parametermhagger@alum.mit.edu, Oct 28, 2011
  7. 06/12 do_for_each_ref(): correctly terminate while processesing extra_refsmhagger@alum.mit.edu, Oct 28, 2011
  8. 07/12 do_for_each_ref_in_array(): new functionmhagger@alum.mit.edu, Oct 28, 2011
  9. 08/12 do_for_each_ref_in_arrays(): new functionmhagger@alum.mit.edu, Oct 28, 2011
  10. 09/12 repack_without_ref(): reimplement using do_for_each_ref_in_array()mhagger@alum.mit.edu, Oct 28, 2011
  11. 10/12 names_conflict(): new function, extracted from is_refname_available()mhagger@alum.mit.edu, Oct 28, 2011
  12. 11/12 names_conflict(): simplify implementationmhagger@alum.mit.edu, Oct 28, 2011
  13. 12/12 is_refname_available(): reimplement using do_for_each_ref_in_array()mhagger@alum.mit.edu, Oct 28, 2011

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.