Skip to content

Commit 6cd0c85

Browse files
committed
Small refactor to make thing tidier
Also repurposed an unused function and deleted another one.
1 parent 41da4e1 commit 6cd0c85

File tree

4 files changed

+46
-43
lines changed

4 files changed

+46
-43
lines changed

src/diff_generate.c

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -709,18 +709,12 @@ static int maybe_modified_submodule(
709709
} else {
710710
if (!info->submodule_cache_initialized) {
711711
info->submodule_cache_initialized = true;
712-
/* Cache the submodule information to avoid having to parse it for every submodule. */
713-
if (git_strmap_new(&info->submodule_cache) == 0) {
714-
if (git_submodule__map(diff->base.repo, info->submodule_cache) < 0) {
715-
/* If the caching failed for whatever reason, bail out and clean up. */
716-
git_submodule *sm = NULL;
717-
git_strmap_foreach_value(info->submodule_cache, sm, {
718-
git_submodule_free(sm);
719-
});
720-
git_strmap_free(info->submodule_cache);
721-
info->submodule_cache = NULL;
722-
}
723-
}
712+
/*
713+
* Try to cache the submodule information to avoid having to parse it for
714+
* every submodule. It is okay if it fails, the cache will still be NULL
715+
* and the submodules will be attempted to be looked up individually.
716+
*/
717+
git_submodule_cache_init(&info->submodule_cache, diff->base.repo);
724718
}
725719
submodule_cache = info->submodule_cache;
726720
}
@@ -1284,13 +1278,8 @@ int git_diff__from_iterators(
12841278
*out = &diff->base;
12851279
else
12861280
git_diff_free(&diff->base);
1287-
if (info.submodule_cache) {
1288-
git_submodule *sm = NULL;
1289-
git_strmap_foreach_value(info.submodule_cache, sm, {
1290-
git_submodule_free(sm);
1291-
});
1292-
git_strmap_free(info.submodule_cache);
1293-
}
1281+
if (info.submodule_cache)
1282+
git_submodule_cache_free(info.submodule_cache);
12941283

12951284
return error;
12961285
}

src/repository.c

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3055,30 +3055,16 @@ int git_repository_set_ident(git_repository *repo, const char *name, const char
30553055

30563056
int git_repository_submodule_cache_all(git_repository *repo)
30573057
{
3058-
int error;
3059-
30603058
GIT_ASSERT_ARG(repo);
3061-
3062-
if ((error = git_strmap_new(&repo->submodule_cache)))
3063-
return error;
3064-
3065-
error = git_submodule__map(repo, repo->submodule_cache);
3066-
return error;
3059+
return git_submodule_cache_init(&repo->submodule_cache, repo);
30673060
}
30683061

30693062
int git_repository_submodule_cache_clear(git_repository *repo)
30703063
{
3071-
git_submodule *sm;
3072-
3064+
int error = 0;
30733065
GIT_ASSERT_ARG(repo);
30743066

3075-
if (repo->submodule_cache == NULL) {
3076-
return 0;
3077-
}
3078-
git_strmap_foreach_value(repo->submodule_cache, sm, {
3079-
git_submodule_free(sm);
3080-
});
3081-
git_strmap_free(repo->submodule_cache);
3082-
repo->submodule_cache = 0;
3067+
error = git_submodule_cache_free(repo->submodule_cache);
3068+
repo->submodule_cache = NULL;
30833069
return 0;
30843070
}

src/submodule.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,34 @@ static int load_submodule_names(git_strmap **out, git_repository *repo, git_conf
249249
return error;
250250
}
251251

252+
int git_submodule_cache_init(git_strmap **out, git_repository *repo)
253+
{
254+
int error = 0;
255+
git_strmap *cache = NULL;
256+
GIT_ASSERT_ARG(out);
257+
GIT_ASSERT_ARG(repo);
258+
if ((error = git_strmap_new(&cache)) < 0)
259+
return error;
260+
if ((error = git_submodule__map(repo, cache)) < 0) {
261+
git_submodule_cache_free(cache);
262+
return error;
263+
}
264+
*out = cache;
265+
return error;
266+
}
267+
268+
int git_submodule_cache_free(git_strmap *cache)
269+
{
270+
git_submodule *sm = NULL;
271+
if (cache == NULL)
272+
return 0;
273+
git_strmap_foreach_value(cache, sm, {
274+
git_submodule_free(sm);
275+
});
276+
git_strmap_free(cache);
277+
return 0;
278+
}
279+
252280
int git_submodule_lookup(
253281
git_submodule **out, /* NULL if user only wants to test existence */
254282
git_repository *repo,

src/submodule.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,6 @@ struct git_submodule {
101101
git_oid wd_oid;
102102
};
103103

104-
/* Force revalidation of submodule data cache (alloc as needed) */
105-
extern int git_submodule_cache_refresh(git_repository *repo);
106-
107-
/* Release all submodules */
108-
extern void git_submodule_cache_free(git_repository *repo);
109-
110104
/* Additional flags on top of public GIT_SUBMODULE_STATUS values */
111105
enum {
112106
GIT_SUBMODULE_STATUS__WD_SCANNED = (1u << 20),
@@ -122,6 +116,12 @@ enum {
122116
#define GIT_SUBMODULE_STATUS__CLEAR_INTERNAL(S) \
123117
((S) & ~(0xFFFFFFFFu << 20))
124118

119+
/* Initialize an external submodule cache for the provided repo. */
120+
extern int git_submodule_cache_init(git_strmap **out, git_repository *repo);
121+
122+
/* Release the resources of the submodule cache. */
123+
extern int git_submodule_cache_free(git_strmap *cache);
124+
125125
/* Submodule lookup with an explicit cache */
126126
extern int git_submodule__lookup_with_cache(
127127
git_submodule **out, git_repository *repo, const char *path, git_strmap *cache);

0 commit comments

Comments
 (0)