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