t-reftable-record: add obj tests for reftable_record_is_deletion()
[alt-git.git] / submodule.c
blob759cf1e1cda9b0ad10a756523b7d5896bfb3a853
1 #include "git-compat-util.h"
2 #include "abspath.h"
3 #include "repository.h"
4 #include "config.h"
5 #include "submodule-config.h"
6 #include "submodule.h"
7 #include "dir.h"
8 #include "diff.h"
9 #include "commit.h"
10 #include "environment.h"
11 #include "gettext.h"
12 #include "hex.h"
13 #include "revision.h"
14 #include "run-command.h"
15 #include "diffcore.h"
16 #include "refs.h"
17 #include "string-list.h"
18 #include "oid-array.h"
19 #include "strvec.h"
20 #include "thread-utils.h"
21 #include "path.h"
22 #include "remote.h"
23 #include "worktree.h"
24 #include "parse-options.h"
25 #include "object-file.h"
26 #include "object-name.h"
27 #include "object-store-ll.h"
28 #include "commit-reach.h"
29 #include "read-cache-ll.h"
30 #include "setup.h"
31 #include "trace2.h"
33 static int config_update_recurse_submodules = RECURSE_SUBMODULES_OFF;
34 static int initialized_fetch_ref_tips;
35 static struct oid_array ref_tips_before_fetch;
36 static struct oid_array ref_tips_after_fetch;
39 * Check if the .gitmodules file is unmerged. Parsing of the .gitmodules file
40 * will be disabled because we can't guess what might be configured in
41 * .gitmodules unless the user resolves the conflict.
43 int is_gitmodules_unmerged(struct index_state *istate)
45 int pos = index_name_pos(istate, GITMODULES_FILE, strlen(GITMODULES_FILE));
46 if (pos < 0) { /* .gitmodules not found or isn't merged */
47 pos = -1 - pos;
48 if (istate->cache_nr > pos) { /* there is a .gitmodules */
49 const struct cache_entry *ce = istate->cache[pos];
50 if (ce_namelen(ce) == strlen(GITMODULES_FILE) &&
51 !strcmp(ce->name, GITMODULES_FILE))
52 return 1;
56 return 0;
60 * Check if the .gitmodules file is safe to write.
62 * Writing to the .gitmodules file requires that the file exists in the
63 * working tree or, if it doesn't, that a brand new .gitmodules file is going
64 * to be created (i.e. it's neither in the index nor in the current branch).
66 * It is not safe to write to .gitmodules if it's not in the working tree but
67 * it is in the index or in the current branch, because writing new values
68 * (and staging them) would blindly overwrite ALL the old content.
70 int is_writing_gitmodules_ok(void)
72 struct object_id oid;
73 return file_exists(GITMODULES_FILE) ||
74 (repo_get_oid(the_repository, GITMODULES_INDEX, &oid) < 0 && repo_get_oid(the_repository, GITMODULES_HEAD, &oid) < 0);
78 * Check if the .gitmodules file has unstaged modifications. This must be
79 * checked before allowing modifications to the .gitmodules file with the
80 * intention to stage them later, because when continuing we would stage the
81 * modifications the user didn't stage herself too. That might change in a
82 * future version when we learn to stage the changes we do ourselves without
83 * staging any previous modifications.
85 int is_staging_gitmodules_ok(struct index_state *istate)
87 int pos = index_name_pos(istate, GITMODULES_FILE, strlen(GITMODULES_FILE));
89 if ((pos >= 0) && (pos < istate->cache_nr)) {
90 struct stat st;
91 if (lstat(GITMODULES_FILE, &st) == 0 &&
92 ie_modified(istate, istate->cache[pos], &st, 0) & DATA_CHANGED)
93 return 0;
96 return 1;
99 static int for_each_remote_ref_submodule(const char *submodule,
100 each_ref_fn fn, void *cb_data)
102 return refs_for_each_remote_ref(repo_get_submodule_ref_store(the_repository,
103 submodule),
104 fn, cb_data);
108 * Try to update the "path" entry in the "submodule.<name>" section of the
109 * .gitmodules file. Return 0 only if a .gitmodules file was found, a section
110 * with the correct path=<oldpath> setting was found and we could update it.
112 int update_path_in_gitmodules(const char *oldpath, const char *newpath)
114 struct strbuf entry = STRBUF_INIT;
115 const struct submodule *submodule;
116 int ret;
118 if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */
119 return -1;
121 if (is_gitmodules_unmerged(the_repository->index))
122 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
124 submodule = submodule_from_path(the_repository, null_oid(), oldpath);
125 if (!submodule || !submodule->name) {
126 warning(_("Could not find section in .gitmodules where path=%s"), oldpath);
127 return -1;
129 strbuf_addstr(&entry, "submodule.");
130 strbuf_addstr(&entry, submodule->name);
131 strbuf_addstr(&entry, ".path");
132 ret = config_set_in_gitmodules_file_gently(entry.buf, newpath);
133 strbuf_release(&entry);
134 return ret;
138 * Try to remove the "submodule.<name>" section from .gitmodules where the given
139 * path is configured. Return 0 only if a .gitmodules file was found, a section
140 * with the correct path=<path> setting was found and we could remove it.
142 int remove_path_from_gitmodules(const char *path)
144 struct strbuf sect = STRBUF_INIT;
145 const struct submodule *submodule;
147 if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */
148 return -1;
150 if (is_gitmodules_unmerged(the_repository->index))
151 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
153 submodule = submodule_from_path(the_repository, null_oid(), path);
154 if (!submodule || !submodule->name) {
155 warning(_("Could not find section in .gitmodules where path=%s"), path);
156 return -1;
158 strbuf_addstr(&sect, "submodule.");
159 strbuf_addstr(&sect, submodule->name);
160 if (git_config_rename_section_in_file(GITMODULES_FILE, sect.buf, NULL) < 0) {
161 /* Maybe the user already did that, don't error out here */
162 warning(_("Could not remove .gitmodules entry for %s"), path);
163 strbuf_release(&sect);
164 return -1;
166 strbuf_release(&sect);
167 return 0;
170 void stage_updated_gitmodules(struct index_state *istate)
172 if (add_file_to_index(istate, GITMODULES_FILE, 0))
173 die(_("staging updated .gitmodules failed"));
176 static struct string_list added_submodule_odb_paths = STRING_LIST_INIT_NODUP;
178 void add_submodule_odb_by_path(const char *path)
180 string_list_insert(&added_submodule_odb_paths, xstrdup(path));
183 int register_all_submodule_odb_as_alternates(void)
185 int i;
186 int ret = added_submodule_odb_paths.nr;
188 for (i = 0; i < added_submodule_odb_paths.nr; i++)
189 add_to_alternates_memory(added_submodule_odb_paths.items[i].string);
190 if (ret) {
191 string_list_clear(&added_submodule_odb_paths, 0);
192 trace2_data_intmax("submodule", the_repository,
193 "register_all_submodule_odb_as_alternates/registered", ret);
194 if (git_env_bool("GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB", 0))
195 BUG("register_all_submodule_odb_as_alternates() called");
197 return ret;
200 void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
201 const char *path)
203 const struct submodule *submodule = submodule_from_path(the_repository,
204 null_oid(),
205 path);
206 if (submodule) {
207 const char *ignore;
208 char *key;
210 key = xstrfmt("submodule.%s.ignore", submodule->name);
211 if (repo_config_get_string_tmp(the_repository, key, &ignore))
212 ignore = submodule->ignore;
213 free(key);
215 if (ignore)
216 handle_ignore_submodules_arg(diffopt, ignore);
217 else if (is_gitmodules_unmerged(the_repository->index))
218 diffopt->flags.ignore_submodules = 1;
222 /* Cheap function that only determines if we're interested in submodules at all */
223 int git_default_submodule_config(const char *var, const char *value,
224 void *cb UNUSED)
226 if (!strcmp(var, "submodule.recurse")) {
227 int v = git_config_bool(var, value) ?
228 RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF;
229 config_update_recurse_submodules = v;
231 return 0;
234 int option_parse_recurse_submodules_worktree_updater(const struct option *opt,
235 const char *arg, int unset)
237 if (unset) {
238 config_update_recurse_submodules = RECURSE_SUBMODULES_OFF;
239 return 0;
241 if (arg)
242 config_update_recurse_submodules =
243 parse_update_recurse_submodules_arg(opt->long_name,
244 arg);
245 else
246 config_update_recurse_submodules = RECURSE_SUBMODULES_ON;
248 return 0;
252 * Determine if a submodule has been initialized at a given 'path'
255 * NEEDSWORK: Emit a warning if submodule.active exists, but is valueless,
256 * ie, the config looks like: "[submodule] active\n".
257 * Since that is an invalid pathspec, we should inform the user.
259 int is_tree_submodule_active(struct repository *repo,
260 const struct object_id *treeish_name,
261 const char *path)
263 int ret = 0;
264 char *key = NULL;
265 char *value = NULL;
266 const struct string_list *sl;
267 const struct submodule *module;
269 module = submodule_from_path(repo, treeish_name, path);
271 /* early return if there isn't a path->module mapping */
272 if (!module)
273 return 0;
275 /* submodule.<name>.active is set */
276 key = xstrfmt("submodule.%s.active", module->name);
277 if (!repo_config_get_bool(repo, key, &ret)) {
278 free(key);
279 return ret;
281 free(key);
283 /* submodule.active is set */
284 if (!repo_config_get_string_multi(repo, "submodule.active", &sl)) {
285 struct pathspec ps;
286 struct strvec args = STRVEC_INIT;
287 const struct string_list_item *item;
289 for_each_string_list_item(item, sl) {
290 strvec_push(&args, item->string);
293 parse_pathspec(&ps, 0, 0, NULL, args.v);
294 ret = match_pathspec(repo->index, &ps, path, strlen(path), 0, NULL, 1);
296 strvec_clear(&args);
297 clear_pathspec(&ps);
298 return ret;
301 /* fallback to checking if the URL is set */
302 key = xstrfmt("submodule.%s.url", module->name);
303 ret = !repo_config_get_string(repo, key, &value);
305 free(value);
306 free(key);
307 return ret;
310 int is_submodule_active(struct repository *repo, const char *path)
312 return is_tree_submodule_active(repo, null_oid(), path);
315 int is_submodule_populated_gently(const char *path, int *return_error_code)
317 int ret = 0;
318 char *gitdir = xstrfmt("%s/.git", path);
320 if (resolve_gitdir_gently(gitdir, return_error_code))
321 ret = 1;
323 free(gitdir);
324 return ret;
328 * Dies if the provided 'prefix' corresponds to an unpopulated submodule
330 void die_in_unpopulated_submodule(struct index_state *istate,
331 const char *prefix)
333 int i, prefixlen;
335 if (!prefix)
336 return;
338 prefixlen = strlen(prefix);
340 for (i = 0; i < istate->cache_nr; i++) {
341 struct cache_entry *ce = istate->cache[i];
342 int ce_len = ce_namelen(ce);
344 if (!S_ISGITLINK(ce->ce_mode))
345 continue;
346 if (prefixlen <= ce_len)
347 continue;
348 if (strncmp(ce->name, prefix, ce_len))
349 continue;
350 if (prefix[ce_len] != '/')
351 continue;
353 die(_("in unpopulated submodule '%s'"), ce->name);
358 * Dies if any paths in the provided pathspec descends into a submodule
360 void die_path_inside_submodule(struct index_state *istate,
361 const struct pathspec *ps)
363 int i, j;
365 for (i = 0; i < istate->cache_nr; i++) {
366 struct cache_entry *ce = istate->cache[i];
367 int ce_len = ce_namelen(ce);
369 if (!S_ISGITLINK(ce->ce_mode))
370 continue;
372 for (j = 0; j < ps->nr ; j++) {
373 const struct pathspec_item *item = &ps->items[j];
375 if (item->len <= ce_len)
376 continue;
377 if (item->match[ce_len] != '/')
378 continue;
379 if (strncmp(ce->name, item->match, ce_len))
380 continue;
381 if (item->len == ce_len + 1)
382 continue;
384 die(_("Pathspec '%s' is in submodule '%.*s'"),
385 item->original, ce_len, ce->name);
390 enum submodule_update_type parse_submodule_update_type(const char *value)
392 if (!strcmp(value, "none"))
393 return SM_UPDATE_NONE;
394 else if (!strcmp(value, "checkout"))
395 return SM_UPDATE_CHECKOUT;
396 else if (!strcmp(value, "rebase"))
397 return SM_UPDATE_REBASE;
398 else if (!strcmp(value, "merge"))
399 return SM_UPDATE_MERGE;
400 else if (*value == '!')
401 return SM_UPDATE_COMMAND;
402 else
403 return SM_UPDATE_UNSPECIFIED;
406 int parse_submodule_update_strategy(const char *value,
407 struct submodule_update_strategy *dst)
409 enum submodule_update_type type;
411 free((void*)dst->command);
412 dst->command = NULL;
414 type = parse_submodule_update_type(value);
415 if (type == SM_UPDATE_UNSPECIFIED)
416 return -1;
418 dst->type = type;
419 if (type == SM_UPDATE_COMMAND)
420 dst->command = xstrdup(value + 1);
422 return 0;
425 const char *submodule_update_type_to_string(enum submodule_update_type type)
427 switch (type) {
428 case SM_UPDATE_CHECKOUT:
429 return "checkout";
430 case SM_UPDATE_MERGE:
431 return "merge";
432 case SM_UPDATE_REBASE:
433 return "rebase";
434 case SM_UPDATE_NONE:
435 return "none";
436 case SM_UPDATE_UNSPECIFIED:
437 case SM_UPDATE_COMMAND:
438 BUG("init_submodule() should handle type %d", type);
439 default:
440 BUG("unexpected update strategy type: %d", type);
444 void handle_ignore_submodules_arg(struct diff_options *diffopt,
445 const char *arg)
447 diffopt->flags.ignore_submodule_set = 1;
448 diffopt->flags.ignore_submodules = 0;
449 diffopt->flags.ignore_untracked_in_submodules = 0;
450 diffopt->flags.ignore_dirty_submodules = 0;
452 if (!strcmp(arg, "all"))
453 diffopt->flags.ignore_submodules = 1;
454 else if (!strcmp(arg, "untracked"))
455 diffopt->flags.ignore_untracked_in_submodules = 1;
456 else if (!strcmp(arg, "dirty"))
457 diffopt->flags.ignore_dirty_submodules = 1;
458 else if (strcmp(arg, "none"))
459 die(_("bad --ignore-submodules argument: %s"), arg);
461 * Please update _git_status() in git-completion.bash when you
462 * add new options
466 static int prepare_submodule_diff_summary(struct repository *r, struct rev_info *rev,
467 const char *path,
468 struct commit *left, struct commit *right,
469 struct commit_list *merge_bases)
471 struct commit_list *list;
473 repo_init_revisions(r, rev, NULL);
474 setup_revisions(0, NULL, rev, NULL);
475 rev->left_right = 1;
476 rev->first_parent_only = 1;
477 left->object.flags |= SYMMETRIC_LEFT;
478 add_pending_object(rev, &left->object, path);
479 add_pending_object(rev, &right->object, path);
480 for (list = merge_bases; list; list = list->next) {
481 list->item->object.flags |= UNINTERESTING;
482 add_pending_object(rev, &list->item->object,
483 oid_to_hex(&list->item->object.oid));
485 return prepare_revision_walk(rev);
488 static void print_submodule_diff_summary(struct repository *r, struct rev_info *rev, struct diff_options *o)
490 static const char format[] = " %m %s";
491 struct strbuf sb = STRBUF_INIT;
492 struct commit *commit;
494 while ((commit = get_revision(rev))) {
495 struct pretty_print_context ctx = {0};
496 ctx.date_mode = rev->date_mode;
497 ctx.output_encoding = get_log_output_encoding();
498 strbuf_setlen(&sb, 0);
499 repo_format_commit_message(r, commit, format, &sb,
500 &ctx);
501 strbuf_addch(&sb, '\n');
502 if (commit->object.flags & SYMMETRIC_LEFT)
503 diff_emit_submodule_del(o, sb.buf);
504 else
505 diff_emit_submodule_add(o, sb.buf);
507 strbuf_release(&sb);
510 void prepare_submodule_repo_env(struct strvec *out)
512 prepare_other_repo_env(out, DEFAULT_GIT_DIR_ENVIRONMENT);
515 static void prepare_submodule_repo_env_in_gitdir(struct strvec *out)
517 prepare_other_repo_env(out, ".");
521 * Initialize a repository struct for a submodule based on the provided 'path'.
523 * Returns the repository struct on success,
524 * NULL when the submodule is not present.
526 static struct repository *open_submodule(const char *path)
528 struct strbuf sb = STRBUF_INIT;
529 struct repository *out = xmalloc(sizeof(*out));
531 if (submodule_to_gitdir(&sb, path) || repo_init(out, sb.buf, NULL)) {
532 strbuf_release(&sb);
533 free(out);
534 return NULL;
537 /* Mark it as a submodule */
538 out->submodule_prefix = xstrdup(path);
540 strbuf_release(&sb);
541 return out;
545 * Helper function to display the submodule header line prior to the full
546 * summary output.
548 * If it can locate the submodule git directory it will create a repository
549 * handle for the submodule and lookup both the left and right commits and
550 * put them into the left and right pointers.
552 static void show_submodule_header(struct diff_options *o,
553 const char *path,
554 struct object_id *one, struct object_id *two,
555 unsigned dirty_submodule,
556 struct repository *sub,
557 struct commit **left, struct commit **right,
558 struct commit_list **merge_bases)
560 const char *message = NULL;
561 struct strbuf sb = STRBUF_INIT;
562 int fast_forward = 0, fast_backward = 0;
564 if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
565 diff_emit_submodule_untracked(o, path);
567 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
568 diff_emit_submodule_modified(o, path);
570 if (is_null_oid(one))
571 message = "(new submodule)";
572 else if (is_null_oid(two))
573 message = "(submodule deleted)";
575 if (!sub) {
576 if (!message)
577 message = "(commits not present)";
578 goto output_header;
582 * Attempt to lookup the commit references, and determine if this is
583 * a fast forward or fast backwards update.
585 *left = lookup_commit_reference(sub, one);
586 *right = lookup_commit_reference(sub, two);
589 * Warn about missing commits in the submodule project, but only if
590 * they aren't null.
592 if ((!is_null_oid(one) && !*left) ||
593 (!is_null_oid(two) && !*right))
594 message = "(commits not present)";
596 *merge_bases = NULL;
597 if (repo_get_merge_bases(sub, *left, *right, merge_bases) < 0) {
598 message = "(corrupt repository)";
599 goto output_header;
602 if (*merge_bases) {
603 if ((*merge_bases)->item == *left)
604 fast_forward = 1;
605 else if ((*merge_bases)->item == *right)
606 fast_backward = 1;
609 if (oideq(one, two)) {
610 strbuf_release(&sb);
611 return;
614 output_header:
615 strbuf_addf(&sb, "Submodule %s ", path);
616 strbuf_add_unique_abbrev(&sb, one, DEFAULT_ABBREV);
617 strbuf_addstr(&sb, (fast_backward || fast_forward) ? ".." : "...");
618 strbuf_add_unique_abbrev(&sb, two, DEFAULT_ABBREV);
619 if (message)
620 strbuf_addf(&sb, " %s\n", message);
621 else
622 strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : "");
623 diff_emit_submodule_header(o, sb.buf);
625 strbuf_release(&sb);
628 void show_submodule_diff_summary(struct diff_options *o, const char *path,
629 struct object_id *one, struct object_id *two,
630 unsigned dirty_submodule)
632 struct rev_info rev = REV_INFO_INIT;
633 struct commit *left = NULL, *right = NULL;
634 struct commit_list *merge_bases = NULL;
635 struct repository *sub;
637 sub = open_submodule(path);
638 show_submodule_header(o, path, one, two, dirty_submodule,
639 sub, &left, &right, &merge_bases);
642 * If we don't have both a left and a right pointer, there is no
643 * reason to try and display a summary. The header line should contain
644 * all the information the user needs.
646 if (!left || !right || !sub)
647 goto out;
649 /* Treat revision walker failure the same as missing commits */
650 if (prepare_submodule_diff_summary(sub, &rev, path, left, right, merge_bases)) {
651 diff_emit_submodule_error(o, "(revision walker failed)\n");
652 goto out;
655 print_submodule_diff_summary(sub, &rev, o);
657 out:
658 free_commit_list(merge_bases);
659 release_revisions(&rev);
660 clear_commit_marks(left, ~0);
661 clear_commit_marks(right, ~0);
662 if (sub) {
663 repo_clear(sub);
664 free(sub);
668 void show_submodule_inline_diff(struct diff_options *o, const char *path,
669 struct object_id *one, struct object_id *two,
670 unsigned dirty_submodule)
672 const struct object_id *old_oid = the_hash_algo->empty_tree, *new_oid = the_hash_algo->empty_tree;
673 struct commit *left = NULL, *right = NULL;
674 struct commit_list *merge_bases = NULL;
675 struct child_process cp = CHILD_PROCESS_INIT;
676 struct strbuf sb = STRBUF_INIT;
677 struct repository *sub;
679 sub = open_submodule(path);
680 show_submodule_header(o, path, one, two, dirty_submodule,
681 sub, &left, &right, &merge_bases);
683 /* We need a valid left and right commit to display a difference */
684 if (!(left || is_null_oid(one)) ||
685 !(right || is_null_oid(two)))
686 goto done;
688 if (left)
689 old_oid = one;
690 if (right)
691 new_oid = two;
693 cp.git_cmd = 1;
694 cp.dir = path;
695 cp.out = -1;
696 cp.no_stdin = 1;
698 /* TODO: other options may need to be passed here. */
699 strvec_pushl(&cp.args, "diff", "--submodule=diff", NULL);
700 strvec_pushf(&cp.args, "--color=%s", want_color(o->use_color) ?
701 "always" : "never");
703 if (o->flags.reverse_diff) {
704 strvec_pushf(&cp.args, "--src-prefix=%s%s/",
705 o->b_prefix, path);
706 strvec_pushf(&cp.args, "--dst-prefix=%s%s/",
707 o->a_prefix, path);
708 } else {
709 strvec_pushf(&cp.args, "--src-prefix=%s%s/",
710 o->a_prefix, path);
711 strvec_pushf(&cp.args, "--dst-prefix=%s%s/",
712 o->b_prefix, path);
714 strvec_push(&cp.args, oid_to_hex(old_oid));
716 * If the submodule has modified content, we will diff against the
717 * work tree, under the assumption that the user has asked for the
718 * diff format and wishes to actually see all differences even if they
719 * haven't yet been committed to the submodule yet.
721 if (!(dirty_submodule & DIRTY_SUBMODULE_MODIFIED))
722 strvec_push(&cp.args, oid_to_hex(new_oid));
724 prepare_submodule_repo_env(&cp.env);
726 if (!is_directory(path)) {
727 /* fall back to absorbed git dir, if any */
728 if (!sub)
729 goto done;
730 cp.dir = sub->gitdir;
731 strvec_push(&cp.env, GIT_DIR_ENVIRONMENT "=.");
732 strvec_push(&cp.env, GIT_WORK_TREE_ENVIRONMENT "=.");
735 if (start_command(&cp)) {
736 diff_emit_submodule_error(o, "(diff failed)\n");
737 goto done;
740 while (strbuf_getwholeline_fd(&sb, cp.out, '\n') != EOF)
741 diff_emit_submodule_pipethrough(o, sb.buf, sb.len);
743 if (finish_command(&cp))
744 diff_emit_submodule_error(o, "(diff failed)\n");
746 done:
747 strbuf_release(&sb);
748 free_commit_list(merge_bases);
749 if (left)
750 clear_commit_marks(left, ~0);
751 if (right)
752 clear_commit_marks(right, ~0);
753 if (sub) {
754 repo_clear(sub);
755 free(sub);
759 int should_update_submodules(void)
761 return config_update_recurse_submodules == RECURSE_SUBMODULES_ON;
764 const struct submodule *submodule_from_ce(const struct cache_entry *ce)
766 if (!S_ISGITLINK(ce->ce_mode))
767 return NULL;
769 if (!should_update_submodules())
770 return NULL;
772 return submodule_from_path(the_repository, null_oid(), ce->name);
776 struct collect_changed_submodules_cb_data {
777 struct repository *repo;
778 struct string_list *changed;
779 const struct object_id *commit_oid;
783 * this would normally be two functions: default_name_from_path() and
784 * path_from_default_name(). Since the default name is the same as
785 * the submodule path we can get away with just one function which only
786 * checks whether there is a submodule in the working directory at that
787 * location.
789 static const char *default_name_or_path(const char *path_or_name)
791 int error_code;
793 if (!is_submodule_populated_gently(path_or_name, &error_code))
794 return NULL;
796 return path_or_name;
800 * Holds relevant information for a changed submodule. Used as the .util
801 * member of the changed submodule name string_list_item.
803 * (super_oid, path) allows the submodule config to be read from _some_
804 * .gitmodules file. We store this information the first time we find a
805 * superproject commit that points to the submodule, but this is
806 * arbitrary - we can choose any (super_oid, path) that matches the
807 * submodule's name.
809 * NEEDSWORK: Storing an arbitrary commit is undesirable because we can't
810 * guarantee that we're reading the commit that the user would expect. A better
811 * scheme would be to just fetch a submodule by its name. This requires two
812 * steps:
813 * - Create a function that behaves like repo_submodule_init(), but accepts a
814 * submodule name instead of treeish_name and path. This should be easy
815 * because repo_submodule_init() internally uses the submodule's name.
817 * - Replace most instances of 'struct submodule' (which is the .gitmodules
818 * config) with just the submodule name. This is OK because we expect
819 * submodule settings to be stored in .git/config (via "git submodule init"),
820 * not .gitmodules. This also lets us delete get_non_gitmodules_submodule(),
821 * which constructs a bogus 'struct submodule' for the sake of giving a
822 * placeholder name to a gitlink.
824 struct changed_submodule_data {
826 * The first superproject commit in the rev walk that points to
827 * the submodule.
829 const struct object_id *super_oid;
831 * Path to the submodule in the superproject commit referenced
832 * by 'super_oid'.
834 char *path;
835 /* The submodule commits that have changed in the rev walk. */
836 struct oid_array new_commits;
839 static void changed_submodule_data_clear(struct changed_submodule_data *cs_data)
841 oid_array_clear(&cs_data->new_commits);
842 free(cs_data->path);
845 static void collect_changed_submodules_cb(struct diff_queue_struct *q,
846 struct diff_options *options UNUSED,
847 void *data)
849 struct collect_changed_submodules_cb_data *me = data;
850 struct string_list *changed = me->changed;
851 const struct object_id *commit_oid = me->commit_oid;
852 int i;
854 for (i = 0; i < q->nr; i++) {
855 struct diff_filepair *p = q->queue[i];
856 const struct submodule *submodule;
857 const char *name;
858 struct string_list_item *item;
859 struct changed_submodule_data *cs_data;
861 if (!S_ISGITLINK(p->two->mode))
862 continue;
864 submodule = submodule_from_path(me->repo,
865 commit_oid, p->two->path);
866 if (submodule)
867 name = submodule->name;
868 else {
869 name = default_name_or_path(p->two->path);
870 /* make sure name does not collide with existing one */
871 if (name)
872 submodule = submodule_from_name(me->repo,
873 commit_oid, name);
874 if (submodule) {
875 warning(_("Submodule in commit %s at path: "
876 "'%s' collides with a submodule named "
877 "the same. Skipping it."),
878 oid_to_hex(commit_oid), p->two->path);
879 name = NULL;
883 if (!name)
884 continue;
886 item = string_list_insert(changed, name);
887 if (item->util)
888 cs_data = item->util;
889 else {
890 item->util = xcalloc(1, sizeof(struct changed_submodule_data));
891 cs_data = item->util;
892 cs_data->super_oid = commit_oid;
893 cs_data->path = xstrdup(p->two->path);
895 oid_array_append(&cs_data->new_commits, &p->two->oid);
900 * Collect the paths of submodules in 'changed' which have changed based on
901 * the revisions as specified in 'argv'. Each entry in 'changed' will also
902 * have a corresponding 'struct oid_array' (in the 'util' field) which lists
903 * what the submodule pointers were updated to during the change.
905 static void collect_changed_submodules(struct repository *r,
906 struct string_list *changed,
907 struct strvec *argv)
909 struct rev_info rev;
910 const struct commit *commit;
911 int save_warning;
912 struct setup_revision_opt s_r_opt = {
913 .assume_dashdash = 1,
916 save_warning = warn_on_object_refname_ambiguity;
917 warn_on_object_refname_ambiguity = 0;
918 repo_init_revisions(r, &rev, NULL);
919 setup_revisions(argv->nr, argv->v, &rev, &s_r_opt);
920 warn_on_object_refname_ambiguity = save_warning;
921 if (prepare_revision_walk(&rev))
922 die(_("revision walk setup failed"));
924 while ((commit = get_revision(&rev))) {
925 struct rev_info diff_rev;
926 struct collect_changed_submodules_cb_data data;
927 data.repo = r;
928 data.changed = changed;
929 data.commit_oid = &commit->object.oid;
931 repo_init_revisions(r, &diff_rev, NULL);
932 diff_rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
933 diff_rev.diffopt.format_callback = collect_changed_submodules_cb;
934 diff_rev.diffopt.format_callback_data = &data;
935 diff_rev.dense_combined_merges = 1;
936 diff_tree_combined_merge(commit, &diff_rev);
937 release_revisions(&diff_rev);
940 reset_revision_walk();
941 release_revisions(&rev);
944 static void free_submodules_data(struct string_list *submodules)
946 struct string_list_item *item;
947 for_each_string_list_item(item, submodules)
948 changed_submodule_data_clear(item->util);
950 string_list_clear(submodules, 1);
953 static int has_remote(const char *refname UNUSED,
954 const struct object_id *oid UNUSED,
955 int flags UNUSED, void *cb_data UNUSED)
957 return 1;
960 static int append_oid_to_argv(const struct object_id *oid, void *data)
962 struct strvec *argv = data;
963 strvec_push(argv, oid_to_hex(oid));
964 return 0;
967 struct has_commit_data {
968 struct repository *repo;
969 int result;
970 const char *path;
971 const struct object_id *super_oid;
974 static int check_has_commit(const struct object_id *oid, void *data)
976 struct has_commit_data *cb = data;
977 struct repository subrepo;
978 enum object_type type;
980 if (repo_submodule_init(&subrepo, cb->repo, cb->path, cb->super_oid)) {
981 cb->result = 0;
982 /* subrepo failed to init, so don't clean it up. */
983 return 0;
986 type = oid_object_info(&subrepo, oid, NULL);
988 switch (type) {
989 case OBJ_COMMIT:
990 goto cleanup;
991 case OBJ_BAD:
993 * Object is missing or invalid. If invalid, an error message
994 * has already been printed.
996 cb->result = 0;
997 goto cleanup;
998 default:
999 die(_("submodule entry '%s' (%s) is a %s, not a commit"),
1000 cb->path, oid_to_hex(oid), type_name(type));
1002 cleanup:
1003 repo_clear(&subrepo);
1004 return 0;
1007 static int submodule_has_commits(struct repository *r,
1008 const char *path,
1009 const struct object_id *super_oid,
1010 struct oid_array *commits)
1012 struct has_commit_data has_commit = {
1013 .repo = r,
1014 .result = 1,
1015 .path = path,
1016 .super_oid = super_oid
1019 if (validate_submodule_path(path) < 0)
1020 exit(128);
1022 oid_array_for_each_unique(commits, check_has_commit, &has_commit);
1024 if (has_commit.result) {
1026 * Even if the submodule is checked out and the commit is
1027 * present, make sure it exists in the submodule's object store
1028 * and that it is reachable from a ref.
1030 struct child_process cp = CHILD_PROCESS_INIT;
1031 struct strbuf out = STRBUF_INIT;
1033 strvec_pushl(&cp.args, "rev-list", "-n", "1", NULL);
1034 oid_array_for_each_unique(commits, append_oid_to_argv, &cp.args);
1035 strvec_pushl(&cp.args, "--not", "--all", NULL);
1037 prepare_submodule_repo_env(&cp.env);
1038 cp.git_cmd = 1;
1039 cp.no_stdin = 1;
1040 cp.dir = path;
1042 if (capture_command(&cp, &out, GIT_MAX_HEXSZ + 1) || out.len)
1043 has_commit.result = 0;
1045 strbuf_release(&out);
1048 return has_commit.result;
1051 static int submodule_needs_pushing(struct repository *r,
1052 const char *path,
1053 struct oid_array *commits)
1055 if (!submodule_has_commits(r, path, null_oid(), commits))
1057 * NOTE: We do consider it safe to return "no" here. The
1058 * correct answer would be "We do not know" instead of
1059 * "No push needed", but it is quite hard to change
1060 * the submodule pointer without having the submodule
1061 * around. If a user did however change the submodules
1062 * without having the submodule around, this indicates
1063 * an expert who knows what they are doing or a
1064 * maintainer integrating work from other people. In
1065 * both cases it should be safe to skip this check.
1067 return 0;
1069 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
1070 struct child_process cp = CHILD_PROCESS_INIT;
1071 struct strbuf buf = STRBUF_INIT;
1072 int needs_pushing = 0;
1074 strvec_push(&cp.args, "rev-list");
1075 oid_array_for_each_unique(commits, append_oid_to_argv, &cp.args);
1076 strvec_pushl(&cp.args, "--not", "--remotes", "-n", "1" , NULL);
1078 prepare_submodule_repo_env(&cp.env);
1079 cp.git_cmd = 1;
1080 cp.no_stdin = 1;
1081 cp.out = -1;
1082 cp.dir = path;
1083 if (start_command(&cp))
1084 die(_("Could not run 'git rev-list <commits> --not --remotes -n 1' command in submodule %s"),
1085 path);
1086 if (strbuf_read(&buf, cp.out, the_hash_algo->hexsz + 1))
1087 needs_pushing = 1;
1088 finish_command(&cp);
1089 close(cp.out);
1090 strbuf_release(&buf);
1091 return needs_pushing;
1094 return 0;
1097 int find_unpushed_submodules(struct repository *r,
1098 struct oid_array *commits,
1099 const char *remotes_name,
1100 struct string_list *needs_pushing)
1102 struct string_list submodules = STRING_LIST_INIT_DUP;
1103 struct string_list_item *name;
1104 struct strvec argv = STRVEC_INIT;
1106 /* argv.v[0] will be ignored by setup_revisions */
1107 strvec_push(&argv, "find_unpushed_submodules");
1108 oid_array_for_each_unique(commits, append_oid_to_argv, &argv);
1109 strvec_push(&argv, "--not");
1110 strvec_pushf(&argv, "--remotes=%s", remotes_name);
1112 collect_changed_submodules(r, &submodules, &argv);
1114 for_each_string_list_item(name, &submodules) {
1115 struct changed_submodule_data *cs_data = name->util;
1116 const struct submodule *submodule;
1117 const char *path = NULL;
1119 submodule = submodule_from_name(r, null_oid(), name->string);
1120 if (submodule)
1121 path = submodule->path;
1122 else
1123 path = default_name_or_path(name->string);
1125 if (!path)
1126 continue;
1128 if (submodule_needs_pushing(r, path, &cs_data->new_commits))
1129 string_list_insert(needs_pushing, path);
1132 free_submodules_data(&submodules);
1133 strvec_clear(&argv);
1135 return needs_pushing->nr;
1138 static int push_submodule(const char *path,
1139 const struct remote *remote,
1140 const struct refspec *rs,
1141 const struct string_list *push_options,
1142 int dry_run)
1144 if (validate_submodule_path(path) < 0)
1145 exit(128);
1147 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
1148 struct child_process cp = CHILD_PROCESS_INIT;
1149 strvec_push(&cp.args, "push");
1151 * When recursing into a submodule, treat any "only" configurations as "on-
1152 * demand", since "only" would not work (we need all submodules to be pushed
1153 * in order to be able to push the superproject).
1155 strvec_push(&cp.args, "--recurse-submodules=only-is-on-demand");
1156 if (dry_run)
1157 strvec_push(&cp.args, "--dry-run");
1159 if (push_options && push_options->nr) {
1160 const struct string_list_item *item;
1161 for_each_string_list_item(item, push_options)
1162 strvec_pushf(&cp.args, "--push-option=%s",
1163 item->string);
1166 if (remote->origin != REMOTE_UNCONFIGURED) {
1167 int i;
1168 strvec_push(&cp.args, remote->name);
1169 for (i = 0; i < rs->raw_nr; i++)
1170 strvec_push(&cp.args, rs->raw[i]);
1173 prepare_submodule_repo_env(&cp.env);
1174 cp.git_cmd = 1;
1175 cp.no_stdin = 1;
1176 cp.dir = path;
1177 if (run_command(&cp))
1178 return 0;
1179 close(cp.out);
1182 return 1;
1186 * Perform a check in the submodule to see if the remote and refspec work.
1187 * Die if the submodule can't be pushed.
1189 static void submodule_push_check(const char *path, const char *head,
1190 const struct remote *remote,
1191 const struct refspec *rs)
1193 struct child_process cp = CHILD_PROCESS_INIT;
1194 int i;
1196 if (validate_submodule_path(path) < 0)
1197 exit(128);
1199 strvec_push(&cp.args, "submodule--helper");
1200 strvec_push(&cp.args, "push-check");
1201 strvec_push(&cp.args, head);
1202 strvec_push(&cp.args, remote->name);
1204 for (i = 0; i < rs->raw_nr; i++)
1205 strvec_push(&cp.args, rs->raw[i]);
1207 prepare_submodule_repo_env(&cp.env);
1208 cp.git_cmd = 1;
1209 cp.no_stdin = 1;
1210 cp.no_stdout = 1;
1211 cp.dir = path;
1214 * Simply indicate if 'submodule--helper push-check' failed.
1215 * More detailed error information will be provided by the
1216 * child process.
1218 if (run_command(&cp))
1219 die(_("process for submodule '%s' failed"), path);
1222 int push_unpushed_submodules(struct repository *r,
1223 struct oid_array *commits,
1224 const struct remote *remote,
1225 const struct refspec *rs,
1226 const struct string_list *push_options,
1227 int dry_run)
1229 int i, ret = 1;
1230 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1232 if (!find_unpushed_submodules(r, commits,
1233 remote->name, &needs_pushing))
1234 return 1;
1237 * Verify that the remote and refspec can be propagated to all
1238 * submodules. This check can be skipped if the remote and refspec
1239 * won't be propagated due to the remote being unconfigured (e.g. a URL
1240 * instead of a remote name).
1242 if (remote->origin != REMOTE_UNCONFIGURED) {
1243 char *head;
1244 struct object_id head_oid;
1246 head = refs_resolve_refdup(get_main_ref_store(the_repository),
1247 "HEAD", 0, &head_oid, NULL);
1248 if (!head)
1249 die(_("Failed to resolve HEAD as a valid ref."));
1251 for (i = 0; i < needs_pushing.nr; i++)
1252 submodule_push_check(needs_pushing.items[i].string,
1253 head, remote, rs);
1254 free(head);
1257 /* Actually push the submodules */
1258 for (i = 0; i < needs_pushing.nr; i++) {
1259 const char *path = needs_pushing.items[i].string;
1260 fprintf(stderr, _("Pushing submodule '%s'\n"), path);
1261 if (!push_submodule(path, remote, rs,
1262 push_options, dry_run)) {
1263 fprintf(stderr, _("Unable to push submodule '%s'\n"), path);
1264 ret = 0;
1268 string_list_clear(&needs_pushing, 0);
1270 return ret;
1273 static int append_oid_to_array(const char *ref UNUSED,
1274 const struct object_id *oid,
1275 int flags UNUSED, void *data)
1277 struct oid_array *array = data;
1278 oid_array_append(array, oid);
1279 return 0;
1282 void check_for_new_submodule_commits(struct object_id *oid)
1284 if (!initialized_fetch_ref_tips) {
1285 refs_for_each_ref(get_main_ref_store(the_repository),
1286 append_oid_to_array, &ref_tips_before_fetch);
1287 initialized_fetch_ref_tips = 1;
1290 oid_array_append(&ref_tips_after_fetch, oid);
1294 * Returns 1 if there is at least one submodule gitdir in
1295 * $GIT_DIR/modules and 0 otherwise. This follows
1296 * submodule_name_to_gitdir(), which looks for submodules in
1297 * $GIT_DIR/modules, not $GIT_COMMON_DIR.
1299 * A submodule can be moved to $GIT_DIR/modules manually by running "git
1300 * submodule absorbgitdirs", or it may be initialized there by "git
1301 * submodule update".
1303 static int repo_has_absorbed_submodules(struct repository *r)
1305 int ret;
1306 struct strbuf buf = STRBUF_INIT;
1308 strbuf_repo_git_path(&buf, r, "modules/");
1309 ret = file_exists(buf.buf) && !is_empty_dir(buf.buf);
1310 strbuf_release(&buf);
1311 return ret;
1314 static void calculate_changed_submodule_paths(struct repository *r,
1315 struct string_list *changed_submodule_names)
1317 struct strvec argv = STRVEC_INIT;
1318 struct string_list_item *name;
1320 /* No need to check if no submodules would be fetched */
1321 if (!submodule_from_path(r, NULL, NULL) &&
1322 !repo_has_absorbed_submodules(r))
1323 return;
1325 strvec_push(&argv, "--"); /* argv[0] program name */
1326 oid_array_for_each_unique(&ref_tips_after_fetch,
1327 append_oid_to_argv, &argv);
1328 strvec_push(&argv, "--not");
1329 oid_array_for_each_unique(&ref_tips_before_fetch,
1330 append_oid_to_argv, &argv);
1333 * Collect all submodules (whether checked out or not) for which new
1334 * commits have been recorded upstream in "changed_submodule_names".
1336 collect_changed_submodules(r, changed_submodule_names, &argv);
1338 for_each_string_list_item(name, changed_submodule_names) {
1339 struct changed_submodule_data *cs_data = name->util;
1340 const struct submodule *submodule;
1341 const char *path = NULL;
1343 submodule = submodule_from_name(r, null_oid(), name->string);
1344 if (submodule)
1345 path = submodule->path;
1346 else
1347 path = default_name_or_path(name->string);
1349 if (!path)
1350 continue;
1352 if (submodule_has_commits(r, path, null_oid(), &cs_data->new_commits)) {
1353 changed_submodule_data_clear(cs_data);
1354 *name->string = '\0';
1358 string_list_remove_empty_items(changed_submodule_names, 1);
1360 strvec_clear(&argv);
1361 oid_array_clear(&ref_tips_before_fetch);
1362 oid_array_clear(&ref_tips_after_fetch);
1363 initialized_fetch_ref_tips = 0;
1366 int submodule_touches_in_range(struct repository *r,
1367 struct object_id *excl_oid,
1368 struct object_id *incl_oid)
1370 struct string_list subs = STRING_LIST_INIT_DUP;
1371 struct strvec args = STRVEC_INIT;
1372 int ret;
1374 /* No need to check if there are no submodules configured */
1375 if (!submodule_from_path(r, NULL, NULL))
1376 return 0;
1378 strvec_push(&args, "--"); /* args[0] program name */
1379 strvec_push(&args, oid_to_hex(incl_oid));
1380 if (!is_null_oid(excl_oid)) {
1381 strvec_push(&args, "--not");
1382 strvec_push(&args, oid_to_hex(excl_oid));
1385 collect_changed_submodules(r, &subs, &args);
1386 ret = subs.nr;
1388 strvec_clear(&args);
1390 free_submodules_data(&subs);
1391 return ret;
1394 struct submodule_parallel_fetch {
1396 * The index of the last index entry processed by
1397 * get_fetch_task_from_index().
1399 int index_count;
1401 * The index of the last string_list entry processed by
1402 * get_fetch_task_from_changed().
1404 int changed_count;
1405 struct strvec args;
1406 struct repository *r;
1407 const char *prefix;
1408 int command_line_option;
1409 int default_option;
1410 int quiet;
1411 int result;
1414 * Names of submodules that have new commits. Generated by
1415 * walking the newly fetched superproject commits.
1417 struct string_list changed_submodule_names;
1419 * Names of submodules that have already been processed. Lets us
1420 * avoid fetching the same submodule more than once.
1422 struct string_list seen_submodule_names;
1424 /* Pending fetches by OIDs */
1425 struct fetch_task **oid_fetch_tasks;
1426 int oid_fetch_tasks_nr, oid_fetch_tasks_alloc;
1428 struct strbuf submodules_with_errors;
1430 #define SPF_INIT { \
1431 .args = STRVEC_INIT, \
1432 .changed_submodule_names = STRING_LIST_INIT_DUP, \
1433 .seen_submodule_names = STRING_LIST_INIT_DUP, \
1434 .submodules_with_errors = STRBUF_INIT, \
1437 static int get_fetch_recurse_config(const struct submodule *submodule,
1438 struct submodule_parallel_fetch *spf)
1440 if (spf->command_line_option != RECURSE_SUBMODULES_DEFAULT)
1441 return spf->command_line_option;
1443 if (submodule) {
1444 char *key;
1445 const char *value;
1447 int fetch_recurse = submodule->fetch_recurse;
1448 key = xstrfmt("submodule.%s.fetchRecurseSubmodules", submodule->name);
1449 if (!repo_config_get_string_tmp(spf->r, key, &value)) {
1450 fetch_recurse = parse_fetch_recurse_submodules_arg(key, value);
1452 free(key);
1454 if (fetch_recurse != RECURSE_SUBMODULES_NONE)
1455 /* local config overrules everything except commandline */
1456 return fetch_recurse;
1459 return spf->default_option;
1463 * Fetch in progress (if callback data) or
1464 * pending (if in oid_fetch_tasks in struct submodule_parallel_fetch)
1466 struct fetch_task {
1467 struct repository *repo;
1468 const struct submodule *sub;
1469 unsigned free_sub : 1; /* Do we need to free the submodule? */
1470 const char *default_argv; /* The default fetch mode. */
1471 struct strvec git_args; /* Args for the child git process. */
1473 struct oid_array *commits; /* Ensure these commits are fetched */
1477 * When a submodule is not defined in .gitmodules, we cannot access it
1478 * via the regular submodule-config. Create a fake submodule, which we can
1479 * work on.
1481 static const struct submodule *get_non_gitmodules_submodule(const char *path)
1483 struct submodule *ret = NULL;
1484 const char *name = default_name_or_path(path);
1486 if (!name)
1487 return NULL;
1489 ret = xmalloc(sizeof(*ret));
1490 memset(ret, 0, sizeof(*ret));
1491 ret->path = name;
1492 ret->name = name;
1494 return (const struct submodule *) ret;
1497 static void fetch_task_release(struct fetch_task *p)
1499 if (p->free_sub)
1500 free((void*)p->sub);
1501 p->free_sub = 0;
1502 p->sub = NULL;
1504 if (p->repo)
1505 repo_clear(p->repo);
1506 FREE_AND_NULL(p->repo);
1508 strvec_clear(&p->git_args);
1511 static struct repository *get_submodule_repo_for(struct repository *r,
1512 const char *path,
1513 const struct object_id *treeish_name)
1515 struct repository *ret = xmalloc(sizeof(*ret));
1517 if (repo_submodule_init(ret, r, path, treeish_name)) {
1518 free(ret);
1519 return NULL;
1522 return ret;
1525 static struct fetch_task *fetch_task_create(struct submodule_parallel_fetch *spf,
1526 const char *path,
1527 const struct object_id *treeish_name)
1529 struct fetch_task *task = xmalloc(sizeof(*task));
1530 memset(task, 0, sizeof(*task));
1532 if (validate_submodule_path(path) < 0)
1533 exit(128);
1535 task->sub = submodule_from_path(spf->r, treeish_name, path);
1537 if (!task->sub) {
1539 * No entry in .gitmodules? Technically not a submodule,
1540 * but historically we supported repositories that happen to be
1541 * in-place where a gitlink is. Keep supporting them.
1543 task->sub = get_non_gitmodules_submodule(path);
1544 if (!task->sub)
1545 goto cleanup;
1547 task->free_sub = 1;
1550 if (string_list_lookup(&spf->seen_submodule_names, task->sub->name))
1551 goto cleanup;
1553 switch (get_fetch_recurse_config(task->sub, spf))
1555 default:
1556 case RECURSE_SUBMODULES_DEFAULT:
1557 case RECURSE_SUBMODULES_ON_DEMAND:
1558 if (!task->sub ||
1559 !string_list_lookup(
1560 &spf->changed_submodule_names,
1561 task->sub->name))
1562 goto cleanup;
1563 task->default_argv = "on-demand";
1564 break;
1565 case RECURSE_SUBMODULES_ON:
1566 task->default_argv = "yes";
1567 break;
1568 case RECURSE_SUBMODULES_OFF:
1569 goto cleanup;
1572 task->repo = get_submodule_repo_for(spf->r, path, treeish_name);
1574 return task;
1576 cleanup:
1577 fetch_task_release(task);
1578 free(task);
1579 return NULL;
1582 static struct fetch_task *
1583 get_fetch_task_from_index(struct submodule_parallel_fetch *spf,
1584 struct strbuf *err)
1586 for (; spf->index_count < spf->r->index->cache_nr; spf->index_count++) {
1587 const struct cache_entry *ce =
1588 spf->r->index->cache[spf->index_count];
1589 struct fetch_task *task;
1591 if (!S_ISGITLINK(ce->ce_mode))
1592 continue;
1594 task = fetch_task_create(spf, ce->name, null_oid());
1595 if (!task)
1596 continue;
1598 if (task->repo) {
1599 if (!spf->quiet)
1600 strbuf_addf(err, _("Fetching submodule %s%s\n"),
1601 spf->prefix, ce->name);
1603 spf->index_count++;
1604 return task;
1605 } else {
1606 struct strbuf empty_submodule_path = STRBUF_INIT;
1608 fetch_task_release(task);
1609 free(task);
1612 * An empty directory is normal,
1613 * the submodule is not initialized
1615 strbuf_addf(&empty_submodule_path, "%s/%s/",
1616 spf->r->worktree,
1617 ce->name);
1618 if (S_ISGITLINK(ce->ce_mode) &&
1619 !is_empty_dir(empty_submodule_path.buf)) {
1620 spf->result = 1;
1621 strbuf_addf(err,
1622 _("Could not access submodule '%s'\n"),
1623 ce->name);
1625 strbuf_release(&empty_submodule_path);
1628 return NULL;
1631 static struct fetch_task *
1632 get_fetch_task_from_changed(struct submodule_parallel_fetch *spf,
1633 struct strbuf *err)
1635 for (; spf->changed_count < spf->changed_submodule_names.nr;
1636 spf->changed_count++) {
1637 struct string_list_item item =
1638 spf->changed_submodule_names.items[spf->changed_count];
1639 struct changed_submodule_data *cs_data = item.util;
1640 struct fetch_task *task;
1642 if (!is_tree_submodule_active(spf->r, cs_data->super_oid,cs_data->path))
1643 continue;
1645 task = fetch_task_create(spf, cs_data->path,
1646 cs_data->super_oid);
1647 if (!task)
1648 continue;
1650 if (!task->repo) {
1651 strbuf_addf(err, _("Could not access submodule '%s' at commit %s\n"),
1652 cs_data->path,
1653 repo_find_unique_abbrev(the_repository, cs_data->super_oid, DEFAULT_ABBREV));
1655 fetch_task_release(task);
1656 free(task);
1657 continue;
1660 if (!spf->quiet)
1661 strbuf_addf(err,
1662 _("Fetching submodule %s%s at commit %s\n"),
1663 spf->prefix, task->sub->path,
1664 repo_find_unique_abbrev(the_repository, cs_data->super_oid,
1665 DEFAULT_ABBREV));
1667 spf->changed_count++;
1669 * NEEDSWORK: Submodules set/unset a value for
1670 * core.worktree when they are populated/unpopulated by
1671 * "git checkout" (and similar commands, see
1672 * submodule_move_head() and
1673 * connect_work_tree_and_git_dir()), but if the
1674 * submodule is unpopulated in another way (e.g. "git
1675 * rm", "rm -r"), core.worktree will still be set even
1676 * though the directory doesn't exist, and the child
1677 * process will crash while trying to chdir into the
1678 * nonexistent directory.
1680 * In this case, we know that the submodule has no
1681 * working tree, so we can work around this by
1682 * setting "--work-tree=." (--bare does not work because
1683 * worktree settings take precedence over bare-ness).
1684 * However, this is not necessarily true in other cases,
1685 * so a generalized solution is still necessary.
1687 * Possible solutions:
1688 * - teach "git [add|rm]" to unset core.worktree and
1689 * discourage users from removing submodules without
1690 * using a Git command.
1691 * - teach submodule child processes to ignore stale
1692 * core.worktree values.
1694 strvec_push(&task->git_args, "--work-tree=.");
1695 return task;
1697 return NULL;
1700 static int get_next_submodule(struct child_process *cp, struct strbuf *err,
1701 void *data, void **task_cb)
1703 struct submodule_parallel_fetch *spf = data;
1704 struct fetch_task *task =
1705 get_fetch_task_from_index(spf, err);
1706 if (!task)
1707 task = get_fetch_task_from_changed(spf, err);
1709 if (task) {
1710 child_process_init(cp);
1711 cp->dir = task->repo->gitdir;
1712 prepare_submodule_repo_env_in_gitdir(&cp->env);
1713 cp->git_cmd = 1;
1714 strvec_init(&cp->args);
1715 if (task->git_args.nr)
1716 strvec_pushv(&cp->args, task->git_args.v);
1717 strvec_pushv(&cp->args, spf->args.v);
1718 strvec_push(&cp->args, task->default_argv);
1719 strvec_pushf(&cp->args, "--submodule-prefix=%s%s/",
1720 spf->prefix, task->sub->path);
1722 *task_cb = task;
1724 string_list_insert(&spf->seen_submodule_names, task->sub->name);
1725 return 1;
1728 if (spf->oid_fetch_tasks_nr) {
1729 struct fetch_task *task =
1730 spf->oid_fetch_tasks[spf->oid_fetch_tasks_nr - 1];
1731 spf->oid_fetch_tasks_nr--;
1733 child_process_init(cp);
1734 prepare_submodule_repo_env_in_gitdir(&cp->env);
1735 cp->git_cmd = 1;
1736 cp->dir = task->repo->gitdir;
1738 strvec_init(&cp->args);
1739 strvec_pushv(&cp->args, spf->args.v);
1740 strvec_push(&cp->args, "on-demand");
1741 strvec_pushf(&cp->args, "--submodule-prefix=%s%s/",
1742 spf->prefix, task->sub->path);
1744 /* NEEDSWORK: have get_default_remote from submodule--helper */
1745 strvec_push(&cp->args, "origin");
1746 oid_array_for_each_unique(task->commits,
1747 append_oid_to_argv, &cp->args);
1749 *task_cb = task;
1750 return 1;
1753 return 0;
1756 static int fetch_start_failure(struct strbuf *err UNUSED,
1757 void *cb, void *task_cb)
1759 struct submodule_parallel_fetch *spf = cb;
1760 struct fetch_task *task = task_cb;
1762 spf->result = 1;
1764 fetch_task_release(task);
1765 return 0;
1768 static int commit_missing_in_sub(const struct object_id *oid, void *data)
1770 struct repository *subrepo = data;
1772 enum object_type type = oid_object_info(subrepo, oid, NULL);
1774 return type != OBJ_COMMIT;
1777 static int fetch_finish(int retvalue, struct strbuf *err UNUSED,
1778 void *cb, void *task_cb)
1780 struct submodule_parallel_fetch *spf = cb;
1781 struct fetch_task *task = task_cb;
1783 struct string_list_item *it;
1784 struct changed_submodule_data *cs_data;
1786 if (!task || !task->sub)
1787 BUG("callback cookie bogus");
1789 if (retvalue) {
1791 * NEEDSWORK: This indicates that the overall fetch
1792 * failed, even though there may be a subsequent fetch
1793 * by commit hash that might work. It may be a good
1794 * idea to not indicate failure in this case, and only
1795 * indicate failure if the subsequent fetch fails.
1797 spf->result = 1;
1799 strbuf_addf(&spf->submodules_with_errors, "\t%s\n",
1800 task->sub->name);
1803 /* Is this the second time we process this submodule? */
1804 if (task->commits)
1805 goto out;
1807 it = string_list_lookup(&spf->changed_submodule_names, task->sub->name);
1808 if (!it)
1809 /* Could be an unchanged submodule, not contained in the list */
1810 goto out;
1812 cs_data = it->util;
1813 oid_array_filter(&cs_data->new_commits,
1814 commit_missing_in_sub,
1815 task->repo);
1817 /* Are there commits we want, but do not exist? */
1818 if (cs_data->new_commits.nr) {
1819 task->commits = &cs_data->new_commits;
1820 ALLOC_GROW(spf->oid_fetch_tasks,
1821 spf->oid_fetch_tasks_nr + 1,
1822 spf->oid_fetch_tasks_alloc);
1823 spf->oid_fetch_tasks[spf->oid_fetch_tasks_nr] = task;
1824 spf->oid_fetch_tasks_nr++;
1825 return 0;
1828 out:
1829 fetch_task_release(task);
1831 return 0;
1834 int fetch_submodules(struct repository *r,
1835 const struct strvec *options,
1836 const char *prefix, int command_line_option,
1837 int default_option,
1838 int quiet, int max_parallel_jobs)
1840 int i;
1841 struct submodule_parallel_fetch spf = SPF_INIT;
1842 const struct run_process_parallel_opts opts = {
1843 .tr2_category = "submodule",
1844 .tr2_label = "parallel/fetch",
1846 .processes = max_parallel_jobs,
1848 .get_next_task = get_next_submodule,
1849 .start_failure = fetch_start_failure,
1850 .task_finished = fetch_finish,
1851 .data = &spf,
1854 spf.r = r;
1855 spf.command_line_option = command_line_option;
1856 spf.default_option = default_option;
1857 spf.quiet = quiet;
1858 spf.prefix = prefix;
1860 if (!r->worktree)
1861 goto out;
1863 if (repo_read_index(r) < 0)
1864 die(_("index file corrupt"));
1866 strvec_push(&spf.args, "fetch");
1867 for (i = 0; i < options->nr; i++)
1868 strvec_push(&spf.args, options->v[i]);
1869 strvec_push(&spf.args, "--recurse-submodules-default");
1870 /* default value, "--submodule-prefix" and its value are added later */
1872 calculate_changed_submodule_paths(r, &spf.changed_submodule_names);
1873 string_list_sort(&spf.changed_submodule_names);
1874 run_processes_parallel(&opts);
1876 if (spf.submodules_with_errors.len > 0)
1877 fprintf(stderr, _("Errors during submodule fetch:\n%s"),
1878 spf.submodules_with_errors.buf);
1881 strvec_clear(&spf.args);
1882 out:
1883 free_submodules_data(&spf.changed_submodule_names);
1884 return spf.result;
1887 unsigned is_submodule_modified(const char *path, int ignore_untracked)
1889 struct child_process cp = CHILD_PROCESS_INIT;
1890 struct strbuf buf = STRBUF_INIT;
1891 FILE *fp;
1892 unsigned dirty_submodule = 0;
1893 const char *git_dir;
1894 int ignore_cp_exit_code = 0;
1896 if (validate_submodule_path(path) < 0)
1897 exit(128);
1899 strbuf_addf(&buf, "%s/.git", path);
1900 git_dir = read_gitfile(buf.buf);
1901 if (!git_dir)
1902 git_dir = buf.buf;
1903 if (!is_git_directory(git_dir)) {
1904 if (is_directory(git_dir))
1905 die(_("'%s' not recognized as a git repository"), git_dir);
1906 strbuf_release(&buf);
1907 /* The submodule is not checked out, so it is not modified */
1908 return 0;
1910 strbuf_reset(&buf);
1912 strvec_pushl(&cp.args, "status", "--porcelain=2", NULL);
1913 if (ignore_untracked)
1914 strvec_push(&cp.args, "-uno");
1916 prepare_submodule_repo_env(&cp.env);
1917 cp.git_cmd = 1;
1918 cp.no_stdin = 1;
1919 cp.out = -1;
1920 cp.dir = path;
1921 if (start_command(&cp))
1922 die(_("Could not run 'git status --porcelain=2' in submodule %s"), path);
1924 fp = xfdopen(cp.out, "r");
1925 while (strbuf_getwholeline(&buf, fp, '\n') != EOF) {
1926 /* regular untracked files */
1927 if (buf.buf[0] == '?')
1928 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
1930 if (buf.buf[0] == 'u' ||
1931 buf.buf[0] == '1' ||
1932 buf.buf[0] == '2') {
1933 /* T = line type, XY = status, SSSS = submodule state */
1934 if (buf.len < strlen("T XY SSSS"))
1935 BUG("invalid status --porcelain=2 line %s",
1936 buf.buf);
1938 if (buf.buf[5] == 'S' && buf.buf[8] == 'U')
1939 /* nested untracked file */
1940 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
1942 if (buf.buf[0] == 'u' ||
1943 buf.buf[0] == '2' ||
1944 memcmp(buf.buf + 5, "S..U", 4))
1945 /* other change */
1946 dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
1949 if ((dirty_submodule & DIRTY_SUBMODULE_MODIFIED) &&
1950 ((dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ||
1951 ignore_untracked)) {
1953 * We're not interested in any further information from
1954 * the child any more, neither output nor its exit code.
1956 ignore_cp_exit_code = 1;
1957 break;
1960 fclose(fp);
1962 if (finish_command(&cp) && !ignore_cp_exit_code)
1963 die(_("'git status --porcelain=2' failed in submodule %s"), path);
1965 strbuf_release(&buf);
1966 return dirty_submodule;
1969 int submodule_uses_gitfile(const char *path)
1971 struct child_process cp = CHILD_PROCESS_INIT;
1972 struct strbuf buf = STRBUF_INIT;
1973 const char *git_dir;
1975 if (validate_submodule_path(path) < 0)
1976 exit(128);
1978 strbuf_addf(&buf, "%s/.git", path);
1979 git_dir = read_gitfile(buf.buf);
1980 if (!git_dir) {
1981 strbuf_release(&buf);
1982 return 0;
1984 strbuf_release(&buf);
1986 /* Now test that all nested submodules use a gitfile too */
1987 strvec_pushl(&cp.args,
1988 "submodule", "foreach", "--quiet", "--recursive",
1989 "test -f .git", NULL);
1991 prepare_submodule_repo_env(&cp.env);
1992 cp.git_cmd = 1;
1993 cp.no_stdin = 1;
1994 cp.no_stderr = 1;
1995 cp.no_stdout = 1;
1996 cp.dir = path;
1997 if (run_command(&cp))
1998 return 0;
2000 return 1;
2004 * Check if it is a bad idea to remove a submodule, i.e. if we'd lose data
2005 * when doing so.
2007 * Return 1 if we'd lose data, return 0 if the removal is fine,
2008 * and negative values for errors.
2010 int bad_to_remove_submodule(const char *path, unsigned flags)
2012 ssize_t len;
2013 struct child_process cp = CHILD_PROCESS_INIT;
2014 struct strbuf buf = STRBUF_INIT;
2015 int ret = 0;
2017 if (validate_submodule_path(path) < 0)
2018 exit(128);
2020 if (!file_exists(path) || is_empty_dir(path))
2021 return 0;
2023 if (!submodule_uses_gitfile(path))
2024 return 1;
2026 strvec_pushl(&cp.args, "status", "--porcelain",
2027 "--ignore-submodules=none", NULL);
2029 if (flags & SUBMODULE_REMOVAL_IGNORE_UNTRACKED)
2030 strvec_push(&cp.args, "-uno");
2031 else
2032 strvec_push(&cp.args, "-uall");
2034 if (!(flags & SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED))
2035 strvec_push(&cp.args, "--ignored");
2037 prepare_submodule_repo_env(&cp.env);
2038 cp.git_cmd = 1;
2039 cp.no_stdin = 1;
2040 cp.out = -1;
2041 cp.dir = path;
2042 if (start_command(&cp)) {
2043 if (flags & SUBMODULE_REMOVAL_DIE_ON_ERROR)
2044 die(_("could not start 'git status' in submodule '%s'"),
2045 path);
2046 ret = -1;
2047 goto out;
2050 len = strbuf_read(&buf, cp.out, 1024);
2051 if (len > 2)
2052 ret = 1;
2053 close(cp.out);
2055 if (finish_command(&cp)) {
2056 if (flags & SUBMODULE_REMOVAL_DIE_ON_ERROR)
2057 die(_("could not run 'git status' in submodule '%s'"),
2058 path);
2059 ret = -1;
2061 out:
2062 strbuf_release(&buf);
2063 return ret;
2066 void submodule_unset_core_worktree(const struct submodule *sub)
2068 struct strbuf config_path = STRBUF_INIT;
2070 if (validate_submodule_path(sub->path) < 0)
2071 exit(128);
2073 submodule_name_to_gitdir(&config_path, the_repository, sub->name);
2074 strbuf_addstr(&config_path, "/config");
2076 if (git_config_set_in_file_gently(config_path.buf, "core.worktree", NULL, NULL))
2077 warning(_("Could not unset core.worktree setting in submodule '%s'"),
2078 sub->path);
2080 strbuf_release(&config_path);
2083 static int submodule_has_dirty_index(const struct submodule *sub)
2085 struct child_process cp = CHILD_PROCESS_INIT;
2087 if (validate_submodule_path(sub->path) < 0)
2088 exit(128);
2090 prepare_submodule_repo_env(&cp.env);
2092 cp.git_cmd = 1;
2093 strvec_pushl(&cp.args, "diff-index", "--quiet",
2094 "--cached", "HEAD", NULL);
2095 cp.no_stdin = 1;
2096 cp.no_stdout = 1;
2097 cp.dir = sub->path;
2098 if (start_command(&cp))
2099 die(_("could not recurse into submodule '%s'"), sub->path);
2101 return finish_command(&cp);
2104 static void submodule_reset_index(const char *path, const char *super_prefix)
2106 struct child_process cp = CHILD_PROCESS_INIT;
2108 if (validate_submodule_path(path) < 0)
2109 exit(128);
2111 prepare_submodule_repo_env(&cp.env);
2113 cp.git_cmd = 1;
2114 cp.no_stdin = 1;
2115 cp.dir = path;
2117 /* TODO: determine if this might overwright untracked files */
2118 strvec_pushl(&cp.args, "read-tree", "-u", "--reset", NULL);
2119 strvec_pushf(&cp.args, "--super-prefix=%s%s/",
2120 (super_prefix ? super_prefix : ""), path);
2122 strvec_push(&cp.args, empty_tree_oid_hex());
2124 if (run_command(&cp))
2125 die(_("could not reset submodule index"));
2129 * Moves a submodule at a given path from a given head to another new head.
2130 * For edge cases (a submodule coming into existence or removing a submodule)
2131 * pass NULL for old or new respectively.
2133 int submodule_move_head(const char *path, const char *super_prefix,
2134 const char *old_head, const char *new_head,
2135 unsigned flags)
2137 int ret = 0;
2138 struct child_process cp = CHILD_PROCESS_INIT;
2139 const struct submodule *sub;
2140 int *error_code_ptr, error_code;
2142 if (!is_submodule_active(the_repository, path))
2143 return 0;
2145 if (flags & SUBMODULE_MOVE_HEAD_FORCE)
2147 * Pass non NULL pointer to is_submodule_populated_gently
2148 * to prevent die()-ing. We'll use connect_work_tree_and_git_dir
2149 * to fixup the submodule in the force case later.
2151 error_code_ptr = &error_code;
2152 else
2153 error_code_ptr = NULL;
2155 if (old_head && !is_submodule_populated_gently(path, error_code_ptr))
2156 return 0;
2158 sub = submodule_from_path(the_repository, null_oid(), path);
2160 if (!sub)
2161 BUG("could not get submodule information for '%s'", path);
2163 if (old_head && !(flags & SUBMODULE_MOVE_HEAD_FORCE)) {
2164 /* Check if the submodule has a dirty index. */
2165 if (submodule_has_dirty_index(sub))
2166 return error(_("submodule '%s' has dirty index"), path);
2169 if (!(flags & SUBMODULE_MOVE_HEAD_DRY_RUN)) {
2170 if (old_head) {
2171 if (!submodule_uses_gitfile(path))
2172 absorb_git_dir_into_superproject(path,
2173 super_prefix);
2174 else {
2175 char *dotgit = xstrfmt("%s/.git", path);
2176 char *git_dir = xstrdup(read_gitfile(dotgit));
2178 free(dotgit);
2179 if (validate_submodule_git_dir(git_dir,
2180 sub->name) < 0)
2181 die(_("refusing to create/use '%s' in "
2182 "another submodule's git dir"),
2183 git_dir);
2184 free(git_dir);
2186 } else {
2187 struct strbuf gitdir = STRBUF_INIT;
2188 submodule_name_to_gitdir(&gitdir, the_repository,
2189 sub->name);
2190 if (validate_submodule_git_dir(gitdir.buf,
2191 sub->name) < 0)
2192 die(_("refusing to create/use '%s' in another "
2193 "submodule's git dir"),
2194 gitdir.buf);
2195 connect_work_tree_and_git_dir(path, gitdir.buf, 0);
2196 strbuf_release(&gitdir);
2198 /* make sure the index is clean as well */
2199 submodule_reset_index(path, super_prefix);
2202 if (old_head && (flags & SUBMODULE_MOVE_HEAD_FORCE)) {
2203 struct strbuf gitdir = STRBUF_INIT;
2204 submodule_name_to_gitdir(&gitdir, the_repository,
2205 sub->name);
2206 connect_work_tree_and_git_dir(path, gitdir.buf, 1);
2207 strbuf_release(&gitdir);
2211 prepare_submodule_repo_env(&cp.env);
2213 cp.git_cmd = 1;
2214 cp.no_stdin = 1;
2215 cp.dir = path;
2217 strvec_pushl(&cp.args, "read-tree", "--recurse-submodules", NULL);
2218 strvec_pushf(&cp.args, "--super-prefix=%s%s/",
2219 (super_prefix ? super_prefix : ""), path);
2221 if (flags & SUBMODULE_MOVE_HEAD_DRY_RUN)
2222 strvec_push(&cp.args, "-n");
2223 else
2224 strvec_push(&cp.args, "-u");
2226 if (flags & SUBMODULE_MOVE_HEAD_FORCE)
2227 strvec_push(&cp.args, "--reset");
2228 else
2229 strvec_push(&cp.args, "-m");
2231 if (!(flags & SUBMODULE_MOVE_HEAD_FORCE))
2232 strvec_push(&cp.args, old_head ? old_head : empty_tree_oid_hex());
2234 strvec_push(&cp.args, new_head ? new_head : empty_tree_oid_hex());
2236 if (run_command(&cp)) {
2237 ret = error(_("Submodule '%s' could not be updated."), path);
2238 goto out;
2241 if (!(flags & SUBMODULE_MOVE_HEAD_DRY_RUN)) {
2242 if (new_head) {
2243 child_process_init(&cp);
2244 /* also set the HEAD accordingly */
2245 cp.git_cmd = 1;
2246 cp.no_stdin = 1;
2247 cp.dir = path;
2249 prepare_submodule_repo_env(&cp.env);
2250 strvec_pushl(&cp.args, "update-ref", "HEAD",
2251 "--no-deref", new_head, NULL);
2253 if (run_command(&cp)) {
2254 ret = -1;
2255 goto out;
2257 } else {
2258 struct strbuf sb = STRBUF_INIT;
2260 strbuf_addf(&sb, "%s/.git", path);
2261 unlink_or_warn(sb.buf);
2262 strbuf_release(&sb);
2264 if (is_empty_dir(path))
2265 rmdir_or_warn(path);
2267 submodule_unset_core_worktree(sub);
2270 out:
2271 return ret;
2274 int validate_submodule_git_dir(char *git_dir, const char *submodule_name)
2276 size_t len = strlen(git_dir), suffix_len = strlen(submodule_name);
2277 char *p;
2278 int ret = 0;
2280 if (len <= suffix_len || (p = git_dir + len - suffix_len)[-1] != '/' ||
2281 strcmp(p, submodule_name))
2282 BUG("submodule name '%s' not a suffix of git dir '%s'",
2283 submodule_name, git_dir);
2286 * We prevent the contents of sibling submodules' git directories to
2287 * clash.
2289 * Example: having a submodule named `hippo` and another one named
2290 * `hippo/hooks` would result in the git directories
2291 * `.git/modules/hippo/` and `.git/modules/hippo/hooks/`, respectively,
2292 * but the latter directory is already designated to contain the hooks
2293 * of the former.
2295 for (; *p; p++) {
2296 if (is_dir_sep(*p)) {
2297 char c = *p;
2299 *p = '\0';
2300 if (is_git_directory(git_dir))
2301 ret = -1;
2302 *p = c;
2304 if (ret < 0)
2305 return error(_("submodule git dir '%s' is "
2306 "inside git dir '%.*s'"),
2307 git_dir,
2308 (int)(p - git_dir), git_dir);
2312 return 0;
2315 int validate_submodule_path(const char *path)
2317 char *p = xstrdup(path);
2318 struct stat st;
2319 int i, ret = 0;
2320 char sep;
2322 for (i = 0; !ret && p[i]; i++) {
2323 if (!is_dir_sep(p[i]))
2324 continue;
2326 sep = p[i];
2327 p[i] = '\0';
2328 /* allow missing components, but no symlinks */
2329 ret = lstat(p, &st) || !S_ISLNK(st.st_mode) ? 0 : -1;
2330 p[i] = sep;
2331 if (ret)
2332 error(_("expected '%.*s' in submodule path '%s' not to "
2333 "be a symbolic link"), i, p, p);
2335 if (!lstat(p, &st) && S_ISLNK(st.st_mode))
2336 ret = error(_("expected submodule path '%s' not to be a "
2337 "symbolic link"), p);
2338 free(p);
2339 return ret;
2344 * Embeds a single submodules git directory into the superprojects git dir,
2345 * non recursively.
2347 static void relocate_single_git_dir_into_superproject(const char *path,
2348 const char *super_prefix)
2350 char *old_git_dir = NULL, *real_old_git_dir = NULL, *real_new_git_dir = NULL;
2351 struct strbuf new_gitdir = STRBUF_INIT;
2352 const struct submodule *sub;
2354 if (validate_submodule_path(path) < 0)
2355 exit(128);
2357 if (submodule_uses_worktrees(path))
2358 die(_("relocate_gitdir for submodule '%s' with "
2359 "more than one worktree not supported"), path);
2361 old_git_dir = xstrfmt("%s/.git", path);
2362 if (read_gitfile(old_git_dir))
2363 /* If it is an actual gitfile, it doesn't need migration. */
2364 return;
2366 real_old_git_dir = real_pathdup(old_git_dir, 1);
2368 sub = submodule_from_path(the_repository, null_oid(), path);
2369 if (!sub)
2370 die(_("could not lookup name for submodule '%s'"), path);
2372 submodule_name_to_gitdir(&new_gitdir, the_repository, sub->name);
2373 if (validate_submodule_git_dir(new_gitdir.buf, sub->name) < 0)
2374 die(_("refusing to move '%s' into an existing git dir"),
2375 real_old_git_dir);
2376 if (safe_create_leading_directories_const(new_gitdir.buf) < 0)
2377 die(_("could not create directory '%s'"), new_gitdir.buf);
2378 real_new_git_dir = real_pathdup(new_gitdir.buf, 1);
2380 fprintf(stderr, _("Migrating git directory of '%s%s' from\n'%s' to\n'%s'\n"),
2381 super_prefix ? super_prefix : "", path,
2382 real_old_git_dir, real_new_git_dir);
2384 relocate_gitdir(path, real_old_git_dir, real_new_git_dir);
2386 free(old_git_dir);
2387 free(real_old_git_dir);
2388 free(real_new_git_dir);
2389 strbuf_release(&new_gitdir);
2392 static void absorb_git_dir_into_superproject_recurse(const char *path,
2393 const char *super_prefix)
2396 struct child_process cp = CHILD_PROCESS_INIT;
2398 if (validate_submodule_path(path) < 0)
2399 exit(128);
2401 cp.dir = path;
2402 cp.git_cmd = 1;
2403 cp.no_stdin = 1;
2404 strvec_pushl(&cp.args, "submodule--helper",
2405 "absorbgitdirs", NULL);
2406 strvec_pushf(&cp.args, "--super-prefix=%s%s/", super_prefix ?
2407 super_prefix : "", path);
2409 prepare_submodule_repo_env(&cp.env);
2410 if (run_command(&cp))
2411 die(_("could not recurse into submodule '%s'"), path);
2415 * Migrate the git directory of the submodule given by path from
2416 * having its git directory within the working tree to the git dir nested
2417 * in its superprojects git dir under modules/.
2419 void absorb_git_dir_into_superproject(const char *path,
2420 const char *super_prefix)
2422 int err_code;
2423 const char *sub_git_dir;
2424 struct strbuf gitdir = STRBUF_INIT;
2426 if (validate_submodule_path(path) < 0)
2427 exit(128);
2429 strbuf_addf(&gitdir, "%s/.git", path);
2430 sub_git_dir = resolve_gitdir_gently(gitdir.buf, &err_code);
2432 /* Not populated? */
2433 if (!sub_git_dir) {
2434 const struct submodule *sub;
2435 struct strbuf sub_gitdir = STRBUF_INIT;
2437 if (err_code == READ_GITFILE_ERR_STAT_FAILED) {
2438 /* unpopulated as expected */
2439 strbuf_release(&gitdir);
2440 return;
2443 if (err_code != READ_GITFILE_ERR_NOT_A_REPO)
2444 /* We don't know what broke here. */
2445 read_gitfile_error_die(err_code, path, NULL);
2448 * Maybe populated, but no git directory was found?
2449 * This can happen if the superproject is a submodule
2450 * itself and was just absorbed. The absorption of the
2451 * superproject did not rewrite the git file links yet,
2452 * fix it now.
2454 sub = submodule_from_path(the_repository, null_oid(), path);
2455 if (!sub)
2456 die(_("could not lookup name for submodule '%s'"), path);
2457 submodule_name_to_gitdir(&sub_gitdir, the_repository, sub->name);
2458 connect_work_tree_and_git_dir(path, sub_gitdir.buf, 0);
2459 strbuf_release(&sub_gitdir);
2460 } else {
2461 /* Is it already absorbed into the superprojects git dir? */
2462 char *real_sub_git_dir = real_pathdup(sub_git_dir, 1);
2463 char *real_common_git_dir = real_pathdup(get_git_common_dir(), 1);
2465 if (!starts_with(real_sub_git_dir, real_common_git_dir))
2466 relocate_single_git_dir_into_superproject(path, super_prefix);
2468 free(real_sub_git_dir);
2469 free(real_common_git_dir);
2471 strbuf_release(&gitdir);
2473 absorb_git_dir_into_superproject_recurse(path, super_prefix);
2476 int get_superproject_working_tree(struct strbuf *buf)
2478 struct child_process cp = CHILD_PROCESS_INIT;
2479 struct strbuf sb = STRBUF_INIT;
2480 struct strbuf one_up = STRBUF_INIT;
2481 char *cwd = xgetcwd();
2482 int ret = 0;
2483 const char *subpath;
2484 int code;
2485 ssize_t len;
2487 if (!is_inside_work_tree())
2489 * FIXME:
2490 * We might have a superproject, but it is harder
2491 * to determine.
2493 return 0;
2495 if (!strbuf_realpath(&one_up, "../", 0))
2496 return 0;
2498 subpath = relative_path(cwd, one_up.buf, &sb);
2499 strbuf_release(&one_up);
2501 prepare_submodule_repo_env(&cp.env);
2502 strvec_pop(&cp.env);
2504 strvec_pushl(&cp.args, "--literal-pathspecs", "-C", "..",
2505 "ls-files", "-z", "--stage", "--full-name", "--",
2506 subpath, NULL);
2507 strbuf_reset(&sb);
2509 cp.no_stdin = 1;
2510 cp.no_stderr = 1;
2511 cp.out = -1;
2512 cp.git_cmd = 1;
2514 if (start_command(&cp))
2515 die(_("could not start ls-files in .."));
2517 len = strbuf_read(&sb, cp.out, PATH_MAX);
2518 close(cp.out);
2520 if (starts_with(sb.buf, "160000")) {
2521 int super_sub_len;
2522 int cwd_len = strlen(cwd);
2523 char *super_sub, *super_wt;
2526 * There is a superproject having this repo as a submodule.
2527 * The format is <mode> SP <hash> SP <stage> TAB <full name> \0,
2528 * We're only interested in the name after the tab.
2530 super_sub = strchr(sb.buf, '\t') + 1;
2531 super_sub_len = strlen(super_sub);
2533 if (super_sub_len > cwd_len ||
2534 strcmp(&cwd[cwd_len - super_sub_len], super_sub))
2535 BUG("returned path string doesn't match cwd?");
2537 super_wt = xstrdup(cwd);
2538 super_wt[cwd_len - super_sub_len] = '\0';
2540 strbuf_realpath(buf, super_wt, 1);
2541 ret = 1;
2542 free(super_wt);
2544 free(cwd);
2545 strbuf_release(&sb);
2547 code = finish_command(&cp);
2549 if (code == 128)
2550 /* '../' is not a git repository */
2551 return 0;
2552 if (code == 0 && len == 0)
2553 /* There is an unrelated git repository at '../' */
2554 return 0;
2555 if (code)
2556 die(_("ls-tree returned unexpected return code %d"), code);
2558 return ret;
2562 * Put the gitdir for a submodule (given relative to the main
2563 * repository worktree) into `buf`, or return -1 on error.
2565 int submodule_to_gitdir(struct strbuf *buf, const char *submodule)
2567 const struct submodule *sub;
2568 const char *git_dir;
2569 int ret = 0;
2571 if (validate_submodule_path(submodule) < 0)
2572 exit(128);
2574 strbuf_reset(buf);
2575 strbuf_addstr(buf, submodule);
2576 strbuf_complete(buf, '/');
2577 strbuf_addstr(buf, ".git");
2579 git_dir = read_gitfile(buf->buf);
2580 if (git_dir) {
2581 strbuf_reset(buf);
2582 strbuf_addstr(buf, git_dir);
2584 if (!is_git_directory(buf->buf)) {
2585 sub = submodule_from_path(the_repository, null_oid(),
2586 submodule);
2587 if (!sub) {
2588 ret = -1;
2589 goto cleanup;
2591 strbuf_reset(buf);
2592 submodule_name_to_gitdir(buf, the_repository, sub->name);
2595 cleanup:
2596 return ret;
2599 void submodule_name_to_gitdir(struct strbuf *buf, struct repository *r,
2600 const char *submodule_name)
2603 * NEEDSWORK: The current way of mapping a submodule's name to
2604 * its location in .git/modules/ has problems with some naming
2605 * schemes. For example, if a submodule is named "foo" and
2606 * another is named "foo/bar" (whether present in the same
2607 * superproject commit or not - the problem will arise if both
2608 * superproject commits have been checked out at any point in
2609 * time), or if two submodule names only have different cases in
2610 * a case-insensitive filesystem.
2612 * There are several solutions, including encoding the path in
2613 * some way, introducing a submodule.<name>.gitdir config in
2614 * .git/config (not .gitmodules) that allows overriding what the
2615 * gitdir of a submodule would be (and teach Git, upon noticing
2616 * a clash, to automatically determine a non-clashing name and
2617 * to write such a config), or introducing a
2618 * submodule.<name>.gitdir config in .gitmodules that repo
2619 * administrators can explicitly set. Nothing has been decided,
2620 * so for now, just append the name at the end of the path.
2622 strbuf_repo_git_path(buf, r, "modules/");
2623 strbuf_addstr(buf, submodule_name);