diff options
Diffstat (limited to 'src/submodule.c')
| -rw-r--r-- | src/submodule.c | 219 |
1 files changed, 162 insertions, 57 deletions
diff --git a/src/submodule.c b/src/submodule.c index 9eaf77dae..769b092a0 100644 --- a/src/submodule.c +++ b/src/submodule.c @@ -77,12 +77,12 @@ __KHASH_IMPL( str, static kh_inline, const char *, void *, 1, str_hash_no_trailing_slash, str_equal_no_trailing_slash); -static int load_submodule_config(git_repository *repo); +static int load_submodule_config(git_repository *repo, bool reload); static git_config_backend *open_gitmodules(git_repository *, bool, const git_oid *); static int lookup_head_remote(git_buf *url, git_repository *repo); static int submodule_get(git_submodule **, git_repository *, const char *, const char *); static int submodule_load_from_config(const git_config_entry *, void *); -static int submodule_load_from_wd_lite(git_submodule *, const char *, void *); +static int submodule_load_from_wd_lite(git_submodule *); static int submodule_update_config(git_submodule *, const char *, const char *, bool, bool); static void submodule_get_index_status(unsigned int *, git_submodule *); static void submodule_get_wd_status(unsigned int *, git_submodule *, git_repository *, git_submodule_ignore_t); @@ -99,27 +99,55 @@ static int submodule_config_key_trunc_puts(git_buf *key, const char *suffix) return git_buf_puts(key, suffix); } +/* lookup submodule or return ENOTFOUND if it doesn't exist */ +static int submodule_lookup( + git_submodule **out, + git_strmap *cache, + const char *name, + const char *alternate) +{ + khiter_t pos; + + /* lock cache */ + + pos = git_strmap_lookup_index(cache, name); + + if (!git_strmap_valid_index(cache, pos) && alternate) + pos = git_strmap_lookup_index(cache, alternate); + + if (!git_strmap_valid_index(cache, pos)) { + /* unlock cache */ + return GIT_ENOTFOUND; /* don't set error - caller will cope */ + } + + if (out != NULL) { + git_submodule *sm = git_strmap_value_at(cache, pos); + GIT_REFCOUNT_INC(sm); + *out = sm; + } + + /* unlock cache */ + + return 0; +} + /* * PUBLIC APIS */ int git_submodule_lookup( - git_submodule **sm_ptr, /* NULL if user only wants to test existence */ + git_submodule **out, /* NULL if user only wants to test existence */ git_repository *repo, - const char *name) /* trailing slash is allowed */ + const char *name) /* trailing slash is allowed */ { int error; - khiter_t pos; assert(repo && name); - if ((error = load_submodule_config(repo)) < 0) + if ((error = load_submodule_config(repo, false)) < 0) return error; - pos = git_strmap_lookup_index(repo->submodules, name); - - if (!git_strmap_valid_index(repo->submodules, pos)) { - error = GIT_ENOTFOUND; + if ((error = submodule_lookup(out, repo->submodules, name, NULL)) < 0) { /* check if a plausible submodule exists at path */ if (git_repository_workdir(repo)) { @@ -137,14 +165,9 @@ int git_submodule_lookup( giterr_set(GITERR_SUBMODULE, (error == GIT_ENOTFOUND) ? "No submodule named '%s'" : "Submodule '%s' has not been added yet", name); - - return error; } - if (sm_ptr) - *sm_ptr = git_strmap_value_at(repo->submodules, pos); - - return 0; + return error; } int git_submodule_foreach( @@ -159,7 +182,7 @@ int git_submodule_foreach( assert(repo && callback); - if ((error = load_submodule_config(repo)) < 0) + if ((error = load_submodule_config(repo, true)) < 0) return error; git_strmap_foreach_value(repo->submodules, sm, { @@ -198,12 +221,15 @@ void git_submodule_config_free(git_repository *repo) if (smcfg == NULL) return; - git_strmap_foreach_value(smcfg, sm, { git_submodule_free(sm); }); + git_strmap_foreach_value(smcfg, sm, { + sm->repo = NULL; /* disconnect from repo */; + git_submodule_free(sm); + }); git_strmap_free(smcfg); } int git_submodule_add_setup( - git_submodule **submodule, + git_submodule **out, git_repository *repo, const char *url, const char *path, @@ -211,7 +237,7 @@ int git_submodule_add_setup( { int error = 0; git_config_backend *mods = NULL; - git_submodule *sm; + git_submodule *sm = NULL; git_buf name = GIT_BUF_INIT, real_url = GIT_BUF_INIT; git_repository_init_options initopt = GIT_REPOSITORY_INIT_OPTIONS_INIT; git_repository *subrepo = NULL; @@ -223,6 +249,7 @@ int git_submodule_add_setup( if (git_submodule_lookup(&sm, repo, path) < 0) giterr_clear(); else { + git_submodule_free(sm); giterr_set(GITERR_SUBMODULE, "Attempt to add a submodule that already exists"); return GIT_EEXISTS; @@ -307,12 +334,16 @@ int git_submodule_add_setup( /* add submodule to hash and "reload" it */ if (!(error = submodule_get(&sm, repo, path, NULL)) && - !(error = git_submodule_reload(sm))) + !(error = git_submodule_reload(sm, false))) error = git_submodule_init(sm, false); cleanup: - if (submodule != NULL) - *submodule = !error ? sm : NULL; + if (error && sm) { + git_submodule_free(sm); + sm = NULL; + } + if (out != NULL) + *out = sm; if (mods != NULL) git_config_file_free(mods); @@ -775,11 +806,60 @@ int git_submodule_open(git_repository **subrepo, git_submodule *sm) return git_submodule__open(subrepo, sm, false); } -int git_submodule_reload_all(git_repository *repo) +static void submodule_cache_remove_item( + git_strmap *cache, + const char *name, + git_submodule *expected, + bool free_after_remove) { + khiter_t pos; + git_submodule *found; + + if (!cache) + return; + + pos = git_strmap_lookup_index(cache, name); + + if (!git_strmap_valid_index(cache, pos)) + return; + + found = git_strmap_value_at(cache, pos); + + if (expected && found != expected) + return; + + git_strmap_set_value_at(cache, pos, NULL); + git_strmap_delete_at(cache, pos); + + if (free_after_remove) + git_submodule_free(found); +} + +int git_submodule_reload_all(git_repository *repo, int force) +{ + int error = 0; + git_submodule *sm; + + GIT_UNUSED(force); assert(repo); - git_submodule_config_free(repo); - return load_submodule_config(repo); + + if (repo->submodules) + git_strmap_foreach_value(repo->submodules, sm, { sm->flags = 0; }); + + error = load_submodule_config(repo, true); + + git_strmap_foreach_value(repo->submodules, sm, { + git_strmap *cache = repo->submodules; + + if ((sm->flags & GIT_SUBMODULE_STATUS__IN_FLAGS) == 0) { + submodule_cache_remove_item(cache, sm->name, sm, true); + + if (sm->path != sm->name) + submodule_cache_remove_item(cache, sm->path, sm, true); + } + }); + + return error; } static void submodule_update_from_index_entry( @@ -855,11 +935,14 @@ static int submodule_update_head(git_submodule *submodule) return 0; } -int git_submodule_reload(git_submodule *submodule) + +int git_submodule_reload(git_submodule *submodule, int force) { int error = 0; git_config_backend *mods; + GIT_UNUSED(force); + assert(submodule); /* refresh index data */ @@ -870,6 +953,10 @@ int git_submodule_reload(git_submodule *submodule) if ((error = submodule_update_head(submodule)) < 0) return error; + /* done if bare */ + if (git_repository_is_bare(submodule->repo)) + return error; + /* refresh config data */ mods = open_gitmodules(submodule->repo, false, NULL); if (mods != NULL) { @@ -893,11 +980,9 @@ int git_submodule_reload(git_submodule *submodule) } /* refresh wd data */ - submodule->flags = submodule->flags & - ~(GIT_SUBMODULE_STATUS_IN_WD | GIT_SUBMODULE_STATUS__WD_OID_VALID); + submodule->flags &= ~GIT_SUBMODULE_STATUS__ALL_WD_FLAGS; - return submodule_load_from_wd_lite( - submodule, submodule->path, submodule->repo); + return submodule_load_from_wd_lite(submodule); } static void submodule_copy_oid_maybe( @@ -1026,6 +1111,13 @@ static void submodule_release(git_submodule *sm) if (!sm) return; + if (sm->repo) { + git_strmap *cache = sm->repo->submodules; + submodule_cache_remove_item(cache, sm->name, sm, false); + if (sm->path != sm->name) + submodule_cache_remove_item(cache, sm->path, sm, false); + } + if (sm->path != sm->name) git__free(sm->path); git__free(sm->name); @@ -1042,17 +1134,15 @@ void git_submodule_free(git_submodule *sm) } static int submodule_get( - git_submodule **sm_ptr, + git_submodule **out, git_repository *repo, const char *name, const char *alternate) { + int error = 0; git_strmap *smcfg = repo->submodules; khiter_t pos; git_submodule *sm; - int error; - - assert(repo && name); pos = git_strmap_lookup_index(smcfg, name); @@ -1068,22 +1158,28 @@ static int submodule_get( */ pos = kh_put(str, smcfg, sm->name, &error); - if (error < 0) { - git_submodule_free(sm); - sm = NULL; - } else if (error == 0) { + if (error < 0) + goto done; + else if (error == 0) { git_submodule_free(sm); sm = git_strmap_value_at(smcfg, pos); } else { + error = 0; git_strmap_set_value_at(smcfg, pos, sm); } } else { sm = git_strmap_value_at(smcfg, pos); } - *sm_ptr = sm; +done: + if (error < 0) + git_submodule_free(sm); + else if (out) { + GIT_REFCOUNT_INC(sm); + *out = sm; + } - return (sm != NULL) ? 0 : -1; + return error; } static int submodule_config_error(const char *property, const char *value) @@ -1143,7 +1239,7 @@ static int submodule_load_from_config( const char *namestart, *property, *alternate = NULL; const char *key = entry->name, *value = entry->value, *path; git_buf name = GIT_BUF_INIT; - git_submodule *sm; + git_submodule *sm = NULL; int error = 0; if (git__prefixcmp(key, "submodule.") != 0) @@ -1230,8 +1326,8 @@ static int submodule_load_from_config( sm->update_default = sm->update; } else if (strcasecmp(property, "fetchRecurseSubmodules") == 0) { - if (git_submodule_parse_recurse(&sm->fetch_recurse, value) < 0) - return -1; + if ((error = git_submodule_parse_recurse(&sm->fetch_recurse, value)) < 0) + goto done; sm->fetch_recurse_default = sm->fetch_recurse; } else if (strcasecmp(property, "ignore") == 0) { @@ -1242,20 +1338,15 @@ static int submodule_load_from_config( /* ignore other unknown submodule properties */ done: + git_submodule_free(sm); /* offset refcount inc from submodule_get() */ git_buf_free(&name); return error; } -static int submodule_load_from_wd_lite( - git_submodule *sm, const char *name, void *payload) +static int submodule_load_from_wd_lite(git_submodule *sm) { git_buf path = GIT_BUF_INIT; - GIT_UNUSED(name); GIT_UNUSED(payload); - - if (git_repository_is_bare(sm->repo)) - return 0; - if (git_buf_joinpath(&path, git_repository_workdir(sm->repo), sm->path) < 0) return -1; @@ -1293,8 +1384,10 @@ static int load_submodule_config_from_index( else sm->flags |= GIT_SUBMODULE_STATUS__INDEX_NOT_SUBMODULE; } else if (S_ISGITLINK(entry->mode)) { - if (!submodule_get(&sm, repo, entry->path, NULL)) + if (!submodule_get(&sm, repo, entry->path, NULL)) { submodule_update_from_index_entry(sm, entry); + git_submodule_free(sm); + } } else if (strcmp(entry->path, GIT_MODULES_FILE) == 0) git_oid_cpy(gitmodules_oid, &entry->id); } @@ -1339,9 +1432,11 @@ static int load_submodule_config_from_head( else sm->flags |= GIT_SUBMODULE_STATUS__HEAD_NOT_SUBMODULE; } else if (S_ISGITLINK(entry->mode)) { - if (!submodule_get(&sm, repo, entry->path, NULL)) + if (!submodule_get(&sm, repo, entry->path, NULL)) { submodule_update_from_head_data( sm, entry->mode, &entry->id); + git_submodule_free(sm); + } } else if (strcmp(entry->path, GIT_MODULES_FILE) == 0 && git_oid_iszero(gitmodules_oid)) { git_oid_cpy(gitmodules_oid, &entry->id); @@ -1395,13 +1490,13 @@ static git_config_backend *open_gitmodules( return mods; } -static int load_submodule_config(git_repository *repo) +static int load_submodule_config(git_repository *repo, bool reload) { int error; git_oid gitmodules_oid; git_config_backend *mods = NULL; - if (repo->submodules) + if (!reload && repo->submodules) return 0; memset(&gitmodules_oid, 0, sizeof(gitmodules_oid)); @@ -1414,6 +1509,8 @@ static int load_submodule_config(git_repository *repo) GITERR_CHECK_ALLOC(repo->submodules); } + /* TODO: only do the following if the sources appear modified */ + /* add submodule information from index */ if ((error = load_submodule_config_from_index(repo, &gitmodules_oid)) < 0) @@ -1433,8 +1530,16 @@ static int load_submodule_config(git_repository *repo) /* shallow scan submodules in work tree */ - if (!git_repository_is_bare(repo)) - error = git_submodule_foreach(repo, submodule_load_from_wd_lite, NULL); + if (!git_repository_is_bare(repo)) { + git_submodule *sm; + + git_strmap_foreach_value(repo->submodules, sm, { + sm->flags &= ~GIT_SUBMODULE_STATUS__ALL_WD_FLAGS; + }); + git_strmap_foreach_value(repo->submodules, sm, { + submodule_load_from_wd_lite(sm); + }); + } cleanup: if (mods != NULL) |
