get_ref_cache(): only create an instance if there is a submodule

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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Jun 18, 2016 at 06:15 UTC 2eed2780f0e36e4a9038d65d9125cde5d5e1d2c6
1 file changed +22 -11
refs/files-backend.c
+22 -11
@@ -954,15 +954,26 @@ static struct ref_cache *lookup_ref_cache(const char *submodule)
954
955 /*
956 * Return a pointer to a ref_cache for the specified submodule. For
957 - * the main repository, use submodule==NULL. The returned structure
958 - * will be allocated and initialized but not necessarily populated; it
959 - * should not be freed.
957 + * the main repository, use submodule==NULL; such a call cannot fail.
958 + * For a submodule, the submodule must exist and be a nonbare
959 + * repository, otherwise return NULL.
960 + *
961 + * The returned structure will be allocated and initialized but not
962 + * necessarily populated; it should not be freed.
963 */
964 static struct ref_cache *get_ref_cache(const char *submodule)
965 {
966 struct ref_cache *refs = lookup_ref_cache(submodule);
964 - if (!refs)
965 - refs = create_ref_cache(submodule);
967 +
968 + if (!refs) {
969 + struct strbuf submodule_sb = STRBUF_INIT;
970 +
971 + strbuf_addstr(&submodule_sb, submodule);
972 + if (is_nonbare_repository_dir(&submodule_sb))
973 + refs = create_ref_cache(submodule);
974 + strbuf_release(&submodule_sb);
975 + }
976 +
977 return refs;
978 }
979
@@ -1341,13 +1352,10 @@ int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sh
1352 return -1;
1353
1354 strbuf_add(&submodule, path, len);
1344 - refs = lookup_ref_cache(submodule.buf);
1355 + refs = get_ref_cache(submodule.buf);
1356 if (!refs) {
1346 - if (!is_nonbare_repository_dir(&submodule)) {
1347 - strbuf_release(&submodule);
1348 - return -1;
1349 - }
1350 - refs = create_ref_cache(submodule.buf);
1357 + strbuf_release(&submodule);
1358 + return -1;
1359 }
1360 strbuf_release(&submodule);
1361
@@ -1885,6 +1893,9 @@ int do_for_each_ref(const char *submodule, const char *prefix,
1893 struct ref_cache *refs;
1894
1895 refs = get_ref_cache(submodule);
1896 + if (!refs)
1897 + return 0;
1898 +
1899 data.prefix = prefix;
1900 data.trim = trim;
1901 data.flags = flags;