]> git.ipfire.org Git - thirdparty/git.git/blame - submodule.c
*.h: move some *_INIT to designated initializers
[thirdparty/git.git] / submodule.c
CommitLineData
e724197f 1
752c0c24 2#include "cache.h"
69aba532 3#include "repository.h"
b2141fc1 4#include "config.h"
851e18c3 5#include "submodule-config.h"
752c0c24
JS
6#include "submodule.h"
7#include "dir.h"
8#include "diff.h"
9#include "commit.h"
10#include "revision.h"
ee6fc514 11#include "run-command.h"
c7e1a736 12#include "diffcore.h"
68d03e4a 13#include "refs.h"
aee9c7d6 14#include "string-list.h"
fe299ec5 15#include "oid-array.h"
dbbcd44f 16#include "strvec.h"
5fee9952 17#include "blob.h"
fe85ee6e 18#include "thread-utils.h"
4638728c 19#include "quote.h"
06bf4ad1 20#include "remote.h"
f6f85861 21#include "worktree.h"
046b4823 22#include "parse-options.h"
0d4a1321 23#include "object-store.h"
64043556 24#include "commit-reach.h"
aee9c7d6 25
d7a3803f 26static int config_update_recurse_submodules = RECURSE_SUBMODULES_OFF;
6859de45 27static int initialized_fetch_ref_tips;
910650d2 28static struct oid_array ref_tips_before_fetch;
29static struct oid_array ref_tips_after_fetch;
6859de45 30
d4e98b58 31/*
34e2ba04
BW
32 * Check if the .gitmodules file is unmerged. Parsing of the .gitmodules file
33 * will be disabled because we can't guess what might be configured in
34 * .gitmodules unless the user resolves the conflict.
d4e98b58 35 */
847a9e5d 36int is_gitmodules_unmerged(struct index_state *istate)
34e2ba04
BW
37{
38 int pos = index_name_pos(istate, GITMODULES_FILE, strlen(GITMODULES_FILE));
39 if (pos < 0) { /* .gitmodules not found or isn't merged */
40 pos = -1 - pos;
41 if (istate->cache_nr > pos) { /* there is a .gitmodules */
42 const struct cache_entry *ce = istate->cache[pos];
43 if (ce_namelen(ce) == strlen(GITMODULES_FILE) &&
44 !strcmp(ce->name, GITMODULES_FILE))
45 return 1;
46 }
47 }
48
49 return 0;
50}
752c0c24 51
b5c259f2
AO
52/*
53 * Check if the .gitmodules file is safe to write.
54 *
55 * Writing to the .gitmodules file requires that the file exists in the
56 * working tree or, if it doesn't, that a brand new .gitmodules file is going
57 * to be created (i.e. it's neither in the index nor in the current branch).
58 *
59 * It is not safe to write to .gitmodules if it's not in the working tree but
60 * it is in the index or in the current branch, because writing new values
61 * (and staging them) would blindly overwrite ALL the old content.
62 */
63int is_writing_gitmodules_ok(void)
64{
65 struct object_id oid;
66 return file_exists(GITMODULES_FILE) ||
67 (get_oid(GITMODULES_INDEX, &oid) < 0 && get_oid(GITMODULES_HEAD, &oid) < 0);
68}
69
5fee9952 70/*
91b83480
BW
71 * Check if the .gitmodules file has unstaged modifications. This must be
72 * checked before allowing modifications to the .gitmodules file with the
73 * intention to stage them later, because when continuing we would stage the
74 * modifications the user didn't stage herself too. That might change in a
75 * future version when we learn to stage the changes we do ourselves without
76 * staging any previous modifications.
5fee9952 77 */
7da9aba4 78int is_staging_gitmodules_ok(struct index_state *istate)
5fee9952 79{
91b83480
BW
80 int pos = index_name_pos(istate, GITMODULES_FILE, strlen(GITMODULES_FILE));
81
82 if ((pos >= 0) && (pos < istate->cache_nr)) {
83 struct stat st;
84 if (lstat(GITMODULES_FILE, &st) == 0 &&
7edee329 85 ie_modified(istate, istate->cache[pos], &st, 0) & DATA_CHANGED)
91b83480
BW
86 return 0;
87 }
88
89 return 1;
5fee9952
JL
90}
91
2e2d4040
NTND
92static int for_each_remote_ref_submodule(const char *submodule,
93 each_ref_fn fn, void *cb_data)
94{
95 return refs_for_each_remote_ref(get_submodule_ref_store(submodule),
96 fn, cb_data);
97}
98
0656781f
JL
99/*
100 * Try to update the "path" entry in the "submodule.<name>" section of the
101 * .gitmodules file. Return 0 only if a .gitmodules file was found, a section
102 * with the correct path=<oldpath> setting was found and we could update it.
103 */
104int update_path_in_gitmodules(const char *oldpath, const char *newpath)
105{
106 struct strbuf entry = STRBUF_INIT;
851e18c3 107 const struct submodule *submodule;
45f5ef3d 108 int ret;
0656781f 109
4c0eeafe 110 if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */
0656781f
JL
111 return -1;
112
68f08b4b 113 if (is_gitmodules_unmerged(the_repository->index))
0656781f
JL
114 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
115
14228447 116 submodule = submodule_from_path(the_repository, null_oid(), oldpath);
851e18c3 117 if (!submodule || !submodule->name) {
0656781f
JL
118 warning(_("Could not find section in .gitmodules where path=%s"), oldpath);
119 return -1;
120 }
121 strbuf_addstr(&entry, "submodule.");
851e18c3 122 strbuf_addstr(&entry, submodule->name);
0656781f 123 strbuf_addstr(&entry, ".path");
45f5ef3d 124 ret = config_set_in_gitmodules_file_gently(entry.buf, newpath);
0656781f 125 strbuf_release(&entry);
45f5ef3d 126 return ret;
0656781f
JL
127}
128
95c16418
JL
129/*
130 * Try to remove the "submodule.<name>" section from .gitmodules where the given
131 * path is configured. Return 0 only if a .gitmodules file was found, a section
132 * with the correct path=<path> setting was found and we could remove it.
133 */
134int remove_path_from_gitmodules(const char *path)
135{
136 struct strbuf sect = STRBUF_INIT;
851e18c3 137 const struct submodule *submodule;
95c16418 138
4c0eeafe 139 if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */
95c16418
JL
140 return -1;
141
68f08b4b 142 if (is_gitmodules_unmerged(the_repository->index))
95c16418
JL
143 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
144
14228447 145 submodule = submodule_from_path(the_repository, null_oid(), path);
851e18c3 146 if (!submodule || !submodule->name) {
95c16418
JL
147 warning(_("Could not find section in .gitmodules where path=%s"), path);
148 return -1;
149 }
150 strbuf_addstr(&sect, "submodule.");
851e18c3 151 strbuf_addstr(&sect, submodule->name);
4c0eeafe 152 if (git_config_rename_section_in_file(GITMODULES_FILE, sect.buf, NULL) < 0) {
95c16418
JL
153 /* Maybe the user already did that, don't error out here */
154 warning(_("Could not remove .gitmodules entry for %s"), path);
155 strbuf_release(&sect);
156 return -1;
157 }
158 strbuf_release(&sect);
159 return 0;
160}
161
3b8317a9 162void stage_updated_gitmodules(struct index_state *istate)
5fee9952 163{
3b8317a9 164 if (add_file_to_index(istate, GITMODULES_FILE, 0))
5fee9952
JL
165 die(_("staging updated .gitmodules failed"));
166}
167
a35e03de
JT
168static struct string_list added_submodule_odb_paths = STRING_LIST_INIT_NODUP;
169
18cfc088
SB
170/* TODO: remove this function, use repo_submodule_init instead. */
171int add_submodule_odb(const char *path)
752c0c24
JS
172{
173 struct strbuf objects_directory = STRBUF_INIT;
de7a7960 174 int ret = 0;
752c0c24 175
99b43a61
JK
176 ret = strbuf_git_path_submodule(&objects_directory, path, "objects/");
177 if (ret)
178 goto done;
de7a7960
JL
179 if (!is_directory(objects_directory.buf)) {
180 ret = -1;
181 goto done;
182 }
a35e03de
JT
183 string_list_insert(&added_submodule_odb_paths,
184 strbuf_detach(&objects_directory, NULL));
de7a7960
JL
185done:
186 strbuf_release(&objects_directory);
187 return ret;
752c0c24
JS
188}
189
8d33c3af
JT
190void add_submodule_odb_by_path(const char *path)
191{
192 string_list_insert(&added_submodule_odb_paths, xstrdup(path));
193}
194
a35e03de
JT
195int register_all_submodule_odb_as_alternates(void)
196{
197 int i;
198 int ret = added_submodule_odb_paths.nr;
199
200 for (i = 0; i < added_submodule_odb_paths.nr; i++)
201 add_to_alternates_memory(added_submodule_odb_paths.items[i].string);
202 if (ret) {
203 string_list_clear(&added_submodule_odb_paths, 0);
204 if (git_env_bool("GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB", 0))
205 BUG("register_all_submodule_odb_as_alternates() called");
206 }
207 return ret;
208}
209
aee9c7d6
JL
210void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
211 const char *path)
212{
3b8fb393 213 const struct submodule *submodule = submodule_from_path(the_repository,
14228447 214 null_oid(),
215 path);
851e18c3 216 if (submodule) {
fdfa9e97
BW
217 const char *ignore;
218 char *key;
aee9c7d6 219
fdfa9e97 220 key = xstrfmt("submodule.%s.ignore", submodule->name);
f1de981e 221 if (repo_config_get_string_tmp(the_repository, key, &ignore))
fdfa9e97
BW
222 ignore = submodule->ignore;
223 free(key);
302ad7a9 224
fdfa9e97
BW
225 if (ignore)
226 handle_ignore_submodules_arg(diffopt, ignore);
68f08b4b 227 else if (is_gitmodules_unmerged(the_repository->index))
0d1e0e78 228 diffopt->flags.ignore_submodules = 1;
046b4823
SB
229 }
230}
231
232/* Cheap function that only determines if we're interested in submodules at all */
233int git_default_submodule_config(const char *var, const char *value, void *cb)
234{
235 if (!strcmp(var, "submodule.recurse")) {
236 int v = git_config_bool(var, value) ?
237 RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF;
238 config_update_recurse_submodules = v;
239 }
240 return 0;
1d789d08
SB
241}
242
d7a3803f
SB
243int option_parse_recurse_submodules_worktree_updater(const struct option *opt,
244 const char *arg, int unset)
245{
246 if (unset) {
247 config_update_recurse_submodules = RECURSE_SUBMODULES_OFF;
248 return 0;
249 }
250 if (arg)
251 config_update_recurse_submodules =
252 parse_update_recurse_submodules_arg(opt->long_name,
253 arg);
254 else
255 config_update_recurse_submodules = RECURSE_SUBMODULES_ON;
256
257 return 0;
258}
259
f9f42560
BW
260/*
261 * Determine if a submodule has been initialized at a given 'path'
262 */
a452128a
AR
263/*
264 * NEEDSWORK: Emit a warning if submodule.active exists, but is valueless,
265 * ie, the config looks like: "[submodule] active\n".
266 * Since that is an invalid pathspec, we should inform the user.
267 */
627d9342 268int is_submodule_active(struct repository *repo, const char *path)
f9f42560
BW
269{
270 int ret = 0;
a086f921
BW
271 char *key = NULL;
272 char *value = NULL;
273 const struct string_list *sl;
627d9342
BW
274 const struct submodule *module;
275
14228447 276 module = submodule_from_path(repo, null_oid(), path);
f9f42560 277
a086f921
BW
278 /* early return if there isn't a path->module mapping */
279 if (!module)
280 return 0;
f9f42560 281
a086f921
BW
282 /* submodule.<name>.active is set */
283 key = xstrfmt("submodule.%s.active", module->name);
627d9342 284 if (!repo_config_get_bool(repo, key, &ret)) {
a086f921
BW
285 free(key);
286 return ret;
287 }
288 free(key);
f9f42560 289
a086f921 290 /* submodule.active is set */
627d9342 291 sl = repo_config_get_value_multi(repo, "submodule.active");
a086f921
BW
292 if (sl) {
293 struct pathspec ps;
c972bf4c 294 struct strvec args = STRVEC_INIT;
a086f921 295 const struct string_list_item *item;
f9f42560 296
a086f921 297 for_each_string_list_item(item, sl) {
c972bf4c 298 strvec_push(&args, item->string);
a086f921
BW
299 }
300
d70a9eb6 301 parse_pathspec(&ps, 0, 0, NULL, args.v);
68f08b4b 302 ret = match_pathspec(repo->index, &ps, path, strlen(path), 0, NULL, 1);
a086f921 303
c972bf4c 304 strvec_clear(&args);
a086f921
BW
305 clear_pathspec(&ps);
306 return ret;
f9f42560
BW
307 }
308
a086f921
BW
309 /* fallback to checking if the URL is set */
310 key = xstrfmt("submodule.%s.url", module->name);
627d9342 311 ret = !repo_config_get_string(repo, key, &value);
a086f921
BW
312
313 free(value);
314 free(key);
f9f42560
BW
315 return ret;
316}
317
15cdc647 318int is_submodule_populated_gently(const char *path, int *return_error_code)
5688c28d
BW
319{
320 int ret = 0;
321 char *gitdir = xstrfmt("%s/.git", path);
322
15cdc647 323 if (resolve_gitdir_gently(gitdir, return_error_code))
5688c28d
BW
324 ret = 1;
325
326 free(gitdir);
327 return ret;
328}
329
bdab9721
BW
330/*
331 * Dies if the provided 'prefix' corresponds to an unpopulated submodule
332 */
847a9e5d 333void die_in_unpopulated_submodule(struct index_state *istate,
bdab9721
BW
334 const char *prefix)
335{
336 int i, prefixlen;
337
338 if (!prefix)
339 return;
340
341 prefixlen = strlen(prefix);
342
343 for (i = 0; i < istate->cache_nr; i++) {
344 struct cache_entry *ce = istate->cache[i];
345 int ce_len = ce_namelen(ce);
346
347 if (!S_ISGITLINK(ce->ce_mode))
348 continue;
349 if (prefixlen <= ce_len)
350 continue;
351 if (strncmp(ce->name, prefix, ce_len))
352 continue;
353 if (prefix[ce_len] != '/')
354 continue;
355
356 die(_("in unpopulated submodule '%s'"), ce->name);
357 }
358}
359
c08397e3
BW
360/*
361 * Dies if any paths in the provided pathspec descends into a submodule
362 */
847a9e5d 363void die_path_inside_submodule(struct index_state *istate,
c08397e3
BW
364 const struct pathspec *ps)
365{
366 int i, j;
367
368 for (i = 0; i < istate->cache_nr; i++) {
369 struct cache_entry *ce = istate->cache[i];
370 int ce_len = ce_namelen(ce);
371
372 if (!S_ISGITLINK(ce->ce_mode))
373 continue;
374
375 for (j = 0; j < ps->nr ; j++) {
376 const struct pathspec_item *item = &ps->items[j];
377
378 if (item->len <= ce_len)
379 continue;
380 if (item->match[ce_len] != '/')
381 continue;
382 if (strncmp(ce->name, item->match, ce_len))
383 continue;
384 if (item->len == ce_len + 1)
385 continue;
386
387 die(_("Pathspec '%s' is in submodule '%.*s'"),
388 item->original, ce_len, ce->name);
389 }
390 }
391}
392
ec6141a0 393enum submodule_update_type parse_submodule_update_type(const char *value)
ea2fa5a3 394{
ea2fa5a3 395 if (!strcmp(value, "none"))
ec6141a0 396 return SM_UPDATE_NONE;
ea2fa5a3 397 else if (!strcmp(value, "checkout"))
ec6141a0 398 return SM_UPDATE_CHECKOUT;
ea2fa5a3 399 else if (!strcmp(value, "rebase"))
ec6141a0 400 return SM_UPDATE_REBASE;
ea2fa5a3 401 else if (!strcmp(value, "merge"))
ec6141a0
BW
402 return SM_UPDATE_MERGE;
403 else if (*value == '!')
404 return SM_UPDATE_COMMAND;
405 else
406 return SM_UPDATE_UNSPECIFIED;
407}
408
409int parse_submodule_update_strategy(const char *value,
410 struct submodule_update_strategy *dst)
411{
412 enum submodule_update_type type;
413
414 free((void*)dst->command);
415 dst->command = NULL;
416
417 type = parse_submodule_update_type(value);
418 if (type == SM_UPDATE_UNSPECIFIED)
ea2fa5a3 419 return -1;
ec6141a0
BW
420
421 dst->type = type;
422 if (type == SM_UPDATE_COMMAND)
423 dst->command = xstrdup(value + 1);
424
ea2fa5a3
SB
425 return 0;
426}
427
3604242f
SB
428const char *submodule_strategy_to_string(const struct submodule_update_strategy *s)
429{
430 struct strbuf sb = STRBUF_INIT;
431 switch (s->type) {
432 case SM_UPDATE_CHECKOUT:
433 return "checkout";
434 case SM_UPDATE_MERGE:
435 return "merge";
436 case SM_UPDATE_REBASE:
437 return "rebase";
438 case SM_UPDATE_NONE:
439 return "none";
440 case SM_UPDATE_UNSPECIFIED:
441 return NULL;
442 case SM_UPDATE_COMMAND:
443 strbuf_addf(&sb, "!%s", s->command);
444 return strbuf_detach(&sb, NULL);
445 }
446 return NULL;
447}
448
46a958b3
JL
449void handle_ignore_submodules_arg(struct diff_options *diffopt,
450 const char *arg)
451{
8ef93124 452 diffopt->flags.ignore_submodule_set = 1;
0d1e0e78
BW
453 diffopt->flags.ignore_submodules = 0;
454 diffopt->flags.ignore_untracked_in_submodules = 0;
455 diffopt->flags.ignore_dirty_submodules = 0;
be4f2b40 456
46a958b3 457 if (!strcmp(arg, "all"))
0d1e0e78 458 diffopt->flags.ignore_submodules = 1;
46a958b3 459 else if (!strcmp(arg, "untracked"))
0d1e0e78 460 diffopt->flags.ignore_untracked_in_submodules = 1;
46a958b3 461 else if (!strcmp(arg, "dirty"))
0d1e0e78 462 diffopt->flags.ignore_dirty_submodules = 1;
aee9c7d6 463 else if (strcmp(arg, "none"))
a4ffbbbb 464 die(_("bad --ignore-submodules argument: %s"), arg);
5a59a230
NTND
465 /*
466 * Please update _git_status() in git-completion.bash when you
467 * add new options
468 */
46a958b3
JL
469}
470
4831c23f
JH
471static int prepare_submodule_diff_summary(struct repository *r, struct rev_info *rev,
472 const char *path,
473 struct commit *left, struct commit *right,
474 struct commit_list *merge_bases)
808a95dc 475{
8e6df650 476 struct commit_list *list;
808a95dc 477
85a1ec2c 478 repo_init_revisions(r, rev, NULL);
808a95dc
JN
479 setup_revisions(0, NULL, rev, NULL);
480 rev->left_right = 1;
481 rev->first_parent_only = 1;
482 left->object.flags |= SYMMETRIC_LEFT;
483 add_pending_object(rev, &left->object, path);
484 add_pending_object(rev, &right->object, path);
808a95dc
JN
485 for (list = merge_bases; list; list = list->next) {
486 list->item->object.flags |= UNINTERESTING;
487 add_pending_object(rev, &list->item->object,
f2fd0760 488 oid_to_hex(&list->item->object.oid));
808a95dc
JN
489 }
490 return prepare_revision_walk(rev);
491}
492
180b154b 493static void print_submodule_diff_summary(struct repository *r, struct rev_info *rev, struct diff_options *o)
808a95dc
JN
494{
495 static const char format[] = " %m %s";
496 struct strbuf sb = STRBUF_INIT;
497 struct commit *commit;
498
499 while ((commit = get_revision(rev))) {
500 struct pretty_print_context ctx = {0};
501 ctx.date_mode = rev->date_mode;
ecaee805 502 ctx.output_encoding = get_log_output_encoding();
808a95dc 503 strbuf_setlen(&sb, 0);
605f0ec1
SB
504 repo_format_commit_message(r, commit, format, &sb,
505 &ctx);
808a95dc 506 strbuf_addch(&sb, '\n');
f3597138
SB
507 if (commit->object.flags & SYMMETRIC_LEFT)
508 diff_emit_submodule_del(o, sb.buf);
509 else
510 diff_emit_submodule_add(o, sb.buf);
808a95dc
JN
511 }
512 strbuf_release(&sb);
513}
514
c972bf4c 515void prepare_submodule_repo_env(struct strvec *out)
6cd5757c 516{
d1fa9435 517 prepare_other_repo_env(out, DEFAULT_GIT_DIR_ENVIRONMENT);
6cd5757c
SB
518}
519
c972bf4c 520static void prepare_submodule_repo_env_in_gitdir(struct strvec *out)
a62387b3 521{
d1fa9435 522 prepare_other_repo_env(out, ".");
a62387b3
SB
523}
524
605f0ec1
SB
525/*
526 * Initialize a repository struct for a submodule based on the provided 'path'.
527 *
528 * Unlike repo_submodule_init, this tolerates submodules not present
529 * in .gitmodules. This function exists only to preserve historical behavior,
530 *
531 * Returns the repository struct on success,
532 * NULL when the submodule is not present.
8e6df650 533 */
605f0ec1
SB
534static struct repository *open_submodule(const char *path)
535{
536 struct strbuf sb = STRBUF_INIT;
537 struct repository *out = xmalloc(sizeof(*out));
538
539 if (submodule_to_gitdir(&sb, path) || repo_init(out, sb.buf, NULL)) {
540 strbuf_release(&sb);
541 free(out);
542 return NULL;
543 }
544
545 /* Mark it as a submodule */
546 out->submodule_prefix = xstrdup(path);
547
548 strbuf_release(&sb);
549 return out;
550}
551
552/*
553 * Helper function to display the submodule header line prior to the full
554 * summary output.
555 *
556 * If it can locate the submodule git directory it will create a repository
557 * handle for the submodule and lookup both the left and right commits and
558 * put them into the left and right pointers.
8e6df650 559 */
605f0ec1
SB
560static void show_submodule_header(struct diff_options *o,
561 const char *path,
602a283a 562 struct object_id *one, struct object_id *two,
f3597138 563 unsigned dirty_submodule,
605f0ec1 564 struct repository *sub,
8e6df650
JK
565 struct commit **left, struct commit **right,
566 struct commit_list **merge_bases)
752c0c24 567{
752c0c24
JS
568 const char *message = NULL;
569 struct strbuf sb = STRBUF_INIT;
752c0c24
JS
570 int fast_forward = 0, fast_backward = 0;
571
c7e1a736 572 if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
f3597138
SB
573 diff_emit_submodule_untracked(o, path);
574
c7e1a736 575 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
f3597138 576 diff_emit_submodule_modified(o, path);
c7e1a736 577
8e6df650
JK
578 if (is_null_oid(one))
579 message = "(new submodule)";
580 else if (is_null_oid(two))
581 message = "(submodule deleted)";
582
605f0ec1 583 if (!sub) {
8e6df650 584 if (!message)
2d94dd2f 585 message = "(commits not present)";
8e6df650
JK
586 goto output_header;
587 }
588
589 /*
590 * Attempt to lookup the commit references, and determine if this is
591 * a fast forward or fast backwards update.
592 */
605f0ec1
SB
593 *left = lookup_commit_reference(sub, one);
594 *right = lookup_commit_reference(sub, two);
8e6df650
JK
595
596 /*
597 * Warn about missing commits in the submodule project, but only if
598 * they aren't null.
599 */
600 if ((!is_null_oid(one) && !*left) ||
601 (!is_null_oid(two) && !*right))
602 message = "(commits not present)";
603
605f0ec1 604 *merge_bases = repo_get_merge_bases(sub, *left, *right);
8e6df650
JK
605 if (*merge_bases) {
606 if ((*merge_bases)->item == *left)
607 fast_forward = 1;
608 else if ((*merge_bases)->item == *right)
609 fast_backward = 1;
610 }
611
4a7e27e9 612 if (oideq(one, two)) {
c7e1a736
JL
613 strbuf_release(&sb);
614 return;
615 }
616
8e6df650 617output_header:
f3597138 618 strbuf_addf(&sb, "Submodule %s ", path);
30e677e0 619 strbuf_add_unique_abbrev(&sb, one, DEFAULT_ABBREV);
a94bb683 620 strbuf_addstr(&sb, (fast_backward || fast_forward) ? ".." : "...");
30e677e0 621 strbuf_add_unique_abbrev(&sb, two, DEFAULT_ABBREV);
752c0c24 622 if (message)
f3597138 623 strbuf_addf(&sb, " %s\n", message);
752c0c24 624 else
f3597138
SB
625 strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : "");
626 diff_emit_submodule_header(o, sb.buf);
752c0c24 627
752c0c24
JS
628 strbuf_release(&sb);
629}
ee6fc514 630
180b154b 631void show_submodule_diff_summary(struct diff_options *o, const char *path,
8e6df650 632 struct object_id *one, struct object_id *two,
f3597138 633 unsigned dirty_submodule)
8e6df650
JK
634{
635 struct rev_info rev;
636 struct commit *left = NULL, *right = NULL;
637 struct commit_list *merge_bases = NULL;
605f0ec1 638 struct repository *sub;
8e6df650 639
605f0ec1 640 sub = open_submodule(path);
f3597138 641 show_submodule_header(o, path, one, two, dirty_submodule,
605f0ec1 642 sub, &left, &right, &merge_bases);
8e6df650
JK
643
644 /*
645 * If we don't have both a left and a right pointer, there is no
646 * reason to try and display a summary. The header line should contain
647 * all the information the user needs.
648 */
605f0ec1 649 if (!left || !right || !sub)
8e6df650
JK
650 goto out;
651
652 /* Treat revision walker failure the same as missing commits */
4831c23f 653 if (prepare_submodule_diff_summary(sub, &rev, path, left, right, merge_bases)) {
f3597138 654 diff_emit_submodule_error(o, "(revision walker failed)\n");
8e6df650
JK
655 goto out;
656 }
657
180b154b 658 print_submodule_diff_summary(sub, &rev, o);
8e6df650
JK
659
660out:
661 if (merge_bases)
662 free_commit_list(merge_bases);
663 clear_commit_marks(left, ~0);
664 clear_commit_marks(right, ~0);
605f0ec1
SB
665 if (sub) {
666 repo_clear(sub);
667 free(sub);
668 }
8e6df650
JK
669}
670
f3597138 671void show_submodule_inline_diff(struct diff_options *o, const char *path,
fd47ae6a 672 struct object_id *one, struct object_id *two,
f3597138 673 unsigned dirty_submodule)
fd47ae6a 674{
bc099914 675 const struct object_id *old_oid = the_hash_algo->empty_tree, *new_oid = the_hash_algo->empty_tree;
fd47ae6a
JK
676 struct commit *left = NULL, *right = NULL;
677 struct commit_list *merge_bases = NULL;
fd47ae6a 678 struct child_process cp = CHILD_PROCESS_INIT;
f3597138 679 struct strbuf sb = STRBUF_INIT;
605f0ec1 680 struct repository *sub;
fd47ae6a 681
605f0ec1 682 sub = open_submodule(path);
f3597138 683 show_submodule_header(o, path, one, two, dirty_submodule,
605f0ec1 684 sub, &left, &right, &merge_bases);
fd47ae6a
JK
685
686 /* We need a valid left and right commit to display a difference */
687 if (!(left || is_null_oid(one)) ||
688 !(right || is_null_oid(two)))
689 goto done;
690
691 if (left)
bc099914 692 old_oid = one;
fd47ae6a 693 if (right)
bc099914 694 new_oid = two;
fd47ae6a 695
fd47ae6a
JK
696 cp.git_cmd = 1;
697 cp.dir = path;
f3597138 698 cp.out = -1;
fd47ae6a
JK
699 cp.no_stdin = 1;
700
701 /* TODO: other options may need to be passed here. */
c972bf4c
JK
702 strvec_pushl(&cp.args, "diff", "--submodule=diff", NULL);
703 strvec_pushf(&cp.args, "--color=%s", want_color(o->use_color) ?
f3597138 704 "always" : "never");
5a522142 705
0d1e0e78 706 if (o->flags.reverse_diff) {
c972bf4c 707 strvec_pushf(&cp.args, "--src-prefix=%s%s/",
f6d8942b 708 o->b_prefix, path);
c972bf4c 709 strvec_pushf(&cp.args, "--dst-prefix=%s%s/",
f6d8942b 710 o->a_prefix, path);
fd47ae6a 711 } else {
c972bf4c 712 strvec_pushf(&cp.args, "--src-prefix=%s%s/",
f6d8942b 713 o->a_prefix, path);
c972bf4c 714 strvec_pushf(&cp.args, "--dst-prefix=%s%s/",
f6d8942b 715 o->b_prefix, path);
fd47ae6a 716 }
c972bf4c 717 strvec_push(&cp.args, oid_to_hex(old_oid));
fd47ae6a
JK
718 /*
719 * If the submodule has modified content, we will diff against the
720 * work tree, under the assumption that the user has asked for the
721 * diff format and wishes to actually see all differences even if they
722 * haven't yet been committed to the submodule yet.
723 */
724 if (!(dirty_submodule & DIRTY_SUBMODULE_MODIFIED))
c972bf4c 725 strvec_push(&cp.args, oid_to_hex(new_oid));
fd47ae6a 726
17b254cd 727 prepare_submodule_repo_env(&cp.env_array);
f1c0368d
DT
728
729 if (!is_directory(path)) {
730 /* fall back to absorbed git dir, if any */
731 if (!sub)
732 goto done;
733 cp.dir = sub->gitdir;
734 strvec_push(&cp.env_array, GIT_DIR_ENVIRONMENT "=.");
735 strvec_push(&cp.env_array, GIT_WORK_TREE_ENVIRONMENT "=.");
736 }
737
67f61efb 738 if (start_command(&cp)) {
f3597138 739 diff_emit_submodule_error(o, "(diff failed)\n");
67f61efb
DT
740 goto done;
741 }
f3597138
SB
742
743 while (strbuf_getwholeline_fd(&sb, cp.out, '\n') != EOF)
744 diff_emit_submodule_pipethrough(o, sb.buf, sb.len);
745
746 if (finish_command(&cp))
747 diff_emit_submodule_error(o, "(diff failed)\n");
fd47ae6a
JK
748
749done:
f3597138 750 strbuf_release(&sb);
fd47ae6a
JK
751 if (merge_bases)
752 free_commit_list(merge_bases);
753 if (left)
754 clear_commit_marks(left, ~0);
755 if (right)
756 clear_commit_marks(right, ~0);
605f0ec1
SB
757 if (sub) {
758 repo_clear(sub);
759 free(sub);
760 }
fd47ae6a
JK
761}
762
84f8925e
SB
763int should_update_submodules(void)
764{
765 return config_update_recurse_submodules == RECURSE_SUBMODULES_ON;
766}
767
768const struct submodule *submodule_from_ce(const struct cache_entry *ce)
769{
770 if (!S_ISGITLINK(ce->ce_mode))
771 return NULL;
772
773 if (!should_update_submodules())
774 return NULL;
775
14228447 776 return submodule_from_path(the_repository, null_oid(), ce->name);
84f8925e
SB
777}
778
aacc5c1a 779static struct oid_array *submodule_commits(struct string_list *submodules,
c68f8375 780 const char *name)
aacc5c1a
BW
781{
782 struct string_list_item *item;
783
c68f8375 784 item = string_list_insert(submodules, name);
aacc5c1a
BW
785 if (item->util)
786 return (struct oid_array *) item->util;
787
788 /* NEEDSWORK: should we have oid_array_init()? */
789 item->util = xcalloc(1, sizeof(struct oid_array));
790 return (struct oid_array *) item->util;
791}
792
c68f8375 793struct collect_changed_submodules_cb_data {
6245b98b 794 struct repository *repo;
c68f8375
HV
795 struct string_list *changed;
796 const struct object_id *commit_oid;
797};
798
799/*
800 * this would normally be two functions: default_name_from_path() and
801 * path_from_default_name(). Since the default name is the same as
802 * the submodule path we can get away with just one function which only
803 * checks whether there is a submodule in the working directory at that
804 * location.
805 */
806static const char *default_name_or_path(const char *path_or_name)
807{
808 int error_code;
809
810 if (!is_submodule_populated_gently(path_or_name, &error_code))
811 return NULL;
812
813 return path_or_name;
814}
815
aacc5c1a
BW
816static void collect_changed_submodules_cb(struct diff_queue_struct *q,
817 struct diff_options *options,
818 void *data)
819{
c68f8375
HV
820 struct collect_changed_submodules_cb_data *me = data;
821 struct string_list *changed = me->changed;
822 const struct object_id *commit_oid = me->commit_oid;
aacc5c1a 823 int i;
aacc5c1a
BW
824
825 for (i = 0; i < q->nr; i++) {
826 struct diff_filepair *p = q->queue[i];
827 struct oid_array *commits;
c68f8375
HV
828 const struct submodule *submodule;
829 const char *name;
830
aacc5c1a
BW
831 if (!S_ISGITLINK(p->two->mode))
832 continue;
833
6245b98b 834 submodule = submodule_from_path(me->repo,
3b8fb393 835 commit_oid, p->two->path);
c68f8375
HV
836 if (submodule)
837 name = submodule->name;
838 else {
839 name = default_name_or_path(p->two->path);
840 /* make sure name does not collide with existing one */
5fc84755 841 if (name)
6245b98b 842 submodule = submodule_from_name(me->repo,
5fc84755 843 commit_oid, name);
c68f8375 844 if (submodule) {
a4ffbbbb 845 warning(_("Submodule in commit %s at path: "
c68f8375 846 "'%s' collides with a submodule named "
a4ffbbbb 847 "the same. Skipping it."),
5fc84755 848 oid_to_hex(commit_oid), p->two->path);
c68f8375
HV
849 name = NULL;
850 }
aacc5c1a 851 }
c68f8375
HV
852
853 if (!name)
854 continue;
855
856 commits = submodule_commits(changed, name);
857 oid_array_append(commits, &p->two->oid);
aacc5c1a
BW
858 }
859}
860
861/*
862 * Collect the paths of submodules in 'changed' which have changed based on
863 * the revisions as specified in 'argv'. Each entry in 'changed' will also
864 * have a corresponding 'struct oid_array' (in the 'util' field) which lists
865 * what the submodule pointers were updated to during the change.
866 */
6245b98b 867static void collect_changed_submodules(struct repository *r,
174d131f 868 struct string_list *changed,
c972bf4c 869 struct strvec *argv)
aacc5c1a
BW
870{
871 struct rev_info rev;
872 const struct commit *commit;
a462bee5
OS
873 int save_warning;
874 struct setup_revision_opt s_r_opt = {
875 .assume_dashdash = 1,
876 };
aacc5c1a 877
a462bee5
OS
878 save_warning = warn_on_object_refname_ambiguity;
879 warn_on_object_refname_ambiguity = 0;
6245b98b 880 repo_init_revisions(r, &rev, NULL);
a462bee5
OS
881 setup_revisions(argv->nr, argv->v, &rev, &s_r_opt);
882 warn_on_object_refname_ambiguity = save_warning;
aacc5c1a 883 if (prepare_revision_walk(&rev))
a4ffbbbb 884 die(_("revision walk setup failed"));
aacc5c1a
BW
885
886 while ((commit = get_revision(&rev))) {
887 struct rev_info diff_rev;
c68f8375 888 struct collect_changed_submodules_cb_data data;
6245b98b 889 data.repo = r;
c68f8375
HV
890 data.changed = changed;
891 data.commit_oid = &commit->object.oid;
aacc5c1a 892
6245b98b 893 repo_init_revisions(r, &diff_rev, NULL);
aacc5c1a
BW
894 diff_rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
895 diff_rev.diffopt.format_callback = collect_changed_submodules_cb;
c68f8375 896 diff_rev.diffopt.format_callback_data = &data;
d01141de
SO
897 diff_rev.dense_combined_merges = 1;
898 diff_tree_combined_merge(commit, &diff_rev);
aacc5c1a
BW
899 }
900
901 reset_revision_walk();
902}
903
904static void free_submodules_oids(struct string_list *submodules)
905{
906 struct string_list_item *item;
907 for_each_string_list_item(item, submodules)
908 oid_array_clear((struct oid_array *) item->util);
909 string_list_clear(submodules, 1);
910}
911
7290ef58
MH
912static int has_remote(const char *refname, const struct object_id *oid,
913 int flags, void *cb_data)
d2b17b32
FG
914{
915 return 1;
916}
917
1b7ba794 918static int append_oid_to_argv(const struct object_id *oid, void *data)
d2b17b32 919{
c972bf4c
JK
920 struct strvec *argv = data;
921 strvec_push(argv, oid_to_hex(oid));
9cfa1c26
HV
922 return 0;
923}
924
3c96aa97 925struct has_commit_data {
6245b98b 926 struct repository *repo;
3c96aa97
SB
927 int result;
928 const char *path;
929};
930
1b7ba794 931static int check_has_commit(const struct object_id *oid, void *data)
d2b17b32 932{
3c96aa97 933 struct has_commit_data *cb = data;
5b6607d2 934
6245b98b 935 enum object_type type = oid_object_info(cb->repo, oid, NULL);
5b6607d2 936
3c96aa97
SB
937 switch (type) {
938 case OBJ_COMMIT:
939 return 0;
940 case OBJ_BAD:
941 /*
942 * Object is missing or invalid. If invalid, an error message
943 * has already been printed.
944 */
945 cb->result = 0;
946 return 0;
947 default:
948 die(_("submodule entry '%s' (%s) is a %s, not a commit"),
debca9d2 949 cb->path, oid_to_hex(oid), type_name(type));
3c96aa97 950 }
5b6607d2
HV
951}
952
6245b98b
NTND
953static int submodule_has_commits(struct repository *r,
954 const char *path,
955 struct oid_array *commits)
5b6607d2 956{
6245b98b 957 struct has_commit_data has_commit = { r, 1, path };
5b6607d2 958
7c8d2b00 959 /*
64127575 960 * Perform a cheap, but incorrect check for the existence of 'commits'.
7c8d2b00 961 * This is done by adding the submodule's object store to the in-core
64127575 962 * object store, and then querying for each commit's existence. If we
7c8d2b00
BW
963 * do not have the commit object anywhere, there is no chance we have
964 * it in the object store of the correct submodule and have it
965 * reachable from a ref, so we can fail early without spawning rev-list
966 * which is expensive.
967 */
5b6607d2
HV
968 if (add_submodule_odb(path))
969 return 0;
970
910650d2 971 oid_array_for_each_unique(commits, check_has_commit, &has_commit);
7c8d2b00 972
3c96aa97 973 if (has_commit.result) {
7c8d2b00
BW
974 /*
975 * Even if the submodule is checked out and the commit is
976 * present, make sure it exists in the submodule's object store
977 * and that it is reachable from a ref.
978 */
979 struct child_process cp = CHILD_PROCESS_INIT;
980 struct strbuf out = STRBUF_INIT;
981
c972bf4c 982 strvec_pushl(&cp.args, "rev-list", "-n", "1", NULL);
7c8d2b00 983 oid_array_for_each_unique(commits, append_oid_to_argv, &cp.args);
c972bf4c 984 strvec_pushl(&cp.args, "--not", "--all", NULL);
7c8d2b00
BW
985
986 prepare_submodule_repo_env(&cp.env_array);
987 cp.git_cmd = 1;
988 cp.no_stdin = 1;
989 cp.dir = path;
990
991 if (capture_command(&cp, &out, GIT_MAX_HEXSZ + 1) || out.len)
3c96aa97 992 has_commit.result = 0;
7c8d2b00
BW
993
994 strbuf_release(&out);
995 }
996
3c96aa97 997 return has_commit.result;
5b6607d2
HV
998}
999
6245b98b
NTND
1000static int submodule_needs_pushing(struct repository *r,
1001 const char *path,
1002 struct oid_array *commits)
5b6607d2 1003{
6245b98b 1004 if (!submodule_has_commits(r, path, commits))
250ab24a
HV
1005 /*
1006 * NOTE: We do consider it safe to return "no" here. The
1007 * correct answer would be "We do not know" instead of
1008 * "No push needed", but it is quite hard to change
1009 * the submodule pointer without having the submodule
1010 * around. If a user did however change the submodules
1011 * without having the submodule around, this indicates
1012 * an expert who knows what they are doing or a
1013 * maintainer integrating work from other people. In
1014 * both cases it should be safe to skip this check.
1015 */
d2b17b32
FG
1016 return 0;
1017
1018 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
d3180279 1019 struct child_process cp = CHILD_PROCESS_INIT;
d2b17b32
FG
1020 struct strbuf buf = STRBUF_INIT;
1021 int needs_pushing = 0;
1022
c972bf4c 1023 strvec_push(&cp.args, "rev-list");
910650d2 1024 oid_array_for_each_unique(commits, append_oid_to_argv, &cp.args);
c972bf4c 1025 strvec_pushl(&cp.args, "--not", "--remotes", "-n", "1" , NULL);
5b6607d2 1026
c12e8656 1027 prepare_submodule_repo_env(&cp.env_array);
d2b17b32
FG
1028 cp.git_cmd = 1;
1029 cp.no_stdin = 1;
1030 cp.out = -1;
1031 cp.dir = path;
1032 if (start_command(&cp))
a4ffbbbb 1033 die(_("Could not run 'git rev-list <commits> --not --remotes -n 1' command in submodule %s"),
5b6607d2 1034 path);
db1ba2a2 1035 if (strbuf_read(&buf, cp.out, the_hash_algo->hexsz + 1))
d2b17b32
FG
1036 needs_pushing = 1;
1037 finish_command(&cp);
1038 close(cp.out);
1039 strbuf_release(&buf);
1040 return needs_pushing;
1041 }
1042
1043 return 0;
1044}
1045
6245b98b 1046int find_unpushed_submodules(struct repository *r,
174d131f
NTND
1047 struct oid_array *commits,
1048 const char *remotes_name,
1049 struct string_list *needs_pushing)
d2b17b32 1050{
14739447 1051 struct string_list submodules = STRING_LIST_INIT_DUP;
c68f8375 1052 struct string_list_item *name;
c972bf4c 1053 struct strvec argv = STRVEC_INIT;
a762e51e 1054
d70a9eb6 1055 /* argv.v[0] will be ignored by setup_revisions */
c972bf4c 1056 strvec_push(&argv, "find_unpushed_submodules");
910650d2 1057 oid_array_for_each_unique(commits, append_oid_to_argv, &argv);
c972bf4c
JK
1058 strvec_push(&argv, "--not");
1059 strvec_pushf(&argv, "--remotes=%s", remotes_name);
9cfa1c26 1060
6245b98b 1061 collect_changed_submodules(r, &submodules, &argv);
d2b17b32 1062
c68f8375
HV
1063 for_each_string_list_item(name, &submodules) {
1064 struct oid_array *commits = name->util;
1065 const struct submodule *submodule;
1066 const char *path = NULL;
1067
14228447 1068 submodule = submodule_from_name(r, null_oid(), name->string);
c68f8375
HV
1069 if (submodule)
1070 path = submodule->path;
1071 else
1072 path = default_name_or_path(name->string);
1073
1074 if (!path)
1075 continue;
5b6607d2 1076
6245b98b 1077 if (submodule_needs_pushing(r, path, commits))
aacc5c1a 1078 string_list_insert(needs_pushing, path);
14739447 1079 }
610b2337
BW
1080
1081 free_submodules_oids(&submodules);
c972bf4c 1082 strvec_clear(&argv);
d2b17b32 1083
a762e51e 1084 return needs_pushing->nr;
d2b17b32
FG
1085}
1086
2a90556d 1087static int push_submodule(const char *path,
06bf4ad1 1088 const struct remote *remote,
60fba4bf 1089 const struct refspec *rs,
2a90556d
BW
1090 const struct string_list *push_options,
1091 int dry_run)
eb21c732 1092{
eb21c732 1093 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
d3180279 1094 struct child_process cp = CHILD_PROCESS_INIT;
c972bf4c 1095 strvec_push(&cp.args, "push");
0301c821 1096 if (dry_run)
c972bf4c 1097 strvec_push(&cp.args, "--dry-run");
eb21c732 1098
2a90556d
BW
1099 if (push_options && push_options->nr) {
1100 const struct string_list_item *item;
1101 for_each_string_list_item(item, push_options)
c972bf4c 1102 strvec_pushf(&cp.args, "--push-option=%s",
f6d8942b 1103 item->string);
2a90556d 1104 }
06bf4ad1
BW
1105
1106 if (remote->origin != REMOTE_UNCONFIGURED) {
1107 int i;
c972bf4c 1108 strvec_push(&cp.args, remote->name);
60fba4bf 1109 for (i = 0; i < rs->raw_nr; i++)
c972bf4c 1110 strvec_push(&cp.args, rs->raw[i]);
06bf4ad1
BW
1111 }
1112
c12e8656 1113 prepare_submodule_repo_env(&cp.env_array);
eb21c732
HV
1114 cp.git_cmd = 1;
1115 cp.no_stdin = 1;
1116 cp.dir = path;
1117 if (run_command(&cp))
1118 return 0;
1119 close(cp.out);
1120 }
1121
1122 return 1;
1123}
1124
06bf4ad1
BW
1125/*
1126 * Perform a check in the submodule to see if the remote and refspec work.
1127 * Die if the submodule can't be pushed.
1128 */
c7be7201
BW
1129static void submodule_push_check(const char *path, const char *head,
1130 const struct remote *remote,
60fba4bf 1131 const struct refspec *rs)
06bf4ad1
BW
1132{
1133 struct child_process cp = CHILD_PROCESS_INIT;
1134 int i;
1135
c972bf4c
JK
1136 strvec_push(&cp.args, "submodule--helper");
1137 strvec_push(&cp.args, "push-check");
1138 strvec_push(&cp.args, head);
1139 strvec_push(&cp.args, remote->name);
06bf4ad1 1140
60fba4bf 1141 for (i = 0; i < rs->raw_nr; i++)
c972bf4c 1142 strvec_push(&cp.args, rs->raw[i]);
06bf4ad1
BW
1143
1144 prepare_submodule_repo_env(&cp.env_array);
1145 cp.git_cmd = 1;
1146 cp.no_stdin = 1;
1147 cp.no_stdout = 1;
1148 cp.dir = path;
1149
1150 /*
1151 * Simply indicate if 'submodule--helper push-check' failed.
1152 * More detailed error information will be provided by the
1153 * child process.
1154 */
1155 if (run_command(&cp))
a4ffbbbb 1156 die(_("process for submodule '%s' failed"), path);
06bf4ad1
BW
1157}
1158
6245b98b 1159int push_unpushed_submodules(struct repository *r,
174d131f 1160 struct oid_array *commits,
06bf4ad1 1161 const struct remote *remote,
60fba4bf 1162 const struct refspec *rs,
2a90556d 1163 const struct string_list *push_options,
0301c821 1164 int dry_run)
eb21c732
HV
1165{
1166 int i, ret = 1;
f93d7c6f 1167 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
eb21c732 1168
6245b98b 1169 if (!find_unpushed_submodules(r, commits,
174d131f 1170 remote->name, &needs_pushing))
eb21c732
HV
1171 return 1;
1172
06bf4ad1
BW
1173 /*
1174 * Verify that the remote and refspec can be propagated to all
1175 * submodules. This check can be skipped if the remote and refspec
1176 * won't be propagated due to the remote being unconfigured (e.g. a URL
1177 * instead of a remote name).
1178 */
c7be7201
BW
1179 if (remote->origin != REMOTE_UNCONFIGURED) {
1180 char *head;
1181 struct object_id head_oid;
1182
0f2dc722 1183 head = resolve_refdup("HEAD", 0, &head_oid, NULL);
c7be7201
BW
1184 if (!head)
1185 die(_("Failed to resolve HEAD as a valid ref."));
1186
06bf4ad1
BW
1187 for (i = 0; i < needs_pushing.nr; i++)
1188 submodule_push_check(needs_pushing.items[i].string,
60fba4bf 1189 head, remote, rs);
c7be7201
BW
1190 free(head);
1191 }
06bf4ad1
BW
1192
1193 /* Actually push the submodules */
eb21c732
HV
1194 for (i = 0; i < needs_pushing.nr; i++) {
1195 const char *path = needs_pushing.items[i].string;
a4ffbbbb 1196 fprintf(stderr, _("Pushing submodule '%s'\n"), path);
60fba4bf 1197 if (!push_submodule(path, remote, rs,
06bf4ad1 1198 push_options, dry_run)) {
a4ffbbbb 1199 fprintf(stderr, _("Unable to push submodule '%s'\n"), path);
eb21c732
HV
1200 ret = 0;
1201 }
1202 }
1203
1204 string_list_clear(&needs_pushing, 0);
1205
1206 return ret;
1207}
1208
419fd786
BW
1209static int append_oid_to_array(const char *ref, const struct object_id *oid,
1210 int flags, void *data)
6859de45 1211{
419fd786
BW
1212 struct oid_array *array = data;
1213 oid_array_append(array, oid);
6859de45
JK
1214 return 0;
1215}
1216
2eb80bcd 1217void check_for_new_submodule_commits(struct object_id *oid)
6859de45
JK
1218{
1219 if (!initialized_fetch_ref_tips) {
419fd786 1220 for_each_ref(append_oid_to_array, &ref_tips_before_fetch);
6859de45
JK
1221 initialized_fetch_ref_tips = 1;
1222 }
1223
910650d2 1224 oid_array_append(&ref_tips_after_fetch, oid);
6859de45
JK
1225}
1226
16dd6fe1
SB
1227static void calculate_changed_submodule_paths(struct repository *r,
1228 struct string_list *changed_submodule_names)
88a21979 1229{
c972bf4c 1230 struct strvec argv = STRVEC_INIT;
bcd73372 1231 struct string_list_item *name;
88a21979 1232
18322bad 1233 /* No need to check if there are no submodules configured */
6245b98b 1234 if (!submodule_from_path(r, NULL, NULL))
18322bad
JL
1235 return;
1236
c972bf4c 1237 strvec_push(&argv, "--"); /* argv[0] program name */
910650d2 1238 oid_array_for_each_unique(&ref_tips_after_fetch,
d1a8460c 1239 append_oid_to_argv, &argv);
c972bf4c 1240 strvec_push(&argv, "--not");
910650d2 1241 oid_array_for_each_unique(&ref_tips_before_fetch,
d1a8460c 1242 append_oid_to_argv, &argv);
88a21979
JL
1243
1244 /*
1245 * Collect all submodules (whether checked out or not) for which new
c68f8375 1246 * commits have been recorded upstream in "changed_submodule_names".
88a21979 1247 */
bcd73372 1248 collect_changed_submodules(r, changed_submodule_names, &argv);
aacc5c1a 1249
bcd73372 1250 for_each_string_list_item(name, changed_submodule_names) {
c68f8375
HV
1251 struct oid_array *commits = name->util;
1252 const struct submodule *submodule;
1253 const char *path = NULL;
1254
14228447 1255 submodule = submodule_from_name(r, null_oid(), name->string);
c68f8375
HV
1256 if (submodule)
1257 path = submodule->path;
1258 else
1259 path = default_name_or_path(name->string);
1260
1261 if (!path)
1262 continue;
aacc5c1a 1263
bcd73372
SB
1264 if (submodule_has_commits(r, path, commits)) {
1265 oid_array_clear(commits);
1266 *name->string = '\0';
1267 }
88a21979 1268 }
6859de45 1269
bcd73372
SB
1270 string_list_remove_empty_items(changed_submodule_names, 1);
1271
c972bf4c 1272 strvec_clear(&argv);
910650d2 1273 oid_array_clear(&ref_tips_before_fetch);
1274 oid_array_clear(&ref_tips_after_fetch);
6859de45 1275 initialized_fetch_ref_tips = 0;
88a21979
JL
1276}
1277
6245b98b 1278int submodule_touches_in_range(struct repository *r,
174d131f 1279 struct object_id *excl_oid,
a6d7eb2c
SB
1280 struct object_id *incl_oid)
1281{
1282 struct string_list subs = STRING_LIST_INIT_DUP;
c972bf4c 1283 struct strvec args = STRVEC_INIT;
a6d7eb2c
SB
1284 int ret;
1285
a6d7eb2c 1286 /* No need to check if there are no submodules configured */
6245b98b 1287 if (!submodule_from_path(r, NULL, NULL))
a6d7eb2c
SB
1288 return 0;
1289
c972bf4c
JK
1290 strvec_push(&args, "--"); /* args[0] program name */
1291 strvec_push(&args, oid_to_hex(incl_oid));
4d36f88b 1292 if (!is_null_oid(excl_oid)) {
c972bf4c
JK
1293 strvec_push(&args, "--not");
1294 strvec_push(&args, oid_to_hex(excl_oid));
4d36f88b 1295 }
a6d7eb2c 1296
6245b98b 1297 collect_changed_submodules(r, &subs, &args);
a6d7eb2c
SB
1298 ret = subs.nr;
1299
c972bf4c 1300 strvec_clear(&args);
a6d7eb2c
SB
1301
1302 free_submodules_oids(&subs);
1303 return ret;
1304}
1305
fe85ee6e
SB
1306struct submodule_parallel_fetch {
1307 int count;
c972bf4c 1308 struct strvec args;
e724197f 1309 struct repository *r;
fe85ee6e
SB
1310 const char *prefix;
1311 int command_line_option;
8fa29159 1312 int default_option;
fe85ee6e
SB
1313 int quiet;
1314 int result;
16dd6fe1
SB
1315
1316 struct string_list changed_submodule_names;
be76c212
SB
1317
1318 /* Pending fetches by OIDs */
1319 struct fetch_task **oid_fetch_tasks;
1320 int oid_fetch_tasks_nr, oid_fetch_tasks_alloc;
02225408
ES
1321
1322 struct strbuf submodules_with_errors;
fe85ee6e 1323};
f69a6e4f
ÆAB
1324#define SPF_INIT { \
1325 .args = STRVEC_INIT, \
1326 .changed_submodule_names = STRING_LIST_INIT_DUP, \
1327 .submodules_with_errors = STRBUF_INIT, \
1328}
fe85ee6e 1329
4b4acedd
HV
1330static int get_fetch_recurse_config(const struct submodule *submodule,
1331 struct submodule_parallel_fetch *spf)
1332{
1333 if (spf->command_line_option != RECURSE_SUBMODULES_DEFAULT)
1334 return spf->command_line_option;
1335
1336 if (submodule) {
1337 char *key;
1338 const char *value;
1339
1340 int fetch_recurse = submodule->fetch_recurse;
1341 key = xstrfmt("submodule.%s.fetchRecurseSubmodules", submodule->name);
f1de981e 1342 if (!repo_config_get_string_tmp(spf->r, key, &value)) {
4b4acedd
HV
1343 fetch_recurse = parse_fetch_recurse_submodules_arg(key, value);
1344 }
1345 free(key);
1346
1347 if (fetch_recurse != RECURSE_SUBMODULES_NONE)
1348 /* local config overrules everything except commandline */
1349 return fetch_recurse;
1350 }
1351
1352 return spf->default_option;
1353}
1354
be76c212
SB
1355/*
1356 * Fetch in progress (if callback data) or
1357 * pending (if in oid_fetch_tasks in struct submodule_parallel_fetch)
1358 */
1359struct fetch_task {
1360 struct repository *repo;
1361 const struct submodule *sub;
1362 unsigned free_sub : 1; /* Do we need to free the submodule? */
1363
1364 struct oid_array *commits; /* Ensure these commits are fetched */
1365};
1366
1367/**
1368 * When a submodule is not defined in .gitmodules, we cannot access it
1369 * via the regular submodule-config. Create a fake submodule, which we can
1370 * work on.
1371 */
1372static const struct submodule *get_non_gitmodules_submodule(const char *path)
1373{
1374 struct submodule *ret = NULL;
1375 const char *name = default_name_or_path(path);
1376
1377 if (!name)
1378 return NULL;
1379
1380 ret = xmalloc(sizeof(*ret));
1381 memset(ret, 0, sizeof(*ret));
1382 ret->path = name;
1383 ret->name = name;
1384
1385 return (const struct submodule *) ret;
1386}
1387
1388static struct fetch_task *fetch_task_create(struct repository *r,
1389 const char *path)
1390{
1391 struct fetch_task *task = xmalloc(sizeof(*task));
1392 memset(task, 0, sizeof(*task));
1393
14228447 1394 task->sub = submodule_from_path(r, null_oid(), path);
be76c212
SB
1395 if (!task->sub) {
1396 /*
1397 * No entry in .gitmodules? Technically not a submodule,
1398 * but historically we supported repositories that happen to be
1399 * in-place where a gitlink is. Keep supporting them.
1400 */
1401 task->sub = get_non_gitmodules_submodule(path);
1402 if (!task->sub) {
1403 free(task);
1404 return NULL;
1405 }
1406
1407 task->free_sub = 1;
1408 }
1409
1410 return task;
1411}
1412
1413static void fetch_task_release(struct fetch_task *p)
1414{
1415 if (p->free_sub)
1416 free((void*)p->sub);
1417 p->free_sub = 0;
1418 p->sub = NULL;
1419
1420 if (p->repo)
1421 repo_clear(p->repo);
1422 FREE_AND_NULL(p->repo);
1423}
1424
26f80ccf
SB
1425static struct repository *get_submodule_repo_for(struct repository *r,
1426 const struct submodule *sub)
1427{
1428 struct repository *ret = xmalloc(sizeof(*ret));
1429
1430 if (repo_submodule_init(ret, r, sub)) {
1431 /*
1432 * No entry in .gitmodules? Technically not a submodule,
1433 * but historically we supported repositories that happen to be
1434 * in-place where a gitlink is. Keep supporting them.
1435 */
1436 struct strbuf gitdir = STRBUF_INIT;
1437 strbuf_repo_worktree_path(&gitdir, r, "%s/.git", sub->path);
1438 if (repo_init(ret, gitdir.buf, NULL)) {
1439 strbuf_release(&gitdir);
1440 free(ret);
1441 return NULL;
1442 }
1443 strbuf_release(&gitdir);
1444 }
1445
1446 return ret;
1447}
1448
fe85ee6e
SB
1449static int get_next_submodule(struct child_process *cp,
1450 struct strbuf *err, void *data, void **task_cb)
7dce19d3 1451{
fe85ee6e 1452 struct submodule_parallel_fetch *spf = data;
6859de45 1453
e724197f 1454 for (; spf->count < spf->r->index->cache_nr; spf->count++) {
e724197f 1455 const struct cache_entry *ce = spf->r->index->cache[spf->count];
26f80ccf 1456 const char *default_argv;
be76c212 1457 struct fetch_task *task;
7dce19d3
JL
1458
1459 if (!S_ISGITLINK(ce->ce_mode))
1460 continue;
1461
be76c212
SB
1462 task = fetch_task_create(spf->r, ce->name);
1463 if (!task)
1464 continue;
492c6c46 1465
be76c212 1466 switch (get_fetch_recurse_config(task->sub, spf))
4b4acedd
HV
1467 {
1468 default:
1469 case RECURSE_SUBMODULES_DEFAULT:
1470 case RECURSE_SUBMODULES_ON_DEMAND:
be76c212 1471 if (!task->sub ||
08a297bd 1472 !string_list_lookup(
16dd6fe1 1473 &spf->changed_submodule_names,
be76c212 1474 task->sub->name))
8f0700dd
JL
1475 continue;
1476 default_argv = "on-demand";
4b4acedd
HV
1477 break;
1478 case RECURSE_SUBMODULES_ON:
1479 default_argv = "yes";
1480 break;
1481 case RECURSE_SUBMODULES_OFF:
1482 continue;
be254a0e
JL
1483 }
1484
be76c212
SB
1485 task->repo = get_submodule_repo_for(spf->r, task->sub);
1486 if (task->repo) {
1487 struct strbuf submodule_prefix = STRBUF_INIT;
fe85ee6e 1488 child_process_init(cp);
be76c212 1489 cp->dir = task->repo->gitdir;
a62387b3 1490 prepare_submodule_repo_env_in_gitdir(&cp->env_array);
fe85ee6e
SB
1491 cp->git_cmd = 1;
1492 if (!spf->quiet)
a4ffbbbb 1493 strbuf_addf(err, _("Fetching submodule %s%s\n"),
fe85ee6e 1494 spf->prefix, ce->name);
c972bf4c 1495 strvec_init(&cp->args);
d70a9eb6 1496 strvec_pushv(&cp->args, spf->args.v);
c972bf4c
JK
1497 strvec_push(&cp->args, default_argv);
1498 strvec_push(&cp->args, "--submodule-prefix");
be76c212
SB
1499
1500 strbuf_addf(&submodule_prefix, "%s%s/",
1501 spf->prefix,
1502 task->sub->path);
c972bf4c 1503 strvec_push(&cp->args, submodule_prefix.buf);
26f80ccf 1504
fe85ee6e 1505 spf->count++;
be76c212
SB
1506 *task_cb = task;
1507
1508 strbuf_release(&submodule_prefix);
fe85ee6e 1509 return 1;
26f80ccf 1510 } else {
505a2765 1511 struct strbuf empty_submodule_path = STRBUF_INIT;
be76c212
SB
1512
1513 fetch_task_release(task);
1514 free(task);
1515
26f80ccf
SB
1516 /*
1517 * An empty directory is normal,
1518 * the submodule is not initialized
1519 */
505a2765
PK
1520 strbuf_addf(&empty_submodule_path, "%s/%s/",
1521 spf->r->worktree,
1522 ce->name);
26f80ccf 1523 if (S_ISGITLINK(ce->ce_mode) &&
505a2765 1524 !is_empty_dir(empty_submodule_path.buf)) {
26f80ccf
SB
1525 spf->result = 1;
1526 strbuf_addf(err,
303b3c1c 1527 _("Could not access submodule '%s'\n"),
26f80ccf
SB
1528 ce->name);
1529 }
505a2765 1530 strbuf_release(&empty_submodule_path);
fe85ee6e 1531 }
7dce19d3 1532 }
be76c212
SB
1533
1534 if (spf->oid_fetch_tasks_nr) {
1535 struct fetch_task *task =
1536 spf->oid_fetch_tasks[spf->oid_fetch_tasks_nr - 1];
1537 struct strbuf submodule_prefix = STRBUF_INIT;
1538 spf->oid_fetch_tasks_nr--;
1539
1540 strbuf_addf(&submodule_prefix, "%s%s/",
1541 spf->prefix, task->sub->path);
1542
1543 child_process_init(cp);
1544 prepare_submodule_repo_env_in_gitdir(&cp->env_array);
1545 cp->git_cmd = 1;
1546 cp->dir = task->repo->gitdir;
1547
c972bf4c 1548 strvec_init(&cp->args);
d70a9eb6 1549 strvec_pushv(&cp->args, spf->args.v);
c972bf4c
JK
1550 strvec_push(&cp->args, "on-demand");
1551 strvec_push(&cp->args, "--submodule-prefix");
1552 strvec_push(&cp->args, submodule_prefix.buf);
be76c212
SB
1553
1554 /* NEEDSWORK: have get_default_remote from submodule--helper */
c972bf4c 1555 strvec_push(&cp->args, "origin");
be76c212
SB
1556 oid_array_for_each_unique(task->commits,
1557 append_oid_to_argv, &cp->args);
1558
1559 *task_cb = task;
7dce19d3 1560 strbuf_release(&submodule_prefix);
be76c212 1561 return 1;
7dce19d3 1562 }
be76c212 1563
fe85ee6e
SB
1564 return 0;
1565}
1566
2a73b3da 1567static int fetch_start_failure(struct strbuf *err,
fe85ee6e
SB
1568 void *cb, void *task_cb)
1569{
1570 struct submodule_parallel_fetch *spf = cb;
be76c212 1571 struct fetch_task *task = task_cb;
fe85ee6e
SB
1572
1573 spf->result = 1;
1574
be76c212 1575 fetch_task_release(task);
fe85ee6e
SB
1576 return 0;
1577}
1578
be76c212
SB
1579static int commit_missing_in_sub(const struct object_id *oid, void *data)
1580{
1581 struct repository *subrepo = data;
1582
1583 enum object_type type = oid_object_info(subrepo, oid, NULL);
1584
1585 return type != OBJ_COMMIT;
1586}
1587
2a73b3da
SB
1588static int fetch_finish(int retvalue, struct strbuf *err,
1589 void *cb, void *task_cb)
fe85ee6e
SB
1590{
1591 struct submodule_parallel_fetch *spf = cb;
be76c212
SB
1592 struct fetch_task *task = task_cb;
1593
1594 struct string_list_item *it;
1595 struct oid_array *commits;
fe85ee6e 1596
02225408
ES
1597 if (!task || !task->sub)
1598 BUG("callback cookie bogus");
1599
1600 if (retvalue) {
bd5e567d
JT
1601 /*
1602 * NEEDSWORK: This indicates that the overall fetch
1603 * failed, even though there may be a subsequent fetch
1604 * by commit hash that might work. It may be a good
1605 * idea to not indicate failure in this case, and only
1606 * indicate failure if the subsequent fetch fails.
1607 */
fe85ee6e
SB
1608 spf->result = 1;
1609
02225408
ES
1610 strbuf_addf(&spf->submodules_with_errors, "\t%s\n",
1611 task->sub->name);
1612 }
be76c212
SB
1613
1614 /* Is this the second time we process this submodule? */
1615 if (task->commits)
1616 goto out;
1617
1618 it = string_list_lookup(&spf->changed_submodule_names, task->sub->name);
1619 if (!it)
1620 /* Could be an unchanged submodule, not contained in the list */
1621 goto out;
1622
1623 commits = it->util;
1624 oid_array_filter(commits,
1625 commit_missing_in_sub,
1626 task->repo);
1627
1628 /* Are there commits we want, but do not exist? */
1629 if (commits->nr) {
1630 task->commits = commits;
1631 ALLOC_GROW(spf->oid_fetch_tasks,
1632 spf->oid_fetch_tasks_nr + 1,
1633 spf->oid_fetch_tasks_alloc);
1634 spf->oid_fetch_tasks[spf->oid_fetch_tasks_nr] = task;
1635 spf->oid_fetch_tasks_nr++;
1636 return 0;
1637 }
1638
1639out:
1640 fetch_task_release(task);
1641
fe85ee6e
SB
1642 return 0;
1643}
1644
e724197f 1645int fetch_populated_submodules(struct repository *r,
c972bf4c 1646 const struct strvec *options,
fe85ee6e 1647 const char *prefix, int command_line_option,
8fa29159 1648 int default_option,
62104ba1 1649 int quiet, int max_parallel_jobs)
fe85ee6e
SB
1650{
1651 int i;
fe85ee6e
SB
1652 struct submodule_parallel_fetch spf = SPF_INIT;
1653
e724197f 1654 spf.r = r;
fe85ee6e 1655 spf.command_line_option = command_line_option;
8fa29159 1656 spf.default_option = default_option;
fe85ee6e
SB
1657 spf.quiet = quiet;
1658 spf.prefix = prefix;
1659
e724197f 1660 if (!r->worktree)
fe85ee6e
SB
1661 goto out;
1662
e724197f 1663 if (repo_read_index(r) < 0)
a4ffbbbb 1664 die(_("index file corrupt"));
fe85ee6e 1665
c972bf4c 1666 strvec_push(&spf.args, "fetch");
d70a9eb6
JK
1667 for (i = 0; i < options->nr; i++)
1668 strvec_push(&spf.args, options->v[i]);
c972bf4c 1669 strvec_push(&spf.args, "--recurse-submodules-default");
fe85ee6e
SB
1670 /* default value, "--submodule-prefix" and its value are added later */
1671
16dd6fe1
SB
1672 calculate_changed_submodule_paths(r, &spf.changed_submodule_names);
1673 string_list_sort(&spf.changed_submodule_names);
ee4512ed
JH
1674 run_processes_parallel_tr2(max_parallel_jobs,
1675 get_next_submodule,
1676 fetch_start_failure,
1677 fetch_finish,
1678 &spf,
1679 "submodule", "parallel/fetch");
fe85ee6e 1680
02225408
ES
1681 if (spf.submodules_with_errors.len > 0)
1682 fprintf(stderr, _("Errors during submodule fetch:\n%s"),
1683 spf.submodules_with_errors.buf);
1684
1685
c972bf4c 1686 strvec_clear(&spf.args);
88a21979 1687out:
bcd73372 1688 free_submodules_oids(&spf.changed_submodule_names);
fe85ee6e 1689 return spf.result;
7dce19d3
JL
1690}
1691
3bfc4504 1692unsigned is_submodule_modified(const char *path, int ignore_untracked)
ee6fc514 1693{
d3180279 1694 struct child_process cp = CHILD_PROCESS_INIT;
ee6fc514 1695 struct strbuf buf = STRBUF_INIT;
af6865a7 1696 FILE *fp;
c7e1a736 1697 unsigned dirty_submodule = 0;
eee49b6c 1698 const char *git_dir;
af6865a7 1699 int ignore_cp_exit_code = 0;
ee6fc514 1700
eee49b6c 1701 strbuf_addf(&buf, "%s/.git", path);
13d6ec91 1702 git_dir = read_gitfile(buf.buf);
eee49b6c
JL
1703 if (!git_dir)
1704 git_dir = buf.buf;
5c896f7c
SB
1705 if (!is_git_directory(git_dir)) {
1706 if (is_directory(git_dir))
1707 die(_("'%s' not recognized as a git repository"), git_dir);
ee6fc514
JL
1708 strbuf_release(&buf);
1709 /* The submodule is not checked out, so it is not modified */
1710 return 0;
ee6fc514
JL
1711 }
1712 strbuf_reset(&buf);
1713
c972bf4c 1714 strvec_pushl(&cp.args, "status", "--porcelain=2", NULL);
3bfc4504 1715 if (ignore_untracked)
c972bf4c 1716 strvec_push(&cp.args, "-uno");
3bfc4504 1717
c12e8656 1718 prepare_submodule_repo_env(&cp.env_array);
ee6fc514
JL
1719 cp.git_cmd = 1;
1720 cp.no_stdin = 1;
1721 cp.out = -1;
eee49b6c 1722 cp.dir = path;
ee6fc514 1723 if (start_command(&cp))
a4ffbbbb 1724 die(_("Could not run 'git status --porcelain=2' in submodule %s"), path);
ee6fc514 1725
af6865a7
SB
1726 fp = xfdopen(cp.out, "r");
1727 while (strbuf_getwholeline(&buf, fp, '\n') != EOF) {
fcecf0b9
SB
1728 /* regular untracked files */
1729 if (buf.buf[0] == '?')
c7e1a736 1730 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
40069d6e
SB
1731
1732 if (buf.buf[0] == 'u' ||
1733 buf.buf[0] == '1' ||
1734 buf.buf[0] == '2') {
1735 /* T = line type, XY = status, SSSS = submodule state */
1736 if (buf.len < strlen("T XY SSSS"))
033abf97 1737 BUG("invalid status --porcelain=2 line %s",
40069d6e
SB
1738 buf.buf);
1739
1740 if (buf.buf[5] == 'S' && buf.buf[8] == 'U')
1741 /* nested untracked file */
1742 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
1743
1744 if (buf.buf[0] == 'u' ||
1745 buf.buf[0] == '2' ||
1746 memcmp(buf.buf + 5, "S..U", 4))
1747 /* other change */
1748 dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
c7e1a736 1749 }
64f9a946
SB
1750
1751 if ((dirty_submodule & DIRTY_SUBMODULE_MODIFIED) &&
1752 ((dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ||
af6865a7
SB
1753 ignore_untracked)) {
1754 /*
1755 * We're not interested in any further information from
1756 * the child any more, neither output nor its exit code.
1757 */
1758 ignore_cp_exit_code = 1;
c7e1a736 1759 break;
af6865a7 1760 }
c7e1a736 1761 }
af6865a7 1762 fclose(fp);
ee6fc514 1763
af6865a7 1764 if (finish_command(&cp) && !ignore_cp_exit_code)
a4ffbbbb 1765 die(_("'git status --porcelain=2' failed in submodule %s"), path);
ee6fc514 1766
ee6fc514 1767 strbuf_release(&buf);
c7e1a736 1768 return dirty_submodule;
ee6fc514 1769}
68d03e4a 1770
293ab15e
JL
1771int submodule_uses_gitfile(const char *path)
1772{
d3180279 1773 struct child_process cp = CHILD_PROCESS_INIT;
293ab15e
JL
1774 struct strbuf buf = STRBUF_INIT;
1775 const char *git_dir;
1776
1777 strbuf_addf(&buf, "%s/.git", path);
1778 git_dir = read_gitfile(buf.buf);
1779 if (!git_dir) {
1780 strbuf_release(&buf);
1781 return 0;
1782 }
1783 strbuf_release(&buf);
1784
1785 /* Now test that all nested submodules use a gitfile too */
afbdba39
JH
1786 strvec_pushl(&cp.args,
1787 "submodule", "foreach", "--quiet", "--recursive",
1788 "test -f .git", NULL);
1789
c12e8656 1790 prepare_submodule_repo_env(&cp.env_array);
293ab15e
JL
1791 cp.git_cmd = 1;
1792 cp.no_stdin = 1;
1793 cp.no_stderr = 1;
1794 cp.no_stdout = 1;
1795 cp.dir = path;
1796 if (run_command(&cp))
1797 return 0;
1798
1799 return 1;
1800}
1801
83b76966
SB
1802/*
1803 * Check if it is a bad idea to remove a submodule, i.e. if we'd lose data
1804 * when doing so.
1805 *
1806 * Return 1 if we'd lose data, return 0 if the removal is fine,
1807 * and negative values for errors.
1808 */
1809int bad_to_remove_submodule(const char *path, unsigned flags)
293ab15e 1810{
293ab15e 1811 ssize_t len;
d3180279 1812 struct child_process cp = CHILD_PROCESS_INIT;
293ab15e 1813 struct strbuf buf = STRBUF_INIT;
83b76966 1814 int ret = 0;
293ab15e 1815
dbe44faa 1816 if (!file_exists(path) || is_empty_dir(path))
83b76966 1817 return 0;
293ab15e
JL
1818
1819 if (!submodule_uses_gitfile(path))
83b76966 1820 return 1;
293ab15e 1821
c972bf4c 1822 strvec_pushl(&cp.args, "status", "--porcelain",
f6d8942b 1823 "--ignore-submodules=none", NULL);
83b76966
SB
1824
1825 if (flags & SUBMODULE_REMOVAL_IGNORE_UNTRACKED)
c972bf4c 1826 strvec_push(&cp.args, "-uno");
83b76966 1827 else
c972bf4c 1828 strvec_push(&cp.args, "-uall");
83b76966
SB
1829
1830 if (!(flags & SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED))
c972bf4c 1831 strvec_push(&cp.args, "--ignored");
293ab15e 1832
c12e8656 1833 prepare_submodule_repo_env(&cp.env_array);
293ab15e
JL
1834 cp.git_cmd = 1;
1835 cp.no_stdin = 1;
1836 cp.out = -1;
1837 cp.dir = path;
83b76966
SB
1838 if (start_command(&cp)) {
1839 if (flags & SUBMODULE_REMOVAL_DIE_ON_ERROR)
35ad44cb 1840 die(_("could not start 'git status' in submodule '%s'"),
83b76966
SB
1841 path);
1842 ret = -1;
1843 goto out;
1844 }
293ab15e
JL
1845
1846 len = strbuf_read(&buf, cp.out, 1024);
1847 if (len > 2)
83b76966 1848 ret = 1;
293ab15e
JL
1849 close(cp.out);
1850
83b76966
SB
1851 if (finish_command(&cp)) {
1852 if (flags & SUBMODULE_REMOVAL_DIE_ON_ERROR)
35ad44cb 1853 die(_("could not run 'git status' in submodule '%s'"),
83b76966
SB
1854 path);
1855 ret = -1;
1856 }
1857out:
293ab15e 1858 strbuf_release(&buf);
83b76966 1859 return ret;
293ab15e
JL
1860}
1861
898c2e65
SB
1862void submodule_unset_core_worktree(const struct submodule *sub)
1863{
ce125d43 1864 struct strbuf config_path = STRBUF_INIT;
898c2e65 1865
ce125d43
JT
1866 submodule_name_to_gitdir(&config_path, the_repository, sub->name);
1867 strbuf_addstr(&config_path, "/config");
1868
1869 if (git_config_set_in_file_gently(config_path.buf, "core.worktree", NULL))
898c2e65
SB
1870 warning(_("Could not unset core.worktree setting in submodule '%s'"),
1871 sub->path);
1872
ce125d43 1873 strbuf_release(&config_path);
898c2e65
SB
1874}
1875
202275b9
SB
1876static const char *get_super_prefix_or_empty(void)
1877{
1878 const char *s = get_super_prefix();
1879 if (!s)
1880 s = "";
1881 return s;
1882}
1883
6e3c1595
SB
1884static int submodule_has_dirty_index(const struct submodule *sub)
1885{
1886 struct child_process cp = CHILD_PROCESS_INIT;
1887
da27bc81 1888 prepare_submodule_repo_env(&cp.env_array);
6e3c1595
SB
1889
1890 cp.git_cmd = 1;
c972bf4c 1891 strvec_pushl(&cp.args, "diff-index", "--quiet",
f6d8942b 1892 "--cached", "HEAD", NULL);
6e3c1595
SB
1893 cp.no_stdin = 1;
1894 cp.no_stdout = 1;
1895 cp.dir = sub->path;
1896 if (start_command(&cp))
a4ffbbbb 1897 die(_("could not recurse into submodule '%s'"), sub->path);
6e3c1595
SB
1898
1899 return finish_command(&cp);
1900}
1901
1902static void submodule_reset_index(const char *path)
1903{
1904 struct child_process cp = CHILD_PROCESS_INIT;
da27bc81 1905 prepare_submodule_repo_env(&cp.env_array);
6e3c1595
SB
1906
1907 cp.git_cmd = 1;
1908 cp.no_stdin = 1;
1909 cp.dir = path;
1910
c972bf4c 1911 strvec_pushf(&cp.args, "--super-prefix=%s%s/",
f6d8942b 1912 get_super_prefix_or_empty(), path);
c972bf4c 1913 strvec_pushl(&cp.args, "read-tree", "-u", "--reset", NULL);
6e3c1595 1914
c972bf4c 1915 strvec_push(&cp.args, empty_tree_oid_hex());
6e3c1595
SB
1916
1917 if (run_command(&cp))
a4ffbbbb 1918 die(_("could not reset submodule index"));
6e3c1595
SB
1919}
1920
1921/**
1922 * Moves a submodule at a given path from a given head to another new head.
1923 * For edge cases (a submodule coming into existence or removing a submodule)
1924 * pass NULL for old or new respectively.
1925 */
1926int submodule_move_head(const char *path,
bc099914
BW
1927 const char *old_head,
1928 const char *new_head,
6e3c1595
SB
1929 unsigned flags)
1930{
1931 int ret = 0;
1932 struct child_process cp = CHILD_PROCESS_INIT;
1933 const struct submodule *sub;
f2d48994 1934 int *error_code_ptr, error_code;
6e3c1595 1935
627d9342 1936 if (!is_submodule_active(the_repository, path))
823bab09
SB
1937 return 0;
1938
f2d48994
SB
1939 if (flags & SUBMODULE_MOVE_HEAD_FORCE)
1940 /*
1941 * Pass non NULL pointer to is_submodule_populated_gently
1942 * to prevent die()-ing. We'll use connect_work_tree_and_git_dir
1943 * to fixup the submodule in the force case later.
1944 */
1945 error_code_ptr = &error_code;
1946 else
1947 error_code_ptr = NULL;
1948
bc099914 1949 if (old_head && !is_submodule_populated_gently(path, error_code_ptr))
f2d48994 1950 return 0;
6e3c1595 1951
14228447 1952 sub = submodule_from_path(the_repository, null_oid(), path);
6e3c1595
SB
1953
1954 if (!sub)
033abf97 1955 BUG("could not get submodule information for '%s'", path);
6e3c1595 1956
bc099914 1957 if (old_head && !(flags & SUBMODULE_MOVE_HEAD_FORCE)) {
6e3c1595
SB
1958 /* Check if the submodule has a dirty index. */
1959 if (submodule_has_dirty_index(sub))
1960 return error(_("submodule '%s' has dirty index"), path);
1961 }
1962
1963 if (!(flags & SUBMODULE_MOVE_HEAD_DRY_RUN)) {
bc099914 1964 if (old_head) {
6e3c1595 1965 if (!submodule_uses_gitfile(path))
cf7a901a 1966 absorb_git_dir_into_superproject(path,
6e3c1595
SB
1967 ABSORB_GITDIR_RECURSE_SUBMODULES);
1968 } else {
ce125d43
JT
1969 struct strbuf gitdir = STRBUF_INIT;
1970 submodule_name_to_gitdir(&gitdir, the_repository,
1971 sub->name);
1972 connect_work_tree_and_git_dir(path, gitdir.buf, 0);
1973 strbuf_release(&gitdir);
6e3c1595
SB
1974
1975 /* make sure the index is clean as well */
1976 submodule_reset_index(path);
1977 }
f2d48994 1978
bc099914 1979 if (old_head && (flags & SUBMODULE_MOVE_HEAD_FORCE)) {
ce125d43
JT
1980 struct strbuf gitdir = STRBUF_INIT;
1981 submodule_name_to_gitdir(&gitdir, the_repository,
1982 sub->name);
1983 connect_work_tree_and_git_dir(path, gitdir.buf, 1);
1984 strbuf_release(&gitdir);
f2d48994 1985 }
6e3c1595
SB
1986 }
1987
da27bc81 1988 prepare_submodule_repo_env(&cp.env_array);
6e3c1595
SB
1989
1990 cp.git_cmd = 1;
1991 cp.no_stdin = 1;
1992 cp.dir = path;
1993
c972bf4c 1994 strvec_pushf(&cp.args, "--super-prefix=%s%s/",
f6d8942b 1995 get_super_prefix_or_empty(), path);
c972bf4c 1996 strvec_pushl(&cp.args, "read-tree", "--recurse-submodules", NULL);
6e3c1595
SB
1997
1998 if (flags & SUBMODULE_MOVE_HEAD_DRY_RUN)
c972bf4c 1999 strvec_push(&cp.args, "-n");
6e3c1595 2000 else
c972bf4c 2001 strvec_push(&cp.args, "-u");
6e3c1595
SB
2002
2003 if (flags & SUBMODULE_MOVE_HEAD_FORCE)
c972bf4c 2004 strvec_push(&cp.args, "--reset");
6e3c1595 2005 else
c972bf4c 2006 strvec_push(&cp.args, "-m");
6e3c1595 2007
7dcc1f4d 2008 if (!(flags & SUBMODULE_MOVE_HEAD_FORCE))
c972bf4c 2009 strvec_push(&cp.args, old_head ? old_head : empty_tree_oid_hex());
7dcc1f4d 2010
c972bf4c 2011 strvec_push(&cp.args, new_head ? new_head : empty_tree_oid_hex());
6e3c1595
SB
2012
2013 if (run_command(&cp)) {
ba95d4e4 2014 ret = error(_("Submodule '%s' could not be updated."), path);
6e3c1595
SB
2015 goto out;
2016 }
2017
2018 if (!(flags & SUBMODULE_MOVE_HEAD_DRY_RUN)) {
bc099914 2019 if (new_head) {
a17062cf 2020 child_process_init(&cp);
6e3c1595 2021 /* also set the HEAD accordingly */
a17062cf
SB
2022 cp.git_cmd = 1;
2023 cp.no_stdin = 1;
2024 cp.dir = path;
6e3c1595 2025
218c8837 2026 prepare_submodule_repo_env(&cp.env_array);
c972bf4c 2027 strvec_pushl(&cp.args, "update-ref", "HEAD",
f6d8942b 2028 "--no-deref", new_head, NULL);
6e3c1595 2029
a17062cf 2030 if (run_command(&cp)) {
6e3c1595
SB
2031 ret = -1;
2032 goto out;
2033 }
2034 } else {
2035 struct strbuf sb = STRBUF_INIT;
2036
2037 strbuf_addf(&sb, "%s/.git", path);
2038 unlink_or_warn(sb.buf);
2039 strbuf_release(&sb);
2040
2041 if (is_empty_dir(path))
2042 rmdir_or_warn(path);
898c2e65
SB
2043
2044 submodule_unset_core_worktree(sub);
6e3c1595
SB
2045 }
2046 }
2047out:
2048 return ret;
2049}
2050
a8dee3ca
JS
2051int validate_submodule_git_dir(char *git_dir, const char *submodule_name)
2052{
2053 size_t len = strlen(git_dir), suffix_len = strlen(submodule_name);
2054 char *p;
2055 int ret = 0;
2056
2057 if (len <= suffix_len || (p = git_dir + len - suffix_len)[-1] != '/' ||
2058 strcmp(p, submodule_name))
2059 BUG("submodule name '%s' not a suffix of git dir '%s'",
2060 submodule_name, git_dir);
2061
2062 /*
2063 * We prevent the contents of sibling submodules' git directories to
2064 * clash.
2065 *
2066 * Example: having a submodule named `hippo` and another one named
2067 * `hippo/hooks` would result in the git directories
2068 * `.git/modules/hippo/` and `.git/modules/hippo/hooks/`, respectively,
2069 * but the latter directory is already designated to contain the hooks
2070 * of the former.
2071 */
2072 for (; *p; p++) {
2073 if (is_dir_sep(*p)) {
2074 char c = *p;
2075
2076 *p = '\0';
2077 if (is_git_directory(git_dir))
2078 ret = -1;
2079 *p = c;
2080
2081 if (ret < 0)
2082 return error(_("submodule git dir '%s' is "
2083 "inside git dir '%.*s'"),
2084 git_dir,
2085 (int)(p - git_dir), git_dir);
2086 }
2087 }
2088
2089 return 0;
2090}
2091
f6f85861
SB
2092/*
2093 * Embeds a single submodules git directory into the superprojects git dir,
2094 * non recursively.
2095 */
cf7a901a 2096static void relocate_single_git_dir_into_superproject(const char *path)
f6f85861
SB
2097{
2098 char *old_git_dir = NULL, *real_old_git_dir = NULL, *real_new_git_dir = NULL;
ce125d43 2099 struct strbuf new_gitdir = STRBUF_INIT;
f6f85861
SB
2100 const struct submodule *sub;
2101
2102 if (submodule_uses_worktrees(path))
2103 die(_("relocate_gitdir for submodule '%s' with "
2104 "more than one worktree not supported"), path);
2105
2106 old_git_dir = xstrfmt("%s/.git", path);
2107 if (read_gitfile(old_git_dir))
2108 /* If it is an actual gitfile, it doesn't need migration. */
2109 return;
2110
ce83eadd 2111 real_old_git_dir = real_pathdup(old_git_dir, 1);
f6f85861 2112
14228447 2113 sub = submodule_from_path(the_repository, null_oid(), path);
f6f85861
SB
2114 if (!sub)
2115 die(_("could not lookup name for submodule '%s'"), path);
2116
ce125d43
JT
2117 submodule_name_to_gitdir(&new_gitdir, the_repository, sub->name);
2118 if (validate_submodule_git_dir(new_gitdir.buf, sub->name) < 0)
a8dee3ca
JS
2119 die(_("refusing to move '%s' into an existing git dir"),
2120 real_old_git_dir);
ce125d43
JT
2121 if (safe_create_leading_directories_const(new_gitdir.buf) < 0)
2122 die(_("could not create directory '%s'"), new_gitdir.buf);
2123 real_new_git_dir = real_pathdup(new_gitdir.buf, 1);
f6f85861 2124
f6f85861 2125 fprintf(stderr, _("Migrating git directory of '%s%s' from\n'%s' to\n'%s'\n"),
202275b9 2126 get_super_prefix_or_empty(), path,
f6f85861
SB
2127 real_old_git_dir, real_new_git_dir);
2128
2129 relocate_gitdir(path, real_old_git_dir, real_new_git_dir);
2130
2131 free(old_git_dir);
2132 free(real_old_git_dir);
2133 free(real_new_git_dir);
ce125d43 2134 strbuf_release(&new_gitdir);
f6f85861
SB
2135}
2136
2137/*
2138 * Migrate the git directory of the submodule given by path from
2139 * having its git directory within the working tree to the git dir nested
2140 * in its superprojects git dir under modules/.
2141 */
cf7a901a 2142void absorb_git_dir_into_superproject(const char *path,
f6f85861
SB
2143 unsigned flags)
2144{
ec9629b3
SB
2145 int err_code;
2146 const char *sub_git_dir;
f6f85861 2147 struct strbuf gitdir = STRBUF_INIT;
f6f85861 2148 strbuf_addf(&gitdir, "%s/.git", path);
ec9629b3 2149 sub_git_dir = resolve_gitdir_gently(gitdir.buf, &err_code);
f6f85861
SB
2150
2151 /* Not populated? */
ec9629b3 2152 if (!sub_git_dir) {
ec9629b3 2153 const struct submodule *sub;
ce125d43 2154 struct strbuf sub_gitdir = STRBUF_INIT;
ec9629b3
SB
2155
2156 if (err_code == READ_GITFILE_ERR_STAT_FAILED) {
2157 /* unpopulated as expected */
2158 strbuf_release(&gitdir);
2159 return;
2160 }
2161
2162 if (err_code != READ_GITFILE_ERR_NOT_A_REPO)
2163 /* We don't know what broke here. */
2164 read_gitfile_error_die(err_code, path, NULL);
2165
2166 /*
2167 * Maybe populated, but no git directory was found?
2168 * This can happen if the superproject is a submodule
2169 * itself and was just absorbed. The absorption of the
2170 * superproject did not rewrite the git file links yet,
2171 * fix it now.
2172 */
14228447 2173 sub = submodule_from_path(the_repository, null_oid(), path);
ec9629b3
SB
2174 if (!sub)
2175 die(_("could not lookup name for submodule '%s'"), path);
ce125d43
JT
2176 submodule_name_to_gitdir(&sub_gitdir, the_repository, sub->name);
2177 connect_work_tree_and_git_dir(path, sub_gitdir.buf, 0);
2178 strbuf_release(&sub_gitdir);
ec9629b3
SB
2179 } else {
2180 /* Is it already absorbed into the superprojects git dir? */
ce83eadd
JS
2181 char *real_sub_git_dir = real_pathdup(sub_git_dir, 1);
2182 char *real_common_git_dir = real_pathdup(get_git_common_dir(), 1);
f6f85861 2183
ec9629b3 2184 if (!starts_with(real_sub_git_dir, real_common_git_dir))
cf7a901a 2185 relocate_single_git_dir_into_superproject(path);
ec9629b3
SB
2186
2187 free(real_sub_git_dir);
2188 free(real_common_git_dir);
2189 }
2190 strbuf_release(&gitdir);
f6f85861
SB
2191
2192 if (flags & ABSORB_GITDIR_RECURSE_SUBMODULES) {
2193 struct child_process cp = CHILD_PROCESS_INIT;
2194 struct strbuf sb = STRBUF_INIT;
2195
2196 if (flags & ~ABSORB_GITDIR_RECURSE_SUBMODULES)
033abf97 2197 BUG("we don't know how to pass the flags down?");
f6f85861 2198
202275b9 2199 strbuf_addstr(&sb, get_super_prefix_or_empty());
f6f85861
SB
2200 strbuf_addstr(&sb, path);
2201 strbuf_addch(&sb, '/');
2202
2203 cp.dir = path;
2204 cp.git_cmd = 1;
2205 cp.no_stdin = 1;
c972bf4c 2206 strvec_pushl(&cp.args, "--super-prefix", sb.buf,
f6d8942b
JK
2207 "submodule--helper",
2208 "absorb-git-dirs", NULL);
f6f85861
SB
2209 prepare_submodule_repo_env(&cp.env_array);
2210 if (run_command(&cp))
2211 die(_("could not recurse into submodule '%s'"), path);
2212
2213 strbuf_release(&sb);
2214 }
f6f85861 2215}
bf0231c6 2216
49d3c4b4 2217int get_superproject_working_tree(struct strbuf *buf)
bf0231c6
SB
2218{
2219 struct child_process cp = CHILD_PROCESS_INIT;
2220 struct strbuf sb = STRBUF_INIT;
4530a85b 2221 struct strbuf one_up = STRBUF_INIT;
bf0231c6 2222 const char *cwd = xgetcwd();
49d3c4b4 2223 int ret = 0;
bf0231c6
SB
2224 const char *subpath;
2225 int code;
2226 ssize_t len;
2227
2228 if (!is_inside_work_tree())
2229 /*
2230 * FIXME:
2231 * We might have a superproject, but it is harder
2232 * to determine.
2233 */
49d3c4b4 2234 return 0;
bf0231c6 2235
4530a85b 2236 if (!strbuf_realpath(&one_up, "../", 0))
49d3c4b4 2237 return 0;
bf0231c6 2238
4530a85b
AM
2239 subpath = relative_path(cwd, one_up.buf, &sb);
2240 strbuf_release(&one_up);
bf0231c6
SB
2241
2242 prepare_submodule_repo_env(&cp.env_array);
c972bf4c 2243 strvec_pop(&cp.env_array);
bf0231c6 2244
c972bf4c 2245 strvec_pushl(&cp.args, "--literal-pathspecs", "-C", "..",
f6d8942b
JK
2246 "ls-files", "-z", "--stage", "--full-name", "--",
2247 subpath, NULL);
bf0231c6
SB
2248 strbuf_reset(&sb);
2249
2250 cp.no_stdin = 1;
2251 cp.no_stderr = 1;
2252 cp.out = -1;
2253 cp.git_cmd = 1;
2254
2255 if (start_command(&cp))
2256 die(_("could not start ls-files in .."));
2257
2258 len = strbuf_read(&sb, cp.out, PATH_MAX);
2259 close(cp.out);
2260
2261 if (starts_with(sb.buf, "160000")) {
2262 int super_sub_len;
2263 int cwd_len = strlen(cwd);
2264 char *super_sub, *super_wt;
2265
2266 /*
2267 * There is a superproject having this repo as a submodule.
2268 * The format is <mode> SP <hash> SP <stage> TAB <full name> \0,
2269 * We're only interested in the name after the tab.
2270 */
2271 super_sub = strchr(sb.buf, '\t') + 1;
c5cbb27c 2272 super_sub_len = strlen(super_sub);
bf0231c6
SB
2273
2274 if (super_sub_len > cwd_len ||
2275 strcmp(&cwd[cwd_len - super_sub_len], super_sub))
c3c3486b 2276 BUG("returned path string doesn't match cwd?");
bf0231c6
SB
2277
2278 super_wt = xstrdup(cwd);
2279 super_wt[cwd_len - super_sub_len] = '\0';
2280
49d3c4b4
AM
2281 strbuf_realpath(buf, super_wt, 1);
2282 ret = 1;
bf0231c6
SB
2283 free(super_wt);
2284 }
2285 strbuf_release(&sb);
2286
2287 code = finish_command(&cp);
2288
2289 if (code == 128)
2290 /* '../' is not a git repository */
49d3c4b4 2291 return 0;
bf0231c6
SB
2292 if (code == 0 && len == 0)
2293 /* There is an unrelated git repository at '../' */
49d3c4b4 2294 return 0;
bf0231c6
SB
2295 if (code)
2296 die(_("ls-tree returned unexpected return code %d"), code);
2297
2298 return ret;
2299}
bbbb7de7 2300
3ce08548
HWN
2301/*
2302 * Put the gitdir for a submodule (given relative to the main
2303 * repository worktree) into `buf`, or return -1 on error.
2304 */
bbbb7de7
NTND
2305int submodule_to_gitdir(struct strbuf *buf, const char *submodule)
2306{
2307 const struct submodule *sub;
2308 const char *git_dir;
2309 int ret = 0;
2310
2311 strbuf_reset(buf);
2312 strbuf_addstr(buf, submodule);
2313 strbuf_complete(buf, '/');
2314 strbuf_addstr(buf, ".git");
2315
2316 git_dir = read_gitfile(buf->buf);
2317 if (git_dir) {
2318 strbuf_reset(buf);
2319 strbuf_addstr(buf, git_dir);
2320 }
2321 if (!is_git_directory(buf->buf)) {
14228447 2322 sub = submodule_from_path(the_repository, null_oid(),
2323 submodule);
bbbb7de7
NTND
2324 if (!sub) {
2325 ret = -1;
2326 goto cleanup;
2327 }
2328 strbuf_reset(buf);
ce125d43 2329 submodule_name_to_gitdir(buf, the_repository, sub->name);
bbbb7de7
NTND
2330 }
2331
2332cleanup:
2333 return ret;
2334}
ce125d43
JT
2335
2336void submodule_name_to_gitdir(struct strbuf *buf, struct repository *r,
2337 const char *submodule_name)
2338{
2339 /*
2340 * NEEDSWORK: The current way of mapping a submodule's name to
2341 * its location in .git/modules/ has problems with some naming
2342 * schemes. For example, if a submodule is named "foo" and
2343 * another is named "foo/bar" (whether present in the same
2344 * superproject commit or not - the problem will arise if both
2345 * superproject commits have been checked out at any point in
2346 * time), or if two submodule names only have different cases in
2347 * a case-insensitive filesystem.
2348 *
2349 * There are several solutions, including encoding the path in
2350 * some way, introducing a submodule.<name>.gitdir config in
2351 * .git/config (not .gitmodules) that allows overriding what the
2352 * gitdir of a submodule would be (and teach Git, upon noticing
2353 * a clash, to automatically determine a non-clashing name and
2354 * to write such a config), or introducing a
2355 * submodule.<name>.gitdir config in .gitmodules that repo
2356 * administrators can explicitly set. Nothing has been decided,
2357 * so for now, just append the name at the end of the path.
2358 */
2359 strbuf_repo_git_path(buf, r, "modules/");
2360 strbuf_addstr(buf, submodule_name);
2361}