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

[PATCH v2 14/18] get_ref_dir(): add function for getting a ref_dir from a ref_entry

From
mhagger@alum.mit.edu <mhagger@alum.mit.edu>
Date
Apr 26, 2012, 22:27 UTC
Message-ID
<1335479227-7877-15-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>

Convert all accesses of a ref_dir within a ref_entry to use this function. This function will later be responsible for reading loose references from disk on demand.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 refs.c |   40 +++++++++++++++++++++++++---------------
 1 file changed, 25 insertions(+), 15 deletions(-)
diff --git a/refs.c b/refs.c
index 1d18ae6..2e71bfa 100644
--- a/refs.c
+++ b/refs.c
@@ -171,6 +171,12 @@ struct ref_entry {
 	char name[FLEX_ARRAY];
 };
 
+static struct ref_dir *get_ref_dir(struct ref_entry *entry)
+{
+	assert(entry->flag & REF_DIR);
+	return &entry->u.subdir;
+}
+
 static struct ref_entry *create_ref_entry(const char *refname,
 					  const unsigned char *sha1, int flag,
 					  int check_name)
@@ -195,7 +201,7 @@ static void clear_ref_dir(struct ref_dir *dir);
 static void free_ref_entry(struct ref_entry *entry)
 {
 	if (entry->flag & REF_DIR)
-		clear_ref_dir(&entry->u.subdir);
+		clear_ref_dir(get_ref_dir(entry));
 	free(entry);
 }
 
@@ -320,7 +326,7 @@ static struct ref_dir *find_containing_dir(struct ref_dir *dir,
 		entry = search_for_subdir(dir, dirname.buf, mkdir);
 		if (!entry)
 			break;
-		dir = &entry->u.subdir;
+		dir = get_ref_dir(entry);
 	}
 
 	strbuf_release(&dirname);
@@ -449,8 +455,9 @@ static int do_for_each_ref_in_dir(struct ref_dir *dir, int offset,
 		struct ref_entry *entry = dir->entries[i];
 		int retval;
 		if (entry->flag & REF_DIR) {
-			sort_ref_dir(&entry->u.subdir);
-			retval = do_for_each_ref_in_dir(&entry->u.subdir, 0,
+			struct ref_dir *subdir = get_ref_dir(entry);
+			sort_ref_dir(subdir);
+			retval = do_for_each_ref_in_dir(subdir, 0,
 							base, fn, trim, flags, cb_data);
 		} else {
 			retval = do_one_ref(base, fn, trim, flags, cb_data, entry);
@@ -495,10 +502,12 @@ static int do_for_each_ref_in_dirs(struct ref_dir *dir1,
 		if (cmp == 0) {
 			if ((e1->flag & REF_DIR) && (e2->flag & REF_DIR)) {
 				/* Both are directories; descend them in parallel. */
-				sort_ref_dir(&e1->u.subdir);
-				sort_ref_dir(&e2->u.subdir);
+				struct ref_dir *subdir1 = get_ref_dir(e1);
+				struct ref_dir *subdir2 = get_ref_dir(e2);
+				sort_ref_dir(subdir1);
+				sort_ref_dir(subdir2);
 				retval = do_for_each_ref_in_dirs(
-						&e1->u.subdir, &e2->u.subdir,
+						subdir1, subdir2,
 						base, fn, trim, flags, cb_data);
 				i1++;
 				i2++;
@@ -521,9 +530,10 @@ static int do_for_each_ref_in_dirs(struct ref_dir *dir1,
 				i2++;
 			}
 			if (e->flag & REF_DIR) {
-				sort_ref_dir(&e->u.subdir);
+				struct ref_dir *subdir = get_ref_dir(e);
+				sort_ref_dir(subdir);
 				retval = do_for_each_ref_in_dir(
-						&e->u.subdir, 0,
+						subdir, 0,
 						base, fn, trim, flags, cb_data);
 			} else {
 				retval = do_one_ref(base, fn, trim, flags, cb_data, e);
@@ -751,11 +761,11 @@ static struct ref_dir *get_packed_refs(struct ref_cache *refs)
 			packed_refs_file = git_path("packed-refs");
 		f = fopen(packed_refs_file, "r");
 		if (f) {
-			read_packed_refs(f, &refs->packed->u.subdir);
+			read_packed_refs(f, get_ref_dir(refs->packed));
 			fclose(f);
 		}
 	}
-	return &refs->packed->u.subdir;
+	return get_ref_dir(refs->packed);
 }
 
 void add_packed_ref(const char *refname, const unsigned char *sha1)
@@ -809,7 +819,7 @@ static void read_loose_refs(struct ref_cache *refs, const char *dirname,
 		} else if (S_ISDIR(st.st_mode)) {
 			strbuf_addch(&refname, '/');
 			read_loose_refs(refs, refname.buf,
-					&search_for_subdir(dir, refname.buf, 1)->u.subdir);
+					get_ref_dir(search_for_subdir(dir, refname.buf, 1)));
 		} else {
 			if (*refs->name) {
 				hashclr(sha1);
@@ -836,10 +846,10 @@ static struct ref_dir *get_loose_refs(struct ref_cache *refs)
 	if (!refs->loose) {
 		refs->loose = create_dir_entry("");
 		read_loose_refs(refs, "refs/",
-				&search_for_subdir(&refs->loose->u.subdir,
-						   "refs/", 1)->u.subdir);
+				get_ref_dir(search_for_subdir(get_ref_dir(refs->loose),
+							      "refs/", 1)));
 	}
-	return &refs->loose->u.subdir;
+	return get_ref_dir(refs->loose);
 }
 
 /* We allow "recursive" symbolic refs. Only within reason, though */
-- 
1.7.10
Previous: mhagger@alum.mit.eduNext: mhagger@alum.mit.edu
Message 22 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.