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>

Stefan Beller committed Nov 28, 2018 at 16:27 UTC d5498e087175ad7bbb0460a40cfb55cc3726226d
6 files changed +43 -35
builtin/grep.c
+10 -7
@@ -404,7 +404,10 @@ static int grep_submodule(struct grep_opt *opt, struct repository *superproject,
404 const struct object_id *oid,
405 const char *filename, const char *path)
406 {
407 - struct repository submodule;
407 + struct repository subrepo;
408 + const struct submodule *sub = submodule_from_path(superproject,
409 + &null_oid, path);
410 +
411 int hit;
412
413 /*
@@ -420,12 +423,12 @@ static int grep_submodule(struct grep_opt *opt, struct repository *superproject,
423 return 0;
424 }
425
423 - if (repo_submodule_init(&submodule, superproject, path)) {
426 + if (repo_submodule_init(&subrepo, superproject, sub)) {
427 grep_read_unlock();
428 return 0;
429 }
430
428 - repo_read_gitmodules(&submodule);
431 + repo_read_gitmodules(&subrepo);
432
433 /*
434 * 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,
440 * store is no longer global and instead is a member of the repository
441 * object.
442 */
440 - add_to_alternates_memory(submodule.objects->objectdir);
443 + add_to_alternates_memory(subrepo.objects->objectdir);
444 grep_read_unlock();
445
446 if (oid) {
@@ -462,14 +465,14 @@ static int grep_submodule(struct grep_opt *opt, struct repository *superproject,
465
466 init_tree_desc(&tree, data, size);
467 hit = grep_tree(opt, pathspec, &tree, &base, base.len,
465 - object->type == OBJ_COMMIT, &submodule);
468 + object->type == OBJ_COMMIT, &subrepo);
469 strbuf_release(&base);
470 free(data);
471 } else {
469 - hit = grep_cache(opt, &submodule, pathspec, 1);
472 + hit = grep_cache(opt, &subrepo, pathspec, 1);
473 }
474
472 - repo_clear(&submodule);
475 + repo_clear(&subrepo);
476 return hit;
477 }
478
builtin/ls-files.c
+7 -5
@@ -206,17 +206,19 @@ static void show_files(struct repository *repo, struct dir_struct *dir);
206 static void show_submodule(struct repository *superproject,
207 struct dir_struct *dir, const char *path)
208 {
209 - struct repository submodule;
209 + struct repository subrepo;
210 + const struct submodule *sub = submodule_from_path(superproject,
211 + &null_oid, path);
212
211 - if (repo_submodule_init(&submodule, superproject, path))
213 + if (repo_submodule_init(&subrepo, superproject, sub))
214 return;
215
214 - if (repo_read_index(&submodule) < 0)
216 + if (repo_read_index(&subrepo) < 0)
217 die("index file corrupt");
218
217 - show_files(&submodule, dir);
219 + show_files(&subrepo, dir);
220
219 - repo_clear(&submodule);
221 + repo_clear(&subrepo);
222 }
223
224 static void show_ce(struct repository *repo, struct dir_struct *dir,
builtin/submodule--helper.c
+1 -1
@@ -2053,7 +2053,7 @@ static int ensure_core_worktree(int argc, const char **argv, const char *prefix)
2053 if (!sub)
2054 BUG("We could get the submodule handle before?");
2055
2056 - if (repo_submodule_init(&subrepo, the_repository, path))
2056 + if (repo_submodule_init(&subrepo, the_repository, sub))
2057 die(_("could not get a repository handle for submodule '%s'"), path);
2058
2059 if (!repo_config_get_string(&subrepo, "core.worktree", &cw)) {
repository.c
+10 -17
@@ -166,30 +166,23 @@ error:
166 return -1;
167 }
168
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,
170 struct repository *superproject,
176 - const char *path)
171 + const struct submodule *sub)
172 {
178 - const struct submodule *sub;
173 struct strbuf gitdir = STRBUF_INIT;
174 struct strbuf worktree = STRBUF_INIT;
175 int ret = 0;
176
183 - sub = submodule_from_path(superproject, &null_oid, path);
177 if (!sub) {
178 ret = -1;
179 goto out;
180 }
181
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);
184
192 - if (repo_init(submodule, gitdir.buf, worktree.buf)) {
185 + if (repo_init(subrepo, gitdir.buf, worktree.buf)) {
186 /*
187 * If initilization fails then it may be due to the submodule
188 * not being populated in the superproject's worktree. Instead
@@ -201,16 +194,16 @@ int repo_submodule_init(struct repository *submodule,
194 strbuf_repo_git_path(&gitdir, superproject,
195 "modules/%s", sub->name);
196
204 - if (repo_init(submodule, gitdir.buf, NULL)) {
197 + if (repo_init(subrepo, gitdir.buf, NULL)) {
198 ret = -1;
199 goto out;
200 }
201 }
202
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);
207
208 out:
209 strbuf_release(&gitdir);
repository.h
+10 -2
@@ -116,9 +116,17 @@ void repo_set_worktree(struct repository *repo, const char *path);
116 void repo_set_hash_algo(struct repository *repo, int algo);
117 void initialize_the_repository(void);
118 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,
128 struct repository *superproject,
121 - const char *path);
129 + const struct submodule *sub);
130 void repo_clear(struct repository *repo);
131
132 /*
t/helper/test-submodule-nested-repo-config.c
+5 -3
@@ -10,19 +10,21 @@ static void die_usage(int argc, const char **argv, const char *msg)
10
11 int cmd__submodule_nested_repo_config(int argc, const char **argv)
12 {
13 - struct repository submodule;
13 + struct repository subrepo;
14 + const struct submodule *sub;
15
16 if (argc < 3)
17 die_usage(argc, argv, "Wrong number of arguments.");
18
19 setup_git_directory();
20
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)) {
23 die_usage(argc, argv, "Submodule not found.");
24 }
25
26 /* Read the config of _child_ submodules. */
25 - print_config_from_gitmodules(&submodule, argv[2]);
27 + print_config_from_gitmodules(&subrepo, argv[2]);
28
29 submodule_free(the_repository);
30