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