Skip to content

Commit d5498e0

Browse files
stefanbellergitster
authored andcommitted
repository: repo_submodule_init to take a submodule struct
When constructing a struct repository for a submodule for some revision of the superproject where the submodule is not contained in the index, it may not be present in the working tree currently either. In that situation giving a 'path' argument is not useful. Upgrade the repo_submodule_init function to take a struct submodule instead. The submodule struct can be obtained via submodule_from_{path, name} or an artificial submodule struct can be passed in. While we are at it, rename the repository struct in the repo_submodule_init function, which is to be initialized, to a name that is not confused with the struct submodule as easily. Perform such renames in similar functions as well. Also move its documentation into the header file. Reviewed-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent bcd7337 commit d5498e0

File tree

6 files changed

+43
-35
lines changed

6 files changed

+43
-35
lines changed

builtin/grep.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,10 @@ static int grep_submodule(struct grep_opt *opt, struct repository *superproject,
404404
const struct object_id *oid,
405405
const char *filename, const char *path)
406406
{
407-
struct repository submodule;
407+
struct repository subrepo;
408+
const struct submodule *sub = submodule_from_path(superproject,
409+
&null_oid, path);
410+
408411
int hit;
409412

410413
/*
@@ -420,12 +423,12 @@ static int grep_submodule(struct grep_opt *opt, struct repository *superproject,
420423
return 0;
421424
}
422425

423-
if (repo_submodule_init(&submodule, superproject, path)) {
426+
if (repo_submodule_init(&subrepo, superproject, sub)) {
424427
grep_read_unlock();
425428
return 0;
426429
}
427430

428-
repo_read_gitmodules(&submodule);
431+
repo_read_gitmodules(&subrepo);
429432

430433
/*
431434
* NEEDSWORK: This adds the submodule's object directory to the list of
@@ -437,7 +440,7 @@ static int grep_submodule(struct grep_opt *opt, struct repository *superproject,
437440
* store is no longer global and instead is a member of the repository
438441
* object.
439442
*/
440-
add_to_alternates_memory(submodule.objects->objectdir);
443+
add_to_alternates_memory(subrepo.objects->objectdir);
441444
grep_read_unlock();
442445

443446
if (oid) {
@@ -462,14 +465,14 @@ static int grep_submodule(struct grep_opt *opt, struct repository *superproject,
462465

463466
init_tree_desc(&tree, data, size);
464467
hit = grep_tree(opt, pathspec, &tree, &base, base.len,
465-
object->type == OBJ_COMMIT, &submodule);
468+
object->type == OBJ_COMMIT, &subrepo);
466469
strbuf_release(&base);
467470
free(data);
468471
} else {
469-
hit = grep_cache(opt, &submodule, pathspec, 1);
472+
hit = grep_cache(opt, &subrepo, pathspec, 1);
470473
}
471474

472-
repo_clear(&submodule);
475+
repo_clear(&subrepo);
473476
return hit;
474477
}
475478

builtin/ls-files.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,17 +206,19 @@ static void show_files(struct repository *repo, struct dir_struct *dir);
206206
static void show_submodule(struct repository *superproject,
207207
struct dir_struct *dir, const char *path)
208208
{
209-
struct repository submodule;
209+
struct repository subrepo;
210+
const struct submodule *sub = submodule_from_path(superproject,
211+
&null_oid, path);
210212

211-
if (repo_submodule_init(&submodule, superproject, path))
213+
if (repo_submodule_init(&subrepo, superproject, sub))
212214
return;
213215

214-
if (repo_read_index(&submodule) < 0)
216+
if (repo_read_index(&subrepo) < 0)
215217
die("index file corrupt");
216218

217-
show_files(&submodule, dir);
219+
show_files(&subrepo, dir);
218220

219-
repo_clear(&submodule);
221+
repo_clear(&subrepo);
220222
}
221223

222224
static void show_ce(struct repository *repo, struct dir_struct *dir,

builtin/submodule--helper.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2053,7 +2053,7 @@ static int ensure_core_worktree(int argc, const char **argv, const char *prefix)
20532053
if (!sub)
20542054
BUG("We could get the submodule handle before?");
20552055

2056-
if (repo_submodule_init(&subrepo, the_repository, path))
2056+
if (repo_submodule_init(&subrepo, the_repository, sub))
20572057
die(_("could not get a repository handle for submodule '%s'"), path);
20582058

20592059
if (!repo_config_get_string(&subrepo, "core.worktree", &cw)) {

repository.c

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -166,30 +166,23 @@ int repo_init(struct repository *repo,
166166
return -1;
167167
}
168168

169-
/*
170-
* Initialize 'submodule' as the submodule given by 'path' in parent repository
171-
* 'superproject'.
172-
* Return 0 upon success and a non-zero value upon failure.
173-
*/
174-
int repo_submodule_init(struct repository *submodule,
169+
int repo_submodule_init(struct repository *subrepo,
175170
struct repository *superproject,
176-
const char *path)
171+
const struct submodule *sub)
177172
{
178-
const struct submodule *sub;
179173
struct strbuf gitdir = STRBUF_INIT;
180174
struct strbuf worktree = STRBUF_INIT;
181175
int ret = 0;
182176

183-
sub = submodule_from_path(superproject, &null_oid, path);
184177
if (!sub) {
185178
ret = -1;
186179
goto out;
187180
}
188181

189-
strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", path);
190-
strbuf_repo_worktree_path(&worktree, superproject, "%s", path);
182+
strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", sub->path);
183+
strbuf_repo_worktree_path(&worktree, superproject, "%s", sub->path);
191184

192-
if (repo_init(submodule, gitdir.buf, worktree.buf)) {
185+
if (repo_init(subrepo, gitdir.buf, worktree.buf)) {
193186
/*
194187
* If initilization fails then it may be due to the submodule
195188
* not being populated in the superproject's worktree. Instead
@@ -201,16 +194,16 @@ int repo_submodule_init(struct repository *submodule,
201194
strbuf_repo_git_path(&gitdir, superproject,
202195
"modules/%s", sub->name);
203196

204-
if (repo_init(submodule, gitdir.buf, NULL)) {
197+
if (repo_init(subrepo, gitdir.buf, NULL)) {
205198
ret = -1;
206199
goto out;
207200
}
208201
}
209202

210-
submodule->submodule_prefix = xstrfmt("%s%s/",
211-
superproject->submodule_prefix ?
212-
superproject->submodule_prefix :
213-
"", path);
203+
subrepo->submodule_prefix = xstrfmt("%s%s/",
204+
superproject->submodule_prefix ?
205+
superproject->submodule_prefix :
206+
"", sub->path);
214207

215208
out:
216209
strbuf_release(&gitdir);

repository.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,17 @@ void repo_set_worktree(struct repository *repo, const char *path);
116116
void repo_set_hash_algo(struct repository *repo, int algo);
117117
void initialize_the_repository(void);
118118
int repo_init(struct repository *r, const char *gitdir, const char *worktree);
119-
int repo_submodule_init(struct repository *submodule,
119+
120+
/*
121+
* Initialize the repository 'subrepo' as the submodule given by the
122+
* struct submodule 'sub' in parent repository 'superproject'.
123+
* Return 0 upon success and a non-zero value upon failure, which may happen
124+
* if the submodule is not found, or 'sub' is NULL.
125+
*/
126+
struct submodule;
127+
int repo_submodule_init(struct repository *subrepo,
120128
struct repository *superproject,
121-
const char *path);
129+
const struct submodule *sub);
122130
void repo_clear(struct repository *repo);
123131

124132
/*

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,21 @@ static void die_usage(int argc, const char **argv, const char *msg)
1010

1111
int cmd__submodule_nested_repo_config(int argc, const char **argv)
1212
{
13-
struct repository submodule;
13+
struct repository subrepo;
14+
const struct submodule *sub;
1415

1516
if (argc < 3)
1617
die_usage(argc, argv, "Wrong number of arguments.");
1718

1819
setup_git_directory();
1920

20-
if (repo_submodule_init(&submodule, the_repository, argv[1])) {
21+
sub = submodule_from_path(the_repository, &null_oid, argv[1]);
22+
if (repo_submodule_init(&subrepo, the_repository, sub)) {
2123
die_usage(argc, argv, "Submodule not found.");
2224
}
2325

2426
/* Read the config of _child_ submodules. */
25-
print_config_from_gitmodules(&submodule, argv[2]);
27+
print_config_from_gitmodules(&subrepo, argv[2]);
2628

2729
submodule_free(the_repository);
2830

0 commit comments

Comments
 (0)