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

[PATCH v2 05/18] refs.c: extract function search_for_subdir()

From
mhagger@alum.mit.edu <mhagger@alum.mit.edu>
Date
Apr 26, 2012, 22:26 UTC
Message-ID
<1335479227-7877-6-git-send-email-mhagger@alum.mit.edu>
In-Reply-To
<1335479227-7877-1-git-send-email-mhagger@alum.mit.edu>
From: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 refs.c |   34 ++++++++++++++++++++++++----------
 1 file changed, 24 insertions(+), 10 deletions(-)
diff --git a/refs.c b/refs.c
index 01fcdc7..5e51c10 100644
--- a/refs.c
+++ b/refs.c
@@ -277,6 +277,27 @@ static struct ref_entry *search_ref_dir(struct ref_dir *dir, const char *refname
 }
 
 /*
+ * Search for a directory entry directly within dir (without
+ * recursing).  Sort dir if necessary.  subdirname must be a directory
+ * name (i.e., end in '/').  If mkdir is set, then create the
+ * directory if it is missing; otherwise, return NULL if the desired
+ * directory cannot be found.
+ */
+static struct ref_entry *search_for_subdir(struct ref_dir *dir,
+					   const char *subdirname, int mkdir)
+{
+	struct ref_entry *entry = search_ref_dir(dir, subdirname);
+	if (!entry) {
+		if (!mkdir)
+			return NULL;
+		entry = create_dir_entry(subdirname);
+		add_entry_to_dir(dir, entry);
+	}
+	assert(entry->flag & REF_DIR);
+	return entry;
+}
+
+/*
  * If refname is a reference name, find the ref_dir within the dir
  * tree that should hold refname.  If refname is a directory name
  * (i.e., ends in '/'), then return that ref_dir itself.  dir must
@@ -294,17 +315,10 @@ static struct ref_dir *find_containing_dir(struct ref_dir *dir,
 	for (slash = strchr(refname_copy, '/'); slash; slash = strchr(slash + 1, '/')) {
 		char tmp = slash[1];
 		slash[1] = '\0';
-		entry = search_ref_dir(dir, refname_copy);
-		if (!entry) {
-			if (!mkdir) {
-				dir = NULL;
-				break;
-			}
-			entry = create_dir_entry(refname_copy);
-			add_entry_to_dir(dir, entry);
-		}
+		entry = search_for_subdir(dir, refname_copy, mkdir);
 		slash[1] = tmp;
-		assert(entry->flag & REF_DIR);
+		if (!entry)
+			break;
 		dir = &entry->u.subdir;
 	}
 
-- 
1.7.10
Previous: mhagger@alum.mit.eduNext: Junio C Hamano
Message 6 of 26 in “Read loose references lazily”
  1. 00/18 Read loose references lazilymhagger@alum.mit.edu, Apr 26, 2012
  2. 01/18 get_ref_dir(): return early if directory cannot be readmhagger@alum.mit.edu, Apr 26, 2012
  3. 02/18 get_ref_dir(): use a strbuf to hold refnamemhagger@alum.mit.edu, Apr 26, 2012
  4. 03/18 get_ref_dir(): rename "base" parameter to "dirname"mhagger@alum.mit.edu, Apr 26, 2012
  5. 04/18 get_ref_dir(): require that the dirname argument ends in '/'mhagger@alum.mit.edu, Apr 26, 2012
  6. 05/18 refs.c: extract function search_for_subdir()mhagger@alum.mit.edu, Apr 26, 2012
  7. Junio C HamanoMay 3, 2012
  8. Junio C HamanoMay 3, 2012
  9. Michael HaggertyMay 4, 2012
  10. 06/18 get_ref_dir(): take the containing directory as argumentmhagger@alum.mit.edu, Apr 26, 2012
  11. 07/18 do_for_each_reflog(): return early on errormhagger@alum.mit.edu, Apr 26, 2012
  12. 08/18 do_for_each_reflog(): use a strbuf to hold logfile namemhagger@alum.mit.edu, Apr 26, 2012
  13. Junio C HamanoApr 26, 2012
  14. Michael HaggertyApr 27, 2012
  15. Junio C HamanoMay 2, 2012
  16. Michael HaggertyMay 3, 2012
  17. 09/18 do_for_each_reflog(): reuse strbuf across recursive function callsmhagger@alum.mit.edu, Apr 26, 2012
  18. 10/18 bisect: copy filename string obtained from git_path()mhagger@alum.mit.edu, Apr 26, 2012
  19. 11/18 find_containing_dir(): use strbuf in implementation of this functionmhagger@alum.mit.edu, Apr 26, 2012
  20. 12/18 refs: wrap top-level ref_dirs in ref_entriesmhagger@alum.mit.edu, Apr 26, 2012
  21. 13/18 read_loose_refs(): rename function from get_ref_dir()mhagger@alum.mit.edu, Apr 26, 2012
  22. 14/18 get_ref_dir(): add function for getting a ref_dir from a ref_entrymhagger@alum.mit.edu, Apr 26, 2012
  23. 15/18 search_for_subdir(): return (ref_dir *) instead of (ref_entry *)mhagger@alum.mit.edu, Apr 26, 2012
  24. 16/18 struct ref_dir: store a reference to the enclosing ref_cachemhagger@alum.mit.edu, Apr 26, 2012
  25. 17/18 read_loose_refs(): eliminate ref_cache argumentmhagger@alum.mit.edu, Apr 26, 2012
  26. 18/18 refs: read loose references lazilymhagger@alum.mit.edu, Apr 26, 2012

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.