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