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