Skip to content

Commit 26f80cc

Browse files
stefanbellergitster
authored andcommitted
submodule: migrate get_next_submodule to use repository structs
We used to recurse into submodules, even if they were broken having only an objects directory. The child process executed in the submodule would fail though if the submodule was broken. This is tested via "fetching submodule into a broken repository" in t5526. This patch tightens the check upfront, such that we do not need to spawn a child process to find out if the submodule is broken. Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent d5498e0 commit 26f80cc

File tree

1 file changed

+44
-12
lines changed

1 file changed

+44
-12
lines changed

submodule.c

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,19 +1253,42 @@ static int get_fetch_recurse_config(const struct submodule *submodule,
12531253
return spf->default_option;
12541254
}
12551255

1256+
static struct repository *get_submodule_repo_for(struct repository *r,
1257+
const struct submodule *sub)
1258+
{
1259+
struct repository *ret = xmalloc(sizeof(*ret));
1260+
1261+
if (repo_submodule_init(ret, r, sub)) {
1262+
/*
1263+
* No entry in .gitmodules? Technically not a submodule,
1264+
* but historically we supported repositories that happen to be
1265+
* in-place where a gitlink is. Keep supporting them.
1266+
*/
1267+
struct strbuf gitdir = STRBUF_INIT;
1268+
strbuf_repo_worktree_path(&gitdir, r, "%s/.git", sub->path);
1269+
if (repo_init(ret, gitdir.buf, NULL)) {
1270+
strbuf_release(&gitdir);
1271+
free(ret);
1272+
return NULL;
1273+
}
1274+
strbuf_release(&gitdir);
1275+
}
1276+
1277+
return ret;
1278+
}
1279+
12561280
static int get_next_submodule(struct child_process *cp,
12571281
struct strbuf *err, void *data, void **task_cb)
12581282
{
12591283
int ret = 0;
12601284
struct submodule_parallel_fetch *spf = data;
12611285

12621286
for (; spf->count < spf->r->index->cache_nr; spf->count++) {
1263-
struct strbuf submodule_path = STRBUF_INIT;
1264-
struct strbuf submodule_git_dir = STRBUF_INIT;
12651287
struct strbuf submodule_prefix = STRBUF_INIT;
12661288
const struct cache_entry *ce = spf->r->index->cache[spf->count];
1267-
const char *git_dir, *default_argv;
1289+
const char *default_argv;
12681290
const struct submodule *submodule;
1291+
struct repository *repo;
12691292
struct submodule default_submodule = SUBMODULE_INIT;
12701293

12711294
if (!S_ISGITLINK(ce->ce_mode))
@@ -1300,15 +1323,11 @@ static int get_next_submodule(struct child_process *cp,
13001323
continue;
13011324
}
13021325

1303-
strbuf_repo_worktree_path(&submodule_path, spf->r, "%s", ce->name);
1304-
strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf);
13051326
strbuf_addf(&submodule_prefix, "%s%s/", spf->prefix, ce->name);
1306-
git_dir = read_gitfile(submodule_git_dir.buf);
1307-
if (!git_dir)
1308-
git_dir = submodule_git_dir.buf;
1309-
if (is_directory(git_dir)) {
1327+
repo = get_submodule_repo_for(spf->r, submodule);
1328+
if (repo) {
13101329
child_process_init(cp);
1311-
cp->dir = strbuf_detach(&submodule_path, NULL);
1330+
cp->dir = xstrdup(repo->worktree);
13121331
prepare_submodule_repo_env(&cp->env_array);
13131332
cp->git_cmd = 1;
13141333
if (!spf->quiet)
@@ -1319,10 +1338,23 @@ static int get_next_submodule(struct child_process *cp,
13191338
argv_array_push(&cp->args, default_argv);
13201339
argv_array_push(&cp->args, "--submodule-prefix");
13211340
argv_array_push(&cp->args, submodule_prefix.buf);
1341+
1342+
repo_clear(repo);
1343+
free(repo);
13221344
ret = 1;
1345+
} else {
1346+
/*
1347+
* An empty directory is normal,
1348+
* the submodule is not initialized
1349+
*/
1350+
if (S_ISGITLINK(ce->ce_mode) &&
1351+
!is_empty_dir(ce->name)) {
1352+
spf->result = 1;
1353+
strbuf_addf(err,
1354+
_("Could not access submodule '%s'"),
1355+
ce->name);
1356+
}
13231357
}
1324-
strbuf_release(&submodule_path);
1325-
strbuf_release(&submodule_git_dir);
13261358
strbuf_release(&submodule_prefix);
13271359
if (ret) {
13281360
spf->count++;

0 commit comments

Comments
 (0)