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

[PATCH v3 06/13] get_ref_cache(): only create an instance if there is a submodule

From
Michael Haggerty <mhagger@alum.mit.edu>
Date
Jun 18, 2016, 04:15 UTC
Message-ID
<1c274be0695c79785604e0534ecdcf402bedb6cc.1466222921.git.mhagger@alum.mit.edu>
In-Reply-To
<cover.1466222921.git.mhagger@alum.mit.edu>

If there is not a nonbare repository where a submodule is supposedly located, then don't instantiate a ref_cache for it.

The analogous check can be removed from resolve_gitlink_ref().
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 refs/files-backend.c | 33 ++++++++++++++++++++++-----------
 1 file changed, 22 insertions(+), 11 deletions(-)
diff --git a/refs/files-backend.c b/refs/files-backend.c
index e15f7ae..b563a7e 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -954,15 +954,26 @@ static struct ref_cache *lookup_ref_cache(const char *submodule)
 
 /*
  * Return a pointer to a ref_cache for the specified submodule. For
- * the main repository, use submodule==NULL. The returned structure
- * will be allocated and initialized but not necessarily populated; it
- * should not be freed.
+ * the main repository, use submodule==NULL; such a call cannot fail.
+ * For a submodule, the submodule must exist and be a nonbare
+ * repository, otherwise return NULL.
+ *
+ * The returned structure will be allocated and initialized but not
+ * necessarily populated; it should not be freed.
  */
 static struct ref_cache *get_ref_cache(const char *submodule)
 {
 	struct ref_cache *refs = lookup_ref_cache(submodule);
-	if (!refs)
-		refs = create_ref_cache(submodule);
+
+	if (!refs) {
+		struct strbuf submodule_sb = STRBUF_INIT;
+
+		strbuf_addstr(&submodule_sb, submodule);
+		if (is_nonbare_repository_dir(&submodule_sb))
+			refs = create_ref_cache(submodule);
+		strbuf_release(&submodule_sb);
+	}
+
 	return refs;
 }
 
@@ -1341,13 +1352,10 @@ int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sh
 		return -1;
 
 	strbuf_add(&submodule, path, len);
-	refs = lookup_ref_cache(submodule.buf);
+	refs = get_ref_cache(submodule.buf);
 	if (!refs) {
-		if (!is_nonbare_repository_dir(&submodule)) {
-			strbuf_release(&submodule);
-			return -1;
-		}
-		refs = create_ref_cache(submodule.buf);
+		strbuf_release(&submodule);
+		return -1;
 	}
 	strbuf_release(&submodule);
 
@@ -1885,6 +1893,9 @@ int do_for_each_ref(const char *submodule, const char *prefix,
 	struct ref_cache *refs;
 
 	refs = get_ref_cache(submodule);
+	if (!refs)
+		return 0;
+
 	data.prefix = prefix;
 	data.trim = trim;
 	data.flags = flags;
-- 
2.8.1
Previous: Michael HaggertyNext: Michael Haggerty
Message 5 of 14 in “Reference iterators”
  1. 00/13 Reference iteratorsMichael Haggerty, Jun 18, 2016
  2. 01/13 refs: remove unnecessary "extern" keywordsMichael Haggerty, Jun 18, 2016
  3. 02/13 do_for_each_ref(): move docstring to the header fileMichael Haggerty, Jun 18, 2016
  4. 04/13 delete_refs(): add a flags argumentMichael Haggerty, Jun 18, 2016
  5. 06/13 get_ref_cache(): only create an instance if there is a submoduleMichael Haggerty, Jun 18, 2016
  6. 07/13 entry_resolves_to_object(): rename function from ref_resolves_to_object()Michael Haggerty, Jun 18, 2016
  7. 08/13 ref_resolves_to_object(): new functionMichael Haggerty, Jun 18, 2016
  8. 05/13 remote rm: handle symbolic refs correctlyMichael Haggerty, Jun 18, 2016
  9. 09/13 refs: introduce an iterator interfaceMichael Haggerty, Jun 18, 2016
  10. 10/13 do_for_each_ref(): reimplement using reference iterationMichael Haggerty, Jun 18, 2016
  11. 13/13 for_each_reflog(): reimplement using iteratorsMichael Haggerty, Jun 18, 2016
  12. 03/13 refs: use name "prefix" consistentlyMichael Haggerty, Jun 18, 2016
  13. 11/13 for_each_reflog(): don't abort for bad referencesMichael Haggerty, Jun 18, 2016
  14. 12/13 dir_iterator: new API for iterating over a directory treeMichael Haggerty, Jun 18, 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.