Skip to content

Commit 8eb8dcf

Browse files
jonathantanmygitster
authored andcommitted
repository: support unabsorbed in repo_submodule_init
In preparation for a subsequent commit that migrates code using add_submodule_odb() to repo_submodule_init(), teach repo_submodule_init() to support submodules with unabsorbed gitdirs. (See the documentation for "git submodule absorbgitdirs" for more information about absorbed and unabsorbed gitdirs.) Signed-off-by: Jonathan Tan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5df5106 commit 8eb8dcf

File tree

7 files changed

+28
-37
lines changed

7 files changed

+28
-37
lines changed

builtin/grep.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -433,17 +433,14 @@ static int grep_submodule(struct grep_opt *opt,
433433
{
434434
struct repository *subrepo;
435435
struct repository *superproject = opt->repo;
436-
const struct submodule *sub;
437436
struct grep_opt subopt;
438437
int hit = 0;
439438

440-
sub = submodule_from_path(superproject, null_oid(), path);
441-
442439
if (!is_submodule_active(superproject, path))
443440
return 0;
444441

445442
subrepo = xmalloc(sizeof(*subrepo));
446-
if (repo_submodule_init(subrepo, superproject, sub)) {
443+
if (repo_submodule_init(subrepo, superproject, path, null_oid())) {
447444
free(subrepo);
448445
return 0;
449446
}

builtin/ls-files.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,8 @@ static void show_submodule(struct repository *superproject,
209209
struct dir_struct *dir, const char *path)
210210
{
211211
struct repository subrepo;
212-
const struct submodule *sub = submodule_from_path(superproject,
213-
null_oid(), path);
214212

215-
if (repo_submodule_init(&subrepo, superproject, sub))
213+
if (repo_submodule_init(&subrepo, superproject, path, null_oid()))
216214
return;
217215

218216
if (repo_read_index(&subrepo) < 0)

builtin/submodule--helper.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2540,7 +2540,6 @@ static int push_check(int argc, const char **argv, const char *prefix)
25402540

25412541
static int ensure_core_worktree(int argc, const char **argv, const char *prefix)
25422542
{
2543-
const struct submodule *sub;
25442543
const char *path;
25452544
const char *cw;
25462545
struct repository subrepo;
@@ -2550,11 +2549,7 @@ static int ensure_core_worktree(int argc, const char **argv, const char *prefix)
25502549

25512550
path = argv[1];
25522551

2553-
sub = submodule_from_path(the_repository, null_oid(), path);
2554-
if (!sub)
2555-
BUG("We could get the submodule handle before?");
2556-
2557-
if (repo_submodule_init(&subrepo, the_repository, sub))
2552+
if (repo_submodule_init(&subrepo, the_repository, path, null_oid()))
25582553
die(_("could not get a repository handle for submodule '%s'"), path);
25592554

25602555
if (!repo_config_get_string_tmp(&subrepo, "core.worktree", &cw)) {

repository.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -190,19 +190,15 @@ int repo_init(struct repository *repo,
190190

191191
int repo_submodule_init(struct repository *subrepo,
192192
struct repository *superproject,
193-
const struct submodule *sub)
193+
const char *path,
194+
const struct object_id *treeish_name)
194195
{
195196
struct strbuf gitdir = STRBUF_INIT;
196197
struct strbuf worktree = STRBUF_INIT;
197198
int ret = 0;
198199

199-
if (!sub) {
200-
ret = -1;
201-
goto out;
202-
}
203-
204-
strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", sub->path);
205-
strbuf_repo_worktree_path(&worktree, superproject, "%s", sub->path);
200+
strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", path);
201+
strbuf_repo_worktree_path(&worktree, superproject, "%s", path);
206202

207203
if (repo_init(subrepo, gitdir.buf, worktree.buf)) {
208204
/*
@@ -212,6 +208,13 @@ int repo_submodule_init(struct repository *subrepo,
212208
* in the superproject's 'modules' directory. In this case the
213209
* submodule would not have a worktree.
214210
*/
211+
const struct submodule *sub =
212+
submodule_from_path(superproject, treeish_name, path);
213+
if (!sub) {
214+
ret = -1;
215+
goto out;
216+
}
217+
215218
strbuf_reset(&gitdir);
216219
strbuf_repo_git_path(&gitdir, superproject,
217220
"modules/%s", sub->name);
@@ -225,7 +228,7 @@ int repo_submodule_init(struct repository *subrepo,
225228
subrepo->submodule_prefix = xstrfmt("%s%s/",
226229
superproject->submodule_prefix ?
227230
superproject->submodule_prefix :
228-
"", sub->path);
231+
"", path);
229232

230233
out:
231234
strbuf_release(&gitdir);

repository.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,15 +172,18 @@ void initialize_the_repository(void);
172172
int repo_init(struct repository *r, const char *gitdir, const char *worktree);
173173

174174
/*
175-
* Initialize the repository 'subrepo' as the submodule given by the
176-
* struct submodule 'sub' in parent repository 'superproject'.
177-
* Return 0 upon success and a non-zero value upon failure, which may happen
178-
* if the submodule is not found, or 'sub' is NULL.
175+
* Initialize the repository 'subrepo' as the submodule at the given path. If
176+
* the submodule's gitdir cannot be found at <path>/.git, this function calls
177+
* submodule_from_path() to try to find it. treeish_name is only used if
178+
* submodule_from_path() needs to be called; see its documentation for more
179+
* information.
180+
* Return 0 upon success and a non-zero value upon failure.
179181
*/
180-
struct submodule;
182+
struct object_id;
181183
int repo_submodule_init(struct repository *subrepo,
182184
struct repository *superproject,
183-
const struct submodule *sub);
185+
const char *path,
186+
const struct object_id *treeish_name);
184187
void repo_clear(struct repository *repo);
185188

186189
/*

submodule.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -520,9 +520,6 @@ static void prepare_submodule_repo_env_in_gitdir(struct strvec *out)
520520
/*
521521
* Initialize a repository struct for a submodule based on the provided 'path'.
522522
*
523-
* Unlike repo_submodule_init, this tolerates submodules not present
524-
* in .gitmodules. This function exists only to preserve historical behavior,
525-
*
526523
* Returns the repository struct on success,
527524
* NULL when the submodule is not present.
528525
*/
@@ -1404,11 +1401,11 @@ static void fetch_task_release(struct fetch_task *p)
14041401
}
14051402

14061403
static struct repository *get_submodule_repo_for(struct repository *r,
1407-
const struct submodule *sub)
1404+
const char *path)
14081405
{
14091406
struct repository *ret = xmalloc(sizeof(*ret));
14101407

1411-
if (repo_submodule_init(ret, r, sub)) {
1408+
if (repo_submodule_init(ret, r, path, null_oid())) {
14121409
free(ret);
14131410
return NULL;
14141411
}
@@ -1452,7 +1449,7 @@ static int get_next_submodule(struct child_process *cp,
14521449
continue;
14531450
}
14541451

1455-
task->repo = get_submodule_repo_for(spf->r, task->sub);
1452+
task->repo = get_submodule_repo_for(spf->r, task->sub->path);
14561453
if (task->repo) {
14571454
struct strbuf submodule_prefix = STRBUF_INIT;
14581455
child_process_init(cp);

t/helper/test-submodule-nested-repo-config.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@ static void die_usage(const char **argv, const char *msg)
1111
int cmd__submodule_nested_repo_config(int argc, const char **argv)
1212
{
1313
struct repository subrepo;
14-
const struct submodule *sub;
1514

1615
if (argc < 3)
1716
die_usage(argv, "Wrong number of arguments.");
1817

1918
setup_git_directory();
2019

21-
sub = submodule_from_path(the_repository, null_oid(), argv[1]);
22-
if (repo_submodule_init(&subrepo, the_repository, sub)) {
20+
if (repo_submodule_init(&subrepo, the_repository, argv[1], null_oid())) {
2321
die_usage(argv, "Submodule not found.");
2422
}
2523

0 commit comments

Comments
 (0)