Merge branch 'jc/gpg-lazy-init'
[git/debian.git] / submodule.c
blob2a057c35b749a564e793a0f80c553744f5189746
1 #include "git-compat-util.h"
2 #include "alloc.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 "hex.h"
11 #include "revision.h"
12 #include "run-command.h"
13 #include "diffcore.h"
14 #include "refs.h"
15 #include "string-list.h"
16 #include "oid-array.h"
17 #include "strvec.h"
18 #include "blob.h"
19 #include "thread-utils.h"
20 #include "quote.h"
21 #include "remote.h"
22 #include "worktree.h"
23 #include "parse-options.h"
24 #include "object-store.h"
25 #include "commit-reach.h"
26 #include "shallow.h"
28 static int config_update_recurse_submodules = RECURSE_SUBMODULES_OFF;
29 static int initialized_fetch_ref_tips;
30 static struct oid_array ref_tips_before_fetch;
31 static struct oid_array ref_tips_after_fetch;
34 * Check if the .gitmodules file is unmerged. Parsing of the .gitmodules file
35 * will be disabled because we can't guess what might be configured in
36 * .gitmodules unless the user resolves the conflict.
38 int is_gitmodules_unmerged(struct index_state *istate)
40 int pos = index_name_pos(istate, GITMODULES_FILE, strlen(GITMODULES_FILE));
41 if (pos < 0) { /* .gitmodules not found or isn't merged */
42 pos = -1 - pos;
43 if (istate->cache_nr > pos) { /* there is a .gitmodules */
44 const struct cache_entry *ce = istate->cache[pos];
45 if (ce_namelen(ce) == strlen(GITMODULES_FILE) &&
46 !strcmp(ce->name, GITMODULES_FILE))
47 return 1;
51 return 0;
55 * Check if the .gitmodules file is safe to write.
57 * Writing to the .gitmodules file requires that the file exists in the
58 * working tree or, if it doesn't, that a brand new .gitmodules file is going
59 * to be created (i.e. it's neither in the index nor in the current branch).
61 * It is not safe to write to .gitmodules if it's not in the working tree but
62 * it is in the index or in the current branch, because writing new values
63 * (and staging them) would blindly overwrite ALL the old content.
65 int is_writing_gitmodules_ok(void)
67 struct object_id oid;
68 return file_exists(GITMODULES_FILE) ||
69 (get_oid(GITMODULES_INDEX, &oid) < 0 && get_oid(GITMODULES_HEAD, &oid) < 0);
73 * Check if the .gitmodules file has unstaged modifications. This must be
74 * checked before allowing modifications to the .gitmodules file with the
75 * intention to stage them later, because when continuing we would stage the
76 * modifications the user didn't stage herself too. That might change in a
77 * future version when we learn to stage the changes we do ourselves without
78 * staging any previous modifications.
80 int is_staging_gitmodules_ok(struct index_state *istate)
82 int pos = index_name_pos(istate, GITMODULES_FILE, strlen(GITMODULES_FILE));
84 if ((pos >= 0) && (pos < istate->cache_nr)) {
85 struct stat st;
86 if (lstat(GITMODULES_FILE, &st) == 0 &&
87 ie_modified(istate, istate->cache[pos], &st, 0) & DATA_CHANGED)
88 return 0;
91 return 1;
94 static int for_each_remote_ref_submodule(const char *submodule,
95 each_ref_fn fn, void *cb_data)
97 return refs_for_each_remote_ref(get_submodule_ref_store(submodule),
98 fn, cb_data);
102 * Try to update the "path" entry in the "submodule.<name>" section of the
103 * .gitmodules file. Return 0 only if a .gitmodules file was found, a section
104 * with the correct path=<oldpath> setting was found and we could update it.
106 int update_path_in_gitmodules(const char *oldpath, const char *newpath)
108 struct strbuf entry = STRBUF_INIT;
109 const struct submodule *submodule;
110 int ret;
112 if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */
113 return -1;
115 if (is_gitmodules_unmerged(the_repository->index))
116 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
118 submodule = submodule_from_path(the_repository, null_oid(), oldpath);
119 if (!submodule || !submodule->name) {
120 warning(_("Could not find section in .gitmodules where path=%s"), oldpath);
121 return -1;
123 strbuf_addstr(&entry, "submodule.");
124 strbuf_addstr(&entry, submodule->name);
125 strbuf_addstr(&entry, ".path");
126 ret = config_set_in_gitmodules_file_gently(entry.buf, newpath);
127 strbuf_release(&entry);
128 return ret;
132 * Try to remove the "submodule.<name>" section from .gitmodules where the given
133 * path is configured. Return 0 only if a .gitmodules file was found, a section
134 * with the correct path=<path> setting was found and we could remove it.
136 int remove_path_from_gitmodules(const char *path)
138 struct strbuf sect = STRBUF_INIT;
139 const struct submodule *submodule;
141 if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */
142 return -1;
144 if (is_gitmodules_unmerged(the_repository->index))
145 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
147 submodule = submodule_from_path(the_repository, null_oid(), path);
148 if (!submodule || !submodule->name) {
149 warning(_("Could not find section in .gitmodules where path=%s"), path);
150 return -1;
152 strbuf_addstr(&sect, "submodule.");
153 strbuf_addstr(&sect, submodule->name);
154 if (git_config_rename_section_in_file(GITMODULES_FILE, sect.buf, NULL) < 0) {
155 /* Maybe the user already did that, don't error out here */
156 warning(_("Could not remove .gitmodules entry for %s"), path);
157 strbuf_release(&sect);
158 return -1;
160 strbuf_release(&sect);
161 return 0;
164 void stage_updated_gitmodules(struct index_state *istate)
166 if (add_file_to_index(istate, GITMODULES_FILE, 0))
167 die(_("staging updated .gitmodules failed"));
170 static struct string_list added_submodule_odb_paths = STRING_LIST_INIT_NODUP;
172 void add_submodule_odb_by_path(const char *path)
174 string_list_insert(&added_submodule_odb_paths, xstrdup(path));
177 int register_all_submodule_odb_as_alternates(void)
179 int i;
180 int ret = added_submodule_odb_paths.nr;
182 for (i = 0; i < added_submodule_odb_paths.nr; i++)
183 add_to_alternates_memory(added_submodule_odb_paths.items[i].string);
184 if (ret) {
185 string_list_clear(&added_submodule_odb_paths, 0);
186 trace2_data_intmax("submodule", the_repository,
187 "register_all_submodule_odb_as_alternates/registered", ret);
188 if (git_env_bool("GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB", 0))
189 BUG("register_all_submodule_odb_as_alternates() called");
191 return ret;
194 void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
195 const char *path)
197 const struct submodule *submodule = submodule_from_path(the_repository,
198 null_oid(),
199 path);
200 if (submodule) {
201 const char *ignore;
202 char *key;
204 key = xstrfmt("submodule.%s.ignore", submodule->name);
205 if (repo_config_get_string_tmp(the_repository, key, &ignore))
206 ignore = submodule->ignore;
207 free(key);
209 if (ignore)
210 handle_ignore_submodules_arg(diffopt, ignore);
211 else if (is_gitmodules_unmerged(the_repository->index))
212 diffopt->flags.ignore_submodules = 1;
216 /* Cheap function that only determines if we're interested in submodules at all */
217 int git_default_submodule_config(const char *var, const char *value,
218 void *cb UNUSED)
220 if (!strcmp(var, "submodule.recurse")) {
221 int v = git_config_bool(var, value) ?
222 RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF;
223 config_update_recurse_submodules = v;
225 return 0;
228 int option_parse_recurse_submodules_worktree_updater(const struct option *opt,
229 const char *arg, int unset)
231 if (unset) {
232 config_update_recurse_submodules = RECURSE_SUBMODULES_OFF;
233 return 0;
235 if (arg)
236 config_update_recurse_submodules =
237 parse_update_recurse_submodules_arg(opt->long_name,
238 arg);
239 else
240 config_update_recurse_submodules = RECURSE_SUBMODULES_ON;
242 return 0;
246 * Determine if a submodule has been initialized at a given 'path'
249 * NEEDSWORK: Emit a warning if submodule.active exists, but is valueless,
250 * ie, the config looks like: "[submodule] active\n".
251 * Since that is an invalid pathspec, we should inform the user.
253 int is_tree_submodule_active(struct repository *repo,
254 const struct object_id *treeish_name,
255 const char *path)
257 int ret = 0;
258 char *key = NULL;
259 char *value = NULL;
260 const struct string_list *sl;
261 const struct submodule *module;
263 module = submodule_from_path(repo, treeish_name, path);
265 /* early return if there isn't a path->module mapping */
266 if (!module)
267 return 0;
269 /* submodule.<name>.active is set */
270 key = xstrfmt("submodule.%s.active", module->name);
271 if (!repo_config_get_bool(repo, key, &ret)) {
272 free(key);
273 return ret;
275 free(key);
277 /* submodule.active is set */
278 sl = repo_config_get_value_multi(repo, "submodule.active");
279 if (sl) {
280 struct pathspec ps;
281 struct strvec args = STRVEC_INIT;
282 const struct string_list_item *item;
284 for_each_string_list_item(item, sl) {
285 strvec_push(&args, item->string);
288 parse_pathspec(&ps, 0, 0, NULL, args.v);
289 ret = match_pathspec(repo->index, &ps, path, strlen(path), 0, NULL, 1);
291 strvec_clear(&args);
292 clear_pathspec(&ps);
293 return ret;
296 /* fallback to checking if the URL is set */
297 key = xstrfmt("submodule.%s.url", module->name);
298 ret = !repo_config_get_string(repo, key, &value);
300 free(value);
301 free(key);
302 return ret;
305 int is_submodule_active(struct repository *repo, const char *path)
307 return is_tree_submodule_active(repo, null_oid(), path);
310 int is_submodule_populated_gently(const char *path, int *return_error_code)
312 int ret = 0;
313 char *gitdir = xstrfmt("%s/.git", path);
315 if (resolve_gitdir_gently(gitdir, return_error_code))
316 ret = 1;
318 free(gitdir);
319 return ret;
323 * Dies if the provided 'prefix' corresponds to an unpopulated submodule
325 void die_in_unpopulated_submodule(struct index_state *istate,
326 const char *prefix)
328 int i, prefixlen;
330 if (!prefix)
331 return;
333 prefixlen = strlen(prefix);
335 for (i = 0; i < istate->cache_nr; i++) {
336 struct cache_entry *ce = istate->cache[i];
337 int ce_len = ce_namelen(ce);
339 if (!S_ISGITLINK(ce->ce_mode))
340 continue;
341 if (prefixlen <= ce_len)
342 continue;
343 if (strncmp(ce->name, prefix, ce_len))
344 continue;
345 if (prefix[ce_len] != '/')
346 continue;
348 die(_("in unpopulated submodule '%s'"), ce->name);
353 * Dies if any paths in the provided pathspec descends into a submodule
355 void die_path_inside_submodule(struct index_state *istate,
356 const struct pathspec *ps)
358 int i, j;
360 for (i = 0; i < istate->cache_nr; i++) {
361 struct cache_entry *ce = istate->cache[i];
362 int ce_len = ce_namelen(ce);
364 if (!S_ISGITLINK(ce->ce_mode))
365 continue;
367 for (j = 0; j < ps->nr ; j++) {
368 const struct pathspec_item *item = &ps->items[j];
370 if (item->len <= ce_len)
371 continue;
372 if (item->match[ce_len] != '/')
373 continue;
374 if (strncmp(ce->name, item->match, ce_len))
375 continue;
376 if (item->len == ce_len + 1)
377 continue;
379 die(_("Pathspec '%s' is in submodule '%.*s'"),
380 item->original, ce_len, ce->name);
385 enum submodule_update_type parse_submodule_update_type(const char *value)
387 if (!strcmp(value, "none"))
388 return SM_UPDATE_NONE;
389 else if (!strcmp(value, "checkout"))
390 return SM_UPDATE_CHECKOUT;
391 else if (!strcmp(value, "rebase"))
392 return SM_UPDATE_REBASE;
393 else if (!strcmp(value, "merge"))
394 return SM_UPDATE_MERGE;
395 else if (*value == '!')
396 return SM_UPDATE_COMMAND;
397 else
398 return SM_UPDATE_UNSPECIFIED;
401 int parse_submodule_update_strategy(const char *value,
402 struct submodule_update_strategy *dst)
404 enum submodule_update_type type;
406 free((void*)dst->command);
407 dst->command = NULL;
409 type = parse_submodule_update_type(value);
410 if (type == SM_UPDATE_UNSPECIFIED)
411 return -1;
413 dst->type = type;
414 if (type == SM_UPDATE_COMMAND)
415 dst->command = xstrdup(value + 1);
417 return 0;
420 const char *submodule_update_type_to_string(enum submodule_update_type type)
422 switch (type) {
423 case SM_UPDATE_CHECKOUT:
424 return "checkout";
425 case SM_UPDATE_MERGE:
426 return "merge";
427 case SM_UPDATE_REBASE:
428 return "rebase";
429 case SM_UPDATE_NONE:
430 return "none";
431 case SM_UPDATE_UNSPECIFIED:
432 case SM_UPDATE_COMMAND:
433 BUG("init_submodule() should handle type %d", type);
434 default:
435 BUG("unexpected update strategy type: %d", type);
439 void handle_ignore_submodules_arg(struct diff_options *diffopt,
440 const char *arg)
442 diffopt->flags.ignore_submodule_set = 1;
443 diffopt->flags.ignore_submodules = 0;
444 diffopt->flags.ignore_untracked_in_submodules = 0;
445 diffopt->flags.ignore_dirty_submodules = 0;
447 if (!strcmp(arg, "all"))
448 diffopt->flags.ignore_submodules = 1;
449 else if (!strcmp(arg, "untracked"))
450 diffopt->flags.ignore_untracked_in_submodules = 1;
451 else if (!strcmp(arg, "dirty"))
452 diffopt->flags.ignore_dirty_submodules = 1;
453 else if (strcmp(arg, "none"))
454 die(_("bad --ignore-submodules argument: %s"), arg);
456 * Please update _git_status() in git-completion.bash when you
457 * add new options
461 static int prepare_submodule_diff_summary(struct repository *r, struct rev_info *rev,
462 const char *path,
463 struct commit *left, struct commit *right,
464 struct commit_list *merge_bases)
466 struct commit_list *list;
468 repo_init_revisions(r, rev, NULL);
469 setup_revisions(0, NULL, rev, NULL);
470 rev->left_right = 1;
471 rev->first_parent_only = 1;
472 left->object.flags |= SYMMETRIC_LEFT;
473 add_pending_object(rev, &left->object, path);
474 add_pending_object(rev, &right->object, path);
475 for (list = merge_bases; list; list = list->next) {
476 list->item->object.flags |= UNINTERESTING;
477 add_pending_object(rev, &list->item->object,
478 oid_to_hex(&list->item->object.oid));
480 return prepare_revision_walk(rev);
483 static void print_submodule_diff_summary(struct repository *r, struct rev_info *rev, struct diff_options *o)
485 static const char format[] = " %m %s";
486 struct strbuf sb = STRBUF_INIT;
487 struct commit *commit;
489 while ((commit = get_revision(rev))) {
490 struct pretty_print_context ctx = {0};
491 ctx.date_mode = rev->date_mode;
492 ctx.output_encoding = get_log_output_encoding();
493 strbuf_setlen(&sb, 0);
494 repo_format_commit_message(r, commit, format, &sb,
495 &ctx);
496 strbuf_addch(&sb, '\n');
497 if (commit->object.flags & SYMMETRIC_LEFT)
498 diff_emit_submodule_del(o, sb.buf);
499 else
500 diff_emit_submodule_add(o, sb.buf);
502 strbuf_release(&sb);
505 void prepare_submodule_repo_env(struct strvec *out)
507 prepare_other_repo_env(out, DEFAULT_GIT_DIR_ENVIRONMENT);
510 static void prepare_submodule_repo_env_in_gitdir(struct strvec *out)
512 prepare_other_repo_env(out, ".");
516 * Initialize a repository struct for a submodule based on the provided 'path'.
518 * Returns the repository struct on success,
519 * NULL when the submodule is not present.
521 static struct repository *open_submodule(const char *path)
523 struct strbuf sb = STRBUF_INIT;
524 struct repository *out = xmalloc(sizeof(*out));
526 if (submodule_to_gitdir(&sb, path) || repo_init(out, sb.buf, NULL)) {
527 strbuf_release(&sb);
528 free(out);
529 return NULL;
532 /* Mark it as a submodule */
533 out->submodule_prefix = xstrdup(path);
535 strbuf_release(&sb);
536 return out;
540 * Helper function to display the submodule header line prior to the full
541 * summary output.
543 * If it can locate the submodule git directory it will create a repository
544 * handle for the submodule and lookup both the left and right commits and
545 * put them into the left and right pointers.
547 static void show_submodule_header(struct diff_options *o,
548 const char *path,
549 struct object_id *one, struct object_id *two,
550 unsigned dirty_submodule,
551 struct repository *sub,
552 struct commit **left, struct commit **right,
553 struct commit_list **merge_bases)
555 const char *message = NULL;
556 struct strbuf sb = STRBUF_INIT;
557 int fast_forward = 0, fast_backward = 0;
559 if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
560 diff_emit_submodule_untracked(o, path);
562 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
563 diff_emit_submodule_modified(o, path);
565 if (is_null_oid(one))
566 message = "(new submodule)";
567 else if (is_null_oid(two))
568 message = "(submodule deleted)";
570 if (!sub) {
571 if (!message)
572 message = "(commits not present)";
573 goto output_header;
577 * Attempt to lookup the commit references, and determine if this is
578 * a fast forward or fast backwards update.
580 *left = lookup_commit_reference(sub, one);
581 *right = lookup_commit_reference(sub, two);
584 * Warn about missing commits in the submodule project, but only if
585 * they aren't null.
587 if ((!is_null_oid(one) && !*left) ||
588 (!is_null_oid(two) && !*right))
589 message = "(commits not present)";
591 *merge_bases = repo_get_merge_bases(sub, *left, *right);
592 if (*merge_bases) {
593 if ((*merge_bases)->item == *left)
594 fast_forward = 1;
595 else if ((*merge_bases)->item == *right)
596 fast_backward = 1;
599 if (oideq(one, two)) {
600 strbuf_release(&sb);
601 return;
604 output_header:
605 strbuf_addf(&sb, "Submodule %s ", path);
606 strbuf_add_unique_abbrev(&sb, one, DEFAULT_ABBREV);
607 strbuf_addstr(&sb, (fast_backward || fast_forward) ? ".." : "...");
608 strbuf_add_unique_abbrev(&sb, two, DEFAULT_ABBREV);
609 if (message)
610 strbuf_addf(&sb, " %s\n", message);
611 else
612 strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : "");
613 diff_emit_submodule_header(o, sb.buf);
615 strbuf_release(&sb);
618 void show_submodule_diff_summary(struct diff_options *o, const char *path,
619 struct object_id *one, struct object_id *two,
620 unsigned dirty_submodule)
622 struct rev_info rev = REV_INFO_INIT;
623 struct commit *left = NULL, *right = NULL;
624 struct commit_list *merge_bases = NULL;
625 struct repository *sub;
627 sub = open_submodule(path);
628 show_submodule_header(o, path, one, two, dirty_submodule,
629 sub, &left, &right, &merge_bases);
632 * If we don't have both a left and a right pointer, there is no
633 * reason to try and display a summary. The header line should contain
634 * all the information the user needs.
636 if (!left || !right || !sub)
637 goto out;
639 /* Treat revision walker failure the same as missing commits */
640 if (prepare_submodule_diff_summary(sub, &rev, path, left, right, merge_bases)) {
641 diff_emit_submodule_error(o, "(revision walker failed)\n");
642 goto out;
645 print_submodule_diff_summary(sub, &rev, o);
647 out:
648 free_commit_list(merge_bases);
649 release_revisions(&rev);
650 clear_commit_marks(left, ~0);
651 clear_commit_marks(right, ~0);
652 if (sub) {
653 repo_clear(sub);
654 free(sub);
658 void show_submodule_inline_diff(struct diff_options *o, const char *path,
659 struct object_id *one, struct object_id *two,
660 unsigned dirty_submodule)
662 const struct object_id *old_oid = the_hash_algo->empty_tree, *new_oid = the_hash_algo->empty_tree;
663 struct commit *left = NULL, *right = NULL;
664 struct commit_list *merge_bases = NULL;
665 struct child_process cp = CHILD_PROCESS_INIT;
666 struct strbuf sb = STRBUF_INIT;
667 struct repository *sub;
669 sub = open_submodule(path);
670 show_submodule_header(o, path, one, two, dirty_submodule,
671 sub, &left, &right, &merge_bases);
673 /* We need a valid left and right commit to display a difference */
674 if (!(left || is_null_oid(one)) ||
675 !(right || is_null_oid(two)))
676 goto done;
678 if (left)
679 old_oid = one;
680 if (right)
681 new_oid = two;
683 cp.git_cmd = 1;
684 cp.dir = path;
685 cp.out = -1;
686 cp.no_stdin = 1;
688 /* TODO: other options may need to be passed here. */
689 strvec_pushl(&cp.args, "diff", "--submodule=diff", NULL);
690 strvec_pushf(&cp.args, "--color=%s", want_color(o->use_color) ?
691 "always" : "never");
693 if (o->flags.reverse_diff) {
694 strvec_pushf(&cp.args, "--src-prefix=%s%s/",
695 o->b_prefix, path);
696 strvec_pushf(&cp.args, "--dst-prefix=%s%s/",
697 o->a_prefix, path);
698 } else {
699 strvec_pushf(&cp.args, "--src-prefix=%s%s/",
700 o->a_prefix, path);
701 strvec_pushf(&cp.args, "--dst-prefix=%s%s/",
702 o->b_prefix, path);
704 strvec_push(&cp.args, oid_to_hex(old_oid));
706 * If the submodule has modified content, we will diff against the
707 * work tree, under the assumption that the user has asked for the
708 * diff format and wishes to actually see all differences even if they
709 * haven't yet been committed to the submodule yet.
711 if (!(dirty_submodule & DIRTY_SUBMODULE_MODIFIED))
712 strvec_push(&cp.args, oid_to_hex(new_oid));
714 prepare_submodule_repo_env(&cp.env);
716 if (!is_directory(path)) {
717 /* fall back to absorbed git dir, if any */
718 if (!sub)
719 goto done;
720 cp.dir = sub->gitdir;
721 strvec_push(&cp.env, GIT_DIR_ENVIRONMENT "=.");
722 strvec_push(&cp.env, GIT_WORK_TREE_ENVIRONMENT "=.");
725 if (start_command(&cp)) {
726 diff_emit_submodule_error(o, "(diff failed)\n");
727 goto done;
730 while (strbuf_getwholeline_fd(&sb, cp.out, '\n') != EOF)
731 diff_emit_submodule_pipethrough(o, sb.buf, sb.len);
733 if (finish_command(&cp))
734 diff_emit_submodule_error(o, "(diff failed)\n");
736 done:
737 strbuf_release(&sb);
738 free_commit_list(merge_bases);
739 if (left)
740 clear_commit_marks(left, ~0);
741 if (right)
742 clear_commit_marks(right, ~0);
743 if (sub) {
744 repo_clear(sub);
745 free(sub);
749 int should_update_submodules(void)
751 return config_update_recurse_submodules == RECURSE_SUBMODULES_ON;
754 const struct submodule *submodule_from_ce(const struct cache_entry *ce)
756 if (!S_ISGITLINK(ce->ce_mode))
757 return NULL;
759 if (!should_update_submodules())
760 return NULL;
762 return submodule_from_path(the_repository, null_oid(), ce->name);
766 struct collect_changed_submodules_cb_data {
767 struct repository *repo;
768 struct string_list *changed;
769 const struct object_id *commit_oid;
773 * this would normally be two functions: default_name_from_path() and
774 * path_from_default_name(). Since the default name is the same as
775 * the submodule path we can get away with just one function which only
776 * checks whether there is a submodule in the working directory at that
777 * location.
779 static const char *default_name_or_path(const char *path_or_name)
781 int error_code;
783 if (!is_submodule_populated_gently(path_or_name, &error_code))
784 return NULL;
786 return path_or_name;
790 * Holds relevant information for a changed submodule. Used as the .util
791 * member of the changed submodule name string_list_item.
793 * (super_oid, path) allows the submodule config to be read from _some_
794 * .gitmodules file. We store this information the first time we find a
795 * superproject commit that points to the submodule, but this is
796 * arbitrary - we can choose any (super_oid, path) that matches the
797 * submodule's name.
799 * NEEDSWORK: Storing an arbitrary commit is undesirable because we can't
800 * guarantee that we're reading the commit that the user would expect. A better
801 * scheme would be to just fetch a submodule by its name. This requires two
802 * steps:
803 * - Create a function that behaves like repo_submodule_init(), but accepts a
804 * submodule name instead of treeish_name and path. This should be easy
805 * because repo_submodule_init() internally uses the submodule's name.
807 * - Replace most instances of 'struct submodule' (which is the .gitmodules
808 * config) with just the submodule name. This is OK because we expect
809 * submodule settings to be stored in .git/config (via "git submodule init"),
810 * not .gitmodules. This also lets us delete get_non_gitmodules_submodule(),
811 * which constructs a bogus 'struct submodule' for the sake of giving a
812 * placeholder name to a gitlink.
814 struct changed_submodule_data {
816 * The first superproject commit in the rev walk that points to
817 * the submodule.
819 const struct object_id *super_oid;
821 * Path to the submodule in the superproject commit referenced
822 * by 'super_oid'.
824 char *path;
825 /* The submodule commits that have changed in the rev walk. */
826 struct oid_array new_commits;
829 static void changed_submodule_data_clear(struct changed_submodule_data *cs_data)
831 oid_array_clear(&cs_data->new_commits);
832 free(cs_data->path);
835 static void collect_changed_submodules_cb(struct diff_queue_struct *q,
836 struct diff_options *options UNUSED,
837 void *data)
839 struct collect_changed_submodules_cb_data *me = data;
840 struct string_list *changed = me->changed;
841 const struct object_id *commit_oid = me->commit_oid;
842 int i;
844 for (i = 0; i < q->nr; i++) {
845 struct diff_filepair *p = q->queue[i];
846 const struct submodule *submodule;
847 const char *name;
848 struct string_list_item *item;
849 struct changed_submodule_data *cs_data;
851 if (!S_ISGITLINK(p->two->mode))
852 continue;
854 submodule = submodule_from_path(me->repo,
855 commit_oid, p->two->path);
856 if (submodule)
857 name = submodule->name;
858 else {
859 name = default_name_or_path(p->two->path);
860 /* make sure name does not collide with existing one */
861 if (name)
862 submodule = submodule_from_name(me->repo,
863 commit_oid, name);
864 if (submodule) {
865 warning(_("Submodule in commit %s at path: "
866 "'%s' collides with a submodule named "
867 "the same. Skipping it."),
868 oid_to_hex(commit_oid), p->two->path);
869 name = NULL;
873 if (!name)
874 continue;
876 item = string_list_insert(changed, name);
877 if (item->util)
878 cs_data = item->util;
879 else {
880 item->util = xcalloc(1, sizeof(struct changed_submodule_data));
881 cs_data = item->util;
882 cs_data->super_oid = commit_oid;
883 cs_data->path = xstrdup(p->two->path);
885 oid_array_append(&cs_data->new_commits, &p->two->oid);
890 * Collect the paths of submodules in 'changed' which have changed based on
891 * the revisions as specified in 'argv'. Each entry in 'changed' will also
892 * have a corresponding 'struct oid_array' (in the 'util' field) which lists
893 * what the submodule pointers were updated to during the change.
895 static void collect_changed_submodules(struct repository *r,
896 struct string_list *changed,
897 struct strvec *argv)
899 struct rev_info rev;
900 const struct commit *commit;
901 int save_warning;
902 struct setup_revision_opt s_r_opt = {
903 .assume_dashdash = 1,
906 save_warning = warn_on_object_refname_ambiguity;
907 warn_on_object_refname_ambiguity = 0;
908 repo_init_revisions(r, &rev, NULL);
909 setup_revisions(argv->nr, argv->v, &rev, &s_r_opt);
910 warn_on_object_refname_ambiguity = save_warning;
911 if (prepare_revision_walk(&rev))
912 die(_("revision walk setup failed"));
914 while ((commit = get_revision(&rev))) {
915 struct rev_info diff_rev;
916 struct collect_changed_submodules_cb_data data;
917 data.repo = r;
918 data.changed = changed;
919 data.commit_oid = &commit->object.oid;
921 repo_init_revisions(r, &diff_rev, NULL);
922 diff_rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
923 diff_rev.diffopt.format_callback = collect_changed_submodules_cb;
924 diff_rev.diffopt.format_callback_data = &data;
925 diff_rev.dense_combined_merges = 1;
926 diff_tree_combined_merge(commit, &diff_rev);
927 release_revisions(&diff_rev);
930 reset_revision_walk();
931 release_revisions(&rev);
934 static void free_submodules_data(struct string_list *submodules)
936 struct string_list_item *item;
937 for_each_string_list_item(item, submodules)
938 changed_submodule_data_clear(item->util);
940 string_list_clear(submodules, 1);
943 static int has_remote(const char *refname UNUSED,
944 const struct object_id *oid UNUSED,
945 int flags UNUSED, void *cb_data UNUSED)
947 return 1;
950 static int append_oid_to_argv(const struct object_id *oid, void *data)
952 struct strvec *argv = data;
953 strvec_push(argv, oid_to_hex(oid));
954 return 0;
957 struct has_commit_data {
958 struct repository *repo;
959 int result;
960 const char *path;
961 const struct object_id *super_oid;
964 static int check_has_commit(const struct object_id *oid, void *data)
966 struct has_commit_data *cb = data;
967 struct repository subrepo;
968 enum object_type type;
970 if (repo_submodule_init(&subrepo, cb->repo, cb->path, cb->super_oid)) {
971 cb->result = 0;
972 /* subrepo failed to init, so don't clean it up. */
973 return 0;
976 type = oid_object_info(&subrepo, oid, NULL);
978 switch (type) {
979 case OBJ_COMMIT:
980 goto cleanup;
981 case OBJ_BAD:
983 * Object is missing or invalid. If invalid, an error message
984 * has already been printed.
986 cb->result = 0;
987 goto cleanup;
988 default:
989 die(_("submodule entry '%s' (%s) is a %s, not a commit"),
990 cb->path, oid_to_hex(oid), type_name(type));
992 cleanup:
993 repo_clear(&subrepo);
994 return 0;
997 static int submodule_has_commits(struct repository *r,
998 const char *path,
999 const struct object_id *super_oid,
1000 struct oid_array *commits)
1002 struct has_commit_data has_commit = {
1003 .repo = r,
1004 .result = 1,
1005 .path = path,
1006 .super_oid = super_oid
1009 oid_array_for_each_unique(commits, check_has_commit, &has_commit);
1011 if (has_commit.result) {
1013 * Even if the submodule is checked out and the commit is
1014 * present, make sure it exists in the submodule's object store
1015 * and that it is reachable from a ref.
1017 struct child_process cp = CHILD_PROCESS_INIT;
1018 struct strbuf out = STRBUF_INIT;
1020 strvec_pushl(&cp.args, "rev-list", "-n", "1", NULL);
1021 oid_array_for_each_unique(commits, append_oid_to_argv, &cp.args);
1022 strvec_pushl(&cp.args, "--not", "--all", NULL);
1024 prepare_submodule_repo_env(&cp.env);
1025 cp.git_cmd = 1;
1026 cp.no_stdin = 1;
1027 cp.dir = path;
1029 if (capture_command(&cp, &out, GIT_MAX_HEXSZ + 1) || out.len)
1030 has_commit.result = 0;
1032 strbuf_release(&out);
1035 return has_commit.result;
1038 static int submodule_needs_pushing(struct repository *r,
1039 const char *path,
1040 struct oid_array *commits)
1042 if (!submodule_has_commits(r, path, null_oid(), commits))
1044 * NOTE: We do consider it safe to return "no" here. The
1045 * correct answer would be "We do not know" instead of
1046 * "No push needed", but it is quite hard to change
1047 * the submodule pointer without having the submodule
1048 * around. If a user did however change the submodules
1049 * without having the submodule around, this indicates
1050 * an expert who knows what they are doing or a
1051 * maintainer integrating work from other people. In
1052 * both cases it should be safe to skip this check.
1054 return 0;
1056 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
1057 struct child_process cp = CHILD_PROCESS_INIT;
1058 struct strbuf buf = STRBUF_INIT;
1059 int needs_pushing = 0;
1061 strvec_push(&cp.args, "rev-list");
1062 oid_array_for_each_unique(commits, append_oid_to_argv, &cp.args);
1063 strvec_pushl(&cp.args, "--not", "--remotes", "-n", "1" , NULL);
1065 prepare_submodule_repo_env(&cp.env);
1066 cp.git_cmd = 1;
1067 cp.no_stdin = 1;
1068 cp.out = -1;
1069 cp.dir = path;
1070 if (start_command(&cp))
1071 die(_("Could not run 'git rev-list <commits> --not --remotes -n 1' command in submodule %s"),
1072 path);
1073 if (strbuf_read(&buf, cp.out, the_hash_algo->hexsz + 1))
1074 needs_pushing = 1;
1075 finish_command(&cp);
1076 close(cp.out);
1077 strbuf_release(&buf);
1078 return needs_pushing;
1081 return 0;
1084 int find_unpushed_submodules(struct repository *r,
1085 struct oid_array *commits,
1086 const char *remotes_name,
1087 struct string_list *needs_pushing)
1089 struct string_list submodules = STRING_LIST_INIT_DUP;
1090 struct string_list_item *name;
1091 struct strvec argv = STRVEC_INIT;
1093 /* argv.v[0] will be ignored by setup_revisions */
1094 strvec_push(&argv, "find_unpushed_submodules");
1095 oid_array_for_each_unique(commits, append_oid_to_argv, &argv);
1096 strvec_push(&argv, "--not");
1097 strvec_pushf(&argv, "--remotes=%s", remotes_name);
1099 collect_changed_submodules(r, &submodules, &argv);
1101 for_each_string_list_item(name, &submodules) {
1102 struct changed_submodule_data *cs_data = name->util;
1103 const struct submodule *submodule;
1104 const char *path = NULL;
1106 submodule = submodule_from_name(r, null_oid(), name->string);
1107 if (submodule)
1108 path = submodule->path;
1109 else
1110 path = default_name_or_path(name->string);
1112 if (!path)
1113 continue;
1115 if (submodule_needs_pushing(r, path, &cs_data->new_commits))
1116 string_list_insert(needs_pushing, path);
1119 free_submodules_data(&submodules);
1120 strvec_clear(&argv);
1122 return needs_pushing->nr;
1125 static int push_submodule(const char *path,
1126 const struct remote *remote,
1127 const struct refspec *rs,
1128 const struct string_list *push_options,
1129 int dry_run)
1131 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
1132 struct child_process cp = CHILD_PROCESS_INIT;
1133 strvec_push(&cp.args, "push");
1135 * When recursing into a submodule, treat any "only" configurations as "on-
1136 * demand", since "only" would not work (we need all submodules to be pushed
1137 * in order to be able to push the superproject).
1139 strvec_push(&cp.args, "--recurse-submodules=only-is-on-demand");
1140 if (dry_run)
1141 strvec_push(&cp.args, "--dry-run");
1143 if (push_options && push_options->nr) {
1144 const struct string_list_item *item;
1145 for_each_string_list_item(item, push_options)
1146 strvec_pushf(&cp.args, "--push-option=%s",
1147 item->string);
1150 if (remote->origin != REMOTE_UNCONFIGURED) {
1151 int i;
1152 strvec_push(&cp.args, remote->name);
1153 for (i = 0; i < rs->raw_nr; i++)
1154 strvec_push(&cp.args, rs->raw[i]);
1157 prepare_submodule_repo_env(&cp.env);
1158 cp.git_cmd = 1;
1159 cp.no_stdin = 1;
1160 cp.dir = path;
1161 if (run_command(&cp))
1162 return 0;
1163 close(cp.out);
1166 return 1;
1170 * Perform a check in the submodule to see if the remote and refspec work.
1171 * Die if the submodule can't be pushed.
1173 static void submodule_push_check(const char *path, const char *head,
1174 const struct remote *remote,
1175 const struct refspec *rs)
1177 struct child_process cp = CHILD_PROCESS_INIT;
1178 int i;
1180 strvec_push(&cp.args, "submodule--helper");
1181 strvec_push(&cp.args, "push-check");
1182 strvec_push(&cp.args, head);
1183 strvec_push(&cp.args, remote->name);
1185 for (i = 0; i < rs->raw_nr; i++)
1186 strvec_push(&cp.args, rs->raw[i]);
1188 prepare_submodule_repo_env(&cp.env);
1189 cp.git_cmd = 1;
1190 cp.no_stdin = 1;
1191 cp.no_stdout = 1;
1192 cp.dir = path;
1195 * Simply indicate if 'submodule--helper push-check' failed.
1196 * More detailed error information will be provided by the
1197 * child process.
1199 if (run_command(&cp))
1200 die(_("process for submodule '%s' failed"), path);
1203 int push_unpushed_submodules(struct repository *r,
1204 struct oid_array *commits,
1205 const struct remote *remote,
1206 const struct refspec *rs,
1207 const struct string_list *push_options,
1208 int dry_run)
1210 int i, ret = 1;
1211 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1213 if (!find_unpushed_submodules(r, commits,
1214 remote->name, &needs_pushing))
1215 return 1;
1218 * Verify that the remote and refspec can be propagated to all
1219 * submodules. This check can be skipped if the remote and refspec
1220 * won't be propagated due to the remote being unconfigured (e.g. a URL
1221 * instead of a remote name).
1223 if (remote->origin != REMOTE_UNCONFIGURED) {
1224 char *head;
1225 struct object_id head_oid;
1227 head = resolve_refdup("HEAD", 0, &head_oid, NULL);
1228 if (!head)
1229 die(_("Failed to resolve HEAD as a valid ref."));
1231 for (i = 0; i < needs_pushing.nr; i++)
1232 submodule_push_check(needs_pushing.items[i].string,
1233 head, remote, rs);
1234 free(head);
1237 /* Actually push the submodules */
1238 for (i = 0; i < needs_pushing.nr; i++) {
1239 const char *path = needs_pushing.items[i].string;
1240 fprintf(stderr, _("Pushing submodule '%s'\n"), path);
1241 if (!push_submodule(path, remote, rs,
1242 push_options, dry_run)) {
1243 fprintf(stderr, _("Unable to push submodule '%s'\n"), path);
1244 ret = 0;
1248 string_list_clear(&needs_pushing, 0);
1250 return ret;
1253 static int append_oid_to_array(const char *ref UNUSED,
1254 const struct object_id *oid,
1255 int flags UNUSED, void *data)
1257 struct oid_array *array = data;
1258 oid_array_append(array, oid);
1259 return 0;
1262 void check_for_new_submodule_commits(struct object_id *oid)
1264 if (!initialized_fetch_ref_tips) {
1265 for_each_ref(append_oid_to_array, &ref_tips_before_fetch);
1266 initialized_fetch_ref_tips = 1;
1269 oid_array_append(&ref_tips_after_fetch, oid);
1273 * Returns 1 if there is at least one submodule gitdir in
1274 * $GIT_DIR/modules and 0 otherwise. This follows
1275 * submodule_name_to_gitdir(), which looks for submodules in
1276 * $GIT_DIR/modules, not $GIT_COMMON_DIR.
1278 * A submodule can be moved to $GIT_DIR/modules manually by running "git
1279 * submodule absorbgitdirs", or it may be initialized there by "git
1280 * submodule update".
1282 static int repo_has_absorbed_submodules(struct repository *r)
1284 int ret;
1285 struct strbuf buf = STRBUF_INIT;
1287 strbuf_repo_git_path(&buf, r, "modules/");
1288 ret = file_exists(buf.buf) && !is_empty_dir(buf.buf);
1289 strbuf_release(&buf);
1290 return ret;
1293 static void calculate_changed_submodule_paths(struct repository *r,
1294 struct string_list *changed_submodule_names)
1296 struct strvec argv = STRVEC_INIT;
1297 struct string_list_item *name;
1299 /* No need to check if no submodules would be fetched */
1300 if (!submodule_from_path(r, NULL, NULL) &&
1301 !repo_has_absorbed_submodules(r))
1302 return;
1304 strvec_push(&argv, "--"); /* argv[0] program name */
1305 oid_array_for_each_unique(&ref_tips_after_fetch,
1306 append_oid_to_argv, &argv);
1307 strvec_push(&argv, "--not");
1308 oid_array_for_each_unique(&ref_tips_before_fetch,
1309 append_oid_to_argv, &argv);
1312 * Collect all submodules (whether checked out or not) for which new
1313 * commits have been recorded upstream in "changed_submodule_names".
1315 collect_changed_submodules(r, changed_submodule_names, &argv);
1317 for_each_string_list_item(name, changed_submodule_names) {
1318 struct changed_submodule_data *cs_data = name->util;
1319 const struct submodule *submodule;
1320 const char *path = NULL;
1322 submodule = submodule_from_name(r, null_oid(), name->string);
1323 if (submodule)
1324 path = submodule->path;
1325 else
1326 path = default_name_or_path(name->string);
1328 if (!path)
1329 continue;
1331 if (submodule_has_commits(r, path, null_oid(), &cs_data->new_commits)) {
1332 changed_submodule_data_clear(cs_data);
1333 *name->string = '\0';
1337 string_list_remove_empty_items(changed_submodule_names, 1);
1339 strvec_clear(&argv);
1340 oid_array_clear(&ref_tips_before_fetch);
1341 oid_array_clear(&ref_tips_after_fetch);
1342 initialized_fetch_ref_tips = 0;
1345 int submodule_touches_in_range(struct repository *r,
1346 struct object_id *excl_oid,
1347 struct object_id *incl_oid)
1349 struct string_list subs = STRING_LIST_INIT_DUP;
1350 struct strvec args = STRVEC_INIT;
1351 int ret;
1353 /* No need to check if there are no submodules configured */
1354 if (!submodule_from_path(r, NULL, NULL))
1355 return 0;
1357 strvec_push(&args, "--"); /* args[0] program name */
1358 strvec_push(&args, oid_to_hex(incl_oid));
1359 if (!is_null_oid(excl_oid)) {
1360 strvec_push(&args, "--not");
1361 strvec_push(&args, oid_to_hex(excl_oid));
1364 collect_changed_submodules(r, &subs, &args);
1365 ret = subs.nr;
1367 strvec_clear(&args);
1369 free_submodules_data(&subs);
1370 return ret;
1373 struct submodule_parallel_fetch {
1375 * The index of the last index entry processed by
1376 * get_fetch_task_from_index().
1378 int index_count;
1380 * The index of the last string_list entry processed by
1381 * get_fetch_task_from_changed().
1383 int changed_count;
1384 struct strvec args;
1385 struct repository *r;
1386 const char *prefix;
1387 int command_line_option;
1388 int default_option;
1389 int quiet;
1390 int result;
1393 * Names of submodules that have new commits. Generated by
1394 * walking the newly fetched superproject commits.
1396 struct string_list changed_submodule_names;
1398 * Names of submodules that have already been processed. Lets us
1399 * avoid fetching the same submodule more than once.
1401 struct string_list seen_submodule_names;
1403 /* Pending fetches by OIDs */
1404 struct fetch_task **oid_fetch_tasks;
1405 int oid_fetch_tasks_nr, oid_fetch_tasks_alloc;
1407 struct strbuf submodules_with_errors;
1409 #define SPF_INIT { \
1410 .args = STRVEC_INIT, \
1411 .changed_submodule_names = STRING_LIST_INIT_DUP, \
1412 .seen_submodule_names = STRING_LIST_INIT_DUP, \
1413 .submodules_with_errors = STRBUF_INIT, \
1416 static int get_fetch_recurse_config(const struct submodule *submodule,
1417 struct submodule_parallel_fetch *spf)
1419 if (spf->command_line_option != RECURSE_SUBMODULES_DEFAULT)
1420 return spf->command_line_option;
1422 if (submodule) {
1423 char *key;
1424 const char *value;
1426 int fetch_recurse = submodule->fetch_recurse;
1427 key = xstrfmt("submodule.%s.fetchRecurseSubmodules", submodule->name);
1428 if (!repo_config_get_string_tmp(spf->r, key, &value)) {
1429 fetch_recurse = parse_fetch_recurse_submodules_arg(key, value);
1431 free(key);
1433 if (fetch_recurse != RECURSE_SUBMODULES_NONE)
1434 /* local config overrules everything except commandline */
1435 return fetch_recurse;
1438 return spf->default_option;
1442 * Fetch in progress (if callback data) or
1443 * pending (if in oid_fetch_tasks in struct submodule_parallel_fetch)
1445 struct fetch_task {
1446 struct repository *repo;
1447 const struct submodule *sub;
1448 unsigned free_sub : 1; /* Do we need to free the submodule? */
1449 const char *default_argv; /* The default fetch mode. */
1450 struct strvec git_args; /* Args for the child git process. */
1452 struct oid_array *commits; /* Ensure these commits are fetched */
1456 * When a submodule is not defined in .gitmodules, we cannot access it
1457 * via the regular submodule-config. Create a fake submodule, which we can
1458 * work on.
1460 static const struct submodule *get_non_gitmodules_submodule(const char *path)
1462 struct submodule *ret = NULL;
1463 const char *name = default_name_or_path(path);
1465 if (!name)
1466 return NULL;
1468 ret = xmalloc(sizeof(*ret));
1469 memset(ret, 0, sizeof(*ret));
1470 ret->path = name;
1471 ret->name = name;
1473 return (const struct submodule *) ret;
1476 static void fetch_task_release(struct fetch_task *p)
1478 if (p->free_sub)
1479 free((void*)p->sub);
1480 p->free_sub = 0;
1481 p->sub = NULL;
1483 if (p->repo)
1484 repo_clear(p->repo);
1485 FREE_AND_NULL(p->repo);
1487 strvec_clear(&p->git_args);
1490 static struct repository *get_submodule_repo_for(struct repository *r,
1491 const char *path,
1492 const struct object_id *treeish_name)
1494 struct repository *ret = xmalloc(sizeof(*ret));
1496 if (repo_submodule_init(ret, r, path, treeish_name)) {
1497 free(ret);
1498 return NULL;
1501 return ret;
1504 static struct fetch_task *fetch_task_create(struct submodule_parallel_fetch *spf,
1505 const char *path,
1506 const struct object_id *treeish_name)
1508 struct fetch_task *task = xmalloc(sizeof(*task));
1509 memset(task, 0, sizeof(*task));
1511 task->sub = submodule_from_path(spf->r, treeish_name, path);
1513 if (!task->sub) {
1515 * No entry in .gitmodules? Technically not a submodule,
1516 * but historically we supported repositories that happen to be
1517 * in-place where a gitlink is. Keep supporting them.
1519 task->sub = get_non_gitmodules_submodule(path);
1520 if (!task->sub)
1521 goto cleanup;
1523 task->free_sub = 1;
1526 if (string_list_lookup(&spf->seen_submodule_names, task->sub->name))
1527 goto cleanup;
1529 switch (get_fetch_recurse_config(task->sub, spf))
1531 default:
1532 case RECURSE_SUBMODULES_DEFAULT:
1533 case RECURSE_SUBMODULES_ON_DEMAND:
1534 if (!task->sub ||
1535 !string_list_lookup(
1536 &spf->changed_submodule_names,
1537 task->sub->name))
1538 goto cleanup;
1539 task->default_argv = "on-demand";
1540 break;
1541 case RECURSE_SUBMODULES_ON:
1542 task->default_argv = "yes";
1543 break;
1544 case RECURSE_SUBMODULES_OFF:
1545 goto cleanup;
1548 task->repo = get_submodule_repo_for(spf->r, path, treeish_name);
1550 return task;
1552 cleanup:
1553 fetch_task_release(task);
1554 free(task);
1555 return NULL;
1558 static struct fetch_task *
1559 get_fetch_task_from_index(struct submodule_parallel_fetch *spf,
1560 struct strbuf *err)
1562 for (; spf->index_count < spf->r->index->cache_nr; spf->index_count++) {
1563 const struct cache_entry *ce =
1564 spf->r->index->cache[spf->index_count];
1565 struct fetch_task *task;
1567 if (!S_ISGITLINK(ce->ce_mode))
1568 continue;
1570 task = fetch_task_create(spf, ce->name, null_oid());
1571 if (!task)
1572 continue;
1574 if (task->repo) {
1575 if (!spf->quiet)
1576 strbuf_addf(err, _("Fetching submodule %s%s\n"),
1577 spf->prefix, ce->name);
1579 spf->index_count++;
1580 return task;
1581 } else {
1582 struct strbuf empty_submodule_path = STRBUF_INIT;
1584 fetch_task_release(task);
1585 free(task);
1588 * An empty directory is normal,
1589 * the submodule is not initialized
1591 strbuf_addf(&empty_submodule_path, "%s/%s/",
1592 spf->r->worktree,
1593 ce->name);
1594 if (S_ISGITLINK(ce->ce_mode) &&
1595 !is_empty_dir(empty_submodule_path.buf)) {
1596 spf->result = 1;
1597 strbuf_addf(err,
1598 _("Could not access submodule '%s'\n"),
1599 ce->name);
1601 strbuf_release(&empty_submodule_path);
1604 return NULL;
1607 static struct fetch_task *
1608 get_fetch_task_from_changed(struct submodule_parallel_fetch *spf,
1609 struct strbuf *err)
1611 for (; spf->changed_count < spf->changed_submodule_names.nr;
1612 spf->changed_count++) {
1613 struct string_list_item item =
1614 spf->changed_submodule_names.items[spf->changed_count];
1615 struct changed_submodule_data *cs_data = item.util;
1616 struct fetch_task *task;
1618 if (!is_tree_submodule_active(spf->r, cs_data->super_oid,cs_data->path))
1619 continue;
1621 task = fetch_task_create(spf, cs_data->path,
1622 cs_data->super_oid);
1623 if (!task)
1624 continue;
1626 if (!task->repo) {
1627 strbuf_addf(err, _("Could not access submodule '%s' at commit %s\n"),
1628 cs_data->path,
1629 find_unique_abbrev(cs_data->super_oid, DEFAULT_ABBREV));
1631 fetch_task_release(task);
1632 free(task);
1633 continue;
1636 if (!spf->quiet)
1637 strbuf_addf(err,
1638 _("Fetching submodule %s%s at commit %s\n"),
1639 spf->prefix, task->sub->path,
1640 find_unique_abbrev(cs_data->super_oid,
1641 DEFAULT_ABBREV));
1643 spf->changed_count++;
1645 * NEEDSWORK: Submodules set/unset a value for
1646 * core.worktree when they are populated/unpopulated by
1647 * "git checkout" (and similar commands, see
1648 * submodule_move_head() and
1649 * connect_work_tree_and_git_dir()), but if the
1650 * submodule is unpopulated in another way (e.g. "git
1651 * rm", "rm -r"), core.worktree will still be set even
1652 * though the directory doesn't exist, and the child
1653 * process will crash while trying to chdir into the
1654 * nonexistent directory.
1656 * In this case, we know that the submodule has no
1657 * working tree, so we can work around this by
1658 * setting "--work-tree=." (--bare does not work because
1659 * worktree settings take precedence over bare-ness).
1660 * However, this is not necessarily true in other cases,
1661 * so a generalized solution is still necessary.
1663 * Possible solutions:
1664 * - teach "git [add|rm]" to unset core.worktree and
1665 * discourage users from removing submodules without
1666 * using a Git command.
1667 * - teach submodule child processes to ignore stale
1668 * core.worktree values.
1670 strvec_push(&task->git_args, "--work-tree=.");
1671 return task;
1673 return NULL;
1676 static int get_next_submodule(struct child_process *cp, struct strbuf *err,
1677 void *data, void **task_cb)
1679 struct submodule_parallel_fetch *spf = data;
1680 struct fetch_task *task =
1681 get_fetch_task_from_index(spf, err);
1682 if (!task)
1683 task = get_fetch_task_from_changed(spf, err);
1685 if (task) {
1686 struct strbuf submodule_prefix = STRBUF_INIT;
1688 child_process_init(cp);
1689 cp->dir = task->repo->gitdir;
1690 prepare_submodule_repo_env_in_gitdir(&cp->env);
1691 cp->git_cmd = 1;
1692 strvec_init(&cp->args);
1693 if (task->git_args.nr)
1694 strvec_pushv(&cp->args, task->git_args.v);
1695 strvec_pushv(&cp->args, spf->args.v);
1696 strvec_push(&cp->args, task->default_argv);
1697 strvec_push(&cp->args, "--submodule-prefix");
1699 strbuf_addf(&submodule_prefix, "%s%s/",
1700 spf->prefix,
1701 task->sub->path);
1702 strvec_push(&cp->args, submodule_prefix.buf);
1703 *task_cb = task;
1705 strbuf_release(&submodule_prefix);
1706 string_list_insert(&spf->seen_submodule_names, task->sub->name);
1707 return 1;
1710 if (spf->oid_fetch_tasks_nr) {
1711 struct fetch_task *task =
1712 spf->oid_fetch_tasks[spf->oid_fetch_tasks_nr - 1];
1713 struct strbuf submodule_prefix = STRBUF_INIT;
1714 spf->oid_fetch_tasks_nr--;
1716 strbuf_addf(&submodule_prefix, "%s%s/",
1717 spf->prefix, task->sub->path);
1719 child_process_init(cp);
1720 prepare_submodule_repo_env_in_gitdir(&cp->env);
1721 cp->git_cmd = 1;
1722 cp->dir = task->repo->gitdir;
1724 strvec_init(&cp->args);
1725 strvec_pushv(&cp->args, spf->args.v);
1726 strvec_push(&cp->args, "on-demand");
1727 strvec_push(&cp->args, "--submodule-prefix");
1728 strvec_push(&cp->args, submodule_prefix.buf);
1730 /* NEEDSWORK: have get_default_remote from submodule--helper */
1731 strvec_push(&cp->args, "origin");
1732 oid_array_for_each_unique(task->commits,
1733 append_oid_to_argv, &cp->args);
1735 *task_cb = task;
1736 strbuf_release(&submodule_prefix);
1737 return 1;
1740 return 0;
1743 static int fetch_start_failure(struct strbuf *err UNUSED,
1744 void *cb, void *task_cb)
1746 struct submodule_parallel_fetch *spf = cb;
1747 struct fetch_task *task = task_cb;
1749 spf->result = 1;
1751 fetch_task_release(task);
1752 return 0;
1755 static int commit_missing_in_sub(const struct object_id *oid, void *data)
1757 struct repository *subrepo = data;
1759 enum object_type type = oid_object_info(subrepo, oid, NULL);
1761 return type != OBJ_COMMIT;
1764 static int fetch_finish(int retvalue, struct strbuf *err UNUSED,
1765 void *cb, void *task_cb)
1767 struct submodule_parallel_fetch *spf = cb;
1768 struct fetch_task *task = task_cb;
1770 struct string_list_item *it;
1771 struct changed_submodule_data *cs_data;
1773 if (!task || !task->sub)
1774 BUG("callback cookie bogus");
1776 if (retvalue) {
1778 * NEEDSWORK: This indicates that the overall fetch
1779 * failed, even though there may be a subsequent fetch
1780 * by commit hash that might work. It may be a good
1781 * idea to not indicate failure in this case, and only
1782 * indicate failure if the subsequent fetch fails.
1784 spf->result = 1;
1786 strbuf_addf(&spf->submodules_with_errors, "\t%s\n",
1787 task->sub->name);
1790 /* Is this the second time we process this submodule? */
1791 if (task->commits)
1792 goto out;
1794 it = string_list_lookup(&spf->changed_submodule_names, task->sub->name);
1795 if (!it)
1796 /* Could be an unchanged submodule, not contained in the list */
1797 goto out;
1799 cs_data = it->util;
1800 oid_array_filter(&cs_data->new_commits,
1801 commit_missing_in_sub,
1802 task->repo);
1804 /* Are there commits we want, but do not exist? */
1805 if (cs_data->new_commits.nr) {
1806 task->commits = &cs_data->new_commits;
1807 ALLOC_GROW(spf->oid_fetch_tasks,
1808 spf->oid_fetch_tasks_nr + 1,
1809 spf->oid_fetch_tasks_alloc);
1810 spf->oid_fetch_tasks[spf->oid_fetch_tasks_nr] = task;
1811 spf->oid_fetch_tasks_nr++;
1812 return 0;
1815 out:
1816 fetch_task_release(task);
1818 return 0;
1821 int fetch_submodules(struct repository *r,
1822 const struct strvec *options,
1823 const char *prefix, int command_line_option,
1824 int default_option,
1825 int quiet, int max_parallel_jobs)
1827 int i;
1828 struct submodule_parallel_fetch spf = SPF_INIT;
1829 const struct run_process_parallel_opts opts = {
1830 .tr2_category = "submodule",
1831 .tr2_label = "parallel/fetch",
1833 .processes = max_parallel_jobs,
1835 .get_next_task = get_next_submodule,
1836 .start_failure = fetch_start_failure,
1837 .task_finished = fetch_finish,
1838 .data = &spf,
1841 spf.r = r;
1842 spf.command_line_option = command_line_option;
1843 spf.default_option = default_option;
1844 spf.quiet = quiet;
1845 spf.prefix = prefix;
1847 if (!r->worktree)
1848 goto out;
1850 if (repo_read_index(r) < 0)
1851 die(_("index file corrupt"));
1853 strvec_push(&spf.args, "fetch");
1854 for (i = 0; i < options->nr; i++)
1855 strvec_push(&spf.args, options->v[i]);
1856 strvec_push(&spf.args, "--recurse-submodules-default");
1857 /* default value, "--submodule-prefix" and its value are added later */
1859 calculate_changed_submodule_paths(r, &spf.changed_submodule_names);
1860 string_list_sort(&spf.changed_submodule_names);
1861 run_processes_parallel(&opts);
1863 if (spf.submodules_with_errors.len > 0)
1864 fprintf(stderr, _("Errors during submodule fetch:\n%s"),
1865 spf.submodules_with_errors.buf);
1868 strvec_clear(&spf.args);
1869 out:
1870 free_submodules_data(&spf.changed_submodule_names);
1871 return spf.result;
1874 unsigned is_submodule_modified(const char *path, int ignore_untracked)
1876 struct child_process cp = CHILD_PROCESS_INIT;
1877 struct strbuf buf = STRBUF_INIT;
1878 FILE *fp;
1879 unsigned dirty_submodule = 0;
1880 const char *git_dir;
1881 int ignore_cp_exit_code = 0;
1883 strbuf_addf(&buf, "%s/.git", path);
1884 git_dir = read_gitfile(buf.buf);
1885 if (!git_dir)
1886 git_dir = buf.buf;
1887 if (!is_git_directory(git_dir)) {
1888 if (is_directory(git_dir))
1889 die(_("'%s' not recognized as a git repository"), git_dir);
1890 strbuf_release(&buf);
1891 /* The submodule is not checked out, so it is not modified */
1892 return 0;
1894 strbuf_reset(&buf);
1896 strvec_pushl(&cp.args, "status", "--porcelain=2", NULL);
1897 if (ignore_untracked)
1898 strvec_push(&cp.args, "-uno");
1900 prepare_submodule_repo_env(&cp.env);
1901 cp.git_cmd = 1;
1902 cp.no_stdin = 1;
1903 cp.out = -1;
1904 cp.dir = path;
1905 if (start_command(&cp))
1906 die(_("Could not run 'git status --porcelain=2' in submodule %s"), path);
1908 fp = xfdopen(cp.out, "r");
1909 while (strbuf_getwholeline(&buf, fp, '\n') != EOF) {
1910 /* regular untracked files */
1911 if (buf.buf[0] == '?')
1912 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
1914 if (buf.buf[0] == 'u' ||
1915 buf.buf[0] == '1' ||
1916 buf.buf[0] == '2') {
1917 /* T = line type, XY = status, SSSS = submodule state */
1918 if (buf.len < strlen("T XY SSSS"))
1919 BUG("invalid status --porcelain=2 line %s",
1920 buf.buf);
1922 if (buf.buf[5] == 'S' && buf.buf[8] == 'U')
1923 /* nested untracked file */
1924 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
1926 if (buf.buf[0] == 'u' ||
1927 buf.buf[0] == '2' ||
1928 memcmp(buf.buf + 5, "S..U", 4))
1929 /* other change */
1930 dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
1933 if ((dirty_submodule & DIRTY_SUBMODULE_MODIFIED) &&
1934 ((dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ||
1935 ignore_untracked)) {
1937 * We're not interested in any further information from
1938 * the child any more, neither output nor its exit code.
1940 ignore_cp_exit_code = 1;
1941 break;
1944 fclose(fp);
1946 if (finish_command(&cp) && !ignore_cp_exit_code)
1947 die(_("'git status --porcelain=2' failed in submodule %s"), path);
1949 strbuf_release(&buf);
1950 return dirty_submodule;
1953 int submodule_uses_gitfile(const char *path)
1955 struct child_process cp = CHILD_PROCESS_INIT;
1956 struct strbuf buf = STRBUF_INIT;
1957 const char *git_dir;
1959 strbuf_addf(&buf, "%s/.git", path);
1960 git_dir = read_gitfile(buf.buf);
1961 if (!git_dir) {
1962 strbuf_release(&buf);
1963 return 0;
1965 strbuf_release(&buf);
1967 /* Now test that all nested submodules use a gitfile too */
1968 strvec_pushl(&cp.args,
1969 "submodule", "foreach", "--quiet", "--recursive",
1970 "test -f .git", NULL);
1972 prepare_submodule_repo_env(&cp.env);
1973 cp.git_cmd = 1;
1974 cp.no_stdin = 1;
1975 cp.no_stderr = 1;
1976 cp.no_stdout = 1;
1977 cp.dir = path;
1978 if (run_command(&cp))
1979 return 0;
1981 return 1;
1985 * Check if it is a bad idea to remove a submodule, i.e. if we'd lose data
1986 * when doing so.
1988 * Return 1 if we'd lose data, return 0 if the removal is fine,
1989 * and negative values for errors.
1991 int bad_to_remove_submodule(const char *path, unsigned flags)
1993 ssize_t len;
1994 struct child_process cp = CHILD_PROCESS_INIT;
1995 struct strbuf buf = STRBUF_INIT;
1996 int ret = 0;
1998 if (!file_exists(path) || is_empty_dir(path))
1999 return 0;
2001 if (!submodule_uses_gitfile(path))
2002 return 1;
2004 strvec_pushl(&cp.args, "status", "--porcelain",
2005 "--ignore-submodules=none", NULL);
2007 if (flags & SUBMODULE_REMOVAL_IGNORE_UNTRACKED)
2008 strvec_push(&cp.args, "-uno");
2009 else
2010 strvec_push(&cp.args, "-uall");
2012 if (!(flags & SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED))
2013 strvec_push(&cp.args, "--ignored");
2015 prepare_submodule_repo_env(&cp.env);
2016 cp.git_cmd = 1;
2017 cp.no_stdin = 1;
2018 cp.out = -1;
2019 cp.dir = path;
2020 if (start_command(&cp)) {
2021 if (flags & SUBMODULE_REMOVAL_DIE_ON_ERROR)
2022 die(_("could not start 'git status' in submodule '%s'"),
2023 path);
2024 ret = -1;
2025 goto out;
2028 len = strbuf_read(&buf, cp.out, 1024);
2029 if (len > 2)
2030 ret = 1;
2031 close(cp.out);
2033 if (finish_command(&cp)) {
2034 if (flags & SUBMODULE_REMOVAL_DIE_ON_ERROR)
2035 die(_("could not run 'git status' in submodule '%s'"),
2036 path);
2037 ret = -1;
2039 out:
2040 strbuf_release(&buf);
2041 return ret;
2044 void submodule_unset_core_worktree(const struct submodule *sub)
2046 struct strbuf config_path = STRBUF_INIT;
2048 submodule_name_to_gitdir(&config_path, the_repository, sub->name);
2049 strbuf_addstr(&config_path, "/config");
2051 if (git_config_set_in_file_gently(config_path.buf, "core.worktree", NULL))
2052 warning(_("Could not unset core.worktree setting in submodule '%s'"),
2053 sub->path);
2055 strbuf_release(&config_path);
2058 static int submodule_has_dirty_index(const struct submodule *sub)
2060 struct child_process cp = CHILD_PROCESS_INIT;
2062 prepare_submodule_repo_env(&cp.env);
2064 cp.git_cmd = 1;
2065 strvec_pushl(&cp.args, "diff-index", "--quiet",
2066 "--cached", "HEAD", NULL);
2067 cp.no_stdin = 1;
2068 cp.no_stdout = 1;
2069 cp.dir = sub->path;
2070 if (start_command(&cp))
2071 die(_("could not recurse into submodule '%s'"), sub->path);
2073 return finish_command(&cp);
2076 static void submodule_reset_index(const char *path, const char *super_prefix)
2078 struct child_process cp = CHILD_PROCESS_INIT;
2079 prepare_submodule_repo_env(&cp.env);
2081 cp.git_cmd = 1;
2082 cp.no_stdin = 1;
2083 cp.dir = path;
2085 /* TODO: determine if this might overwright untracked files */
2086 strvec_pushl(&cp.args, "read-tree", "-u", "--reset", NULL);
2087 strvec_pushf(&cp.args, "--super-prefix=%s%s/",
2088 (super_prefix ? super_prefix : ""), path);
2090 strvec_push(&cp.args, empty_tree_oid_hex());
2092 if (run_command(&cp))
2093 die(_("could not reset submodule index"));
2097 * Moves a submodule at a given path from a given head to another new head.
2098 * For edge cases (a submodule coming into existence or removing a submodule)
2099 * pass NULL for old or new respectively.
2101 int submodule_move_head(const char *path, const char *super_prefix,
2102 const char *old_head, const char *new_head,
2103 unsigned flags)
2105 int ret = 0;
2106 struct child_process cp = CHILD_PROCESS_INIT;
2107 const struct submodule *sub;
2108 int *error_code_ptr, error_code;
2110 if (!is_submodule_active(the_repository, path))
2111 return 0;
2113 if (flags & SUBMODULE_MOVE_HEAD_FORCE)
2115 * Pass non NULL pointer to is_submodule_populated_gently
2116 * to prevent die()-ing. We'll use connect_work_tree_and_git_dir
2117 * to fixup the submodule in the force case later.
2119 error_code_ptr = &error_code;
2120 else
2121 error_code_ptr = NULL;
2123 if (old_head && !is_submodule_populated_gently(path, error_code_ptr))
2124 return 0;
2126 sub = submodule_from_path(the_repository, null_oid(), path);
2128 if (!sub)
2129 BUG("could not get submodule information for '%s'", path);
2131 if (old_head && !(flags & SUBMODULE_MOVE_HEAD_FORCE)) {
2132 /* Check if the submodule has a dirty index. */
2133 if (submodule_has_dirty_index(sub))
2134 return error(_("submodule '%s' has dirty index"), path);
2137 if (!(flags & SUBMODULE_MOVE_HEAD_DRY_RUN)) {
2138 if (old_head) {
2139 if (!submodule_uses_gitfile(path))
2140 absorb_git_dir_into_superproject(path,
2141 super_prefix);
2142 } else {
2143 struct strbuf gitdir = STRBUF_INIT;
2144 submodule_name_to_gitdir(&gitdir, the_repository,
2145 sub->name);
2146 connect_work_tree_and_git_dir(path, gitdir.buf, 0);
2147 strbuf_release(&gitdir);
2149 /* make sure the index is clean as well */
2150 submodule_reset_index(path, super_prefix);
2153 if (old_head && (flags & SUBMODULE_MOVE_HEAD_FORCE)) {
2154 struct strbuf gitdir = STRBUF_INIT;
2155 submodule_name_to_gitdir(&gitdir, the_repository,
2156 sub->name);
2157 connect_work_tree_and_git_dir(path, gitdir.buf, 1);
2158 strbuf_release(&gitdir);
2162 prepare_submodule_repo_env(&cp.env);
2164 cp.git_cmd = 1;
2165 cp.no_stdin = 1;
2166 cp.dir = path;
2168 strvec_pushl(&cp.args, "read-tree", "--recurse-submodules", NULL);
2169 strvec_pushf(&cp.args, "--super-prefix=%s%s/",
2170 (super_prefix ? super_prefix : ""), path);
2172 if (flags & SUBMODULE_MOVE_HEAD_DRY_RUN)
2173 strvec_push(&cp.args, "-n");
2174 else
2175 strvec_push(&cp.args, "-u");
2177 if (flags & SUBMODULE_MOVE_HEAD_FORCE)
2178 strvec_push(&cp.args, "--reset");
2179 else
2180 strvec_push(&cp.args, "-m");
2182 if (!(flags & SUBMODULE_MOVE_HEAD_FORCE))
2183 strvec_push(&cp.args, old_head ? old_head : empty_tree_oid_hex());
2185 strvec_push(&cp.args, new_head ? new_head : empty_tree_oid_hex());
2187 if (run_command(&cp)) {
2188 ret = error(_("Submodule '%s' could not be updated."), path);
2189 goto out;
2192 if (!(flags & SUBMODULE_MOVE_HEAD_DRY_RUN)) {
2193 if (new_head) {
2194 child_process_init(&cp);
2195 /* also set the HEAD accordingly */
2196 cp.git_cmd = 1;
2197 cp.no_stdin = 1;
2198 cp.dir = path;
2200 prepare_submodule_repo_env(&cp.env);
2201 strvec_pushl(&cp.args, "update-ref", "HEAD",
2202 "--no-deref", new_head, NULL);
2204 if (run_command(&cp)) {
2205 ret = -1;
2206 goto out;
2208 } else {
2209 struct strbuf sb = STRBUF_INIT;
2211 strbuf_addf(&sb, "%s/.git", path);
2212 unlink_or_warn(sb.buf);
2213 strbuf_release(&sb);
2215 if (is_empty_dir(path))
2216 rmdir_or_warn(path);
2218 submodule_unset_core_worktree(sub);
2221 out:
2222 return ret;
2225 int validate_submodule_git_dir(char *git_dir, const char *submodule_name)
2227 size_t len = strlen(git_dir), suffix_len = strlen(submodule_name);
2228 char *p;
2229 int ret = 0;
2231 if (len <= suffix_len || (p = git_dir + len - suffix_len)[-1] != '/' ||
2232 strcmp(p, submodule_name))
2233 BUG("submodule name '%s' not a suffix of git dir '%s'",
2234 submodule_name, git_dir);
2237 * We prevent the contents of sibling submodules' git directories to
2238 * clash.
2240 * Example: having a submodule named `hippo` and another one named
2241 * `hippo/hooks` would result in the git directories
2242 * `.git/modules/hippo/` and `.git/modules/hippo/hooks/`, respectively,
2243 * but the latter directory is already designated to contain the hooks
2244 * of the former.
2246 for (; *p; p++) {
2247 if (is_dir_sep(*p)) {
2248 char c = *p;
2250 *p = '\0';
2251 if (is_git_directory(git_dir))
2252 ret = -1;
2253 *p = c;
2255 if (ret < 0)
2256 return error(_("submodule git dir '%s' is "
2257 "inside git dir '%.*s'"),
2258 git_dir,
2259 (int)(p - git_dir), git_dir);
2263 return 0;
2267 * Embeds a single submodules git directory into the superprojects git dir,
2268 * non recursively.
2270 static void relocate_single_git_dir_into_superproject(const char *path,
2271 const char *super_prefix)
2273 char *old_git_dir = NULL, *real_old_git_dir = NULL, *real_new_git_dir = NULL;
2274 struct strbuf new_gitdir = STRBUF_INIT;
2275 const struct submodule *sub;
2277 if (submodule_uses_worktrees(path))
2278 die(_("relocate_gitdir for submodule '%s' with "
2279 "more than one worktree not supported"), path);
2281 old_git_dir = xstrfmt("%s/.git", path);
2282 if (read_gitfile(old_git_dir))
2283 /* If it is an actual gitfile, it doesn't need migration. */
2284 return;
2286 real_old_git_dir = real_pathdup(old_git_dir, 1);
2288 sub = submodule_from_path(the_repository, null_oid(), path);
2289 if (!sub)
2290 die(_("could not lookup name for submodule '%s'"), path);
2292 submodule_name_to_gitdir(&new_gitdir, the_repository, sub->name);
2293 if (validate_submodule_git_dir(new_gitdir.buf, sub->name) < 0)
2294 die(_("refusing to move '%s' into an existing git dir"),
2295 real_old_git_dir);
2296 if (safe_create_leading_directories_const(new_gitdir.buf) < 0)
2297 die(_("could not create directory '%s'"), new_gitdir.buf);
2298 real_new_git_dir = real_pathdup(new_gitdir.buf, 1);
2300 fprintf(stderr, _("Migrating git directory of '%s%s' from\n'%s' to\n'%s'\n"),
2301 super_prefix ? super_prefix : "", path,
2302 real_old_git_dir, real_new_git_dir);
2304 relocate_gitdir(path, real_old_git_dir, real_new_git_dir);
2306 free(old_git_dir);
2307 free(real_old_git_dir);
2308 free(real_new_git_dir);
2309 strbuf_release(&new_gitdir);
2312 static void absorb_git_dir_into_superproject_recurse(const char *path,
2313 const char *super_prefix)
2316 struct child_process cp = CHILD_PROCESS_INIT;
2318 cp.dir = path;
2319 cp.git_cmd = 1;
2320 cp.no_stdin = 1;
2321 strvec_pushl(&cp.args, "submodule--helper",
2322 "absorbgitdirs", NULL);
2323 strvec_pushf(&cp.args, "--super-prefix=%s%s/", super_prefix ?
2324 super_prefix : "", path);
2326 prepare_submodule_repo_env(&cp.env);
2327 if (run_command(&cp))
2328 die(_("could not recurse into submodule '%s'"), path);
2332 * Migrate the git directory of the submodule given by path from
2333 * having its git directory within the working tree to the git dir nested
2334 * in its superprojects git dir under modules/.
2336 void absorb_git_dir_into_superproject(const char *path,
2337 const char *super_prefix)
2339 int err_code;
2340 const char *sub_git_dir;
2341 struct strbuf gitdir = STRBUF_INIT;
2342 strbuf_addf(&gitdir, "%s/.git", path);
2343 sub_git_dir = resolve_gitdir_gently(gitdir.buf, &err_code);
2345 /* Not populated? */
2346 if (!sub_git_dir) {
2347 const struct submodule *sub;
2348 struct strbuf sub_gitdir = STRBUF_INIT;
2350 if (err_code == READ_GITFILE_ERR_STAT_FAILED) {
2351 /* unpopulated as expected */
2352 strbuf_release(&gitdir);
2353 return;
2356 if (err_code != READ_GITFILE_ERR_NOT_A_REPO)
2357 /* We don't know what broke here. */
2358 read_gitfile_error_die(err_code, path, NULL);
2361 * Maybe populated, but no git directory was found?
2362 * This can happen if the superproject is a submodule
2363 * itself and was just absorbed. The absorption of the
2364 * superproject did not rewrite the git file links yet,
2365 * fix it now.
2367 sub = submodule_from_path(the_repository, null_oid(), path);
2368 if (!sub)
2369 die(_("could not lookup name for submodule '%s'"), path);
2370 submodule_name_to_gitdir(&sub_gitdir, the_repository, sub->name);
2371 connect_work_tree_and_git_dir(path, sub_gitdir.buf, 0);
2372 strbuf_release(&sub_gitdir);
2373 } else {
2374 /* Is it already absorbed into the superprojects git dir? */
2375 char *real_sub_git_dir = real_pathdup(sub_git_dir, 1);
2376 char *real_common_git_dir = real_pathdup(get_git_common_dir(), 1);
2378 if (!starts_with(real_sub_git_dir, real_common_git_dir))
2379 relocate_single_git_dir_into_superproject(path, super_prefix);
2381 free(real_sub_git_dir);
2382 free(real_common_git_dir);
2384 strbuf_release(&gitdir);
2386 absorb_git_dir_into_superproject_recurse(path, super_prefix);
2389 int get_superproject_working_tree(struct strbuf *buf)
2391 struct child_process cp = CHILD_PROCESS_INIT;
2392 struct strbuf sb = STRBUF_INIT;
2393 struct strbuf one_up = STRBUF_INIT;
2394 char *cwd = xgetcwd();
2395 int ret = 0;
2396 const char *subpath;
2397 int code;
2398 ssize_t len;
2400 if (!is_inside_work_tree())
2402 * FIXME:
2403 * We might have a superproject, but it is harder
2404 * to determine.
2406 return 0;
2408 if (!strbuf_realpath(&one_up, "../", 0))
2409 return 0;
2411 subpath = relative_path(cwd, one_up.buf, &sb);
2412 strbuf_release(&one_up);
2414 prepare_submodule_repo_env(&cp.env);
2415 strvec_pop(&cp.env);
2417 strvec_pushl(&cp.args, "--literal-pathspecs", "-C", "..",
2418 "ls-files", "-z", "--stage", "--full-name", "--",
2419 subpath, NULL);
2420 strbuf_reset(&sb);
2422 cp.no_stdin = 1;
2423 cp.no_stderr = 1;
2424 cp.out = -1;
2425 cp.git_cmd = 1;
2427 if (start_command(&cp))
2428 die(_("could not start ls-files in .."));
2430 len = strbuf_read(&sb, cp.out, PATH_MAX);
2431 close(cp.out);
2433 if (starts_with(sb.buf, "160000")) {
2434 int super_sub_len;
2435 int cwd_len = strlen(cwd);
2436 char *super_sub, *super_wt;
2439 * There is a superproject having this repo as a submodule.
2440 * The format is <mode> SP <hash> SP <stage> TAB <full name> \0,
2441 * We're only interested in the name after the tab.
2443 super_sub = strchr(sb.buf, '\t') + 1;
2444 super_sub_len = strlen(super_sub);
2446 if (super_sub_len > cwd_len ||
2447 strcmp(&cwd[cwd_len - super_sub_len], super_sub))
2448 BUG("returned path string doesn't match cwd?");
2450 super_wt = xstrdup(cwd);
2451 super_wt[cwd_len - super_sub_len] = '\0';
2453 strbuf_realpath(buf, super_wt, 1);
2454 ret = 1;
2455 free(super_wt);
2457 free(cwd);
2458 strbuf_release(&sb);
2460 code = finish_command(&cp);
2462 if (code == 128)
2463 /* '../' is not a git repository */
2464 return 0;
2465 if (code == 0 && len == 0)
2466 /* There is an unrelated git repository at '../' */
2467 return 0;
2468 if (code)
2469 die(_("ls-tree returned unexpected return code %d"), code);
2471 return ret;
2475 * Put the gitdir for a submodule (given relative to the main
2476 * repository worktree) into `buf`, or return -1 on error.
2478 int submodule_to_gitdir(struct strbuf *buf, const char *submodule)
2480 const struct submodule *sub;
2481 const char *git_dir;
2482 int ret = 0;
2484 strbuf_reset(buf);
2485 strbuf_addstr(buf, submodule);
2486 strbuf_complete(buf, '/');
2487 strbuf_addstr(buf, ".git");
2489 git_dir = read_gitfile(buf->buf);
2490 if (git_dir) {
2491 strbuf_reset(buf);
2492 strbuf_addstr(buf, git_dir);
2494 if (!is_git_directory(buf->buf)) {
2495 sub = submodule_from_path(the_repository, null_oid(),
2496 submodule);
2497 if (!sub) {
2498 ret = -1;
2499 goto cleanup;
2501 strbuf_reset(buf);
2502 submodule_name_to_gitdir(buf, the_repository, sub->name);
2505 cleanup:
2506 return ret;
2509 void submodule_name_to_gitdir(struct strbuf *buf, struct repository *r,
2510 const char *submodule_name)
2513 * NEEDSWORK: The current way of mapping a submodule's name to
2514 * its location in .git/modules/ has problems with some naming
2515 * schemes. For example, if a submodule is named "foo" and
2516 * another is named "foo/bar" (whether present in the same
2517 * superproject commit or not - the problem will arise if both
2518 * superproject commits have been checked out at any point in
2519 * time), or if two submodule names only have different cases in
2520 * a case-insensitive filesystem.
2522 * There are several solutions, including encoding the path in
2523 * some way, introducing a submodule.<name>.gitdir config in
2524 * .git/config (not .gitmodules) that allows overriding what the
2525 * gitdir of a submodule would be (and teach Git, upon noticing
2526 * a clash, to automatically determine a non-clashing name and
2527 * to write such a config), or introducing a
2528 * submodule.<name>.gitdir config in .gitmodules that repo
2529 * administrators can explicitly set. Nothing has been decided,
2530 * so for now, just append the name at the end of the path.
2532 strbuf_repo_git_path(buf, r, "modules/");
2533 strbuf_addstr(buf, submodule_name);