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