Merge branch 'ab/valgrind-fixes' into maint
[git/debian.git] / submodule.c
blob86c8f0f89dbdb0e82cb18054392a884c70a71811
2 #include "cache.h"
3 #include "repository.h"
4 #include "config.h"
5 #include "submodule-config.h"
6 #include "submodule.h"
7 #include "dir.h"
8 #include "diff.h"
9 #include "commit.h"
10 #include "revision.h"
11 #include "run-command.h"
12 #include "diffcore.h"
13 #include "refs.h"
14 #include "string-list.h"
15 #include "oid-array.h"
16 #include "strvec.h"
17 #include "blob.h"
18 #include "thread-utils.h"
19 #include "quote.h"
20 #include "remote.h"
21 #include "worktree.h"
22 #include "parse-options.h"
23 #include "object-store.h"
24 #include "commit-reach.h"
25 #include "shallow.h"
27 static int config_update_recurse_submodules = RECURSE_SUBMODULES_OFF;
28 static int initialized_fetch_ref_tips;
29 static struct oid_array ref_tips_before_fetch;
30 static struct oid_array ref_tips_after_fetch;
33 * Check if the .gitmodules file is unmerged. Parsing of the .gitmodules file
34 * will be disabled because we can't guess what might be configured in
35 * .gitmodules unless the user resolves the conflict.
37 int is_gitmodules_unmerged(struct index_state *istate)
39 int pos = index_name_pos(istate, GITMODULES_FILE, strlen(GITMODULES_FILE));
40 if (pos < 0) { /* .gitmodules not found or isn't merged */
41 pos = -1 - pos;
42 if (istate->cache_nr > pos) { /* there is a .gitmodules */
43 const struct cache_entry *ce = istate->cache[pos];
44 if (ce_namelen(ce) == strlen(GITMODULES_FILE) &&
45 !strcmp(ce->name, GITMODULES_FILE))
46 return 1;
50 return 0;
54 * Check if the .gitmodules file is safe to write.
56 * Writing to the .gitmodules file requires that the file exists in the
57 * working tree or, if it doesn't, that a brand new .gitmodules file is going
58 * to be created (i.e. it's neither in the index nor in the current branch).
60 * It is not safe to write to .gitmodules if it's not in the working tree but
61 * it is in the index or in the current branch, because writing new values
62 * (and staging them) would blindly overwrite ALL the old content.
64 int is_writing_gitmodules_ok(void)
66 struct object_id oid;
67 return file_exists(GITMODULES_FILE) ||
68 (get_oid(GITMODULES_INDEX, &oid) < 0 && get_oid(GITMODULES_HEAD, &oid) < 0);
72 * Check if the .gitmodules file has unstaged modifications. This must be
73 * checked before allowing modifications to the .gitmodules file with the
74 * intention to stage them later, because when continuing we would stage the
75 * modifications the user didn't stage herself too. That might change in a
76 * future version when we learn to stage the changes we do ourselves without
77 * staging any previous modifications.
79 int is_staging_gitmodules_ok(struct index_state *istate)
81 int pos = index_name_pos(istate, GITMODULES_FILE, strlen(GITMODULES_FILE));
83 if ((pos >= 0) && (pos < istate->cache_nr)) {
84 struct stat st;
85 if (lstat(GITMODULES_FILE, &st) == 0 &&
86 ie_modified(istate, istate->cache[pos], &st, 0) & DATA_CHANGED)
87 return 0;
90 return 1;
93 static int for_each_remote_ref_submodule(const char *submodule,
94 each_ref_fn fn, void *cb_data)
96 return refs_for_each_remote_ref(get_submodule_ref_store(submodule),
97 fn, cb_data);
101 * Try to update the "path" entry in the "submodule.<name>" section of the
102 * .gitmodules file. Return 0 only if a .gitmodules file was found, a section
103 * with the correct path=<oldpath> setting was found and we could update it.
105 int update_path_in_gitmodules(const char *oldpath, const char *newpath)
107 struct strbuf entry = STRBUF_INIT;
108 const struct submodule *submodule;
109 int ret;
111 if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */
112 return -1;
114 if (is_gitmodules_unmerged(the_repository->index))
115 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
117 submodule = submodule_from_path(the_repository, null_oid(), oldpath);
118 if (!submodule || !submodule->name) {
119 warning(_("Could not find section in .gitmodules where path=%s"), oldpath);
120 return -1;
122 strbuf_addstr(&entry, "submodule.");
123 strbuf_addstr(&entry, submodule->name);
124 strbuf_addstr(&entry, ".path");
125 ret = config_set_in_gitmodules_file_gently(entry.buf, newpath);
126 strbuf_release(&entry);
127 return ret;
131 * Try to remove the "submodule.<name>" section from .gitmodules where the given
132 * path is configured. Return 0 only if a .gitmodules file was found, a section
133 * with the correct path=<path> setting was found and we could remove it.
135 int remove_path_from_gitmodules(const char *path)
137 struct strbuf sect = STRBUF_INIT;
138 const struct submodule *submodule;
140 if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */
141 return -1;
143 if (is_gitmodules_unmerged(the_repository->index))
144 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
146 submodule = submodule_from_path(the_repository, null_oid(), path);
147 if (!submodule || !submodule->name) {
148 warning(_("Could not find section in .gitmodules where path=%s"), path);
149 return -1;
151 strbuf_addstr(&sect, "submodule.");
152 strbuf_addstr(&sect, submodule->name);
153 if (git_config_rename_section_in_file(GITMODULES_FILE, sect.buf, NULL) < 0) {
154 /* Maybe the user already did that, don't error out here */
155 warning(_("Could not remove .gitmodules entry for %s"), path);
156 strbuf_release(&sect);
157 return -1;
159 strbuf_release(&sect);
160 return 0;
163 void stage_updated_gitmodules(struct index_state *istate)
165 if (add_file_to_index(istate, GITMODULES_FILE, 0))
166 die(_("staging updated .gitmodules failed"));
169 static struct string_list added_submodule_odb_paths = STRING_LIST_INIT_NODUP;
171 void add_submodule_odb_by_path(const char *path)
173 string_list_insert(&added_submodule_odb_paths, xstrdup(path));
176 int register_all_submodule_odb_as_alternates(void)
178 int i;
179 int ret = added_submodule_odb_paths.nr;
181 for (i = 0; i < added_submodule_odb_paths.nr; i++)
182 add_to_alternates_memory(added_submodule_odb_paths.items[i].string);
183 if (ret) {
184 string_list_clear(&added_submodule_odb_paths, 0);
185 trace2_data_intmax("submodule", the_repository,
186 "register_all_submodule_odb_as_alternates/registered", ret);
187 if (git_env_bool("GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB", 0))
188 BUG("register_all_submodule_odb_as_alternates() called");
190 return ret;
193 void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
194 const char *path)
196 const struct submodule *submodule = submodule_from_path(the_repository,
197 null_oid(),
198 path);
199 if (submodule) {
200 const char *ignore;
201 char *key;
203 key = xstrfmt("submodule.%s.ignore", submodule->name);
204 if (repo_config_get_string_tmp(the_repository, key, &ignore))
205 ignore = submodule->ignore;
206 free(key);
208 if (ignore)
209 handle_ignore_submodules_arg(diffopt, ignore);
210 else if (is_gitmodules_unmerged(the_repository->index))
211 diffopt->flags.ignore_submodules = 1;
215 /* Cheap function that only determines if we're interested in submodules at all */
216 int git_default_submodule_config(const char *var, const char *value, void *cb)
218 if (!strcmp(var, "submodule.recurse")) {
219 int v = git_config_bool(var, value) ?
220 RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF;
221 config_update_recurse_submodules = v;
223 return 0;
226 int option_parse_recurse_submodules_worktree_updater(const struct option *opt,
227 const char *arg, int unset)
229 if (unset) {
230 config_update_recurse_submodules = RECURSE_SUBMODULES_OFF;
231 return 0;
233 if (arg)
234 config_update_recurse_submodules =
235 parse_update_recurse_submodules_arg(opt->long_name,
236 arg);
237 else
238 config_update_recurse_submodules = RECURSE_SUBMODULES_ON;
240 return 0;
244 * Determine if a submodule has been initialized at a given 'path'
247 * NEEDSWORK: Emit a warning if submodule.active exists, but is valueless,
248 * ie, the config looks like: "[submodule] active\n".
249 * Since that is an invalid pathspec, we should inform the user.
251 int is_tree_submodule_active(struct repository *repo,
252 const struct object_id *treeish_name,
253 const char *path)
255 int ret = 0;
256 char *key = NULL;
257 char *value = NULL;
258 const struct string_list *sl;
259 const struct submodule *module;
261 module = submodule_from_path(repo, treeish_name, path);
263 /* early return if there isn't a path->module mapping */
264 if (!module)
265 return 0;
267 /* submodule.<name>.active is set */
268 key = xstrfmt("submodule.%s.active", module->name);
269 if (!repo_config_get_bool(repo, key, &ret)) {
270 free(key);
271 return ret;
273 free(key);
275 /* submodule.active is set */
276 sl = repo_config_get_value_multi(repo, "submodule.active");
277 if (sl) {
278 struct pathspec ps;
279 struct strvec args = STRVEC_INIT;
280 const struct string_list_item *item;
282 for_each_string_list_item(item, sl) {
283 strvec_push(&args, item->string);
286 parse_pathspec(&ps, 0, 0, NULL, args.v);
287 ret = match_pathspec(repo->index, &ps, path, strlen(path), 0, NULL, 1);
289 strvec_clear(&args);
290 clear_pathspec(&ps);
291 return ret;
294 /* fallback to checking if the URL is set */
295 key = xstrfmt("submodule.%s.url", module->name);
296 ret = !repo_config_get_string(repo, key, &value);
298 free(value);
299 free(key);
300 return ret;
303 int is_submodule_active(struct repository *repo, const char *path)
305 return is_tree_submodule_active(repo, null_oid(), path);
308 int is_submodule_populated_gently(const char *path, int *return_error_code)
310 int ret = 0;
311 char *gitdir = xstrfmt("%s/.git", path);
313 if (resolve_gitdir_gently(gitdir, return_error_code))
314 ret = 1;
316 free(gitdir);
317 return ret;
321 * Dies if the provided 'prefix' corresponds to an unpopulated submodule
323 void die_in_unpopulated_submodule(struct index_state *istate,
324 const char *prefix)
326 int i, prefixlen;
328 if (!prefix)
329 return;
331 prefixlen = strlen(prefix);
333 for (i = 0; i < istate->cache_nr; i++) {
334 struct cache_entry *ce = istate->cache[i];
335 int ce_len = ce_namelen(ce);
337 if (!S_ISGITLINK(ce->ce_mode))
338 continue;
339 if (prefixlen <= ce_len)
340 continue;
341 if (strncmp(ce->name, prefix, ce_len))
342 continue;
343 if (prefix[ce_len] != '/')
344 continue;
346 die(_("in unpopulated submodule '%s'"), ce->name);
351 * Dies if any paths in the provided pathspec descends into a submodule
353 void die_path_inside_submodule(struct index_state *istate,
354 const struct pathspec *ps)
356 int i, j;
358 for (i = 0; i < istate->cache_nr; i++) {
359 struct cache_entry *ce = istate->cache[i];
360 int ce_len = ce_namelen(ce);
362 if (!S_ISGITLINK(ce->ce_mode))
363 continue;
365 for (j = 0; j < ps->nr ; j++) {
366 const struct pathspec_item *item = &ps->items[j];
368 if (item->len <= ce_len)
369 continue;
370 if (item->match[ce_len] != '/')
371 continue;
372 if (strncmp(ce->name, item->match, ce_len))
373 continue;
374 if (item->len == ce_len + 1)
375 continue;
377 die(_("Pathspec '%s' is in submodule '%.*s'"),
378 item->original, ce_len, ce->name);
383 enum submodule_update_type parse_submodule_update_type(const char *value)
385 if (!strcmp(value, "none"))
386 return SM_UPDATE_NONE;
387 else if (!strcmp(value, "checkout"))
388 return SM_UPDATE_CHECKOUT;
389 else if (!strcmp(value, "rebase"))
390 return SM_UPDATE_REBASE;
391 else if (!strcmp(value, "merge"))
392 return SM_UPDATE_MERGE;
393 else if (*value == '!')
394 return SM_UPDATE_COMMAND;
395 else
396 return SM_UPDATE_UNSPECIFIED;
399 int parse_submodule_update_strategy(const char *value,
400 struct submodule_update_strategy *dst)
402 enum submodule_update_type type;
404 free((void*)dst->command);
405 dst->command = NULL;
407 type = parse_submodule_update_type(value);
408 if (type == SM_UPDATE_UNSPECIFIED)
409 return -1;
411 dst->type = type;
412 if (type == SM_UPDATE_COMMAND)
413 dst->command = xstrdup(value + 1);
415 return 0;
418 const char *submodule_strategy_to_string(const struct submodule_update_strategy *s)
420 struct strbuf sb = STRBUF_INIT;
421 switch (s->type) {
422 case SM_UPDATE_CHECKOUT:
423 return "checkout";
424 case SM_UPDATE_MERGE:
425 return "merge";
426 case SM_UPDATE_REBASE:
427 return "rebase";
428 case SM_UPDATE_NONE:
429 return "none";
430 case SM_UPDATE_UNSPECIFIED:
431 return NULL;
432 case SM_UPDATE_COMMAND:
433 strbuf_addf(&sb, "!%s", s->command);
434 return strbuf_detach(&sb, NULL);
436 return NULL;
439 void handle_ignore_submodules_arg(struct diff_options *diffopt,
440 const char *arg)
442 diffopt->flags.ignore_submodule_set = 1;
443 diffopt->flags.ignore_submodules = 0;
444 diffopt->flags.ignore_untracked_in_submodules = 0;
445 diffopt->flags.ignore_dirty_submodules = 0;
447 if (!strcmp(arg, "all"))
448 diffopt->flags.ignore_submodules = 1;
449 else if (!strcmp(arg, "untracked"))
450 diffopt->flags.ignore_untracked_in_submodules = 1;
451 else if (!strcmp(arg, "dirty"))
452 diffopt->flags.ignore_dirty_submodules = 1;
453 else if (strcmp(arg, "none"))
454 die(_("bad --ignore-submodules argument: %s"), arg);
456 * Please update _git_status() in git-completion.bash when you
457 * add new options
461 static int prepare_submodule_diff_summary(struct repository *r, struct rev_info *rev,
462 const char *path,
463 struct commit *left, struct commit *right,
464 struct commit_list *merge_bases)
466 struct commit_list *list;
468 repo_init_revisions(r, rev, NULL);
469 setup_revisions(0, NULL, rev, NULL);
470 rev->left_right = 1;
471 rev->first_parent_only = 1;
472 left->object.flags |= SYMMETRIC_LEFT;
473 add_pending_object(rev, &left->object, path);
474 add_pending_object(rev, &right->object, path);
475 for (list = merge_bases; list; list = list->next) {
476 list->item->object.flags |= UNINTERESTING;
477 add_pending_object(rev, &list->item->object,
478 oid_to_hex(&list->item->object.oid));
480 return prepare_revision_walk(rev);
483 static void print_submodule_diff_summary(struct repository *r, struct rev_info *rev, struct diff_options *o)
485 static const char format[] = " %m %s";
486 struct strbuf sb = STRBUF_INIT;
487 struct commit *commit;
489 while ((commit = get_revision(rev))) {
490 struct pretty_print_context ctx = {0};
491 ctx.date_mode = rev->date_mode;
492 ctx.output_encoding = get_log_output_encoding();
493 strbuf_setlen(&sb, 0);
494 repo_format_commit_message(r, commit, format, &sb,
495 &ctx);
496 strbuf_addch(&sb, '\n');
497 if (commit->object.flags & SYMMETRIC_LEFT)
498 diff_emit_submodule_del(o, sb.buf);
499 else
500 diff_emit_submodule_add(o, sb.buf);
502 strbuf_release(&sb);
505 void prepare_submodule_repo_env(struct strvec *out)
507 prepare_other_repo_env(out, DEFAULT_GIT_DIR_ENVIRONMENT);
510 static void prepare_submodule_repo_env_in_gitdir(struct strvec *out)
512 prepare_other_repo_env(out, ".");
516 * Initialize a repository struct for a submodule based on the provided 'path'.
518 * Returns the repository struct on success,
519 * NULL when the submodule is not present.
521 static struct repository *open_submodule(const char *path)
523 struct strbuf sb = STRBUF_INIT;
524 struct repository *out = xmalloc(sizeof(*out));
526 if (submodule_to_gitdir(&sb, path) || repo_init(out, sb.buf, NULL)) {
527 strbuf_release(&sb);
528 free(out);
529 return NULL;
532 /* Mark it as a submodule */
533 out->submodule_prefix = xstrdup(path);
535 strbuf_release(&sb);
536 return out;
540 * Helper function to display the submodule header line prior to the full
541 * summary output.
543 * If it can locate the submodule git directory it will create a repository
544 * handle for the submodule and lookup both the left and right commits and
545 * put them into the left and right pointers.
547 static void show_submodule_header(struct diff_options *o,
548 const char *path,
549 struct object_id *one, struct object_id *two,
550 unsigned dirty_submodule,
551 struct repository *sub,
552 struct commit **left, struct commit **right,
553 struct commit_list **merge_bases)
555 const char *message = NULL;
556 struct strbuf sb = STRBUF_INIT;
557 int fast_forward = 0, fast_backward = 0;
559 if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
560 diff_emit_submodule_untracked(o, path);
562 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
563 diff_emit_submodule_modified(o, path);
565 if (is_null_oid(one))
566 message = "(new submodule)";
567 else if (is_null_oid(two))
568 message = "(submodule deleted)";
570 if (!sub) {
571 if (!message)
572 message = "(commits not present)";
573 goto output_header;
577 * Attempt to lookup the commit references, and determine if this is
578 * a fast forward or fast backwards update.
580 *left = lookup_commit_reference(sub, one);
581 *right = lookup_commit_reference(sub, two);
584 * Warn about missing commits in the submodule project, but only if
585 * they aren't null.
587 if ((!is_null_oid(one) && !*left) ||
588 (!is_null_oid(two) && !*right))
589 message = "(commits not present)";
591 *merge_bases = repo_get_merge_bases(sub, *left, *right);
592 if (*merge_bases) {
593 if ((*merge_bases)->item == *left)
594 fast_forward = 1;
595 else if ((*merge_bases)->item == *right)
596 fast_backward = 1;
599 if (oideq(one, two)) {
600 strbuf_release(&sb);
601 return;
604 output_header:
605 strbuf_addf(&sb, "Submodule %s ", path);
606 strbuf_add_unique_abbrev(&sb, one, DEFAULT_ABBREV);
607 strbuf_addstr(&sb, (fast_backward || fast_forward) ? ".." : "...");
608 strbuf_add_unique_abbrev(&sb, two, DEFAULT_ABBREV);
609 if (message)
610 strbuf_addf(&sb, " %s\n", message);
611 else
612 strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : "");
613 diff_emit_submodule_header(o, sb.buf);
615 strbuf_release(&sb);
618 void show_submodule_diff_summary(struct diff_options *o, const char *path,
619 struct object_id *one, struct object_id *two,
620 unsigned dirty_submodule)
622 struct rev_info rev;
623 struct commit *left = NULL, *right = NULL;
624 struct commit_list *merge_bases = NULL;
625 struct repository *sub;
627 sub = open_submodule(path);
628 show_submodule_header(o, path, one, two, dirty_submodule,
629 sub, &left, &right, &merge_bases);
632 * If we don't have both a left and a right pointer, there is no
633 * reason to try and display a summary. The header line should contain
634 * all the information the user needs.
636 if (!left || !right || !sub)
637 goto out;
639 /* Treat revision walker failure the same as missing commits */
640 if (prepare_submodule_diff_summary(sub, &rev, path, left, right, merge_bases)) {
641 diff_emit_submodule_error(o, "(revision walker failed)\n");
642 goto out;
645 print_submodule_diff_summary(sub, &rev, o);
647 out:
648 if (merge_bases)
649 free_commit_list(merge_bases);
650 clear_commit_marks(left, ~0);
651 clear_commit_marks(right, ~0);
652 if (sub) {
653 repo_clear(sub);
654 free(sub);
658 void show_submodule_inline_diff(struct diff_options *o, const char *path,
659 struct object_id *one, struct object_id *two,
660 unsigned dirty_submodule)
662 const struct object_id *old_oid = the_hash_algo->empty_tree, *new_oid = the_hash_algo->empty_tree;
663 struct commit *left = NULL, *right = NULL;
664 struct commit_list *merge_bases = NULL;
665 struct child_process cp = CHILD_PROCESS_INIT;
666 struct strbuf sb = STRBUF_INIT;
667 struct repository *sub;
669 sub = open_submodule(path);
670 show_submodule_header(o, path, one, two, dirty_submodule,
671 sub, &left, &right, &merge_bases);
673 /* We need a valid left and right commit to display a difference */
674 if (!(left || is_null_oid(one)) ||
675 !(right || is_null_oid(two)))
676 goto done;
678 if (left)
679 old_oid = one;
680 if (right)
681 new_oid = two;
683 cp.git_cmd = 1;
684 cp.dir = path;
685 cp.out = -1;
686 cp.no_stdin = 1;
688 /* TODO: other options may need to be passed here. */
689 strvec_pushl(&cp.args, "diff", "--submodule=diff", NULL);
690 strvec_pushf(&cp.args, "--color=%s", want_color(o->use_color) ?
691 "always" : "never");
693 if (o->flags.reverse_diff) {
694 strvec_pushf(&cp.args, "--src-prefix=%s%s/",
695 o->b_prefix, path);
696 strvec_pushf(&cp.args, "--dst-prefix=%s%s/",
697 o->a_prefix, path);
698 } else {
699 strvec_pushf(&cp.args, "--src-prefix=%s%s/",
700 o->a_prefix, path);
701 strvec_pushf(&cp.args, "--dst-prefix=%s%s/",
702 o->b_prefix, path);
704 strvec_push(&cp.args, oid_to_hex(old_oid));
706 * If the submodule has modified content, we will diff against the
707 * work tree, under the assumption that the user has asked for the
708 * diff format and wishes to actually see all differences even if they
709 * haven't yet been committed to the submodule yet.
711 if (!(dirty_submodule & DIRTY_SUBMODULE_MODIFIED))
712 strvec_push(&cp.args, oid_to_hex(new_oid));
714 prepare_submodule_repo_env(&cp.env_array);
716 if (!is_directory(path)) {
717 /* fall back to absorbed git dir, if any */
718 if (!sub)
719 goto done;
720 cp.dir = sub->gitdir;
721 strvec_push(&cp.env_array, GIT_DIR_ENVIRONMENT "=.");
722 strvec_push(&cp.env_array, GIT_WORK_TREE_ENVIRONMENT "=.");
725 if (start_command(&cp)) {
726 diff_emit_submodule_error(o, "(diff failed)\n");
727 goto done;
730 while (strbuf_getwholeline_fd(&sb, cp.out, '\n') != EOF)
731 diff_emit_submodule_pipethrough(o, sb.buf, sb.len);
733 if (finish_command(&cp))
734 diff_emit_submodule_error(o, "(diff failed)\n");
736 done:
737 strbuf_release(&sb);
738 if (merge_bases)
739 free_commit_list(merge_bases);
740 if (left)
741 clear_commit_marks(left, ~0);
742 if (right)
743 clear_commit_marks(right, ~0);
744 if (sub) {
745 repo_clear(sub);
746 free(sub);
750 int should_update_submodules(void)
752 return config_update_recurse_submodules == RECURSE_SUBMODULES_ON;
755 const struct submodule *submodule_from_ce(const struct cache_entry *ce)
757 if (!S_ISGITLINK(ce->ce_mode))
758 return NULL;
760 if (!should_update_submodules())
761 return NULL;
763 return submodule_from_path(the_repository, null_oid(), ce->name);
767 struct collect_changed_submodules_cb_data {
768 struct repository *repo;
769 struct string_list *changed;
770 const struct object_id *commit_oid;
774 * this would normally be two functions: default_name_from_path() and
775 * path_from_default_name(). Since the default name is the same as
776 * the submodule path we can get away with just one function which only
777 * checks whether there is a submodule in the working directory at that
778 * location.
780 static const char *default_name_or_path(const char *path_or_name)
782 int error_code;
784 if (!is_submodule_populated_gently(path_or_name, &error_code))
785 return NULL;
787 return path_or_name;
791 * Holds relevant information for a changed submodule. Used as the .util
792 * member of the changed submodule name string_list_item.
794 * (super_oid, path) allows the submodule config to be read from _some_
795 * .gitmodules file. We store this information the first time we find a
796 * superproject commit that points to the submodule, but this is
797 * arbitrary - we can choose any (super_oid, path) that matches the
798 * submodule's name.
800 * NEEDSWORK: Storing an arbitrary commit is undesirable because we can't
801 * guarantee that we're reading the commit that the user would expect. A better
802 * scheme would be to just fetch a submodule by its name. This requires two
803 * steps:
804 * - Create a function that behaves like repo_submodule_init(), but accepts a
805 * submodule name instead of treeish_name and path. This should be easy
806 * because repo_submodule_init() internally uses the submodule's name.
808 * - Replace most instances of 'struct submodule' (which is the .gitmodules
809 * config) with just the submodule name. This is OK because we expect
810 * submodule settings to be stored in .git/config (via "git submodule init"),
811 * not .gitmodules. This also lets us delete get_non_gitmodules_submodule(),
812 * which constructs a bogus 'struct submodule' for the sake of giving a
813 * placeholder name to a gitlink.
815 struct changed_submodule_data {
817 * The first superproject commit in the rev walk that points to
818 * the submodule.
820 const struct object_id *super_oid;
822 * Path to the submodule in the superproject commit referenced
823 * by 'super_oid'.
825 char *path;
826 /* The submodule commits that have changed in the rev walk. */
827 struct oid_array new_commits;
830 static void changed_submodule_data_clear(struct changed_submodule_data *cs_data)
832 oid_array_clear(&cs_data->new_commits);
833 free(cs_data->path);
836 static void collect_changed_submodules_cb(struct diff_queue_struct *q,
837 struct diff_options *options,
838 void *data)
840 struct collect_changed_submodules_cb_data *me = data;
841 struct string_list *changed = me->changed;
842 const struct object_id *commit_oid = me->commit_oid;
843 int i;
845 for (i = 0; i < q->nr; i++) {
846 struct diff_filepair *p = q->queue[i];
847 const struct submodule *submodule;
848 const char *name;
849 struct string_list_item *item;
850 struct changed_submodule_data *cs_data;
852 if (!S_ISGITLINK(p->two->mode))
853 continue;
855 submodule = submodule_from_path(me->repo,
856 commit_oid, p->two->path);
857 if (submodule)
858 name = submodule->name;
859 else {
860 name = default_name_or_path(p->two->path);
861 /* make sure name does not collide with existing one */
862 if (name)
863 submodule = submodule_from_name(me->repo,
864 commit_oid, name);
865 if (submodule) {
866 warning(_("Submodule in commit %s at path: "
867 "'%s' collides with a submodule named "
868 "the same. Skipping it."),
869 oid_to_hex(commit_oid), p->two->path);
870 name = NULL;
874 if (!name)
875 continue;
877 item = string_list_insert(changed, name);
878 if (item->util)
879 cs_data = item->util;
880 else {
881 item->util = xcalloc(1, sizeof(struct changed_submodule_data));
882 cs_data = item->util;
883 cs_data->super_oid = commit_oid;
884 cs_data->path = xstrdup(p->two->path);
886 oid_array_append(&cs_data->new_commits, &p->two->oid);
891 * Collect the paths of submodules in 'changed' which have changed based on
892 * the revisions as specified in 'argv'. Each entry in 'changed' will also
893 * have a corresponding 'struct oid_array' (in the 'util' field) which lists
894 * what the submodule pointers were updated to during the change.
896 static void collect_changed_submodules(struct repository *r,
897 struct string_list *changed,
898 struct strvec *argv)
900 struct rev_info rev;
901 const struct commit *commit;
902 int save_warning;
903 struct setup_revision_opt s_r_opt = {
904 .assume_dashdash = 1,
907 save_warning = warn_on_object_refname_ambiguity;
908 warn_on_object_refname_ambiguity = 0;
909 repo_init_revisions(r, &rev, NULL);
910 setup_revisions(argv->nr, argv->v, &rev, &s_r_opt);
911 warn_on_object_refname_ambiguity = save_warning;
912 if (prepare_revision_walk(&rev))
913 die(_("revision walk setup failed"));
915 while ((commit = get_revision(&rev))) {
916 struct rev_info diff_rev;
917 struct collect_changed_submodules_cb_data data;
918 data.repo = r;
919 data.changed = changed;
920 data.commit_oid = &commit->object.oid;
922 repo_init_revisions(r, &diff_rev, NULL);
923 diff_rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
924 diff_rev.diffopt.format_callback = collect_changed_submodules_cb;
925 diff_rev.diffopt.format_callback_data = &data;
926 diff_rev.dense_combined_merges = 1;
927 diff_tree_combined_merge(commit, &diff_rev);
930 reset_revision_walk();
933 static void free_submodules_data(struct string_list *submodules)
935 struct string_list_item *item;
936 for_each_string_list_item(item, submodules)
937 changed_submodule_data_clear(item->util);
939 string_list_clear(submodules, 1);
942 static int has_remote(const char *refname, const struct object_id *oid,
943 int flags, void *cb_data)
945 return 1;
948 static int append_oid_to_argv(const struct object_id *oid, void *data)
950 struct strvec *argv = data;
951 strvec_push(argv, oid_to_hex(oid));
952 return 0;
955 struct has_commit_data {
956 struct repository *repo;
957 int result;
958 const char *path;
959 const struct object_id *super_oid;
962 static int check_has_commit(const struct object_id *oid, void *data)
964 struct has_commit_data *cb = data;
965 struct repository subrepo;
966 enum object_type type;
968 if (repo_submodule_init(&subrepo, cb->repo, cb->path, cb->super_oid)) {
969 cb->result = 0;
970 /* subrepo failed to init, so don't clean it up. */
971 return 0;
974 type = oid_object_info(&subrepo, oid, NULL);
976 switch (type) {
977 case OBJ_COMMIT:
978 goto cleanup;
979 case OBJ_BAD:
981 * Object is missing or invalid. If invalid, an error message
982 * has already been printed.
984 cb->result = 0;
985 goto cleanup;
986 default:
987 die(_("submodule entry '%s' (%s) is a %s, not a commit"),
988 cb->path, oid_to_hex(oid), type_name(type));
990 cleanup:
991 repo_clear(&subrepo);
992 return 0;
995 static int submodule_has_commits(struct repository *r,
996 const char *path,
997 const struct object_id *super_oid,
998 struct oid_array *commits)
1000 struct has_commit_data has_commit = {
1001 .repo = r,
1002 .result = 1,
1003 .path = path,
1004 .super_oid = super_oid
1007 oid_array_for_each_unique(commits, check_has_commit, &has_commit);
1009 if (has_commit.result) {
1011 * Even if the submodule is checked out and the commit is
1012 * present, make sure it exists in the submodule's object store
1013 * and that it is reachable from a ref.
1015 struct child_process cp = CHILD_PROCESS_INIT;
1016 struct strbuf out = STRBUF_INIT;
1018 strvec_pushl(&cp.args, "rev-list", "-n", "1", NULL);
1019 oid_array_for_each_unique(commits, append_oid_to_argv, &cp.args);
1020 strvec_pushl(&cp.args, "--not", "--all", NULL);
1022 prepare_submodule_repo_env(&cp.env_array);
1023 cp.git_cmd = 1;
1024 cp.no_stdin = 1;
1025 cp.dir = path;
1027 if (capture_command(&cp, &out, GIT_MAX_HEXSZ + 1) || out.len)
1028 has_commit.result = 0;
1030 strbuf_release(&out);
1033 return has_commit.result;
1036 static int submodule_needs_pushing(struct repository *r,
1037 const char *path,
1038 struct oid_array *commits)
1040 if (!submodule_has_commits(r, path, null_oid(), commits))
1042 * NOTE: We do consider it safe to return "no" here. The
1043 * correct answer would be "We do not know" instead of
1044 * "No push needed", but it is quite hard to change
1045 * the submodule pointer without having the submodule
1046 * around. If a user did however change the submodules
1047 * without having the submodule around, this indicates
1048 * an expert who knows what they are doing or a
1049 * maintainer integrating work from other people. In
1050 * both cases it should be safe to skip this check.
1052 return 0;
1054 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
1055 struct child_process cp = CHILD_PROCESS_INIT;
1056 struct strbuf buf = STRBUF_INIT;
1057 int needs_pushing = 0;
1059 strvec_push(&cp.args, "rev-list");
1060 oid_array_for_each_unique(commits, append_oid_to_argv, &cp.args);
1061 strvec_pushl(&cp.args, "--not", "--remotes", "-n", "1" , NULL);
1063 prepare_submodule_repo_env(&cp.env_array);
1064 cp.git_cmd = 1;
1065 cp.no_stdin = 1;
1066 cp.out = -1;
1067 cp.dir = path;
1068 if (start_command(&cp))
1069 die(_("Could not run 'git rev-list <commits> --not --remotes -n 1' command in submodule %s"),
1070 path);
1071 if (strbuf_read(&buf, cp.out, the_hash_algo->hexsz + 1))
1072 needs_pushing = 1;
1073 finish_command(&cp);
1074 close(cp.out);
1075 strbuf_release(&buf);
1076 return needs_pushing;
1079 return 0;
1082 int find_unpushed_submodules(struct repository *r,
1083 struct oid_array *commits,
1084 const char *remotes_name,
1085 struct string_list *needs_pushing)
1087 struct string_list submodules = STRING_LIST_INIT_DUP;
1088 struct string_list_item *name;
1089 struct strvec argv = STRVEC_INIT;
1091 /* argv.v[0] will be ignored by setup_revisions */
1092 strvec_push(&argv, "find_unpushed_submodules");
1093 oid_array_for_each_unique(commits, append_oid_to_argv, &argv);
1094 strvec_push(&argv, "--not");
1095 strvec_pushf(&argv, "--remotes=%s", remotes_name);
1097 collect_changed_submodules(r, &submodules, &argv);
1099 for_each_string_list_item(name, &submodules) {
1100 struct changed_submodule_data *cs_data = name->util;
1101 const struct submodule *submodule;
1102 const char *path = NULL;
1104 submodule = submodule_from_name(r, null_oid(), name->string);
1105 if (submodule)
1106 path = submodule->path;
1107 else
1108 path = default_name_or_path(name->string);
1110 if (!path)
1111 continue;
1113 if (submodule_needs_pushing(r, path, &cs_data->new_commits))
1114 string_list_insert(needs_pushing, path);
1117 free_submodules_data(&submodules);
1118 strvec_clear(&argv);
1120 return needs_pushing->nr;
1123 static int push_submodule(const char *path,
1124 const struct remote *remote,
1125 const struct refspec *rs,
1126 const struct string_list *push_options,
1127 int dry_run)
1129 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
1130 struct child_process cp = CHILD_PROCESS_INIT;
1131 strvec_push(&cp.args, "push");
1132 if (dry_run)
1133 strvec_push(&cp.args, "--dry-run");
1135 if (push_options && push_options->nr) {
1136 const struct string_list_item *item;
1137 for_each_string_list_item(item, push_options)
1138 strvec_pushf(&cp.args, "--push-option=%s",
1139 item->string);
1142 if (remote->origin != REMOTE_UNCONFIGURED) {
1143 int i;
1144 strvec_push(&cp.args, remote->name);
1145 for (i = 0; i < rs->raw_nr; i++)
1146 strvec_push(&cp.args, rs->raw[i]);
1149 prepare_submodule_repo_env(&cp.env_array);
1150 cp.git_cmd = 1;
1151 cp.no_stdin = 1;
1152 cp.dir = path;
1153 if (run_command(&cp))
1154 return 0;
1155 close(cp.out);
1158 return 1;
1162 * Perform a check in the submodule to see if the remote and refspec work.
1163 * Die if the submodule can't be pushed.
1165 static void submodule_push_check(const char *path, const char *head,
1166 const struct remote *remote,
1167 const struct refspec *rs)
1169 struct child_process cp = CHILD_PROCESS_INIT;
1170 int i;
1172 strvec_push(&cp.args, "submodule--helper");
1173 strvec_push(&cp.args, "push-check");
1174 strvec_push(&cp.args, head);
1175 strvec_push(&cp.args, remote->name);
1177 for (i = 0; i < rs->raw_nr; i++)
1178 strvec_push(&cp.args, rs->raw[i]);
1180 prepare_submodule_repo_env(&cp.env_array);
1181 cp.git_cmd = 1;
1182 cp.no_stdin = 1;
1183 cp.no_stdout = 1;
1184 cp.dir = path;
1187 * Simply indicate if 'submodule--helper push-check' failed.
1188 * More detailed error information will be provided by the
1189 * child process.
1191 if (run_command(&cp))
1192 die(_("process for submodule '%s' failed"), path);
1195 int push_unpushed_submodules(struct repository *r,
1196 struct oid_array *commits,
1197 const struct remote *remote,
1198 const struct refspec *rs,
1199 const struct string_list *push_options,
1200 int dry_run)
1202 int i, ret = 1;
1203 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1205 if (!find_unpushed_submodules(r, commits,
1206 remote->name, &needs_pushing))
1207 return 1;
1210 * Verify that the remote and refspec can be propagated to all
1211 * submodules. This check can be skipped if the remote and refspec
1212 * won't be propagated due to the remote being unconfigured (e.g. a URL
1213 * instead of a remote name).
1215 if (remote->origin != REMOTE_UNCONFIGURED) {
1216 char *head;
1217 struct object_id head_oid;
1219 head = resolve_refdup("HEAD", 0, &head_oid, NULL);
1220 if (!head)
1221 die(_("Failed to resolve HEAD as a valid ref."));
1223 for (i = 0; i < needs_pushing.nr; i++)
1224 submodule_push_check(needs_pushing.items[i].string,
1225 head, remote, rs);
1226 free(head);
1229 /* Actually push the submodules */
1230 for (i = 0; i < needs_pushing.nr; i++) {
1231 const char *path = needs_pushing.items[i].string;
1232 fprintf(stderr, _("Pushing submodule '%s'\n"), path);
1233 if (!push_submodule(path, remote, rs,
1234 push_options, dry_run)) {
1235 fprintf(stderr, _("Unable to push submodule '%s'\n"), path);
1236 ret = 0;
1240 string_list_clear(&needs_pushing, 0);
1242 return ret;
1245 static int append_oid_to_array(const char *ref, const struct object_id *oid,
1246 int flags, void *data)
1248 struct oid_array *array = data;
1249 oid_array_append(array, oid);
1250 return 0;
1253 void check_for_new_submodule_commits(struct object_id *oid)
1255 if (!initialized_fetch_ref_tips) {
1256 for_each_ref(append_oid_to_array, &ref_tips_before_fetch);
1257 initialized_fetch_ref_tips = 1;
1260 oid_array_append(&ref_tips_after_fetch, oid);
1264 * Returns 1 if there is at least one submodule gitdir in
1265 * $GIT_DIR/modules and 0 otherwise. This follows
1266 * submodule_name_to_gitdir(), which looks for submodules in
1267 * $GIT_DIR/modules, not $GIT_COMMON_DIR.
1269 * A submodule can be moved to $GIT_DIR/modules manually by running "git
1270 * submodule absorbgitdirs", or it may be initialized there by "git
1271 * submodule update".
1273 static int repo_has_absorbed_submodules(struct repository *r)
1275 int ret;
1276 struct strbuf buf = STRBUF_INIT;
1278 strbuf_repo_git_path(&buf, r, "modules/");
1279 ret = file_exists(buf.buf) && !is_empty_dir(buf.buf);
1280 strbuf_release(&buf);
1281 return ret;
1284 static void calculate_changed_submodule_paths(struct repository *r,
1285 struct string_list *changed_submodule_names)
1287 struct strvec argv = STRVEC_INIT;
1288 struct string_list_item *name;
1290 /* No need to check if no submodules would be fetched */
1291 if (!submodule_from_path(r, NULL, NULL) &&
1292 !repo_has_absorbed_submodules(r))
1293 return;
1295 strvec_push(&argv, "--"); /* argv[0] program name */
1296 oid_array_for_each_unique(&ref_tips_after_fetch,
1297 append_oid_to_argv, &argv);
1298 strvec_push(&argv, "--not");
1299 oid_array_for_each_unique(&ref_tips_before_fetch,
1300 append_oid_to_argv, &argv);
1303 * Collect all submodules (whether checked out or not) for which new
1304 * commits have been recorded upstream in "changed_submodule_names".
1306 collect_changed_submodules(r, changed_submodule_names, &argv);
1308 for_each_string_list_item(name, changed_submodule_names) {
1309 struct changed_submodule_data *cs_data = name->util;
1310 const struct submodule *submodule;
1311 const char *path = NULL;
1313 submodule = submodule_from_name(r, null_oid(), name->string);
1314 if (submodule)
1315 path = submodule->path;
1316 else
1317 path = default_name_or_path(name->string);
1319 if (!path)
1320 continue;
1322 if (submodule_has_commits(r, path, null_oid(), &cs_data->new_commits)) {
1323 changed_submodule_data_clear(cs_data);
1324 *name->string = '\0';
1328 string_list_remove_empty_items(changed_submodule_names, 1);
1330 strvec_clear(&argv);
1331 oid_array_clear(&ref_tips_before_fetch);
1332 oid_array_clear(&ref_tips_after_fetch);
1333 initialized_fetch_ref_tips = 0;
1336 int submodule_touches_in_range(struct repository *r,
1337 struct object_id *excl_oid,
1338 struct object_id *incl_oid)
1340 struct string_list subs = STRING_LIST_INIT_DUP;
1341 struct strvec args = STRVEC_INIT;
1342 int ret;
1344 /* No need to check if there are no submodules configured */
1345 if (!submodule_from_path(r, NULL, NULL))
1346 return 0;
1348 strvec_push(&args, "--"); /* args[0] program name */
1349 strvec_push(&args, oid_to_hex(incl_oid));
1350 if (!is_null_oid(excl_oid)) {
1351 strvec_push(&args, "--not");
1352 strvec_push(&args, oid_to_hex(excl_oid));
1355 collect_changed_submodules(r, &subs, &args);
1356 ret = subs.nr;
1358 strvec_clear(&args);
1360 free_submodules_data(&subs);
1361 return ret;
1364 struct submodule_parallel_fetch {
1366 * The index of the last index entry processed by
1367 * get_fetch_task_from_index().
1369 int index_count;
1371 * The index of the last string_list entry processed by
1372 * get_fetch_task_from_changed().
1374 int changed_count;
1375 struct strvec args;
1376 struct repository *r;
1377 const char *prefix;
1378 int command_line_option;
1379 int default_option;
1380 int quiet;
1381 int result;
1384 * Names of submodules that have new commits. Generated by
1385 * walking the newly fetched superproject commits.
1387 struct string_list changed_submodule_names;
1389 * Names of submodules that have already been processed. Lets us
1390 * avoid fetching the same submodule more than once.
1392 struct string_list seen_submodule_names;
1394 /* Pending fetches by OIDs */
1395 struct fetch_task **oid_fetch_tasks;
1396 int oid_fetch_tasks_nr, oid_fetch_tasks_alloc;
1398 struct strbuf submodules_with_errors;
1400 #define SPF_INIT { \
1401 .args = STRVEC_INIT, \
1402 .changed_submodule_names = STRING_LIST_INIT_DUP, \
1403 .seen_submodule_names = STRING_LIST_INIT_DUP, \
1404 .submodules_with_errors = STRBUF_INIT, \
1407 static int get_fetch_recurse_config(const struct submodule *submodule,
1408 struct submodule_parallel_fetch *spf)
1410 if (spf->command_line_option != RECURSE_SUBMODULES_DEFAULT)
1411 return spf->command_line_option;
1413 if (submodule) {
1414 char *key;
1415 const char *value;
1417 int fetch_recurse = submodule->fetch_recurse;
1418 key = xstrfmt("submodule.%s.fetchRecurseSubmodules", submodule->name);
1419 if (!repo_config_get_string_tmp(spf->r, key, &value)) {
1420 fetch_recurse = parse_fetch_recurse_submodules_arg(key, value);
1422 free(key);
1424 if (fetch_recurse != RECURSE_SUBMODULES_NONE)
1425 /* local config overrules everything except commandline */
1426 return fetch_recurse;
1429 return spf->default_option;
1433 * Fetch in progress (if callback data) or
1434 * pending (if in oid_fetch_tasks in struct submodule_parallel_fetch)
1436 struct fetch_task {
1437 struct repository *repo;
1438 const struct submodule *sub;
1439 unsigned free_sub : 1; /* Do we need to free the submodule? */
1440 const char *default_argv; /* The default fetch mode. */
1441 struct strvec git_args; /* Args for the child git process. */
1443 struct oid_array *commits; /* Ensure these commits are fetched */
1447 * When a submodule is not defined in .gitmodules, we cannot access it
1448 * via the regular submodule-config. Create a fake submodule, which we can
1449 * work on.
1451 static const struct submodule *get_non_gitmodules_submodule(const char *path)
1453 struct submodule *ret = NULL;
1454 const char *name = default_name_or_path(path);
1456 if (!name)
1457 return NULL;
1459 ret = xmalloc(sizeof(*ret));
1460 memset(ret, 0, sizeof(*ret));
1461 ret->path = name;
1462 ret->name = name;
1464 return (const struct submodule *) ret;
1467 static void fetch_task_release(struct fetch_task *p)
1469 if (p->free_sub)
1470 free((void*)p->sub);
1471 p->free_sub = 0;
1472 p->sub = NULL;
1474 if (p->repo)
1475 repo_clear(p->repo);
1476 FREE_AND_NULL(p->repo);
1478 strvec_clear(&p->git_args);
1481 static struct repository *get_submodule_repo_for(struct repository *r,
1482 const char *path,
1483 const struct object_id *treeish_name)
1485 struct repository *ret = xmalloc(sizeof(*ret));
1487 if (repo_submodule_init(ret, r, path, treeish_name)) {
1488 free(ret);
1489 return NULL;
1492 return ret;
1495 static struct fetch_task *fetch_task_create(struct submodule_parallel_fetch *spf,
1496 const char *path,
1497 const struct object_id *treeish_name)
1499 struct fetch_task *task = xmalloc(sizeof(*task));
1500 memset(task, 0, sizeof(*task));
1502 task->sub = submodule_from_path(spf->r, treeish_name, path);
1504 if (!task->sub) {
1506 * No entry in .gitmodules? Technically not a submodule,
1507 * but historically we supported repositories that happen to be
1508 * in-place where a gitlink is. Keep supporting them.
1510 task->sub = get_non_gitmodules_submodule(path);
1511 if (!task->sub)
1512 goto cleanup;
1514 task->free_sub = 1;
1517 if (string_list_lookup(&spf->seen_submodule_names, task->sub->name))
1518 goto cleanup;
1520 switch (get_fetch_recurse_config(task->sub, spf))
1522 default:
1523 case RECURSE_SUBMODULES_DEFAULT:
1524 case RECURSE_SUBMODULES_ON_DEMAND:
1525 if (!task->sub ||
1526 !string_list_lookup(
1527 &spf->changed_submodule_names,
1528 task->sub->name))
1529 goto cleanup;
1530 task->default_argv = "on-demand";
1531 break;
1532 case RECURSE_SUBMODULES_ON:
1533 task->default_argv = "yes";
1534 break;
1535 case RECURSE_SUBMODULES_OFF:
1536 goto cleanup;
1539 task->repo = get_submodule_repo_for(spf->r, path, treeish_name);
1541 return task;
1543 cleanup:
1544 fetch_task_release(task);
1545 free(task);
1546 return NULL;
1549 static struct fetch_task *
1550 get_fetch_task_from_index(struct submodule_parallel_fetch *spf,
1551 struct strbuf *err)
1553 for (; spf->index_count < spf->r->index->cache_nr; spf->index_count++) {
1554 const struct cache_entry *ce =
1555 spf->r->index->cache[spf->index_count];
1556 struct fetch_task *task;
1558 if (!S_ISGITLINK(ce->ce_mode))
1559 continue;
1561 task = fetch_task_create(spf, ce->name, null_oid());
1562 if (!task)
1563 continue;
1565 if (task->repo) {
1566 if (!spf->quiet)
1567 strbuf_addf(err, _("Fetching submodule %s%s\n"),
1568 spf->prefix, ce->name);
1570 spf->index_count++;
1571 return task;
1572 } else {
1573 struct strbuf empty_submodule_path = STRBUF_INIT;
1575 fetch_task_release(task);
1576 free(task);
1579 * An empty directory is normal,
1580 * the submodule is not initialized
1582 strbuf_addf(&empty_submodule_path, "%s/%s/",
1583 spf->r->worktree,
1584 ce->name);
1585 if (S_ISGITLINK(ce->ce_mode) &&
1586 !is_empty_dir(empty_submodule_path.buf)) {
1587 spf->result = 1;
1588 strbuf_addf(err,
1589 _("Could not access submodule '%s'\n"),
1590 ce->name);
1592 strbuf_release(&empty_submodule_path);
1595 return NULL;
1598 static struct fetch_task *
1599 get_fetch_task_from_changed(struct submodule_parallel_fetch *spf,
1600 struct strbuf *err)
1602 for (; spf->changed_count < spf->changed_submodule_names.nr;
1603 spf->changed_count++) {
1604 struct string_list_item item =
1605 spf->changed_submodule_names.items[spf->changed_count];
1606 struct changed_submodule_data *cs_data = item.util;
1607 struct fetch_task *task;
1609 if (!is_tree_submodule_active(spf->r, cs_data->super_oid,cs_data->path))
1610 continue;
1612 task = fetch_task_create(spf, cs_data->path,
1613 cs_data->super_oid);
1614 if (!task)
1615 continue;
1617 if (!task->repo) {
1618 strbuf_addf(err, _("Could not access submodule '%s' at commit %s\n"),
1619 cs_data->path,
1620 find_unique_abbrev(cs_data->super_oid, DEFAULT_ABBREV));
1622 fetch_task_release(task);
1623 free(task);
1624 continue;
1627 if (!spf->quiet)
1628 strbuf_addf(err,
1629 _("Fetching submodule %s%s at commit %s\n"),
1630 spf->prefix, task->sub->path,
1631 find_unique_abbrev(cs_data->super_oid,
1632 DEFAULT_ABBREV));
1634 spf->changed_count++;
1636 * NEEDSWORK: Submodules set/unset a value for
1637 * core.worktree when they are populated/unpopulated by
1638 * "git checkout" (and similar commands, see
1639 * submodule_move_head() and
1640 * connect_work_tree_and_git_dir()), but if the
1641 * submodule is unpopulated in another way (e.g. "git
1642 * rm", "rm -r"), core.worktree will still be set even
1643 * though the directory doesn't exist, and the child
1644 * process will crash while trying to chdir into the
1645 * nonexistent directory.
1647 * In this case, we know that the submodule has no
1648 * working tree, so we can work around this by
1649 * setting "--work-tree=." (--bare does not work because
1650 * worktree settings take precedence over bare-ness).
1651 * However, this is not necessarily true in other cases,
1652 * so a generalized solution is still necessary.
1654 * Possible solutions:
1655 * - teach "git [add|rm]" to unset core.worktree and
1656 * discourage users from removing submodules without
1657 * using a Git command.
1658 * - teach submodule child processes to ignore stale
1659 * core.worktree values.
1661 strvec_push(&task->git_args, "--work-tree=.");
1662 return task;
1664 return NULL;
1667 static int get_next_submodule(struct child_process *cp, struct strbuf *err,
1668 void *data, void **task_cb)
1670 struct submodule_parallel_fetch *spf = data;
1671 struct fetch_task *task =
1672 get_fetch_task_from_index(spf, err);
1673 if (!task)
1674 task = get_fetch_task_from_changed(spf, err);
1676 if (task) {
1677 struct strbuf submodule_prefix = STRBUF_INIT;
1679 child_process_init(cp);
1680 cp->dir = task->repo->gitdir;
1681 prepare_submodule_repo_env_in_gitdir(&cp->env_array);
1682 cp->git_cmd = 1;
1683 strvec_init(&cp->args);
1684 if (task->git_args.nr)
1685 strvec_pushv(&cp->args, task->git_args.v);
1686 strvec_pushv(&cp->args, spf->args.v);
1687 strvec_push(&cp->args, task->default_argv);
1688 strvec_push(&cp->args, "--submodule-prefix");
1690 strbuf_addf(&submodule_prefix, "%s%s/",
1691 spf->prefix,
1692 task->sub->path);
1693 strvec_push(&cp->args, submodule_prefix.buf);
1694 *task_cb = task;
1696 strbuf_release(&submodule_prefix);
1697 string_list_insert(&spf->seen_submodule_names, task->sub->name);
1698 return 1;
1701 if (spf->oid_fetch_tasks_nr) {
1702 struct fetch_task *task =
1703 spf->oid_fetch_tasks[spf->oid_fetch_tasks_nr - 1];
1704 struct strbuf submodule_prefix = STRBUF_INIT;
1705 spf->oid_fetch_tasks_nr--;
1707 strbuf_addf(&submodule_prefix, "%s%s/",
1708 spf->prefix, task->sub->path);
1710 child_process_init(cp);
1711 prepare_submodule_repo_env_in_gitdir(&cp->env_array);
1712 cp->git_cmd = 1;
1713 cp->dir = task->repo->gitdir;
1715 strvec_init(&cp->args);
1716 strvec_pushv(&cp->args, spf->args.v);
1717 strvec_push(&cp->args, "on-demand");
1718 strvec_push(&cp->args, "--submodule-prefix");
1719 strvec_push(&cp->args, submodule_prefix.buf);
1721 /* NEEDSWORK: have get_default_remote from submodule--helper */
1722 strvec_push(&cp->args, "origin");
1723 oid_array_for_each_unique(task->commits,
1724 append_oid_to_argv, &cp->args);
1726 *task_cb = task;
1727 strbuf_release(&submodule_prefix);
1728 return 1;
1731 return 0;
1734 static int fetch_start_failure(struct strbuf *err,
1735 void *cb, void *task_cb)
1737 struct submodule_parallel_fetch *spf = cb;
1738 struct fetch_task *task = task_cb;
1740 spf->result = 1;
1742 fetch_task_release(task);
1743 return 0;
1746 static int commit_missing_in_sub(const struct object_id *oid, void *data)
1748 struct repository *subrepo = data;
1750 enum object_type type = oid_object_info(subrepo, oid, NULL);
1752 return type != OBJ_COMMIT;
1755 static int fetch_finish(int retvalue, struct strbuf *err,
1756 void *cb, void *task_cb)
1758 struct submodule_parallel_fetch *spf = cb;
1759 struct fetch_task *task = task_cb;
1761 struct string_list_item *it;
1762 struct changed_submodule_data *cs_data;
1764 if (!task || !task->sub)
1765 BUG("callback cookie bogus");
1767 if (retvalue) {
1769 * NEEDSWORK: This indicates that the overall fetch
1770 * failed, even though there may be a subsequent fetch
1771 * by commit hash that might work. It may be a good
1772 * idea to not indicate failure in this case, and only
1773 * indicate failure if the subsequent fetch fails.
1775 spf->result = 1;
1777 strbuf_addf(&spf->submodules_with_errors, "\t%s\n",
1778 task->sub->name);
1781 /* Is this the second time we process this submodule? */
1782 if (task->commits)
1783 goto out;
1785 it = string_list_lookup(&spf->changed_submodule_names, task->sub->name);
1786 if (!it)
1787 /* Could be an unchanged submodule, not contained in the list */
1788 goto out;
1790 cs_data = it->util;
1791 oid_array_filter(&cs_data->new_commits,
1792 commit_missing_in_sub,
1793 task->repo);
1795 /* Are there commits we want, but do not exist? */
1796 if (cs_data->new_commits.nr) {
1797 task->commits = &cs_data->new_commits;
1798 ALLOC_GROW(spf->oid_fetch_tasks,
1799 spf->oid_fetch_tasks_nr + 1,
1800 spf->oid_fetch_tasks_alloc);
1801 spf->oid_fetch_tasks[spf->oid_fetch_tasks_nr] = task;
1802 spf->oid_fetch_tasks_nr++;
1803 return 0;
1806 out:
1807 fetch_task_release(task);
1809 return 0;
1812 int fetch_submodules(struct repository *r,
1813 const struct strvec *options,
1814 const char *prefix, int command_line_option,
1815 int default_option,
1816 int quiet, int max_parallel_jobs)
1818 int i;
1819 struct submodule_parallel_fetch spf = SPF_INIT;
1821 spf.r = r;
1822 spf.command_line_option = command_line_option;
1823 spf.default_option = default_option;
1824 spf.quiet = quiet;
1825 spf.prefix = prefix;
1827 if (!r->worktree)
1828 goto out;
1830 if (repo_read_index(r) < 0)
1831 die(_("index file corrupt"));
1833 strvec_push(&spf.args, "fetch");
1834 for (i = 0; i < options->nr; i++)
1835 strvec_push(&spf.args, options->v[i]);
1836 strvec_push(&spf.args, "--recurse-submodules-default");
1837 /* default value, "--submodule-prefix" and its value are added later */
1839 calculate_changed_submodule_paths(r, &spf.changed_submodule_names);
1840 string_list_sort(&spf.changed_submodule_names);
1841 run_processes_parallel_tr2(max_parallel_jobs,
1842 get_next_submodule,
1843 fetch_start_failure,
1844 fetch_finish,
1845 &spf,
1846 "submodule", "parallel/fetch");
1848 if (spf.submodules_with_errors.len > 0)
1849 fprintf(stderr, _("Errors during submodule fetch:\n%s"),
1850 spf.submodules_with_errors.buf);
1853 strvec_clear(&spf.args);
1854 out:
1855 free_submodules_data(&spf.changed_submodule_names);
1856 return spf.result;
1859 unsigned is_submodule_modified(const char *path, int ignore_untracked)
1861 struct child_process cp = CHILD_PROCESS_INIT;
1862 struct strbuf buf = STRBUF_INIT;
1863 FILE *fp;
1864 unsigned dirty_submodule = 0;
1865 const char *git_dir;
1866 int ignore_cp_exit_code = 0;
1868 strbuf_addf(&buf, "%s/.git", path);
1869 git_dir = read_gitfile(buf.buf);
1870 if (!git_dir)
1871 git_dir = buf.buf;
1872 if (!is_git_directory(git_dir)) {
1873 if (is_directory(git_dir))
1874 die(_("'%s' not recognized as a git repository"), git_dir);
1875 strbuf_release(&buf);
1876 /* The submodule is not checked out, so it is not modified */
1877 return 0;
1879 strbuf_reset(&buf);
1881 strvec_pushl(&cp.args, "status", "--porcelain=2", NULL);
1882 if (ignore_untracked)
1883 strvec_push(&cp.args, "-uno");
1885 prepare_submodule_repo_env(&cp.env_array);
1886 cp.git_cmd = 1;
1887 cp.no_stdin = 1;
1888 cp.out = -1;
1889 cp.dir = path;
1890 if (start_command(&cp))
1891 die(_("Could not run 'git status --porcelain=2' in submodule %s"), path);
1893 fp = xfdopen(cp.out, "r");
1894 while (strbuf_getwholeline(&buf, fp, '\n') != EOF) {
1895 /* regular untracked files */
1896 if (buf.buf[0] == '?')
1897 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
1899 if (buf.buf[0] == 'u' ||
1900 buf.buf[0] == '1' ||
1901 buf.buf[0] == '2') {
1902 /* T = line type, XY = status, SSSS = submodule state */
1903 if (buf.len < strlen("T XY SSSS"))
1904 BUG("invalid status --porcelain=2 line %s",
1905 buf.buf);
1907 if (buf.buf[5] == 'S' && buf.buf[8] == 'U')
1908 /* nested untracked file */
1909 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
1911 if (buf.buf[0] == 'u' ||
1912 buf.buf[0] == '2' ||
1913 memcmp(buf.buf + 5, "S..U", 4))
1914 /* other change */
1915 dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
1918 if ((dirty_submodule & DIRTY_SUBMODULE_MODIFIED) &&
1919 ((dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ||
1920 ignore_untracked)) {
1922 * We're not interested in any further information from
1923 * the child any more, neither output nor its exit code.
1925 ignore_cp_exit_code = 1;
1926 break;
1929 fclose(fp);
1931 if (finish_command(&cp) && !ignore_cp_exit_code)
1932 die(_("'git status --porcelain=2' failed in submodule %s"), path);
1934 strbuf_release(&buf);
1935 return dirty_submodule;
1938 int submodule_uses_gitfile(const char *path)
1940 struct child_process cp = CHILD_PROCESS_INIT;
1941 struct strbuf buf = STRBUF_INIT;
1942 const char *git_dir;
1944 strbuf_addf(&buf, "%s/.git", path);
1945 git_dir = read_gitfile(buf.buf);
1946 if (!git_dir) {
1947 strbuf_release(&buf);
1948 return 0;
1950 strbuf_release(&buf);
1952 /* Now test that all nested submodules use a gitfile too */
1953 strvec_pushl(&cp.args,
1954 "submodule", "foreach", "--quiet", "--recursive",
1955 "test -f .git", NULL);
1957 prepare_submodule_repo_env(&cp.env_array);
1958 cp.git_cmd = 1;
1959 cp.no_stdin = 1;
1960 cp.no_stderr = 1;
1961 cp.no_stdout = 1;
1962 cp.dir = path;
1963 if (run_command(&cp))
1964 return 0;
1966 return 1;
1970 * Check if it is a bad idea to remove a submodule, i.e. if we'd lose data
1971 * when doing so.
1973 * Return 1 if we'd lose data, return 0 if the removal is fine,
1974 * and negative values for errors.
1976 int bad_to_remove_submodule(const char *path, unsigned flags)
1978 ssize_t len;
1979 struct child_process cp = CHILD_PROCESS_INIT;
1980 struct strbuf buf = STRBUF_INIT;
1981 int ret = 0;
1983 if (!file_exists(path) || is_empty_dir(path))
1984 return 0;
1986 if (!submodule_uses_gitfile(path))
1987 return 1;
1989 strvec_pushl(&cp.args, "status", "--porcelain",
1990 "--ignore-submodules=none", NULL);
1992 if (flags & SUBMODULE_REMOVAL_IGNORE_UNTRACKED)
1993 strvec_push(&cp.args, "-uno");
1994 else
1995 strvec_push(&cp.args, "-uall");
1997 if (!(flags & SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED))
1998 strvec_push(&cp.args, "--ignored");
2000 prepare_submodule_repo_env(&cp.env_array);
2001 cp.git_cmd = 1;
2002 cp.no_stdin = 1;
2003 cp.out = -1;
2004 cp.dir = path;
2005 if (start_command(&cp)) {
2006 if (flags & SUBMODULE_REMOVAL_DIE_ON_ERROR)
2007 die(_("could not start 'git status' in submodule '%s'"),
2008 path);
2009 ret = -1;
2010 goto out;
2013 len = strbuf_read(&buf, cp.out, 1024);
2014 if (len > 2)
2015 ret = 1;
2016 close(cp.out);
2018 if (finish_command(&cp)) {
2019 if (flags & SUBMODULE_REMOVAL_DIE_ON_ERROR)
2020 die(_("could not run 'git status' in submodule '%s'"),
2021 path);
2022 ret = -1;
2024 out:
2025 strbuf_release(&buf);
2026 return ret;
2029 void submodule_unset_core_worktree(const struct submodule *sub)
2031 struct strbuf config_path = STRBUF_INIT;
2033 submodule_name_to_gitdir(&config_path, the_repository, sub->name);
2034 strbuf_addstr(&config_path, "/config");
2036 if (git_config_set_in_file_gently(config_path.buf, "core.worktree", NULL))
2037 warning(_("Could not unset core.worktree setting in submodule '%s'"),
2038 sub->path);
2040 strbuf_release(&config_path);
2043 static const char *get_super_prefix_or_empty(void)
2045 const char *s = get_super_prefix();
2046 if (!s)
2047 s = "";
2048 return s;
2051 static int submodule_has_dirty_index(const struct submodule *sub)
2053 struct child_process cp = CHILD_PROCESS_INIT;
2055 prepare_submodule_repo_env(&cp.env_array);
2057 cp.git_cmd = 1;
2058 strvec_pushl(&cp.args, "diff-index", "--quiet",
2059 "--cached", "HEAD", NULL);
2060 cp.no_stdin = 1;
2061 cp.no_stdout = 1;
2062 cp.dir = sub->path;
2063 if (start_command(&cp))
2064 die(_("could not recurse into submodule '%s'"), sub->path);
2066 return finish_command(&cp);
2069 static void submodule_reset_index(const char *path)
2071 struct child_process cp = CHILD_PROCESS_INIT;
2072 prepare_submodule_repo_env(&cp.env_array);
2074 cp.git_cmd = 1;
2075 cp.no_stdin = 1;
2076 cp.dir = path;
2078 strvec_pushf(&cp.args, "--super-prefix=%s%s/",
2079 get_super_prefix_or_empty(), path);
2080 /* TODO: determine if this might overwright untracked files */
2081 strvec_pushl(&cp.args, "read-tree", "-u", "--reset", NULL);
2083 strvec_push(&cp.args, empty_tree_oid_hex());
2085 if (run_command(&cp))
2086 die(_("could not reset submodule index"));
2090 * Moves a submodule at a given path from a given head to another new head.
2091 * For edge cases (a submodule coming into existence or removing a submodule)
2092 * pass NULL for old or new respectively.
2094 int submodule_move_head(const char *path,
2095 const char *old_head,
2096 const char *new_head,
2097 unsigned flags)
2099 int ret = 0;
2100 struct child_process cp = CHILD_PROCESS_INIT;
2101 const struct submodule *sub;
2102 int *error_code_ptr, error_code;
2104 if (!is_submodule_active(the_repository, path))
2105 return 0;
2107 if (flags & SUBMODULE_MOVE_HEAD_FORCE)
2109 * Pass non NULL pointer to is_submodule_populated_gently
2110 * to prevent die()-ing. We'll use connect_work_tree_and_git_dir
2111 * to fixup the submodule in the force case later.
2113 error_code_ptr = &error_code;
2114 else
2115 error_code_ptr = NULL;
2117 if (old_head && !is_submodule_populated_gently(path, error_code_ptr))
2118 return 0;
2120 sub = submodule_from_path(the_repository, null_oid(), path);
2122 if (!sub)
2123 BUG("could not get submodule information for '%s'", path);
2125 if (old_head && !(flags & SUBMODULE_MOVE_HEAD_FORCE)) {
2126 /* Check if the submodule has a dirty index. */
2127 if (submodule_has_dirty_index(sub))
2128 return error(_("submodule '%s' has dirty index"), path);
2131 if (!(flags & SUBMODULE_MOVE_HEAD_DRY_RUN)) {
2132 if (old_head) {
2133 if (!submodule_uses_gitfile(path))
2134 absorb_git_dir_into_superproject(path,
2135 ABSORB_GITDIR_RECURSE_SUBMODULES);
2136 } else {
2137 struct strbuf gitdir = STRBUF_INIT;
2138 submodule_name_to_gitdir(&gitdir, the_repository,
2139 sub->name);
2140 connect_work_tree_and_git_dir(path, gitdir.buf, 0);
2141 strbuf_release(&gitdir);
2143 /* make sure the index is clean as well */
2144 submodule_reset_index(path);
2147 if (old_head && (flags & SUBMODULE_MOVE_HEAD_FORCE)) {
2148 struct strbuf gitdir = STRBUF_INIT;
2149 submodule_name_to_gitdir(&gitdir, the_repository,
2150 sub->name);
2151 connect_work_tree_and_git_dir(path, gitdir.buf, 1);
2152 strbuf_release(&gitdir);
2156 prepare_submodule_repo_env(&cp.env_array);
2158 cp.git_cmd = 1;
2159 cp.no_stdin = 1;
2160 cp.dir = path;
2162 strvec_pushf(&cp.args, "--super-prefix=%s%s/",
2163 get_super_prefix_or_empty(), path);
2164 strvec_pushl(&cp.args, "read-tree", "--recurse-submodules", NULL);
2166 if (flags & SUBMODULE_MOVE_HEAD_DRY_RUN)
2167 strvec_push(&cp.args, "-n");
2168 else
2169 strvec_push(&cp.args, "-u");
2171 if (flags & SUBMODULE_MOVE_HEAD_FORCE)
2172 strvec_push(&cp.args, "--reset");
2173 else
2174 strvec_push(&cp.args, "-m");
2176 if (!(flags & SUBMODULE_MOVE_HEAD_FORCE))
2177 strvec_push(&cp.args, old_head ? old_head : empty_tree_oid_hex());
2179 strvec_push(&cp.args, new_head ? new_head : empty_tree_oid_hex());
2181 if (run_command(&cp)) {
2182 ret = error(_("Submodule '%s' could not be updated."), path);
2183 goto out;
2186 if (!(flags & SUBMODULE_MOVE_HEAD_DRY_RUN)) {
2187 if (new_head) {
2188 child_process_init(&cp);
2189 /* also set the HEAD accordingly */
2190 cp.git_cmd = 1;
2191 cp.no_stdin = 1;
2192 cp.dir = path;
2194 prepare_submodule_repo_env(&cp.env_array);
2195 strvec_pushl(&cp.args, "update-ref", "HEAD",
2196 "--no-deref", new_head, NULL);
2198 if (run_command(&cp)) {
2199 ret = -1;
2200 goto out;
2202 } else {
2203 struct strbuf sb = STRBUF_INIT;
2205 strbuf_addf(&sb, "%s/.git", path);
2206 unlink_or_warn(sb.buf);
2207 strbuf_release(&sb);
2209 if (is_empty_dir(path))
2210 rmdir_or_warn(path);
2212 submodule_unset_core_worktree(sub);
2215 out:
2216 return ret;
2219 int validate_submodule_git_dir(char *git_dir, const char *submodule_name)
2221 size_t len = strlen(git_dir), suffix_len = strlen(submodule_name);
2222 char *p;
2223 int ret = 0;
2225 if (len <= suffix_len || (p = git_dir + len - suffix_len)[-1] != '/' ||
2226 strcmp(p, submodule_name))
2227 BUG("submodule name '%s' not a suffix of git dir '%s'",
2228 submodule_name, git_dir);
2231 * We prevent the contents of sibling submodules' git directories to
2232 * clash.
2234 * Example: having a submodule named `hippo` and another one named
2235 * `hippo/hooks` would result in the git directories
2236 * `.git/modules/hippo/` and `.git/modules/hippo/hooks/`, respectively,
2237 * but the latter directory is already designated to contain the hooks
2238 * of the former.
2240 for (; *p; p++) {
2241 if (is_dir_sep(*p)) {
2242 char c = *p;
2244 *p = '\0';
2245 if (is_git_directory(git_dir))
2246 ret = -1;
2247 *p = c;
2249 if (ret < 0)
2250 return error(_("submodule git dir '%s' is "
2251 "inside git dir '%.*s'"),
2252 git_dir,
2253 (int)(p - git_dir), git_dir);
2257 return 0;
2261 * Embeds a single submodules git directory into the superprojects git dir,
2262 * non recursively.
2264 static void relocate_single_git_dir_into_superproject(const char *path)
2266 char *old_git_dir = NULL, *real_old_git_dir = NULL, *real_new_git_dir = NULL;
2267 struct strbuf new_gitdir = STRBUF_INIT;
2268 const struct submodule *sub;
2270 if (submodule_uses_worktrees(path))
2271 die(_("relocate_gitdir for submodule '%s' with "
2272 "more than one worktree not supported"), path);
2274 old_git_dir = xstrfmt("%s/.git", path);
2275 if (read_gitfile(old_git_dir))
2276 /* If it is an actual gitfile, it doesn't need migration. */
2277 return;
2279 real_old_git_dir = real_pathdup(old_git_dir, 1);
2281 sub = submodule_from_path(the_repository, null_oid(), path);
2282 if (!sub)
2283 die(_("could not lookup name for submodule '%s'"), path);
2285 submodule_name_to_gitdir(&new_gitdir, the_repository, sub->name);
2286 if (validate_submodule_git_dir(new_gitdir.buf, sub->name) < 0)
2287 die(_("refusing to move '%s' into an existing git dir"),
2288 real_old_git_dir);
2289 if (safe_create_leading_directories_const(new_gitdir.buf) < 0)
2290 die(_("could not create directory '%s'"), new_gitdir.buf);
2291 real_new_git_dir = real_pathdup(new_gitdir.buf, 1);
2293 fprintf(stderr, _("Migrating git directory of '%s%s' from\n'%s' to\n'%s'\n"),
2294 get_super_prefix_or_empty(), path,
2295 real_old_git_dir, real_new_git_dir);
2297 relocate_gitdir(path, real_old_git_dir, real_new_git_dir);
2299 free(old_git_dir);
2300 free(real_old_git_dir);
2301 free(real_new_git_dir);
2302 strbuf_release(&new_gitdir);
2306 * Migrate the git directory of the submodule given by path from
2307 * having its git directory within the working tree to the git dir nested
2308 * in its superprojects git dir under modules/.
2310 void absorb_git_dir_into_superproject(const char *path,
2311 unsigned flags)
2313 int err_code;
2314 const char *sub_git_dir;
2315 struct strbuf gitdir = STRBUF_INIT;
2316 strbuf_addf(&gitdir, "%s/.git", path);
2317 sub_git_dir = resolve_gitdir_gently(gitdir.buf, &err_code);
2319 /* Not populated? */
2320 if (!sub_git_dir) {
2321 const struct submodule *sub;
2322 struct strbuf sub_gitdir = STRBUF_INIT;
2324 if (err_code == READ_GITFILE_ERR_STAT_FAILED) {
2325 /* unpopulated as expected */
2326 strbuf_release(&gitdir);
2327 return;
2330 if (err_code != READ_GITFILE_ERR_NOT_A_REPO)
2331 /* We don't know what broke here. */
2332 read_gitfile_error_die(err_code, path, NULL);
2335 * Maybe populated, but no git directory was found?
2336 * This can happen if the superproject is a submodule
2337 * itself and was just absorbed. The absorption of the
2338 * superproject did not rewrite the git file links yet,
2339 * fix it now.
2341 sub = submodule_from_path(the_repository, null_oid(), path);
2342 if (!sub)
2343 die(_("could not lookup name for submodule '%s'"), path);
2344 submodule_name_to_gitdir(&sub_gitdir, the_repository, sub->name);
2345 connect_work_tree_and_git_dir(path, sub_gitdir.buf, 0);
2346 strbuf_release(&sub_gitdir);
2347 } else {
2348 /* Is it already absorbed into the superprojects git dir? */
2349 char *real_sub_git_dir = real_pathdup(sub_git_dir, 1);
2350 char *real_common_git_dir = real_pathdup(get_git_common_dir(), 1);
2352 if (!starts_with(real_sub_git_dir, real_common_git_dir))
2353 relocate_single_git_dir_into_superproject(path);
2355 free(real_sub_git_dir);
2356 free(real_common_git_dir);
2358 strbuf_release(&gitdir);
2360 if (flags & ABSORB_GITDIR_RECURSE_SUBMODULES) {
2361 struct child_process cp = CHILD_PROCESS_INIT;
2362 struct strbuf sb = STRBUF_INIT;
2364 if (flags & ~ABSORB_GITDIR_RECURSE_SUBMODULES)
2365 BUG("we don't know how to pass the flags down?");
2367 strbuf_addstr(&sb, get_super_prefix_or_empty());
2368 strbuf_addstr(&sb, path);
2369 strbuf_addch(&sb, '/');
2371 cp.dir = path;
2372 cp.git_cmd = 1;
2373 cp.no_stdin = 1;
2374 strvec_pushl(&cp.args, "--super-prefix", sb.buf,
2375 "submodule--helper",
2376 "absorb-git-dirs", NULL);
2377 prepare_submodule_repo_env(&cp.env_array);
2378 if (run_command(&cp))
2379 die(_("could not recurse into submodule '%s'"), path);
2381 strbuf_release(&sb);
2385 int get_superproject_working_tree(struct strbuf *buf)
2387 struct child_process cp = CHILD_PROCESS_INIT;
2388 struct strbuf sb = STRBUF_INIT;
2389 struct strbuf one_up = STRBUF_INIT;
2390 const char *cwd = xgetcwd();
2391 int ret = 0;
2392 const char *subpath;
2393 int code;
2394 ssize_t len;
2396 if (!is_inside_work_tree())
2398 * FIXME:
2399 * We might have a superproject, but it is harder
2400 * to determine.
2402 return 0;
2404 if (!strbuf_realpath(&one_up, "../", 0))
2405 return 0;
2407 subpath = relative_path(cwd, one_up.buf, &sb);
2408 strbuf_release(&one_up);
2410 prepare_submodule_repo_env(&cp.env_array);
2411 strvec_pop(&cp.env_array);
2413 strvec_pushl(&cp.args, "--literal-pathspecs", "-C", "..",
2414 "ls-files", "-z", "--stage", "--full-name", "--",
2415 subpath, NULL);
2416 strbuf_reset(&sb);
2418 cp.no_stdin = 1;
2419 cp.no_stderr = 1;
2420 cp.out = -1;
2421 cp.git_cmd = 1;
2423 if (start_command(&cp))
2424 die(_("could not start ls-files in .."));
2426 len = strbuf_read(&sb, cp.out, PATH_MAX);
2427 close(cp.out);
2429 if (starts_with(sb.buf, "160000")) {
2430 int super_sub_len;
2431 int cwd_len = strlen(cwd);
2432 char *super_sub, *super_wt;
2435 * There is a superproject having this repo as a submodule.
2436 * The format is <mode> SP <hash> SP <stage> TAB <full name> \0,
2437 * We're only interested in the name after the tab.
2439 super_sub = strchr(sb.buf, '\t') + 1;
2440 super_sub_len = strlen(super_sub);
2442 if (super_sub_len > cwd_len ||
2443 strcmp(&cwd[cwd_len - super_sub_len], super_sub))
2444 BUG("returned path string doesn't match cwd?");
2446 super_wt = xstrdup(cwd);
2447 super_wt[cwd_len - super_sub_len] = '\0';
2449 strbuf_realpath(buf, super_wt, 1);
2450 ret = 1;
2451 free(super_wt);
2453 strbuf_release(&sb);
2455 code = finish_command(&cp);
2457 if (code == 128)
2458 /* '../' is not a git repository */
2459 return 0;
2460 if (code == 0 && len == 0)
2461 /* There is an unrelated git repository at '../' */
2462 return 0;
2463 if (code)
2464 die(_("ls-tree returned unexpected return code %d"), code);
2466 return ret;
2470 * Put the gitdir for a submodule (given relative to the main
2471 * repository worktree) into `buf`, or return -1 on error.
2473 int submodule_to_gitdir(struct strbuf *buf, const char *submodule)
2475 const struct submodule *sub;
2476 const char *git_dir;
2477 int ret = 0;
2479 strbuf_reset(buf);
2480 strbuf_addstr(buf, submodule);
2481 strbuf_complete(buf, '/');
2482 strbuf_addstr(buf, ".git");
2484 git_dir = read_gitfile(buf->buf);
2485 if (git_dir) {
2486 strbuf_reset(buf);
2487 strbuf_addstr(buf, git_dir);
2489 if (!is_git_directory(buf->buf)) {
2490 sub = submodule_from_path(the_repository, null_oid(),
2491 submodule);
2492 if (!sub) {
2493 ret = -1;
2494 goto cleanup;
2496 strbuf_reset(buf);
2497 submodule_name_to_gitdir(buf, the_repository, sub->name);
2500 cleanup:
2501 return ret;
2504 void submodule_name_to_gitdir(struct strbuf *buf, struct repository *r,
2505 const char *submodule_name)
2508 * NEEDSWORK: The current way of mapping a submodule's name to
2509 * its location in .git/modules/ has problems with some naming
2510 * schemes. For example, if a submodule is named "foo" and
2511 * another is named "foo/bar" (whether present in the same
2512 * superproject commit or not - the problem will arise if both
2513 * superproject commits have been checked out at any point in
2514 * time), or if two submodule names only have different cases in
2515 * a case-insensitive filesystem.
2517 * There are several solutions, including encoding the path in
2518 * some way, introducing a submodule.<name>.gitdir config in
2519 * .git/config (not .gitmodules) that allows overriding what the
2520 * gitdir of a submodule would be (and teach Git, upon noticing
2521 * a clash, to automatically determine a non-clashing name and
2522 * to write such a config), or introducing a
2523 * submodule.<name>.gitdir config in .gitmodules that repo
2524 * administrators can explicitly set. Nothing has been decided,
2525 * so for now, just append the name at the end of the path.
2527 strbuf_repo_git_path(buf, r, "modules/");
2528 strbuf_addstr(buf, submodule_name);