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