The seventh batch
[alt-git.git] / submodule.c
blob74d5766f07c31149047afe190add31473182b3ef
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "abspath.h"
5 #include "repository.h"
6 #include "config.h"
7 #include "submodule-config.h"
8 #include "submodule.h"
9 #include "dir.h"
10 #include "diff.h"
11 #include "commit.h"
12 #include "environment.h"
13 #include "gettext.h"
14 #include "hex.h"
15 #include "revision.h"
16 #include "run-command.h"
17 #include "diffcore.h"
18 #include "refs.h"
19 #include "string-list.h"
20 #include "oid-array.h"
21 #include "strvec.h"
22 #include "thread-utils.h"
23 #include "path.h"
24 #include "remote.h"
25 #include "worktree.h"
26 #include "parse-options.h"
27 #include "object-file.h"
28 #include "object-name.h"
29 #include "object-store-ll.h"
30 #include "commit-reach.h"
31 #include "read-cache-ll.h"
32 #include "setup.h"
33 #include "trace2.h"
35 static int config_update_recurse_submodules = RECURSE_SUBMODULES_OFF;
36 static int initialized_fetch_ref_tips;
37 static struct oid_array ref_tips_before_fetch;
38 static struct oid_array ref_tips_after_fetch;
41 * Check if the .gitmodules file is unmerged. Parsing of the .gitmodules file
42 * will be disabled because we can't guess what might be configured in
43 * .gitmodules unless the user resolves the conflict.
45 int is_gitmodules_unmerged(struct index_state *istate)
47 int pos = index_name_pos(istate, GITMODULES_FILE, strlen(GITMODULES_FILE));
48 if (pos < 0) { /* .gitmodules not found or isn't merged */
49 pos = -1 - pos;
50 if (istate->cache_nr > pos) { /* there is a .gitmodules */
51 const struct cache_entry *ce = istate->cache[pos];
52 if (ce_namelen(ce) == strlen(GITMODULES_FILE) &&
53 !strcmp(ce->name, GITMODULES_FILE))
54 return 1;
58 return 0;
62 * Check if the .gitmodules file is safe to write.
64 * Writing to the .gitmodules file requires that the file exists in the
65 * working tree or, if it doesn't, that a brand new .gitmodules file is going
66 * to be created (i.e. it's neither in the index nor in the current branch).
68 * It is not safe to write to .gitmodules if it's not in the working tree but
69 * it is in the index or in the current branch, because writing new values
70 * (and staging them) would blindly overwrite ALL the old content.
72 int is_writing_gitmodules_ok(void)
74 struct object_id oid;
75 return file_exists(GITMODULES_FILE) ||
76 (repo_get_oid(the_repository, GITMODULES_INDEX, &oid) < 0 && repo_get_oid(the_repository, GITMODULES_HEAD, &oid) < 0);
80 * Check if the .gitmodules file has unstaged modifications. This must be
81 * checked before allowing modifications to the .gitmodules file with the
82 * intention to stage them later, because when continuing we would stage the
83 * modifications the user didn't stage herself too. That might change in a
84 * future version when we learn to stage the changes we do ourselves without
85 * staging any previous modifications.
87 int is_staging_gitmodules_ok(struct index_state *istate)
89 int pos = index_name_pos(istate, GITMODULES_FILE, strlen(GITMODULES_FILE));
91 if ((pos >= 0) && (pos < istate->cache_nr)) {
92 struct stat st;
93 if (lstat(GITMODULES_FILE, &st) == 0 &&
94 ie_modified(istate, istate->cache[pos], &st, 0) & DATA_CHANGED)
95 return 0;
98 return 1;
101 static int for_each_remote_ref_submodule(const char *submodule,
102 each_ref_fn fn, void *cb_data)
104 return refs_for_each_remote_ref(repo_get_submodule_ref_store(the_repository,
105 submodule),
106 fn, cb_data);
110 * Try to update the "path" entry in the "submodule.<name>" section of the
111 * .gitmodules file. Return 0 only if a .gitmodules file was found, a section
112 * with the correct path=<oldpath> setting was found and we could update it.
114 int update_path_in_gitmodules(const char *oldpath, const char *newpath)
116 struct strbuf entry = STRBUF_INIT;
117 const struct submodule *submodule;
118 int ret;
120 if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */
121 return -1;
123 if (is_gitmodules_unmerged(the_repository->index))
124 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
126 submodule = submodule_from_path(the_repository, null_oid(), oldpath);
127 if (!submodule || !submodule->name) {
128 warning(_("Could not find section in .gitmodules where path=%s"), oldpath);
129 return -1;
131 strbuf_addstr(&entry, "submodule.");
132 strbuf_addstr(&entry, submodule->name);
133 strbuf_addstr(&entry, ".path");
134 ret = config_set_in_gitmodules_file_gently(entry.buf, newpath);
135 strbuf_release(&entry);
136 return ret;
140 * Try to remove the "submodule.<name>" section from .gitmodules where the given
141 * path is configured. Return 0 only if a .gitmodules file was found, a section
142 * with the correct path=<path> setting was found and we could remove it.
144 int remove_path_from_gitmodules(const char *path)
146 struct strbuf sect = STRBUF_INIT;
147 const struct submodule *submodule;
149 if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */
150 return -1;
152 if (is_gitmodules_unmerged(the_repository->index))
153 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
155 submodule = submodule_from_path(the_repository, null_oid(), path);
156 if (!submodule || !submodule->name) {
157 warning(_("Could not find section in .gitmodules where path=%s"), path);
158 return -1;
160 strbuf_addstr(&sect, "submodule.");
161 strbuf_addstr(&sect, submodule->name);
162 if (repo_config_rename_section_in_file(the_repository, GITMODULES_FILE, sect.buf, NULL) < 0) {
163 /* Maybe the user already did that, don't error out here */
164 warning(_("Could not remove .gitmodules entry for %s"), path);
165 strbuf_release(&sect);
166 return -1;
168 strbuf_release(&sect);
169 return 0;
172 void stage_updated_gitmodules(struct index_state *istate)
174 if (add_file_to_index(istate, GITMODULES_FILE, 0))
175 die(_("staging updated .gitmodules failed"));
178 static struct string_list added_submodule_odb_paths = STRING_LIST_INIT_DUP;
180 void add_submodule_odb_by_path(const char *path)
182 string_list_insert(&added_submodule_odb_paths, path);
185 int register_all_submodule_odb_as_alternates(void)
187 int i;
188 int ret = added_submodule_odb_paths.nr;
190 for (i = 0; i < added_submodule_odb_paths.nr; i++)
191 add_to_alternates_memory(added_submodule_odb_paths.items[i].string);
192 if (ret) {
193 string_list_clear(&added_submodule_odb_paths, 0);
194 trace2_data_intmax("submodule", the_repository,
195 "register_all_submodule_odb_as_alternates/registered", ret);
196 if (git_env_bool("GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB", 0))
197 BUG("register_all_submodule_odb_as_alternates() called");
199 return ret;
202 void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
203 const char *path)
205 const struct submodule *submodule = submodule_from_path(the_repository,
206 null_oid(),
207 path);
208 if (submodule) {
209 const char *ignore;
210 char *key;
212 key = xstrfmt("submodule.%s.ignore", submodule->name);
213 if (repo_config_get_string_tmp(the_repository, key, &ignore))
214 ignore = submodule->ignore;
215 free(key);
217 if (ignore)
218 handle_ignore_submodules_arg(diffopt, ignore);
219 else if (is_gitmodules_unmerged(the_repository->index))
220 diffopt->flags.ignore_submodules = 1;
224 /* Cheap function that only determines if we're interested in submodules at all */
225 int git_default_submodule_config(const char *var, const char *value,
226 void *cb UNUSED)
228 if (!strcmp(var, "submodule.recurse")) {
229 int v = git_config_bool(var, value) ?
230 RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF;
231 config_update_recurse_submodules = v;
233 return 0;
236 int option_parse_recurse_submodules_worktree_updater(const struct option *opt,
237 const char *arg, int unset)
239 if (unset) {
240 config_update_recurse_submodules = RECURSE_SUBMODULES_OFF;
241 return 0;
243 if (arg)
244 config_update_recurse_submodules =
245 parse_update_recurse_submodules_arg(opt->long_name,
246 arg);
247 else
248 config_update_recurse_submodules = RECURSE_SUBMODULES_ON;
250 return 0;
254 * Determine if a submodule has been initialized at a given 'path'
257 * NEEDSWORK: Emit a warning if submodule.active exists, but is valueless,
258 * ie, the config looks like: "[submodule] active\n".
259 * Since that is an invalid pathspec, we should inform the user.
261 int is_tree_submodule_active(struct repository *repo,
262 const struct object_id *treeish_name,
263 const char *path)
265 int ret = 0;
266 char *key = NULL;
267 char *value = NULL;
268 const struct string_list *sl;
269 const struct submodule *module;
271 module = submodule_from_path(repo, treeish_name, path);
273 /* early return if there isn't a path->module mapping */
274 if (!module)
275 return 0;
277 /* submodule.<name>.active is set */
278 key = xstrfmt("submodule.%s.active", module->name);
279 if (!repo_config_get_bool(repo, key, &ret)) {
280 free(key);
281 return ret;
283 free(key);
285 /* submodule.active is set */
286 if (!repo_config_get_string_multi(repo, "submodule.active", &sl)) {
287 struct pathspec ps;
288 struct strvec args = STRVEC_INIT;
289 const struct string_list_item *item;
291 for_each_string_list_item(item, sl) {
292 strvec_push(&args, item->string);
295 parse_pathspec(&ps, 0, 0, NULL, args.v);
296 ret = match_pathspec(repo->index, &ps, path, strlen(path), 0, NULL, 1);
298 strvec_clear(&args);
299 clear_pathspec(&ps);
300 return ret;
303 /* fallback to checking if the URL is set */
304 key = xstrfmt("submodule.%s.url", module->name);
305 ret = !repo_config_get_string(repo, key, &value);
307 free(value);
308 free(key);
309 return ret;
312 int is_submodule_active(struct repository *repo, const char *path)
314 return is_tree_submodule_active(repo, null_oid(), path);
317 int is_submodule_populated_gently(const char *path, int *return_error_code)
319 int ret = 0;
320 char *gitdir = xstrfmt("%s/.git", path);
322 if (resolve_gitdir_gently(gitdir, return_error_code))
323 ret = 1;
325 free(gitdir);
326 return ret;
330 * Dies if the provided 'prefix' corresponds to an unpopulated submodule
332 void die_in_unpopulated_submodule(struct index_state *istate,
333 const char *prefix)
335 int i, prefixlen;
337 if (!prefix)
338 return;
340 prefixlen = strlen(prefix);
342 for (i = 0; i < istate->cache_nr; i++) {
343 struct cache_entry *ce = istate->cache[i];
344 int ce_len = ce_namelen(ce);
346 if (!S_ISGITLINK(ce->ce_mode))
347 continue;
348 if (prefixlen <= ce_len)
349 continue;
350 if (strncmp(ce->name, prefix, ce_len))
351 continue;
352 if (prefix[ce_len] != '/')
353 continue;
355 die(_("in unpopulated submodule '%s'"), ce->name);
360 * Dies if any paths in the provided pathspec descends into a submodule
362 void die_path_inside_submodule(struct index_state *istate,
363 const struct pathspec *ps)
365 int i, j;
367 for (i = 0; i < istate->cache_nr; i++) {
368 struct cache_entry *ce = istate->cache[i];
369 int ce_len = ce_namelen(ce);
371 if (!S_ISGITLINK(ce->ce_mode))
372 continue;
374 for (j = 0; j < ps->nr ; j++) {
375 const struct pathspec_item *item = &ps->items[j];
377 if (item->len <= ce_len)
378 continue;
379 if (item->match[ce_len] != '/')
380 continue;
381 if (strncmp(ce->name, item->match, ce_len))
382 continue;
383 if (item->len == ce_len + 1)
384 continue;
386 die(_("Pathspec '%s' is in submodule '%.*s'"),
387 item->original, ce_len, ce->name);
392 enum submodule_update_type parse_submodule_update_type(const char *value)
394 if (!strcmp(value, "none"))
395 return SM_UPDATE_NONE;
396 else if (!strcmp(value, "checkout"))
397 return SM_UPDATE_CHECKOUT;
398 else if (!strcmp(value, "rebase"))
399 return SM_UPDATE_REBASE;
400 else if (!strcmp(value, "merge"))
401 return SM_UPDATE_MERGE;
402 else if (*value == '!')
403 return SM_UPDATE_COMMAND;
404 else
405 return SM_UPDATE_UNSPECIFIED;
408 int parse_submodule_update_strategy(const char *value,
409 struct submodule_update_strategy *dst)
411 enum submodule_update_type type;
413 free((void*)dst->command);
414 dst->command = NULL;
416 type = parse_submodule_update_type(value);
417 if (type == SM_UPDATE_UNSPECIFIED)
418 return -1;
420 dst->type = type;
421 if (type == SM_UPDATE_COMMAND)
422 dst->command = xstrdup(value + 1);
424 return 0;
427 void submodule_update_strategy_release(struct submodule_update_strategy *strategy)
429 free((char *) strategy->command);
432 const char *submodule_update_type_to_string(enum submodule_update_type type)
434 switch (type) {
435 case SM_UPDATE_CHECKOUT:
436 return "checkout";
437 case SM_UPDATE_MERGE:
438 return "merge";
439 case SM_UPDATE_REBASE:
440 return "rebase";
441 case SM_UPDATE_NONE:
442 return "none";
443 case SM_UPDATE_UNSPECIFIED:
444 case SM_UPDATE_COMMAND:
445 BUG("init_submodule() should handle type %d", type);
446 default:
447 BUG("unexpected update strategy type: %d", type);
451 void handle_ignore_submodules_arg(struct diff_options *diffopt,
452 const char *arg)
454 diffopt->flags.ignore_submodule_set = 1;
455 diffopt->flags.ignore_submodules = 0;
456 diffopt->flags.ignore_untracked_in_submodules = 0;
457 diffopt->flags.ignore_dirty_submodules = 0;
459 if (!strcmp(arg, "all"))
460 diffopt->flags.ignore_submodules = 1;
461 else if (!strcmp(arg, "untracked"))
462 diffopt->flags.ignore_untracked_in_submodules = 1;
463 else if (!strcmp(arg, "dirty"))
464 diffopt->flags.ignore_dirty_submodules = 1;
465 else if (strcmp(arg, "none"))
466 die(_("bad --ignore-submodules argument: %s"), arg);
468 * Please update _git_status() in git-completion.bash when you
469 * add new options
473 static int prepare_submodule_diff_summary(struct repository *r, struct rev_info *rev,
474 const char *path,
475 struct commit *left, struct commit *right,
476 struct commit_list *merge_bases)
478 struct commit_list *list;
480 repo_init_revisions(r, rev, NULL);
481 setup_revisions(0, NULL, rev, NULL);
482 rev->left_right = 1;
483 rev->first_parent_only = 1;
484 left->object.flags |= SYMMETRIC_LEFT;
485 add_pending_object(rev, &left->object, path);
486 add_pending_object(rev, &right->object, path);
487 for (list = merge_bases; list; list = list->next) {
488 list->item->object.flags |= UNINTERESTING;
489 add_pending_object(rev, &list->item->object,
490 oid_to_hex(&list->item->object.oid));
492 return prepare_revision_walk(rev);
495 static void print_submodule_diff_summary(struct repository *r, struct rev_info *rev, struct diff_options *o)
497 static const char format[] = " %m %s";
498 struct strbuf sb = STRBUF_INIT;
499 struct commit *commit;
501 while ((commit = get_revision(rev))) {
502 struct pretty_print_context ctx = {0};
503 ctx.date_mode = rev->date_mode;
504 ctx.output_encoding = get_log_output_encoding();
505 strbuf_setlen(&sb, 0);
506 repo_format_commit_message(r, commit, format, &sb,
507 &ctx);
508 strbuf_addch(&sb, '\n');
509 if (commit->object.flags & SYMMETRIC_LEFT)
510 diff_emit_submodule_del(o, sb.buf);
511 else
512 diff_emit_submodule_add(o, sb.buf);
514 strbuf_release(&sb);
517 void prepare_submodule_repo_env(struct strvec *out)
519 prepare_other_repo_env(out, DEFAULT_GIT_DIR_ENVIRONMENT);
522 static void prepare_submodule_repo_env_in_gitdir(struct strvec *out)
524 prepare_other_repo_env(out, ".");
528 * Initialize a repository struct for a submodule based on the provided 'path'.
530 * Returns the repository struct on success,
531 * NULL when the submodule is not present.
533 static struct repository *open_submodule(const char *path)
535 struct strbuf sb = STRBUF_INIT;
536 struct repository *out = xmalloc(sizeof(*out));
538 if (submodule_to_gitdir(&sb, path) || repo_init(out, sb.buf, NULL)) {
539 strbuf_release(&sb);
540 free(out);
541 return NULL;
544 /* Mark it as a submodule */
545 out->submodule_prefix = xstrdup(path);
547 strbuf_release(&sb);
548 return out;
552 * Helper function to display the submodule header line prior to the full
553 * summary output.
555 * If it can locate the submodule git directory it will create a repository
556 * handle for the submodule and lookup both the left and right commits and
557 * put them into the left and right pointers.
559 static void show_submodule_header(struct diff_options *o,
560 const char *path,
561 struct object_id *one, struct object_id *two,
562 unsigned dirty_submodule,
563 struct repository *sub,
564 struct commit **left, struct commit **right,
565 struct commit_list **merge_bases)
567 const char *message = NULL;
568 struct strbuf sb = STRBUF_INIT;
569 int fast_forward = 0, fast_backward = 0;
571 if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
572 diff_emit_submodule_untracked(o, path);
574 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
575 diff_emit_submodule_modified(o, path);
577 if (is_null_oid(one))
578 message = "(new submodule)";
579 else if (is_null_oid(two))
580 message = "(submodule deleted)";
582 if (!sub) {
583 if (!message)
584 message = "(commits not present)";
585 goto output_header;
589 * Attempt to lookup the commit references, and determine if this is
590 * a fast forward or fast backwards update.
592 *left = lookup_commit_reference(sub, one);
593 *right = lookup_commit_reference(sub, two);
596 * Warn about missing commits in the submodule project, but only if
597 * they aren't null.
599 if ((!is_null_oid(one) && !*left) ||
600 (!is_null_oid(two) && !*right))
601 message = "(commits not present)";
603 *merge_bases = NULL;
604 if (repo_get_merge_bases(sub, *left, *right, merge_bases) < 0) {
605 message = "(corrupt repository)";
606 goto output_header;
609 if (*merge_bases) {
610 if ((*merge_bases)->item == *left)
611 fast_forward = 1;
612 else if ((*merge_bases)->item == *right)
613 fast_backward = 1;
616 if (oideq(one, two)) {
617 strbuf_release(&sb);
618 return;
621 output_header:
622 strbuf_addf(&sb, "Submodule %s ", path);
623 strbuf_add_unique_abbrev(&sb, one, DEFAULT_ABBREV);
624 strbuf_addstr(&sb, (fast_backward || fast_forward) ? ".." : "...");
625 strbuf_add_unique_abbrev(&sb, two, DEFAULT_ABBREV);
626 if (message)
627 strbuf_addf(&sb, " %s\n", message);
628 else
629 strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : "");
630 diff_emit_submodule_header(o, sb.buf);
632 strbuf_release(&sb);
635 void show_submodule_diff_summary(struct diff_options *o, const char *path,
636 struct object_id *one, struct object_id *two,
637 unsigned dirty_submodule)
639 struct rev_info rev = REV_INFO_INIT;
640 struct commit *left = NULL, *right = NULL;
641 struct commit_list *merge_bases = NULL;
642 struct repository *sub;
644 sub = open_submodule(path);
645 show_submodule_header(o, path, one, two, dirty_submodule,
646 sub, &left, &right, &merge_bases);
649 * If we don't have both a left and a right pointer, there is no
650 * reason to try and display a summary. The header line should contain
651 * all the information the user needs.
653 if (!left || !right || !sub)
654 goto out;
656 /* Treat revision walker failure the same as missing commits */
657 if (prepare_submodule_diff_summary(sub, &rev, path, left, right, merge_bases)) {
658 diff_emit_submodule_error(o, "(revision walker failed)\n");
659 goto out;
662 print_submodule_diff_summary(sub, &rev, o);
664 out:
665 free_commit_list(merge_bases);
666 release_revisions(&rev);
667 clear_commit_marks(left, ~0);
668 clear_commit_marks(right, ~0);
669 if (sub) {
670 repo_clear(sub);
671 free(sub);
675 void show_submodule_inline_diff(struct diff_options *o, const char *path,
676 struct object_id *one, struct object_id *two,
677 unsigned dirty_submodule)
679 const struct object_id *old_oid = the_hash_algo->empty_tree, *new_oid = the_hash_algo->empty_tree;
680 struct commit *left = NULL, *right = NULL;
681 struct commit_list *merge_bases = NULL;
682 struct child_process cp = CHILD_PROCESS_INIT;
683 struct strbuf sb = STRBUF_INIT;
684 struct repository *sub;
686 sub = open_submodule(path);
687 show_submodule_header(o, path, one, two, dirty_submodule,
688 sub, &left, &right, &merge_bases);
690 /* We need a valid left and right commit to display a difference */
691 if (!(left || is_null_oid(one)) ||
692 !(right || is_null_oid(two)))
693 goto done;
695 if (left)
696 old_oid = one;
697 if (right)
698 new_oid = two;
700 cp.git_cmd = 1;
701 cp.dir = path;
702 cp.out = -1;
703 cp.no_stdin = 1;
705 /* TODO: other options may need to be passed here. */
706 strvec_pushl(&cp.args, "diff", "--submodule=diff", NULL);
707 strvec_pushf(&cp.args, "--color=%s", want_color(o->use_color) ?
708 "always" : "never");
710 if (o->flags.reverse_diff) {
711 strvec_pushf(&cp.args, "--src-prefix=%s%s/",
712 o->b_prefix, path);
713 strvec_pushf(&cp.args, "--dst-prefix=%s%s/",
714 o->a_prefix, path);
715 } else {
716 strvec_pushf(&cp.args, "--src-prefix=%s%s/",
717 o->a_prefix, path);
718 strvec_pushf(&cp.args, "--dst-prefix=%s%s/",
719 o->b_prefix, path);
721 strvec_push(&cp.args, oid_to_hex(old_oid));
723 * If the submodule has modified content, we will diff against the
724 * work tree, under the assumption that the user has asked for the
725 * diff format and wishes to actually see all differences even if they
726 * haven't yet been committed to the submodule yet.
728 if (!(dirty_submodule & DIRTY_SUBMODULE_MODIFIED))
729 strvec_push(&cp.args, oid_to_hex(new_oid));
731 prepare_submodule_repo_env(&cp.env);
733 if (!is_directory(path)) {
734 /* fall back to absorbed git dir, if any */
735 if (!sub)
736 goto done;
737 cp.dir = sub->gitdir;
738 strvec_push(&cp.env, GIT_DIR_ENVIRONMENT "=.");
739 strvec_push(&cp.env, GIT_WORK_TREE_ENVIRONMENT "=.");
742 if (start_command(&cp)) {
743 diff_emit_submodule_error(o, "(diff failed)\n");
744 goto done;
747 while (strbuf_getwholeline_fd(&sb, cp.out, '\n') != EOF)
748 diff_emit_submodule_pipethrough(o, sb.buf, sb.len);
750 if (finish_command(&cp))
751 diff_emit_submodule_error(o, "(diff failed)\n");
753 done:
754 strbuf_release(&sb);
755 free_commit_list(merge_bases);
756 if (left)
757 clear_commit_marks(left, ~0);
758 if (right)
759 clear_commit_marks(right, ~0);
760 if (sub) {
761 repo_clear(sub);
762 free(sub);
766 int should_update_submodules(void)
768 return config_update_recurse_submodules == RECURSE_SUBMODULES_ON;
771 const struct submodule *submodule_from_ce(const struct cache_entry *ce)
773 if (!S_ISGITLINK(ce->ce_mode))
774 return NULL;
776 if (!should_update_submodules())
777 return NULL;
779 return submodule_from_path(the_repository, null_oid(), ce->name);
783 struct collect_changed_submodules_cb_data {
784 struct repository *repo;
785 struct string_list *changed;
786 const struct object_id *commit_oid;
790 * this would normally be two functions: default_name_from_path() and
791 * path_from_default_name(). Since the default name is the same as
792 * the submodule path we can get away with just one function which only
793 * checks whether there is a submodule in the working directory at that
794 * location.
796 static const char *default_name_or_path(const char *path_or_name)
798 int error_code;
800 if (!is_submodule_populated_gently(path_or_name, &error_code))
801 return NULL;
803 return path_or_name;
807 * Holds relevant information for a changed submodule. Used as the .util
808 * member of the changed submodule name string_list_item.
810 * (super_oid, path) allows the submodule config to be read from _some_
811 * .gitmodules file. We store this information the first time we find a
812 * superproject commit that points to the submodule, but this is
813 * arbitrary - we can choose any (super_oid, path) that matches the
814 * submodule's name.
816 * NEEDSWORK: Storing an arbitrary commit is undesirable because we can't
817 * guarantee that we're reading the commit that the user would expect. A better
818 * scheme would be to just fetch a submodule by its name. This requires two
819 * steps:
820 * - Create a function that behaves like repo_submodule_init(), but accepts a
821 * submodule name instead of treeish_name and path. This should be easy
822 * because repo_submodule_init() internally uses the submodule's name.
824 * - Replace most instances of 'struct submodule' (which is the .gitmodules
825 * config) with just the submodule name. This is OK because we expect
826 * submodule settings to be stored in .git/config (via "git submodule init"),
827 * not .gitmodules. This also lets us delete get_non_gitmodules_submodule(),
828 * which constructs a bogus 'struct submodule' for the sake of giving a
829 * placeholder name to a gitlink.
831 struct changed_submodule_data {
833 * The first superproject commit in the rev walk that points to
834 * the submodule.
836 const struct object_id *super_oid;
838 * Path to the submodule in the superproject commit referenced
839 * by 'super_oid'.
841 char *path;
842 /* The submodule commits that have changed in the rev walk. */
843 struct oid_array new_commits;
846 static void changed_submodule_data_clear(struct changed_submodule_data *cs_data)
848 oid_array_clear(&cs_data->new_commits);
849 free(cs_data->path);
852 static void collect_changed_submodules_cb(struct diff_queue_struct *q,
853 struct diff_options *options UNUSED,
854 void *data)
856 struct collect_changed_submodules_cb_data *me = data;
857 struct string_list *changed = me->changed;
858 const struct object_id *commit_oid = me->commit_oid;
859 int i;
861 for (i = 0; i < q->nr; i++) {
862 struct diff_filepair *p = q->queue[i];
863 const struct submodule *submodule;
864 const char *name;
865 struct string_list_item *item;
866 struct changed_submodule_data *cs_data;
868 if (!S_ISGITLINK(p->two->mode))
869 continue;
871 submodule = submodule_from_path(me->repo,
872 commit_oid, p->two->path);
873 if (submodule)
874 name = submodule->name;
875 else {
876 name = default_name_or_path(p->two->path);
877 /* make sure name does not collide with existing one */
878 if (name)
879 submodule = submodule_from_name(me->repo,
880 commit_oid, name);
881 if (submodule) {
882 warning(_("Submodule in commit %s at path: "
883 "'%s' collides with a submodule named "
884 "the same. Skipping it."),
885 oid_to_hex(commit_oid), p->two->path);
886 name = NULL;
890 if (!name)
891 continue;
893 item = string_list_insert(changed, name);
894 if (item->util)
895 cs_data = item->util;
896 else {
897 item->util = xcalloc(1, sizeof(struct changed_submodule_data));
898 cs_data = item->util;
899 cs_data->super_oid = commit_oid;
900 cs_data->path = xstrdup(p->two->path);
902 oid_array_append(&cs_data->new_commits, &p->two->oid);
907 * Collect the paths of submodules in 'changed' which have changed based on
908 * the revisions as specified in 'argv'. Each entry in 'changed' will also
909 * have a corresponding 'struct oid_array' (in the 'util' field) which lists
910 * what the submodule pointers were updated to during the change.
912 static void collect_changed_submodules(struct repository *r,
913 struct string_list *changed,
914 struct strvec *argv)
916 struct rev_info rev;
917 const struct commit *commit;
918 int save_warning;
919 struct setup_revision_opt s_r_opt = {
920 .assume_dashdash = 1,
923 save_warning = warn_on_object_refname_ambiguity;
924 warn_on_object_refname_ambiguity = 0;
925 repo_init_revisions(r, &rev, NULL);
926 setup_revisions(argv->nr, argv->v, &rev, &s_r_opt);
927 warn_on_object_refname_ambiguity = save_warning;
928 if (prepare_revision_walk(&rev))
929 die(_("revision walk setup failed"));
931 while ((commit = get_revision(&rev))) {
932 struct rev_info diff_rev;
933 struct collect_changed_submodules_cb_data data;
934 data.repo = r;
935 data.changed = changed;
936 data.commit_oid = &commit->object.oid;
938 repo_init_revisions(r, &diff_rev, NULL);
939 diff_rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
940 diff_rev.diffopt.format_callback = collect_changed_submodules_cb;
941 diff_rev.diffopt.format_callback_data = &data;
942 diff_rev.dense_combined_merges = 1;
943 diff_tree_combined_merge(commit, &diff_rev);
944 release_revisions(&diff_rev);
947 reset_revision_walk();
948 release_revisions(&rev);
951 static void free_submodules_data(struct string_list *submodules)
953 struct string_list_item *item;
954 for_each_string_list_item(item, submodules)
955 changed_submodule_data_clear(item->util);
957 string_list_clear(submodules, 1);
960 static int has_remote(const char *refname UNUSED,
961 const char *referent UNUSED,
962 const struct object_id *oid UNUSED,
963 int flags UNUSED, void *cb_data UNUSED)
965 return 1;
968 static int append_oid_to_argv(const struct object_id *oid, void *data)
970 struct strvec *argv = data;
971 strvec_push(argv, oid_to_hex(oid));
972 return 0;
975 struct has_commit_data {
976 struct repository *repo;
977 int result;
978 const char *path;
979 const struct object_id *super_oid;
982 static int check_has_commit(const struct object_id *oid, void *data)
984 struct has_commit_data *cb = data;
985 struct repository subrepo;
986 enum object_type type;
988 if (repo_submodule_init(&subrepo, cb->repo, cb->path, cb->super_oid)) {
989 cb->result = 0;
990 /* subrepo failed to init, so don't clean it up. */
991 return 0;
994 type = oid_object_info(&subrepo, oid, NULL);
996 switch (type) {
997 case OBJ_COMMIT:
998 goto cleanup;
999 case OBJ_BAD:
1001 * Object is missing or invalid. If invalid, an error message
1002 * has already been printed.
1004 cb->result = 0;
1005 goto cleanup;
1006 default:
1007 die(_("submodule entry '%s' (%s) is a %s, not a commit"),
1008 cb->path, oid_to_hex(oid), type_name(type));
1010 cleanup:
1011 repo_clear(&subrepo);
1012 return 0;
1015 static int submodule_has_commits(struct repository *r,
1016 const char *path,
1017 const struct object_id *super_oid,
1018 struct oid_array *commits)
1020 struct has_commit_data has_commit = {
1021 .repo = r,
1022 .result = 1,
1023 .path = path,
1024 .super_oid = super_oid
1027 if (validate_submodule_path(path) < 0)
1028 exit(128);
1030 oid_array_for_each_unique(commits, check_has_commit, &has_commit);
1032 if (has_commit.result) {
1034 * Even if the submodule is checked out and the commit is
1035 * present, make sure it exists in the submodule's object store
1036 * and that it is reachable from a ref.
1038 struct child_process cp = CHILD_PROCESS_INIT;
1039 struct strbuf out = STRBUF_INIT;
1041 strvec_pushl(&cp.args, "rev-list", "-n", "1", NULL);
1042 oid_array_for_each_unique(commits, append_oid_to_argv, &cp.args);
1043 strvec_pushl(&cp.args, "--not", "--all", NULL);
1045 prepare_submodule_repo_env(&cp.env);
1046 cp.git_cmd = 1;
1047 cp.no_stdin = 1;
1048 cp.dir = path;
1050 if (capture_command(&cp, &out, GIT_MAX_HEXSZ + 1) || out.len)
1051 has_commit.result = 0;
1053 strbuf_release(&out);
1056 return has_commit.result;
1059 static int submodule_needs_pushing(struct repository *r,
1060 const char *path,
1061 struct oid_array *commits)
1063 if (!submodule_has_commits(r, path, null_oid(), commits))
1065 * NOTE: We do consider it safe to return "no" here. The
1066 * correct answer would be "We do not know" instead of
1067 * "No push needed", but it is quite hard to change
1068 * the submodule pointer without having the submodule
1069 * around. If a user did however change the submodules
1070 * without having the submodule around, this indicates
1071 * an expert who knows what they are doing or a
1072 * maintainer integrating work from other people. In
1073 * both cases it should be safe to skip this check.
1075 return 0;
1077 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
1078 struct child_process cp = CHILD_PROCESS_INIT;
1079 struct strbuf buf = STRBUF_INIT;
1080 int needs_pushing = 0;
1082 strvec_push(&cp.args, "rev-list");
1083 oid_array_for_each_unique(commits, append_oid_to_argv, &cp.args);
1084 strvec_pushl(&cp.args, "--not", "--remotes", "-n", "1" , NULL);
1086 prepare_submodule_repo_env(&cp.env);
1087 cp.git_cmd = 1;
1088 cp.no_stdin = 1;
1089 cp.out = -1;
1090 cp.dir = path;
1091 if (start_command(&cp))
1092 die(_("Could not run 'git rev-list <commits> --not --remotes -n 1' command in submodule %s"),
1093 path);
1094 if (strbuf_read(&buf, cp.out, the_hash_algo->hexsz + 1))
1095 needs_pushing = 1;
1096 finish_command(&cp);
1097 close(cp.out);
1098 strbuf_release(&buf);
1099 return needs_pushing;
1102 return 0;
1105 int find_unpushed_submodules(struct repository *r,
1106 struct oid_array *commits,
1107 const char *remotes_name,
1108 struct string_list *needs_pushing)
1110 struct string_list submodules = STRING_LIST_INIT_DUP;
1111 struct string_list_item *name;
1112 struct strvec argv = STRVEC_INIT;
1114 /* argv.v[0] will be ignored by setup_revisions */
1115 strvec_push(&argv, "find_unpushed_submodules");
1116 oid_array_for_each_unique(commits, append_oid_to_argv, &argv);
1117 strvec_push(&argv, "--not");
1118 strvec_pushf(&argv, "--remotes=%s", remotes_name);
1120 collect_changed_submodules(r, &submodules, &argv);
1122 for_each_string_list_item(name, &submodules) {
1123 struct changed_submodule_data *cs_data = name->util;
1124 const struct submodule *submodule;
1125 const char *path = NULL;
1127 submodule = submodule_from_name(r, null_oid(), name->string);
1128 if (submodule)
1129 path = submodule->path;
1130 else
1131 path = default_name_or_path(name->string);
1133 if (!path)
1134 continue;
1136 if (submodule_needs_pushing(r, path, &cs_data->new_commits))
1137 string_list_insert(needs_pushing, path);
1140 free_submodules_data(&submodules);
1141 strvec_clear(&argv);
1143 return needs_pushing->nr;
1146 static int push_submodule(const char *path,
1147 const struct remote *remote,
1148 const struct refspec *rs,
1149 const struct string_list *push_options,
1150 int dry_run)
1152 if (validate_submodule_path(path) < 0)
1153 exit(128);
1155 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
1156 struct child_process cp = CHILD_PROCESS_INIT;
1157 strvec_push(&cp.args, "push");
1159 * When recursing into a submodule, treat any "only" configurations as "on-
1160 * demand", since "only" would not work (we need all submodules to be pushed
1161 * in order to be able to push the superproject).
1163 strvec_push(&cp.args, "--recurse-submodules=only-is-on-demand");
1164 if (dry_run)
1165 strvec_push(&cp.args, "--dry-run");
1167 if (push_options && push_options->nr) {
1168 const struct string_list_item *item;
1169 for_each_string_list_item(item, push_options)
1170 strvec_pushf(&cp.args, "--push-option=%s",
1171 item->string);
1174 if (remote->origin != REMOTE_UNCONFIGURED) {
1175 int i;
1176 strvec_push(&cp.args, remote->name);
1177 for (i = 0; i < rs->raw_nr; i++)
1178 strvec_push(&cp.args, rs->raw[i]);
1181 prepare_submodule_repo_env(&cp.env);
1182 cp.git_cmd = 1;
1183 cp.no_stdin = 1;
1184 cp.dir = path;
1185 if (run_command(&cp))
1186 return 0;
1187 close(cp.out);
1190 return 1;
1194 * Perform a check in the submodule to see if the remote and refspec work.
1195 * Die if the submodule can't be pushed.
1197 static void submodule_push_check(const char *path, const char *head,
1198 const struct remote *remote,
1199 const struct refspec *rs)
1201 struct child_process cp = CHILD_PROCESS_INIT;
1202 int i;
1204 if (validate_submodule_path(path) < 0)
1205 exit(128);
1207 strvec_push(&cp.args, "submodule--helper");
1208 strvec_push(&cp.args, "push-check");
1209 strvec_push(&cp.args, head);
1210 strvec_push(&cp.args, remote->name);
1212 for (i = 0; i < rs->raw_nr; i++)
1213 strvec_push(&cp.args, rs->raw[i]);
1215 prepare_submodule_repo_env(&cp.env);
1216 cp.git_cmd = 1;
1217 cp.no_stdin = 1;
1218 cp.no_stdout = 1;
1219 cp.dir = path;
1222 * Simply indicate if 'submodule--helper push-check' failed.
1223 * More detailed error information will be provided by the
1224 * child process.
1226 if (run_command(&cp))
1227 die(_("process for submodule '%s' failed"), path);
1230 int push_unpushed_submodules(struct repository *r,
1231 struct oid_array *commits,
1232 const struct remote *remote,
1233 const struct refspec *rs,
1234 const struct string_list *push_options,
1235 int dry_run)
1237 int i, ret = 1;
1238 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1240 if (!find_unpushed_submodules(r, commits,
1241 remote->name, &needs_pushing))
1242 return 1;
1245 * Verify that the remote and refspec can be propagated to all
1246 * submodules. This check can be skipped if the remote and refspec
1247 * won't be propagated due to the remote being unconfigured (e.g. a URL
1248 * instead of a remote name).
1250 if (remote->origin != REMOTE_UNCONFIGURED) {
1251 char *head;
1252 struct object_id head_oid;
1254 head = refs_resolve_refdup(get_main_ref_store(the_repository),
1255 "HEAD", 0, &head_oid, NULL);
1256 if (!head)
1257 die(_("Failed to resolve HEAD as a valid ref."));
1259 for (i = 0; i < needs_pushing.nr; i++)
1260 submodule_push_check(needs_pushing.items[i].string,
1261 head, remote, rs);
1262 free(head);
1265 /* Actually push the submodules */
1266 for (i = 0; i < needs_pushing.nr; i++) {
1267 const char *path = needs_pushing.items[i].string;
1268 fprintf(stderr, _("Pushing submodule '%s'\n"), path);
1269 if (!push_submodule(path, remote, rs,
1270 push_options, dry_run)) {
1271 fprintf(stderr, _("Unable to push submodule '%s'\n"), path);
1272 ret = 0;
1276 string_list_clear(&needs_pushing, 0);
1278 return ret;
1281 static int append_oid_to_array(const char *ref UNUSED,
1282 const char *referent UNUSED,
1283 const struct object_id *oid,
1284 int flags UNUSED, void *data)
1286 struct oid_array *array = data;
1287 oid_array_append(array, oid);
1288 return 0;
1291 void check_for_new_submodule_commits(struct object_id *oid)
1293 if (!initialized_fetch_ref_tips) {
1294 refs_for_each_ref(get_main_ref_store(the_repository),
1295 append_oid_to_array, &ref_tips_before_fetch);
1296 initialized_fetch_ref_tips = 1;
1299 oid_array_append(&ref_tips_after_fetch, oid);
1303 * Returns 1 if there is at least one submodule gitdir in
1304 * $GIT_DIR/modules and 0 otherwise. This follows
1305 * submodule_name_to_gitdir(), which looks for submodules in
1306 * $GIT_DIR/modules, not $GIT_COMMON_DIR.
1308 * A submodule can be moved to $GIT_DIR/modules manually by running "git
1309 * submodule absorbgitdirs", or it may be initialized there by "git
1310 * submodule update".
1312 static int repo_has_absorbed_submodules(struct repository *r)
1314 int ret;
1315 struct strbuf buf = STRBUF_INIT;
1317 strbuf_repo_git_path(&buf, r, "modules/");
1318 ret = file_exists(buf.buf) && !is_empty_dir(buf.buf);
1319 strbuf_release(&buf);
1320 return ret;
1323 static void calculate_changed_submodule_paths(struct repository *r,
1324 struct string_list *changed_submodule_names)
1326 struct strvec argv = STRVEC_INIT;
1327 struct string_list_item *name;
1329 /* No need to check if no submodules would be fetched */
1330 if (!submodule_from_path(r, NULL, NULL) &&
1331 !repo_has_absorbed_submodules(r))
1332 return;
1334 strvec_push(&argv, "--"); /* argv[0] program name */
1335 oid_array_for_each_unique(&ref_tips_after_fetch,
1336 append_oid_to_argv, &argv);
1337 strvec_push(&argv, "--not");
1338 oid_array_for_each_unique(&ref_tips_before_fetch,
1339 append_oid_to_argv, &argv);
1342 * Collect all submodules (whether checked out or not) for which new
1343 * commits have been recorded upstream in "changed_submodule_names".
1345 collect_changed_submodules(r, changed_submodule_names, &argv);
1347 for_each_string_list_item(name, changed_submodule_names) {
1348 struct changed_submodule_data *cs_data = name->util;
1349 const struct submodule *submodule;
1350 const char *path = NULL;
1352 submodule = submodule_from_name(r, null_oid(), name->string);
1353 if (submodule)
1354 path = submodule->path;
1355 else
1356 path = default_name_or_path(name->string);
1358 if (!path)
1359 continue;
1361 if (submodule_has_commits(r, path, null_oid(), &cs_data->new_commits)) {
1362 changed_submodule_data_clear(cs_data);
1363 *name->string = '\0';
1367 string_list_remove_empty_items(changed_submodule_names, 1);
1369 strvec_clear(&argv);
1370 oid_array_clear(&ref_tips_before_fetch);
1371 oid_array_clear(&ref_tips_after_fetch);
1372 initialized_fetch_ref_tips = 0;
1375 int submodule_touches_in_range(struct repository *r,
1376 struct object_id *excl_oid,
1377 struct object_id *incl_oid)
1379 struct string_list subs = STRING_LIST_INIT_DUP;
1380 struct strvec args = STRVEC_INIT;
1381 int ret;
1383 /* No need to check if there are no submodules configured */
1384 if (!submodule_from_path(r, NULL, NULL))
1385 return 0;
1387 strvec_push(&args, "--"); /* args[0] program name */
1388 strvec_push(&args, oid_to_hex(incl_oid));
1389 if (!is_null_oid(excl_oid)) {
1390 strvec_push(&args, "--not");
1391 strvec_push(&args, oid_to_hex(excl_oid));
1394 collect_changed_submodules(r, &subs, &args);
1395 ret = subs.nr;
1397 strvec_clear(&args);
1399 free_submodules_data(&subs);
1400 return ret;
1403 struct submodule_parallel_fetch {
1405 * The index of the last index entry processed by
1406 * get_fetch_task_from_index().
1408 int index_count;
1410 * The index of the last string_list entry processed by
1411 * get_fetch_task_from_changed().
1413 int changed_count;
1414 struct strvec args;
1415 struct repository *r;
1416 const char *prefix;
1417 int command_line_option;
1418 int default_option;
1419 int quiet;
1420 int result;
1423 * Names of submodules that have new commits. Generated by
1424 * walking the newly fetched superproject commits.
1426 struct string_list changed_submodule_names;
1428 * Names of submodules that have already been processed. Lets us
1429 * avoid fetching the same submodule more than once.
1431 struct string_list seen_submodule_names;
1433 /* Pending fetches by OIDs */
1434 struct fetch_task **oid_fetch_tasks;
1435 int oid_fetch_tasks_nr, oid_fetch_tasks_alloc;
1437 struct strbuf submodules_with_errors;
1439 #define SPF_INIT { \
1440 .args = STRVEC_INIT, \
1441 .changed_submodule_names = STRING_LIST_INIT_DUP, \
1442 .seen_submodule_names = STRING_LIST_INIT_DUP, \
1443 .submodules_with_errors = STRBUF_INIT, \
1446 static int get_fetch_recurse_config(const struct submodule *submodule,
1447 struct submodule_parallel_fetch *spf)
1449 if (spf->command_line_option != RECURSE_SUBMODULES_DEFAULT)
1450 return spf->command_line_option;
1452 if (submodule) {
1453 char *key;
1454 const char *value;
1456 int fetch_recurse = submodule->fetch_recurse;
1457 key = xstrfmt("submodule.%s.fetchRecurseSubmodules", submodule->name);
1458 if (!repo_config_get_string_tmp(spf->r, key, &value)) {
1459 fetch_recurse = parse_fetch_recurse_submodules_arg(key, value);
1461 free(key);
1463 if (fetch_recurse != RECURSE_SUBMODULES_NONE)
1464 /* local config overrules everything except commandline */
1465 return fetch_recurse;
1468 return spf->default_option;
1472 * Fetch in progress (if callback data) or
1473 * pending (if in oid_fetch_tasks in struct submodule_parallel_fetch)
1475 struct fetch_task {
1476 struct repository *repo;
1477 const struct submodule *sub;
1478 unsigned free_sub : 1; /* Do we need to free the submodule? */
1479 const char *default_argv; /* The default fetch mode. */
1480 struct strvec git_args; /* Args for the child git process. */
1482 struct oid_array *commits; /* Ensure these commits are fetched */
1486 * When a submodule is not defined in .gitmodules, we cannot access it
1487 * via the regular submodule-config. Create a fake submodule, which we can
1488 * work on.
1490 static const struct submodule *get_non_gitmodules_submodule(const char *path)
1492 struct submodule *ret = NULL;
1493 const char *name = default_name_or_path(path);
1495 if (!name)
1496 return NULL;
1498 ret = xmalloc(sizeof(*ret));
1499 memset(ret, 0, sizeof(*ret));
1500 ret->path = name;
1501 ret->name = name;
1503 return (const struct submodule *) ret;
1506 static void fetch_task_free(struct fetch_task *p)
1508 if (p->free_sub)
1509 free((void*)p->sub);
1510 p->free_sub = 0;
1511 p->sub = NULL;
1513 if (p->repo)
1514 repo_clear(p->repo);
1515 FREE_AND_NULL(p->repo);
1517 strvec_clear(&p->git_args);
1518 free(p);
1521 static struct repository *get_submodule_repo_for(struct repository *r,
1522 const char *path,
1523 const struct object_id *treeish_name)
1525 struct repository *ret = xmalloc(sizeof(*ret));
1527 if (repo_submodule_init(ret, r, path, treeish_name)) {
1528 free(ret);
1529 return NULL;
1532 return ret;
1535 static struct fetch_task *fetch_task_create(struct submodule_parallel_fetch *spf,
1536 const char *path,
1537 const struct object_id *treeish_name)
1539 struct fetch_task *task = xmalloc(sizeof(*task));
1540 memset(task, 0, sizeof(*task));
1542 if (validate_submodule_path(path) < 0)
1543 exit(128);
1545 task->sub = submodule_from_path(spf->r, treeish_name, path);
1547 if (!task->sub) {
1549 * No entry in .gitmodules? Technically not a submodule,
1550 * but historically we supported repositories that happen to be
1551 * in-place where a gitlink is. Keep supporting them.
1553 task->sub = get_non_gitmodules_submodule(path);
1554 if (!task->sub)
1555 goto cleanup;
1557 task->free_sub = 1;
1560 if (string_list_lookup(&spf->seen_submodule_names, task->sub->name))
1561 goto cleanup;
1563 switch (get_fetch_recurse_config(task->sub, spf))
1565 default:
1566 case RECURSE_SUBMODULES_DEFAULT:
1567 case RECURSE_SUBMODULES_ON_DEMAND:
1568 if (!task->sub ||
1569 !string_list_lookup(
1570 &spf->changed_submodule_names,
1571 task->sub->name))
1572 goto cleanup;
1573 task->default_argv = "on-demand";
1574 break;
1575 case RECURSE_SUBMODULES_ON:
1576 task->default_argv = "yes";
1577 break;
1578 case RECURSE_SUBMODULES_OFF:
1579 goto cleanup;
1582 task->repo = get_submodule_repo_for(spf->r, path, treeish_name);
1584 return task;
1586 cleanup:
1587 fetch_task_free(task);
1588 return NULL;
1591 static struct fetch_task *
1592 get_fetch_task_from_index(struct submodule_parallel_fetch *spf,
1593 struct strbuf *err)
1595 for (; spf->index_count < spf->r->index->cache_nr; spf->index_count++) {
1596 const struct cache_entry *ce =
1597 spf->r->index->cache[spf->index_count];
1598 struct fetch_task *task;
1600 if (!S_ISGITLINK(ce->ce_mode))
1601 continue;
1603 task = fetch_task_create(spf, ce->name, null_oid());
1604 if (!task)
1605 continue;
1607 if (task->repo) {
1608 if (!spf->quiet)
1609 strbuf_addf(err, _("Fetching submodule %s%s\n"),
1610 spf->prefix, ce->name);
1612 spf->index_count++;
1613 return task;
1614 } else {
1615 struct strbuf empty_submodule_path = STRBUF_INIT;
1617 fetch_task_free(task);
1620 * An empty directory is normal,
1621 * the submodule is not initialized
1623 strbuf_addf(&empty_submodule_path, "%s/%s/",
1624 spf->r->worktree,
1625 ce->name);
1626 if (S_ISGITLINK(ce->ce_mode) &&
1627 !is_empty_dir(empty_submodule_path.buf)) {
1628 spf->result = 1;
1629 strbuf_addf(err,
1630 _("Could not access submodule '%s'\n"),
1631 ce->name);
1633 strbuf_release(&empty_submodule_path);
1636 return NULL;
1639 static struct fetch_task *
1640 get_fetch_task_from_changed(struct submodule_parallel_fetch *spf,
1641 struct strbuf *err)
1643 for (; spf->changed_count < spf->changed_submodule_names.nr;
1644 spf->changed_count++) {
1645 struct string_list_item item =
1646 spf->changed_submodule_names.items[spf->changed_count];
1647 struct changed_submodule_data *cs_data = item.util;
1648 struct fetch_task *task;
1650 if (!is_tree_submodule_active(spf->r, cs_data->super_oid,cs_data->path))
1651 continue;
1653 task = fetch_task_create(spf, cs_data->path,
1654 cs_data->super_oid);
1655 if (!task)
1656 continue;
1658 if (!task->repo) {
1659 strbuf_addf(err, _("Could not access submodule '%s' at commit %s\n"),
1660 cs_data->path,
1661 repo_find_unique_abbrev(the_repository, cs_data->super_oid, DEFAULT_ABBREV));
1663 fetch_task_free(task);
1664 continue;
1667 if (!spf->quiet)
1668 strbuf_addf(err,
1669 _("Fetching submodule %s%s at commit %s\n"),
1670 spf->prefix, task->sub->path,
1671 repo_find_unique_abbrev(the_repository, cs_data->super_oid,
1672 DEFAULT_ABBREV));
1674 spf->changed_count++;
1676 * NEEDSWORK: Submodules set/unset a value for
1677 * core.worktree when they are populated/unpopulated by
1678 * "git checkout" (and similar commands, see
1679 * submodule_move_head() and
1680 * connect_work_tree_and_git_dir()), but if the
1681 * submodule is unpopulated in another way (e.g. "git
1682 * rm", "rm -r"), core.worktree will still be set even
1683 * though the directory doesn't exist, and the child
1684 * process will crash while trying to chdir into the
1685 * nonexistent directory.
1687 * In this case, we know that the submodule has no
1688 * working tree, so we can work around this by
1689 * setting "--work-tree=." (--bare does not work because
1690 * worktree settings take precedence over bare-ness).
1691 * However, this is not necessarily true in other cases,
1692 * so a generalized solution is still necessary.
1694 * Possible solutions:
1695 * - teach "git [add|rm]" to unset core.worktree and
1696 * discourage users from removing submodules without
1697 * using a Git command.
1698 * - teach submodule child processes to ignore stale
1699 * core.worktree values.
1701 strvec_push(&task->git_args, "--work-tree=.");
1702 return task;
1704 return NULL;
1707 static int get_next_submodule(struct child_process *cp, struct strbuf *err,
1708 void *data, void **task_cb)
1710 struct submodule_parallel_fetch *spf = data;
1711 struct fetch_task *task =
1712 get_fetch_task_from_index(spf, err);
1713 if (!task)
1714 task = get_fetch_task_from_changed(spf, err);
1716 if (task) {
1717 child_process_init(cp);
1718 cp->dir = task->repo->gitdir;
1719 prepare_submodule_repo_env_in_gitdir(&cp->env);
1720 cp->git_cmd = 1;
1721 strvec_init(&cp->args);
1722 if (task->git_args.nr)
1723 strvec_pushv(&cp->args, task->git_args.v);
1724 strvec_pushv(&cp->args, spf->args.v);
1725 strvec_push(&cp->args, task->default_argv);
1726 strvec_pushf(&cp->args, "--submodule-prefix=%s%s/",
1727 spf->prefix, task->sub->path);
1729 *task_cb = task;
1731 string_list_insert(&spf->seen_submodule_names, task->sub->name);
1732 return 1;
1735 if (spf->oid_fetch_tasks_nr) {
1736 struct fetch_task *task =
1737 spf->oid_fetch_tasks[spf->oid_fetch_tasks_nr - 1];
1738 spf->oid_fetch_tasks_nr--;
1740 child_process_init(cp);
1741 prepare_submodule_repo_env_in_gitdir(&cp->env);
1742 cp->git_cmd = 1;
1743 cp->dir = task->repo->gitdir;
1745 strvec_init(&cp->args);
1746 strvec_pushv(&cp->args, spf->args.v);
1747 strvec_push(&cp->args, "on-demand");
1748 strvec_pushf(&cp->args, "--submodule-prefix=%s%s/",
1749 spf->prefix, task->sub->path);
1751 /* NEEDSWORK: have get_default_remote from submodule--helper */
1752 strvec_push(&cp->args, "origin");
1753 oid_array_for_each_unique(task->commits,
1754 append_oid_to_argv, &cp->args);
1756 *task_cb = task;
1757 return 1;
1760 return 0;
1763 static int fetch_start_failure(struct strbuf *err UNUSED,
1764 void *cb, void *task_cb)
1766 struct submodule_parallel_fetch *spf = cb;
1767 struct fetch_task *task = task_cb;
1769 spf->result = 1;
1771 fetch_task_free(task);
1772 return 0;
1775 static int commit_missing_in_sub(const struct object_id *oid, void *data)
1777 struct repository *subrepo = data;
1779 enum object_type type = oid_object_info(subrepo, oid, NULL);
1781 return type != OBJ_COMMIT;
1784 static int fetch_finish(int retvalue, struct strbuf *err UNUSED,
1785 void *cb, void *task_cb)
1787 struct submodule_parallel_fetch *spf = cb;
1788 struct fetch_task *task = task_cb;
1790 struct string_list_item *it;
1791 struct changed_submodule_data *cs_data;
1793 if (!task || !task->sub)
1794 BUG("callback cookie bogus");
1796 if (retvalue) {
1798 * NEEDSWORK: This indicates that the overall fetch
1799 * failed, even though there may be a subsequent fetch
1800 * by commit hash that might work. It may be a good
1801 * idea to not indicate failure in this case, and only
1802 * indicate failure if the subsequent fetch fails.
1804 spf->result = 1;
1806 strbuf_addf(&spf->submodules_with_errors, "\t%s\n",
1807 task->sub->name);
1810 /* Is this the second time we process this submodule? */
1811 if (task->commits)
1812 goto out;
1814 it = string_list_lookup(&spf->changed_submodule_names, task->sub->name);
1815 if (!it)
1816 /* Could be an unchanged submodule, not contained in the list */
1817 goto out;
1819 cs_data = it->util;
1820 oid_array_filter(&cs_data->new_commits,
1821 commit_missing_in_sub,
1822 task->repo);
1824 /* Are there commits we want, but do not exist? */
1825 if (cs_data->new_commits.nr) {
1826 task->commits = &cs_data->new_commits;
1827 ALLOC_GROW(spf->oid_fetch_tasks,
1828 spf->oid_fetch_tasks_nr + 1,
1829 spf->oid_fetch_tasks_alloc);
1830 spf->oid_fetch_tasks[spf->oid_fetch_tasks_nr] = task;
1831 spf->oid_fetch_tasks_nr++;
1832 return 0;
1835 out:
1836 fetch_task_free(task);
1837 return 0;
1840 int fetch_submodules(struct repository *r,
1841 const struct strvec *options,
1842 const char *prefix, int command_line_option,
1843 int default_option,
1844 int quiet, int max_parallel_jobs)
1846 int i;
1847 struct submodule_parallel_fetch spf = SPF_INIT;
1848 const struct run_process_parallel_opts opts = {
1849 .tr2_category = "submodule",
1850 .tr2_label = "parallel/fetch",
1852 .processes = max_parallel_jobs,
1854 .get_next_task = get_next_submodule,
1855 .start_failure = fetch_start_failure,
1856 .task_finished = fetch_finish,
1857 .data = &spf,
1860 spf.r = r;
1861 spf.command_line_option = command_line_option;
1862 spf.default_option = default_option;
1863 spf.quiet = quiet;
1864 spf.prefix = prefix;
1866 if (!r->worktree)
1867 goto out;
1869 if (repo_read_index(r) < 0)
1870 die(_("index file corrupt"));
1872 strvec_push(&spf.args, "fetch");
1873 for (i = 0; i < options->nr; i++)
1874 strvec_push(&spf.args, options->v[i]);
1875 strvec_push(&spf.args, "--recurse-submodules-default");
1876 /* default value, "--submodule-prefix" and its value are added later */
1878 calculate_changed_submodule_paths(r, &spf.changed_submodule_names);
1879 string_list_sort(&spf.changed_submodule_names);
1880 run_processes_parallel(&opts);
1882 if (spf.submodules_with_errors.len > 0)
1883 fprintf(stderr, _("Errors during submodule fetch:\n%s"),
1884 spf.submodules_with_errors.buf);
1887 strvec_clear(&spf.args);
1888 out:
1889 free_submodules_data(&spf.changed_submodule_names);
1890 string_list_clear(&spf.seen_submodule_names, 0);
1891 strbuf_release(&spf.submodules_with_errors);
1892 free(spf.oid_fetch_tasks);
1893 return spf.result;
1896 unsigned is_submodule_modified(const char *path, int ignore_untracked)
1898 struct child_process cp = CHILD_PROCESS_INIT;
1899 struct strbuf buf = STRBUF_INIT;
1900 FILE *fp;
1901 unsigned dirty_submodule = 0;
1902 const char *git_dir;
1903 int ignore_cp_exit_code = 0;
1905 if (validate_submodule_path(path) < 0)
1906 exit(128);
1908 strbuf_addf(&buf, "%s/.git", path);
1909 git_dir = read_gitfile(buf.buf);
1910 if (!git_dir)
1911 git_dir = buf.buf;
1912 if (!is_git_directory(git_dir)) {
1913 if (is_directory(git_dir))
1914 die(_("'%s' not recognized as a git repository"), git_dir);
1915 strbuf_release(&buf);
1916 /* The submodule is not checked out, so it is not modified */
1917 return 0;
1919 strbuf_reset(&buf);
1921 strvec_pushl(&cp.args, "status", "--porcelain=2", NULL);
1922 if (ignore_untracked)
1923 strvec_push(&cp.args, "-uno");
1925 prepare_submodule_repo_env(&cp.env);
1926 cp.git_cmd = 1;
1927 cp.no_stdin = 1;
1928 cp.out = -1;
1929 cp.dir = path;
1930 if (start_command(&cp))
1931 die(_("Could not run 'git status --porcelain=2' in submodule %s"), path);
1933 fp = xfdopen(cp.out, "r");
1934 while (strbuf_getwholeline(&buf, fp, '\n') != EOF) {
1935 /* regular untracked files */
1936 if (buf.buf[0] == '?')
1937 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
1939 if (buf.buf[0] == 'u' ||
1940 buf.buf[0] == '1' ||
1941 buf.buf[0] == '2') {
1942 /* T = line type, XY = status, SSSS = submodule state */
1943 if (buf.len < strlen("T XY SSSS"))
1944 BUG("invalid status --porcelain=2 line %s",
1945 buf.buf);
1947 if (buf.buf[5] == 'S' && buf.buf[8] == 'U')
1948 /* nested untracked file */
1949 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
1951 if (buf.buf[0] == 'u' ||
1952 buf.buf[0] == '2' ||
1953 memcmp(buf.buf + 5, "S..U", 4))
1954 /* other change */
1955 dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
1958 if ((dirty_submodule & DIRTY_SUBMODULE_MODIFIED) &&
1959 ((dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ||
1960 ignore_untracked)) {
1962 * We're not interested in any further information from
1963 * the child any more, neither output nor its exit code.
1965 ignore_cp_exit_code = 1;
1966 break;
1969 fclose(fp);
1971 if (finish_command(&cp) && !ignore_cp_exit_code)
1972 die(_("'git status --porcelain=2' failed in submodule %s"), path);
1974 strbuf_release(&buf);
1975 return dirty_submodule;
1978 int submodule_uses_gitfile(const char *path)
1980 struct child_process cp = CHILD_PROCESS_INIT;
1981 struct strbuf buf = STRBUF_INIT;
1982 const char *git_dir;
1984 if (validate_submodule_path(path) < 0)
1985 exit(128);
1987 strbuf_addf(&buf, "%s/.git", path);
1988 git_dir = read_gitfile(buf.buf);
1989 if (!git_dir) {
1990 strbuf_release(&buf);
1991 return 0;
1993 strbuf_release(&buf);
1995 /* Now test that all nested submodules use a gitfile too */
1996 strvec_pushl(&cp.args,
1997 "submodule", "foreach", "--quiet", "--recursive",
1998 "test -f .git", NULL);
2000 prepare_submodule_repo_env(&cp.env);
2001 cp.git_cmd = 1;
2002 cp.no_stdin = 1;
2003 cp.no_stderr = 1;
2004 cp.no_stdout = 1;
2005 cp.dir = path;
2006 if (run_command(&cp))
2007 return 0;
2009 return 1;
2013 * Check if it is a bad idea to remove a submodule, i.e. if we'd lose data
2014 * when doing so.
2016 * Return 1 if we'd lose data, return 0 if the removal is fine,
2017 * and negative values for errors.
2019 int bad_to_remove_submodule(const char *path, unsigned flags)
2021 ssize_t len;
2022 struct child_process cp = CHILD_PROCESS_INIT;
2023 struct strbuf buf = STRBUF_INIT;
2024 int ret = 0;
2026 if (validate_submodule_path(path) < 0)
2027 exit(128);
2029 if (!file_exists(path) || is_empty_dir(path))
2030 return 0;
2032 if (!submodule_uses_gitfile(path))
2033 return 1;
2035 strvec_pushl(&cp.args, "status", "--porcelain",
2036 "--ignore-submodules=none", NULL);
2038 if (flags & SUBMODULE_REMOVAL_IGNORE_UNTRACKED)
2039 strvec_push(&cp.args, "-uno");
2040 else
2041 strvec_push(&cp.args, "-uall");
2043 if (!(flags & SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED))
2044 strvec_push(&cp.args, "--ignored");
2046 prepare_submodule_repo_env(&cp.env);
2047 cp.git_cmd = 1;
2048 cp.no_stdin = 1;
2049 cp.out = -1;
2050 cp.dir = path;
2051 if (start_command(&cp)) {
2052 if (flags & SUBMODULE_REMOVAL_DIE_ON_ERROR)
2053 die(_("could not start 'git status' in submodule '%s'"),
2054 path);
2055 ret = -1;
2056 goto out;
2059 len = strbuf_read(&buf, cp.out, 1024);
2060 if (len > 2)
2061 ret = 1;
2062 close(cp.out);
2064 if (finish_command(&cp)) {
2065 if (flags & SUBMODULE_REMOVAL_DIE_ON_ERROR)
2066 die(_("could not run 'git status' in submodule '%s'"),
2067 path);
2068 ret = -1;
2070 out:
2071 strbuf_release(&buf);
2072 return ret;
2075 void submodule_unset_core_worktree(const struct submodule *sub)
2077 struct strbuf config_path = STRBUF_INIT;
2079 if (validate_submodule_path(sub->path) < 0)
2080 exit(128);
2082 submodule_name_to_gitdir(&config_path, the_repository, sub->name);
2083 strbuf_addstr(&config_path, "/config");
2085 if (git_config_set_in_file_gently(config_path.buf, "core.worktree", NULL, NULL))
2086 warning(_("Could not unset core.worktree setting in submodule '%s'"),
2087 sub->path);
2089 strbuf_release(&config_path);
2092 static int submodule_has_dirty_index(const struct submodule *sub)
2094 struct child_process cp = CHILD_PROCESS_INIT;
2096 if (validate_submodule_path(sub->path) < 0)
2097 exit(128);
2099 prepare_submodule_repo_env(&cp.env);
2101 cp.git_cmd = 1;
2102 strvec_pushl(&cp.args, "diff-index", "--quiet",
2103 "--cached", "HEAD", NULL);
2104 cp.no_stdin = 1;
2105 cp.no_stdout = 1;
2106 cp.dir = sub->path;
2107 if (start_command(&cp))
2108 die(_("could not recurse into submodule '%s'"), sub->path);
2110 return finish_command(&cp);
2113 static void submodule_reset_index(const char *path, const char *super_prefix)
2115 struct child_process cp = CHILD_PROCESS_INIT;
2117 if (validate_submodule_path(path) < 0)
2118 exit(128);
2120 prepare_submodule_repo_env(&cp.env);
2122 cp.git_cmd = 1;
2123 cp.no_stdin = 1;
2124 cp.dir = path;
2126 /* TODO: determine if this might overwright untracked files */
2127 strvec_pushl(&cp.args, "read-tree", "-u", "--reset", NULL);
2128 strvec_pushf(&cp.args, "--super-prefix=%s%s/",
2129 (super_prefix ? super_prefix : ""), path);
2131 strvec_push(&cp.args, empty_tree_oid_hex(the_repository->hash_algo));
2133 if (run_command(&cp))
2134 die(_("could not reset submodule index"));
2138 * Moves a submodule at a given path from a given head to another new head.
2139 * For edge cases (a submodule coming into existence or removing a submodule)
2140 * pass NULL for old or new respectively.
2142 int submodule_move_head(const char *path, const char *super_prefix,
2143 const char *old_head, const char *new_head,
2144 unsigned flags)
2146 int ret = 0;
2147 struct child_process cp = CHILD_PROCESS_INIT;
2148 const struct submodule *sub;
2149 int *error_code_ptr, error_code;
2151 if (!is_submodule_active(the_repository, path))
2152 return 0;
2154 if (flags & SUBMODULE_MOVE_HEAD_FORCE)
2156 * Pass non NULL pointer to is_submodule_populated_gently
2157 * to prevent die()-ing. We'll use connect_work_tree_and_git_dir
2158 * to fixup the submodule in the force case later.
2160 error_code_ptr = &error_code;
2161 else
2162 error_code_ptr = NULL;
2164 if (old_head && !is_submodule_populated_gently(path, error_code_ptr))
2165 return 0;
2167 sub = submodule_from_path(the_repository, null_oid(), path);
2169 if (!sub)
2170 BUG("could not get submodule information for '%s'", path);
2172 if (old_head && !(flags & SUBMODULE_MOVE_HEAD_FORCE)) {
2173 /* Check if the submodule has a dirty index. */
2174 if (submodule_has_dirty_index(sub))
2175 return error(_("submodule '%s' has dirty index"), path);
2178 if (!(flags & SUBMODULE_MOVE_HEAD_DRY_RUN)) {
2179 if (old_head) {
2180 if (!submodule_uses_gitfile(path))
2181 absorb_git_dir_into_superproject(path,
2182 super_prefix);
2183 else {
2184 char *dotgit = xstrfmt("%s/.git", path);
2185 char *git_dir = xstrdup(read_gitfile(dotgit));
2187 free(dotgit);
2188 if (validate_submodule_git_dir(git_dir,
2189 sub->name) < 0)
2190 die(_("refusing to create/use '%s' in "
2191 "another submodule's git dir"),
2192 git_dir);
2193 free(git_dir);
2195 } else {
2196 struct strbuf gitdir = STRBUF_INIT;
2197 submodule_name_to_gitdir(&gitdir, the_repository,
2198 sub->name);
2199 if (validate_submodule_git_dir(gitdir.buf,
2200 sub->name) < 0)
2201 die(_("refusing to create/use '%s' in another "
2202 "submodule's git dir"),
2203 gitdir.buf);
2204 connect_work_tree_and_git_dir(path, gitdir.buf, 0);
2205 strbuf_release(&gitdir);
2207 /* make sure the index is clean as well */
2208 submodule_reset_index(path, super_prefix);
2211 if (old_head && (flags & SUBMODULE_MOVE_HEAD_FORCE)) {
2212 struct strbuf gitdir = STRBUF_INIT;
2213 submodule_name_to_gitdir(&gitdir, the_repository,
2214 sub->name);
2215 connect_work_tree_and_git_dir(path, gitdir.buf, 1);
2216 strbuf_release(&gitdir);
2220 prepare_submodule_repo_env(&cp.env);
2222 cp.git_cmd = 1;
2223 cp.no_stdin = 1;
2224 cp.dir = path;
2226 strvec_pushl(&cp.args, "read-tree", "--recurse-submodules", NULL);
2227 strvec_pushf(&cp.args, "--super-prefix=%s%s/",
2228 (super_prefix ? super_prefix : ""), path);
2230 if (flags & SUBMODULE_MOVE_HEAD_DRY_RUN)
2231 strvec_push(&cp.args, "-n");
2232 else
2233 strvec_push(&cp.args, "-u");
2235 if (flags & SUBMODULE_MOVE_HEAD_FORCE)
2236 strvec_push(&cp.args, "--reset");
2237 else
2238 strvec_push(&cp.args, "-m");
2240 if (!(flags & SUBMODULE_MOVE_HEAD_FORCE))
2241 strvec_push(&cp.args, old_head ? old_head : empty_tree_oid_hex(the_repository->hash_algo));
2243 strvec_push(&cp.args, new_head ? new_head : empty_tree_oid_hex(the_repository->hash_algo));
2245 if (run_command(&cp)) {
2246 ret = error(_("Submodule '%s' could not be updated."), path);
2247 goto out;
2250 if (!(flags & SUBMODULE_MOVE_HEAD_DRY_RUN)) {
2251 if (new_head) {
2252 child_process_init(&cp);
2253 /* also set the HEAD accordingly */
2254 cp.git_cmd = 1;
2255 cp.no_stdin = 1;
2256 cp.dir = path;
2258 prepare_submodule_repo_env(&cp.env);
2259 strvec_pushl(&cp.args, "update-ref", "HEAD",
2260 "--no-deref", new_head, NULL);
2262 if (run_command(&cp)) {
2263 ret = -1;
2264 goto out;
2266 } else {
2267 struct strbuf sb = STRBUF_INIT;
2269 strbuf_addf(&sb, "%s/.git", path);
2270 unlink_or_warn(sb.buf);
2271 strbuf_release(&sb);
2273 if (is_empty_dir(path))
2274 rmdir_or_warn(path);
2276 submodule_unset_core_worktree(sub);
2279 out:
2280 return ret;
2283 int validate_submodule_git_dir(char *git_dir, const char *submodule_name)
2285 size_t len = strlen(git_dir), suffix_len = strlen(submodule_name);
2286 char *p;
2287 int ret = 0;
2289 if (len <= suffix_len || (p = git_dir + len - suffix_len)[-1] != '/' ||
2290 strcmp(p, submodule_name))
2291 BUG("submodule name '%s' not a suffix of git dir '%s'",
2292 submodule_name, git_dir);
2295 * We prevent the contents of sibling submodules' git directories to
2296 * clash.
2298 * Example: having a submodule named `hippo` and another one named
2299 * `hippo/hooks` would result in the git directories
2300 * `.git/modules/hippo/` and `.git/modules/hippo/hooks/`, respectively,
2301 * but the latter directory is already designated to contain the hooks
2302 * of the former.
2304 for (; *p; p++) {
2305 if (is_dir_sep(*p)) {
2306 char c = *p;
2308 *p = '\0';
2309 if (is_git_directory(git_dir))
2310 ret = -1;
2311 *p = c;
2313 if (ret < 0)
2314 return error(_("submodule git dir '%s' is "
2315 "inside git dir '%.*s'"),
2316 git_dir,
2317 (int)(p - git_dir), git_dir);
2321 return 0;
2324 int validate_submodule_path(const char *path)
2326 char *p = xstrdup(path);
2327 struct stat st;
2328 int i, ret = 0;
2329 char sep;
2331 for (i = 0; !ret && p[i]; i++) {
2332 if (!is_dir_sep(p[i]))
2333 continue;
2335 sep = p[i];
2336 p[i] = '\0';
2337 /* allow missing components, but no symlinks */
2338 ret = lstat(p, &st) || !S_ISLNK(st.st_mode) ? 0 : -1;
2339 p[i] = sep;
2340 if (ret)
2341 error(_("expected '%.*s' in submodule path '%s' not to "
2342 "be a symbolic link"), i, p, p);
2344 if (!lstat(p, &st) && S_ISLNK(st.st_mode))
2345 ret = error(_("expected submodule path '%s' not to be a "
2346 "symbolic link"), p);
2347 free(p);
2348 return ret;
2353 * Embeds a single submodules git directory into the superprojects git dir,
2354 * non recursively.
2356 static void relocate_single_git_dir_into_superproject(const char *path,
2357 const char *super_prefix)
2359 char *old_git_dir = NULL, *real_old_git_dir = NULL, *real_new_git_dir = NULL;
2360 struct strbuf new_gitdir = STRBUF_INIT;
2361 const struct submodule *sub;
2363 if (validate_submodule_path(path) < 0)
2364 exit(128);
2366 if (submodule_uses_worktrees(path))
2367 die(_("relocate_gitdir for submodule '%s' with "
2368 "more than one worktree not supported"), path);
2370 old_git_dir = xstrfmt("%s/.git", path);
2371 if (read_gitfile(old_git_dir))
2372 /* If it is an actual gitfile, it doesn't need migration. */
2373 return;
2375 real_old_git_dir = real_pathdup(old_git_dir, 1);
2377 sub = submodule_from_path(the_repository, null_oid(), path);
2378 if (!sub)
2379 die(_("could not lookup name for submodule '%s'"), path);
2381 submodule_name_to_gitdir(&new_gitdir, the_repository, sub->name);
2382 if (validate_submodule_git_dir(new_gitdir.buf, sub->name) < 0)
2383 die(_("refusing to move '%s' into an existing git dir"),
2384 real_old_git_dir);
2385 if (safe_create_leading_directories_const(new_gitdir.buf) < 0)
2386 die(_("could not create directory '%s'"), new_gitdir.buf);
2387 real_new_git_dir = real_pathdup(new_gitdir.buf, 1);
2389 fprintf(stderr, _("Migrating git directory of '%s%s' from\n'%s' to\n'%s'\n"),
2390 super_prefix ? super_prefix : "", path,
2391 real_old_git_dir, real_new_git_dir);
2393 relocate_gitdir(path, real_old_git_dir, real_new_git_dir);
2395 free(old_git_dir);
2396 free(real_old_git_dir);
2397 free(real_new_git_dir);
2398 strbuf_release(&new_gitdir);
2401 static void absorb_git_dir_into_superproject_recurse(const char *path,
2402 const char *super_prefix)
2405 struct child_process cp = CHILD_PROCESS_INIT;
2407 if (validate_submodule_path(path) < 0)
2408 exit(128);
2410 cp.dir = path;
2411 cp.git_cmd = 1;
2412 cp.no_stdin = 1;
2413 strvec_pushl(&cp.args, "submodule--helper",
2414 "absorbgitdirs", NULL);
2415 strvec_pushf(&cp.args, "--super-prefix=%s%s/", super_prefix ?
2416 super_prefix : "", path);
2418 prepare_submodule_repo_env(&cp.env);
2419 if (run_command(&cp))
2420 die(_("could not recurse into submodule '%s'"), path);
2424 * Migrate the git directory of the submodule given by path from
2425 * having its git directory within the working tree to the git dir nested
2426 * in its superprojects git dir under modules/.
2428 void absorb_git_dir_into_superproject(const char *path,
2429 const char *super_prefix)
2431 int err_code;
2432 const char *sub_git_dir;
2433 struct strbuf gitdir = STRBUF_INIT;
2435 if (validate_submodule_path(path) < 0)
2436 exit(128);
2438 strbuf_addf(&gitdir, "%s/.git", path);
2439 sub_git_dir = resolve_gitdir_gently(gitdir.buf, &err_code);
2441 /* Not populated? */
2442 if (!sub_git_dir) {
2443 const struct submodule *sub;
2444 struct strbuf sub_gitdir = STRBUF_INIT;
2446 if (err_code == READ_GITFILE_ERR_STAT_FAILED) {
2447 /* unpopulated as expected */
2448 strbuf_release(&gitdir);
2449 return;
2452 if (err_code != READ_GITFILE_ERR_NOT_A_REPO)
2453 /* We don't know what broke here. */
2454 read_gitfile_error_die(err_code, path, NULL);
2457 * Maybe populated, but no git directory was found?
2458 * This can happen if the superproject is a submodule
2459 * itself and was just absorbed. The absorption of the
2460 * superproject did not rewrite the git file links yet,
2461 * fix it now.
2463 sub = submodule_from_path(the_repository, null_oid(), path);
2464 if (!sub)
2465 die(_("could not lookup name for submodule '%s'"), path);
2466 submodule_name_to_gitdir(&sub_gitdir, the_repository, sub->name);
2467 connect_work_tree_and_git_dir(path, sub_gitdir.buf, 0);
2468 strbuf_release(&sub_gitdir);
2469 } else {
2470 /* Is it already absorbed into the superprojects git dir? */
2471 char *real_sub_git_dir = real_pathdup(sub_git_dir, 1);
2472 char *real_common_git_dir = real_pathdup(repo_get_common_dir(the_repository), 1);
2474 if (!starts_with(real_sub_git_dir, real_common_git_dir))
2475 relocate_single_git_dir_into_superproject(path, super_prefix);
2477 free(real_sub_git_dir);
2478 free(real_common_git_dir);
2480 strbuf_release(&gitdir);
2482 absorb_git_dir_into_superproject_recurse(path, super_prefix);
2485 int get_superproject_working_tree(struct strbuf *buf)
2487 struct child_process cp = CHILD_PROCESS_INIT;
2488 struct strbuf sb = STRBUF_INIT;
2489 struct strbuf one_up = STRBUF_INIT;
2490 char *cwd = xgetcwd();
2491 int ret = 0;
2492 const char *subpath;
2493 int code;
2494 ssize_t len;
2496 if (!is_inside_work_tree())
2498 * FIXME:
2499 * We might have a superproject, but it is harder
2500 * to determine.
2502 return 0;
2504 if (!strbuf_realpath(&one_up, "../", 0))
2505 return 0;
2507 subpath = relative_path(cwd, one_up.buf, &sb);
2508 strbuf_release(&one_up);
2510 prepare_submodule_repo_env(&cp.env);
2511 strvec_pop(&cp.env);
2513 strvec_pushl(&cp.args, "--literal-pathspecs", "-C", "..",
2514 "ls-files", "-z", "--stage", "--full-name", "--",
2515 subpath, NULL);
2516 strbuf_reset(&sb);
2518 cp.no_stdin = 1;
2519 cp.no_stderr = 1;
2520 cp.out = -1;
2521 cp.git_cmd = 1;
2523 if (start_command(&cp))
2524 die(_("could not start ls-files in .."));
2526 len = strbuf_read(&sb, cp.out, PATH_MAX);
2527 close(cp.out);
2529 if (starts_with(sb.buf, "160000")) {
2530 int super_sub_len;
2531 int cwd_len = strlen(cwd);
2532 char *super_sub, *super_wt;
2535 * There is a superproject having this repo as a submodule.
2536 * The format is <mode> SP <hash> SP <stage> TAB <full name> \0,
2537 * We're only interested in the name after the tab.
2539 super_sub = strchr(sb.buf, '\t') + 1;
2540 super_sub_len = strlen(super_sub);
2542 if (super_sub_len > cwd_len ||
2543 strcmp(&cwd[cwd_len - super_sub_len], super_sub))
2544 BUG("returned path string doesn't match cwd?");
2546 super_wt = xstrdup(cwd);
2547 super_wt[cwd_len - super_sub_len] = '\0';
2549 strbuf_realpath(buf, super_wt, 1);
2550 ret = 1;
2551 free(super_wt);
2553 free(cwd);
2554 strbuf_release(&sb);
2556 code = finish_command(&cp);
2558 if (code == 128)
2559 /* '../' is not a git repository */
2560 return 0;
2561 if (code == 0 && len == 0)
2562 /* There is an unrelated git repository at '../' */
2563 return 0;
2564 if (code)
2565 die(_("ls-tree returned unexpected return code %d"), code);
2567 return ret;
2571 * Put the gitdir for a submodule (given relative to the main
2572 * repository worktree) into `buf`, or return -1 on error.
2574 int submodule_to_gitdir(struct strbuf *buf, const char *submodule)
2576 const struct submodule *sub;
2577 const char *git_dir;
2578 int ret = 0;
2580 if (validate_submodule_path(submodule) < 0)
2581 exit(128);
2583 strbuf_reset(buf);
2584 strbuf_addstr(buf, submodule);
2585 strbuf_complete(buf, '/');
2586 strbuf_addstr(buf, ".git");
2588 git_dir = read_gitfile(buf->buf);
2589 if (git_dir) {
2590 strbuf_reset(buf);
2591 strbuf_addstr(buf, git_dir);
2593 if (!is_git_directory(buf->buf)) {
2594 sub = submodule_from_path(the_repository, null_oid(),
2595 submodule);
2596 if (!sub) {
2597 ret = -1;
2598 goto cleanup;
2600 strbuf_reset(buf);
2601 submodule_name_to_gitdir(buf, the_repository, sub->name);
2604 cleanup:
2605 return ret;
2608 void submodule_name_to_gitdir(struct strbuf *buf, struct repository *r,
2609 const char *submodule_name)
2612 * NEEDSWORK: The current way of mapping a submodule's name to
2613 * its location in .git/modules/ has problems with some naming
2614 * schemes. For example, if a submodule is named "foo" and
2615 * another is named "foo/bar" (whether present in the same
2616 * superproject commit or not - the problem will arise if both
2617 * superproject commits have been checked out at any point in
2618 * time), or if two submodule names only have different cases in
2619 * a case-insensitive filesystem.
2621 * There are several solutions, including encoding the path in
2622 * some way, introducing a submodule.<name>.gitdir config in
2623 * .git/config (not .gitmodules) that allows overriding what the
2624 * gitdir of a submodule would be (and teach Git, upon noticing
2625 * a clash, to automatically determine a non-clashing name and
2626 * to write such a config), or introducing a
2627 * submodule.<name>.gitdir config in .gitmodules that repo
2628 * administrators can explicitly set. Nothing has been decided,
2629 * so for now, just append the name at the end of the path.
2631 strbuf_repo_git_path(buf, r, "modules/");
2632 strbuf_addstr(buf, submodule_name);