2 #include "repository.h"
4 #include "submodule-config.h"
10 #include "run-command.h"
13 #include "string-list.h"
14 #include "sha1-array.h"
15 #include "argv-array.h"
17 #include "thread-utils.h"
21 #include "parse-options.h"
23 static int config_update_recurse_submodules = RECURSE_SUBMODULES_OFF;
24 static struct string_list changed_submodule_paths = STRING_LIST_INIT_DUP;
25 static int initialized_fetch_ref_tips;
26 static struct oid_array ref_tips_before_fetch;
27 static struct oid_array ref_tips_after_fetch;
30 * Check if the .gitmodules file is unmerged. Parsing of the .gitmodules file
31 * will be disabled because we can't guess what might be configured in
32 * .gitmodules unless the user resolves the conflict.
34 int is_gitmodules_unmerged(const struct index_state *istate)
36 int pos = index_name_pos(istate, GITMODULES_FILE, strlen(GITMODULES_FILE));
37 if (pos < 0) { /* .gitmodules not found or isn't merged */
39 if (istate->cache_nr > pos) { /* there is a .gitmodules */
40 const struct cache_entry *ce = istate->cache[pos];
41 if (ce_namelen(ce) == strlen(GITMODULES_FILE) &&
42 !strcmp(ce->name, GITMODULES_FILE))
51 * Check if the .gitmodules file has unstaged modifications. This must be
52 * checked before allowing modifications to the .gitmodules file with the
53 * intention to stage them later, because when continuing we would stage the
54 * modifications the user didn't stage herself too. That might change in a
55 * future version when we learn to stage the changes we do ourselves without
56 * staging any previous modifications.
58 int is_staging_gitmodules_ok(const struct index_state *istate)
60 int pos = index_name_pos(istate, GITMODULES_FILE, strlen(GITMODULES_FILE));
62 if ((pos >= 0) && (pos < istate->cache_nr)) {
64 if (lstat(GITMODULES_FILE, &st) == 0 &&
65 ce_match_stat(istate->cache[pos], &st, 0) & DATA_CHANGED)
73 * Try to update the "path" entry in the "submodule.<name>" section of the
74 * .gitmodules file. Return 0 only if a .gitmodules file was found, a section
75 * with the correct path=<oldpath> setting was found and we could update it.
77 int update_path_in_gitmodules(const char *oldpath, const char *newpath)
79 struct strbuf entry = STRBUF_INIT;
80 const struct submodule *submodule;
82 if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */
85 if (is_gitmodules_unmerged(&the_index))
86 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
88 submodule = submodule_from_path(&null_oid, oldpath);
89 if (!submodule || !submodule->name) {
90 warning(_("Could not find section in .gitmodules where path=%s"), oldpath);
93 strbuf_addstr(&entry, "submodule.");
94 strbuf_addstr(&entry, submodule->name);
95 strbuf_addstr(&entry, ".path");
96 if (git_config_set_in_file_gently(GITMODULES_FILE, entry.buf, newpath) < 0) {
97 /* Maybe the user already did that, don't error out here */
98 warning(_("Could not update .gitmodules entry %s"), entry.buf);
99 strbuf_release(&entry);
102 strbuf_release(&entry);
107 * Try to remove the "submodule.<name>" section from .gitmodules where the given
108 * path is configured. Return 0 only if a .gitmodules file was found, a section
109 * with the correct path=<path> setting was found and we could remove it.
111 int remove_path_from_gitmodules(const char *path)
113 struct strbuf sect = STRBUF_INIT;
114 const struct submodule *submodule;
116 if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */
119 if (is_gitmodules_unmerged(&the_index))
120 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
122 submodule = submodule_from_path(&null_oid, path);
123 if (!submodule || !submodule->name) {
124 warning(_("Could not find section in .gitmodules where path=%s"), path);
127 strbuf_addstr(§, "submodule.");
128 strbuf_addstr(§, submodule->name);
129 if (git_config_rename_section_in_file(GITMODULES_FILE, sect.buf, NULL) < 0) {
130 /* Maybe the user already did that, don't error out here */
131 warning(_("Could not remove .gitmodules entry for %s"), path);
132 strbuf_release(§);
135 strbuf_release(§);
139 void stage_updated_gitmodules(void)
141 if (add_file_to_cache(GITMODULES_FILE, 0))
142 die(_("staging updated .gitmodules failed"));
145 static int add_submodule_odb(const char *path)
147 struct strbuf objects_directory = STRBUF_INIT;
150 ret = strbuf_git_path_submodule(&objects_directory, path, "objects/");
153 if (!is_directory(objects_directory.buf)) {
157 add_to_alternates_memory(objects_directory.buf);
159 strbuf_release(&objects_directory);
163 void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
166 const struct submodule *submodule = submodule_from_path(&null_oid, path);
171 key = xstrfmt("submodule.%s.ignore", submodule->name);
172 if (repo_config_get_string_const(the_repository, key, &ignore))
173 ignore = submodule->ignore;
177 handle_ignore_submodules_arg(diffopt, ignore);
178 else if (is_gitmodules_unmerged(&the_index))
179 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
183 /* Cheap function that only determines if we're interested in submodules at all */
184 int git_default_submodule_config(const char *var, const char *value, void *cb)
186 if (!strcmp(var, "submodule.recurse")) {
187 int v = git_config_bool(var, value) ?
188 RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF;
189 config_update_recurse_submodules = v;
194 int option_parse_recurse_submodules_worktree_updater(const struct option *opt,
195 const char *arg, int unset)
198 config_update_recurse_submodules = RECURSE_SUBMODULES_OFF;
202 config_update_recurse_submodules =
203 parse_update_recurse_submodules_arg(opt->long_name,
206 config_update_recurse_submodules = RECURSE_SUBMODULES_ON;
212 * Determine if a submodule has been initialized at a given 'path'
214 int is_submodule_active(struct repository *repo, const char *path)
219 const struct string_list *sl;
220 const struct submodule *module;
222 module = submodule_from_cache(repo, &null_oid, path);
224 /* early return if there isn't a path->module mapping */
228 /* submodule.<name>.active is set */
229 key = xstrfmt("submodule.%s.active", module->name);
230 if (!repo_config_get_bool(repo, key, &ret)) {
236 /* submodule.active is set */
237 sl = repo_config_get_value_multi(repo, "submodule.active");
240 struct argv_array args = ARGV_ARRAY_INIT;
241 const struct string_list_item *item;
243 for_each_string_list_item(item, sl) {
244 argv_array_push(&args, item->string);
247 parse_pathspec(&ps, 0, 0, NULL, args.argv);
248 ret = match_pathspec(&ps, path, strlen(path), 0, NULL, 1);
250 argv_array_clear(&args);
255 /* fallback to checking if the URL is set */
256 key = xstrfmt("submodule.%s.url", module->name);
257 ret = !repo_config_get_string(repo, key, &value);
264 int is_submodule_populated_gently(const char *path, int *return_error_code)
267 char *gitdir = xstrfmt("%s/.git", path);
269 if (resolve_gitdir_gently(gitdir, return_error_code))
277 * Dies if the provided 'prefix' corresponds to an unpopulated submodule
279 void die_in_unpopulated_submodule(const struct index_state *istate,
287 prefixlen = strlen(prefix);
289 for (i = 0; i < istate->cache_nr; i++) {
290 struct cache_entry *ce = istate->cache[i];
291 int ce_len = ce_namelen(ce);
293 if (!S_ISGITLINK(ce->ce_mode))
295 if (prefixlen <= ce_len)
297 if (strncmp(ce->name, prefix, ce_len))
299 if (prefix[ce_len] != '/')
302 die(_("in unpopulated submodule '%s'"), ce->name);
307 * Dies if any paths in the provided pathspec descends into a submodule
309 void die_path_inside_submodule(const struct index_state *istate,
310 const struct pathspec *ps)
314 for (i = 0; i < istate->cache_nr; i++) {
315 struct cache_entry *ce = istate->cache[i];
316 int ce_len = ce_namelen(ce);
318 if (!S_ISGITLINK(ce->ce_mode))
321 for (j = 0; j < ps->nr ; j++) {
322 const struct pathspec_item *item = &ps->items[j];
324 if (item->len <= ce_len)
326 if (item->match[ce_len] != '/')
328 if (strncmp(ce->name, item->match, ce_len))
330 if (item->len == ce_len + 1)
333 die(_("Pathspec '%s' is in submodule '%.*s'"),
334 item->original, ce_len, ce->name);
339 enum submodule_update_type parse_submodule_update_type(const char *value)
341 if (!strcmp(value, "none"))
342 return SM_UPDATE_NONE;
343 else if (!strcmp(value, "checkout"))
344 return SM_UPDATE_CHECKOUT;
345 else if (!strcmp(value, "rebase"))
346 return SM_UPDATE_REBASE;
347 else if (!strcmp(value, "merge"))
348 return SM_UPDATE_MERGE;
349 else if (*value == '!')
350 return SM_UPDATE_COMMAND;
352 return SM_UPDATE_UNSPECIFIED;
355 int parse_submodule_update_strategy(const char *value,
356 struct submodule_update_strategy *dst)
358 enum submodule_update_type type;
360 free((void*)dst->command);
363 type = parse_submodule_update_type(value);
364 if (type == SM_UPDATE_UNSPECIFIED)
368 if (type == SM_UPDATE_COMMAND)
369 dst->command = xstrdup(value + 1);
374 const char *submodule_strategy_to_string(const struct submodule_update_strategy *s)
376 struct strbuf sb = STRBUF_INIT;
378 case SM_UPDATE_CHECKOUT:
380 case SM_UPDATE_MERGE:
382 case SM_UPDATE_REBASE:
386 case SM_UPDATE_UNSPECIFIED:
388 case SM_UPDATE_COMMAND:
389 strbuf_addf(&sb, "!%s", s->command);
390 return strbuf_detach(&sb, NULL);
395 void handle_ignore_submodules_arg(struct diff_options *diffopt,
398 DIFF_OPT_CLR(diffopt, IGNORE_SUBMODULES);
399 DIFF_OPT_CLR(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
400 DIFF_OPT_CLR(diffopt, IGNORE_DIRTY_SUBMODULES);
402 if (!strcmp(arg, "all"))
403 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
404 else if (!strcmp(arg, "untracked"))
405 DIFF_OPT_SET(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
406 else if (!strcmp(arg, "dirty"))
407 DIFF_OPT_SET(diffopt, IGNORE_DIRTY_SUBMODULES);
408 else if (strcmp(arg, "none"))
409 die("bad --ignore-submodules argument: %s", arg);
412 static int prepare_submodule_summary(struct rev_info *rev, const char *path,
413 struct commit *left, struct commit *right,
414 struct commit_list *merge_bases)
416 struct commit_list *list;
418 init_revisions(rev, NULL);
419 setup_revisions(0, NULL, rev, NULL);
421 rev->first_parent_only = 1;
422 left->object.flags |= SYMMETRIC_LEFT;
423 add_pending_object(rev, &left->object, path);
424 add_pending_object(rev, &right->object, path);
425 for (list = merge_bases; list; list = list->next) {
426 list->item->object.flags |= UNINTERESTING;
427 add_pending_object(rev, &list->item->object,
428 oid_to_hex(&list->item->object.oid));
430 return prepare_revision_walk(rev);
433 static void print_submodule_summary(struct rev_info *rev, FILE *f,
434 const char *line_prefix,
435 const char *del, const char *add, const char *reset)
437 static const char format[] = " %m %s";
438 struct strbuf sb = STRBUF_INIT;
439 struct commit *commit;
441 while ((commit = get_revision(rev))) {
442 struct pretty_print_context ctx = {0};
443 ctx.date_mode = rev->date_mode;
444 ctx.output_encoding = get_log_output_encoding();
445 strbuf_setlen(&sb, 0);
446 strbuf_addstr(&sb, line_prefix);
447 if (commit->object.flags & SYMMETRIC_LEFT) {
449 strbuf_addstr(&sb, del);
452 strbuf_addstr(&sb, add);
453 format_commit_message(commit, format, &sb, &ctx);
455 strbuf_addstr(&sb, reset);
456 strbuf_addch(&sb, '\n');
457 fprintf(f, "%s", sb.buf);
462 static void prepare_submodule_repo_env_no_git_dir(struct argv_array *out)
464 const char * const *var;
466 for (var = local_repo_env; *var; var++) {
467 if (strcmp(*var, CONFIG_DATA_ENVIRONMENT))
468 argv_array_push(out, *var);
472 void prepare_submodule_repo_env(struct argv_array *out)
474 prepare_submodule_repo_env_no_git_dir(out);
475 argv_array_pushf(out, "%s=%s", GIT_DIR_ENVIRONMENT,
476 DEFAULT_GIT_DIR_ENVIRONMENT);
479 /* Helper function to display the submodule header line prior to the full
480 * summary output. If it can locate the submodule objects directory it will
481 * attempt to lookup both the left and right commits and put them into the
482 * left and right pointers.
484 static void show_submodule_header(FILE *f, const char *path,
485 const char *line_prefix,
486 struct object_id *one, struct object_id *two,
487 unsigned dirty_submodule, const char *meta,
489 struct commit **left, struct commit **right,
490 struct commit_list **merge_bases)
492 const char *message = NULL;
493 struct strbuf sb = STRBUF_INIT;
494 int fast_forward = 0, fast_backward = 0;
496 if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
497 fprintf(f, "%sSubmodule %s contains untracked content\n",
499 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
500 fprintf(f, "%sSubmodule %s contains modified content\n",
503 if (is_null_oid(one))
504 message = "(new submodule)";
505 else if (is_null_oid(two))
506 message = "(submodule deleted)";
508 if (add_submodule_odb(path)) {
510 message = "(not initialized)";
515 * Attempt to lookup the commit references, and determine if this is
516 * a fast forward or fast backwards update.
518 *left = lookup_commit_reference(one);
519 *right = lookup_commit_reference(two);
522 * Warn about missing commits in the submodule project, but only if
525 if ((!is_null_oid(one) && !*left) ||
526 (!is_null_oid(two) && !*right))
527 message = "(commits not present)";
529 *merge_bases = get_merge_bases(*left, *right);
531 if ((*merge_bases)->item == *left)
533 else if ((*merge_bases)->item == *right)
537 if (!oidcmp(one, two)) {
543 strbuf_addf(&sb, "%s%sSubmodule %s ", line_prefix, meta, path);
544 strbuf_add_unique_abbrev(&sb, one->hash, DEFAULT_ABBREV);
545 strbuf_addstr(&sb, (fast_backward || fast_forward) ? ".." : "...");
546 strbuf_add_unique_abbrev(&sb, two->hash, DEFAULT_ABBREV);
548 strbuf_addf(&sb, " %s%s\n", message, reset);
550 strbuf_addf(&sb, "%s:%s\n", fast_backward ? " (rewind)" : "", reset);
551 fwrite(sb.buf, sb.len, 1, f);
556 void show_submodule_summary(FILE *f, const char *path,
557 const char *line_prefix,
558 struct object_id *one, struct object_id *two,
559 unsigned dirty_submodule, const char *meta,
560 const char *del, const char *add, const char *reset)
563 struct commit *left = NULL, *right = NULL;
564 struct commit_list *merge_bases = NULL;
566 show_submodule_header(f, path, line_prefix, one, two, dirty_submodule,
567 meta, reset, &left, &right, &merge_bases);
570 * If we don't have both a left and a right pointer, there is no
571 * reason to try and display a summary. The header line should contain
572 * all the information the user needs.
577 /* Treat revision walker failure the same as missing commits */
578 if (prepare_submodule_summary(&rev, path, left, right, merge_bases)) {
579 fprintf(f, "%s(revision walker failed)\n", line_prefix);
583 print_submodule_summary(&rev, f, line_prefix, del, add, reset);
587 free_commit_list(merge_bases);
588 clear_commit_marks(left, ~0);
589 clear_commit_marks(right, ~0);
592 void show_submodule_inline_diff(FILE *f, const char *path,
593 const char *line_prefix,
594 struct object_id *one, struct object_id *two,
595 unsigned dirty_submodule, const char *meta,
596 const char *del, const char *add, const char *reset,
597 const struct diff_options *o)
599 const struct object_id *old = &empty_tree_oid, *new = &empty_tree_oid;
600 struct commit *left = NULL, *right = NULL;
601 struct commit_list *merge_bases = NULL;
602 struct strbuf submodule_dir = STRBUF_INIT;
603 struct child_process cp = CHILD_PROCESS_INIT;
605 show_submodule_header(f, path, line_prefix, one, two, dirty_submodule,
606 meta, reset, &left, &right, &merge_bases);
608 /* We need a valid left and right commit to display a difference */
609 if (!(left || is_null_oid(one)) ||
610 !(right || is_null_oid(two)))
621 cp.out = dup(fileno(f));
624 /* TODO: other options may need to be passed here. */
625 argv_array_pushl(&cp.args, "diff", "--submodule=diff", NULL);
627 argv_array_pushf(&cp.args, "--line-prefix=%s", line_prefix);
628 if (DIFF_OPT_TST(o, REVERSE_DIFF)) {
629 argv_array_pushf(&cp.args, "--src-prefix=%s%s/",
631 argv_array_pushf(&cp.args, "--dst-prefix=%s%s/",
634 argv_array_pushf(&cp.args, "--src-prefix=%s%s/",
636 argv_array_pushf(&cp.args, "--dst-prefix=%s%s/",
639 argv_array_push(&cp.args, oid_to_hex(old));
641 * If the submodule has modified content, we will diff against the
642 * work tree, under the assumption that the user has asked for the
643 * diff format and wishes to actually see all differences even if they
644 * haven't yet been committed to the submodule yet.
646 if (!(dirty_submodule & DIRTY_SUBMODULE_MODIFIED))
647 argv_array_push(&cp.args, oid_to_hex(new));
649 prepare_submodule_repo_env(&cp.env_array);
650 if (run_command(&cp))
651 fprintf(f, "(diff failed)\n");
654 strbuf_release(&submodule_dir);
656 free_commit_list(merge_bases);
658 clear_commit_marks(left, ~0);
660 clear_commit_marks(right, ~0);
663 int should_update_submodules(void)
665 return config_update_recurse_submodules == RECURSE_SUBMODULES_ON;
668 const struct submodule *submodule_from_ce(const struct cache_entry *ce)
670 if (!S_ISGITLINK(ce->ce_mode))
673 if (!should_update_submodules())
676 return submodule_from_path(&null_oid, ce->name);
679 static struct oid_array *submodule_commits(struct string_list *submodules,
682 struct string_list_item *item;
684 item = string_list_insert(submodules, path);
686 return (struct oid_array *) item->util;
688 /* NEEDSWORK: should we have oid_array_init()? */
689 item->util = xcalloc(1, sizeof(struct oid_array));
690 return (struct oid_array *) item->util;
693 static void collect_changed_submodules_cb(struct diff_queue_struct *q,
694 struct diff_options *options,
698 struct string_list *changed = data;
700 for (i = 0; i < q->nr; i++) {
701 struct diff_filepair *p = q->queue[i];
702 struct oid_array *commits;
703 if (!S_ISGITLINK(p->two->mode))
706 if (S_ISGITLINK(p->one->mode)) {
708 * NEEDSWORK: We should honor the name configured in
709 * the .gitmodules file of the commit we are examining
710 * here to be able to correctly follow submodules
711 * being moved around.
713 commits = submodule_commits(changed, p->two->path);
714 oid_array_append(commits, &p->two->oid);
716 /* Submodule is new or was moved here */
718 * NEEDSWORK: When the .git directories of submodules
719 * live inside the superprojects .git directory some
720 * day we should fetch new submodules directly into
721 * that location too when config or options request
722 * that so they can be checked out from there.
730 * Collect the paths of submodules in 'changed' which have changed based on
731 * the revisions as specified in 'argv'. Each entry in 'changed' will also
732 * have a corresponding 'struct oid_array' (in the 'util' field) which lists
733 * what the submodule pointers were updated to during the change.
735 static void collect_changed_submodules(struct string_list *changed,
736 struct argv_array *argv)
739 const struct commit *commit;
741 init_revisions(&rev, NULL);
742 setup_revisions(argv->argc, argv->argv, &rev, NULL);
743 if (prepare_revision_walk(&rev))
744 die("revision walk setup failed");
746 while ((commit = get_revision(&rev))) {
747 struct rev_info diff_rev;
749 init_revisions(&diff_rev, NULL);
750 diff_rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
751 diff_rev.diffopt.format_callback = collect_changed_submodules_cb;
752 diff_rev.diffopt.format_callback_data = changed;
753 diff_tree_combined_merge(commit, 1, &diff_rev);
756 reset_revision_walk();
759 static void free_submodules_oids(struct string_list *submodules)
761 struct string_list_item *item;
762 for_each_string_list_item(item, submodules)
763 oid_array_clear((struct oid_array *) item->util);
764 string_list_clear(submodules, 1);
767 static int has_remote(const char *refname, const struct object_id *oid,
768 int flags, void *cb_data)
773 static int append_oid_to_argv(const struct object_id *oid, void *data)
775 struct argv_array *argv = data;
776 argv_array_push(argv, oid_to_hex(oid));
780 static int check_has_commit(const struct object_id *oid, void *data)
782 int *has_commit = data;
784 if (!lookup_commit_reference(oid))
790 static int submodule_has_commits(const char *path, struct oid_array *commits)
795 * Perform a cheap, but incorrect check for the existence of 'commits'.
796 * This is done by adding the submodule's object store to the in-core
797 * object store, and then querying for each commit's existence. If we
798 * do not have the commit object anywhere, there is no chance we have
799 * it in the object store of the correct submodule and have it
800 * reachable from a ref, so we can fail early without spawning rev-list
801 * which is expensive.
803 if (add_submodule_odb(path))
806 oid_array_for_each_unique(commits, check_has_commit, &has_commit);
810 * Even if the submodule is checked out and the commit is
811 * present, make sure it exists in the submodule's object store
812 * and that it is reachable from a ref.
814 struct child_process cp = CHILD_PROCESS_INIT;
815 struct strbuf out = STRBUF_INIT;
817 argv_array_pushl(&cp.args, "rev-list", "-n", "1", NULL);
818 oid_array_for_each_unique(commits, append_oid_to_argv, &cp.args);
819 argv_array_pushl(&cp.args, "--not", "--all", NULL);
821 prepare_submodule_repo_env(&cp.env_array);
826 if (capture_command(&cp, &out, GIT_MAX_HEXSZ + 1) || out.len)
829 strbuf_release(&out);
835 static int submodule_needs_pushing(const char *path, struct oid_array *commits)
837 if (!submodule_has_commits(path, commits))
839 * NOTE: We do consider it safe to return "no" here. The
840 * correct answer would be "We do not know" instead of
841 * "No push needed", but it is quite hard to change
842 * the submodule pointer without having the submodule
843 * around. If a user did however change the submodules
844 * without having the submodule around, this indicates
845 * an expert who knows what they are doing or a
846 * maintainer integrating work from other people. In
847 * both cases it should be safe to skip this check.
851 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
852 struct child_process cp = CHILD_PROCESS_INIT;
853 struct strbuf buf = STRBUF_INIT;
854 int needs_pushing = 0;
856 argv_array_push(&cp.args, "rev-list");
857 oid_array_for_each_unique(commits, append_oid_to_argv, &cp.args);
858 argv_array_pushl(&cp.args, "--not", "--remotes", "-n", "1" , NULL);
860 prepare_submodule_repo_env(&cp.env_array);
865 if (start_command(&cp))
866 die("Could not run 'git rev-list <commits> --not --remotes -n 1' command in submodule %s",
868 if (strbuf_read(&buf, cp.out, 41))
872 strbuf_release(&buf);
873 return needs_pushing;
879 int find_unpushed_submodules(struct oid_array *commits,
880 const char *remotes_name, struct string_list *needs_pushing)
882 struct string_list submodules = STRING_LIST_INIT_DUP;
883 struct string_list_item *submodule;
884 struct argv_array argv = ARGV_ARRAY_INIT;
886 /* argv.argv[0] will be ignored by setup_revisions */
887 argv_array_push(&argv, "find_unpushed_submodules");
888 oid_array_for_each_unique(commits, append_oid_to_argv, &argv);
889 argv_array_push(&argv, "--not");
890 argv_array_pushf(&argv, "--remotes=%s", remotes_name);
892 collect_changed_submodules(&submodules, &argv);
894 for_each_string_list_item(submodule, &submodules) {
895 struct oid_array *commits = submodule->util;
896 const char *path = submodule->string;
898 if (submodule_needs_pushing(path, commits))
899 string_list_insert(needs_pushing, path);
902 free_submodules_oids(&submodules);
903 argv_array_clear(&argv);
905 return needs_pushing->nr;
908 static int push_submodule(const char *path,
909 const struct remote *remote,
910 const char **refspec, int refspec_nr,
911 const struct string_list *push_options,
914 if (add_submodule_odb(path))
917 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
918 struct child_process cp = CHILD_PROCESS_INIT;
919 argv_array_push(&cp.args, "push");
921 argv_array_push(&cp.args, "--dry-run");
923 if (push_options && push_options->nr) {
924 const struct string_list_item *item;
925 for_each_string_list_item(item, push_options)
926 argv_array_pushf(&cp.args, "--push-option=%s",
930 if (remote->origin != REMOTE_UNCONFIGURED) {
932 argv_array_push(&cp.args, remote->name);
933 for (i = 0; i < refspec_nr; i++)
934 argv_array_push(&cp.args, refspec[i]);
937 prepare_submodule_repo_env(&cp.env_array);
941 if (run_command(&cp))
950 * Perform a check in the submodule to see if the remote and refspec work.
951 * Die if the submodule can't be pushed.
953 static void submodule_push_check(const char *path, const struct remote *remote,
954 const char **refspec, int refspec_nr)
956 struct child_process cp = CHILD_PROCESS_INIT;
959 argv_array_push(&cp.args, "submodule--helper");
960 argv_array_push(&cp.args, "push-check");
961 argv_array_push(&cp.args, remote->name);
963 for (i = 0; i < refspec_nr; i++)
964 argv_array_push(&cp.args, refspec[i]);
966 prepare_submodule_repo_env(&cp.env_array);
973 * Simply indicate if 'submodule--helper push-check' failed.
974 * More detailed error information will be provided by the
977 if (run_command(&cp))
978 die("process for submodule '%s' failed", path);
981 int push_unpushed_submodules(struct oid_array *commits,
982 const struct remote *remote,
983 const char **refspec, int refspec_nr,
984 const struct string_list *push_options,
988 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
990 if (!find_unpushed_submodules(commits, remote->name, &needs_pushing))
994 * Verify that the remote and refspec can be propagated to all
995 * submodules. This check can be skipped if the remote and refspec
996 * won't be propagated due to the remote being unconfigured (e.g. a URL
997 * instead of a remote name).
999 if (remote->origin != REMOTE_UNCONFIGURED)
1000 for (i = 0; i < needs_pushing.nr; i++)
1001 submodule_push_check(needs_pushing.items[i].string,
1002 remote, refspec, refspec_nr);
1004 /* Actually push the submodules */
1005 for (i = 0; i < needs_pushing.nr; i++) {
1006 const char *path = needs_pushing.items[i].string;
1007 fprintf(stderr, "Pushing submodule '%s'\n", path);
1008 if (!push_submodule(path, remote, refspec, refspec_nr,
1009 push_options, dry_run)) {
1010 fprintf(stderr, "Unable to push submodule '%s'\n", path);
1015 string_list_clear(&needs_pushing, 0);
1020 static int append_oid_to_array(const char *ref, const struct object_id *oid,
1021 int flags, void *data)
1023 struct oid_array *array = data;
1024 oid_array_append(array, oid);
1028 void check_for_new_submodule_commits(struct object_id *oid)
1030 if (!initialized_fetch_ref_tips) {
1031 for_each_ref(append_oid_to_array, &ref_tips_before_fetch);
1032 initialized_fetch_ref_tips = 1;
1035 oid_array_append(&ref_tips_after_fetch, oid);
1038 static void calculate_changed_submodule_paths(void)
1040 struct argv_array argv = ARGV_ARRAY_INIT;
1041 struct string_list changed_submodules = STRING_LIST_INIT_DUP;
1042 const struct string_list_item *item;
1044 /* No need to check if there are no submodules configured */
1045 if (!submodule_from_path(NULL, NULL))
1048 argv_array_push(&argv, "--"); /* argv[0] program name */
1049 oid_array_for_each_unique(&ref_tips_after_fetch,
1050 append_oid_to_argv, &argv);
1051 argv_array_push(&argv, "--not");
1052 oid_array_for_each_unique(&ref_tips_before_fetch,
1053 append_oid_to_argv, &argv);
1056 * Collect all submodules (whether checked out or not) for which new
1057 * commits have been recorded upstream in "changed_submodule_paths".
1059 collect_changed_submodules(&changed_submodules, &argv);
1061 for_each_string_list_item(item, &changed_submodules) {
1062 struct oid_array *commits = item->util;
1063 const char *path = item->string;
1065 if (!submodule_has_commits(path, commits))
1066 string_list_append(&changed_submodule_paths, path);
1069 free_submodules_oids(&changed_submodules);
1070 argv_array_clear(&argv);
1071 oid_array_clear(&ref_tips_before_fetch);
1072 oid_array_clear(&ref_tips_after_fetch);
1073 initialized_fetch_ref_tips = 0;
1076 int submodule_touches_in_range(struct object_id *excl_oid,
1077 struct object_id *incl_oid)
1079 struct string_list subs = STRING_LIST_INIT_DUP;
1080 struct argv_array args = ARGV_ARRAY_INIT;
1083 /* No need to check if there are no submodules configured */
1084 if (!submodule_from_path(NULL, NULL))
1087 argv_array_push(&args, "--"); /* args[0] program name */
1088 argv_array_push(&args, oid_to_hex(incl_oid));
1089 argv_array_push(&args, "--not");
1090 argv_array_push(&args, oid_to_hex(excl_oid));
1092 collect_changed_submodules(&subs, &args);
1095 argv_array_clear(&args);
1097 free_submodules_oids(&subs);
1101 struct submodule_parallel_fetch {
1103 struct argv_array args;
1104 const char *work_tree;
1106 int command_line_option;
1111 #define SPF_INIT {0, ARGV_ARRAY_INIT, NULL, NULL, 0, 0, 0, 0}
1113 static int get_next_submodule(struct child_process *cp,
1114 struct strbuf *err, void *data, void **task_cb)
1117 struct submodule_parallel_fetch *spf = data;
1119 for (; spf->count < active_nr; spf->count++) {
1120 struct strbuf submodule_path = STRBUF_INIT;
1121 struct strbuf submodule_git_dir = STRBUF_INIT;
1122 struct strbuf submodule_prefix = STRBUF_INIT;
1123 const struct cache_entry *ce = active_cache[spf->count];
1124 const char *git_dir, *default_argv;
1125 const struct submodule *submodule;
1127 if (!S_ISGITLINK(ce->ce_mode))
1130 submodule = submodule_from_path(&null_oid, ce->name);
1132 default_argv = "yes";
1133 if (spf->command_line_option == RECURSE_SUBMODULES_DEFAULT) {
1134 int fetch_recurse = RECURSE_SUBMODULES_NONE;
1140 fetch_recurse = submodule->fetch_recurse;
1141 key = xstrfmt("submodule.%s.fetchRecurseSubmodules", submodule->name);
1142 if (!repo_config_get_string_const(the_repository, key, &value)) {
1143 fetch_recurse = parse_fetch_recurse_submodules_arg(key, value);
1148 if (fetch_recurse != RECURSE_SUBMODULES_NONE) {
1149 if (fetch_recurse == RECURSE_SUBMODULES_OFF)
1151 if (fetch_recurse == RECURSE_SUBMODULES_ON_DEMAND) {
1152 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
1154 default_argv = "on-demand";
1157 if (spf->default_option == RECURSE_SUBMODULES_OFF)
1159 if (spf->default_option == RECURSE_SUBMODULES_ON_DEMAND) {
1160 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
1162 default_argv = "on-demand";
1165 } else if (spf->command_line_option == RECURSE_SUBMODULES_ON_DEMAND) {
1166 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
1168 default_argv = "on-demand";
1171 strbuf_addf(&submodule_path, "%s/%s", spf->work_tree, ce->name);
1172 strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf);
1173 strbuf_addf(&submodule_prefix, "%s%s/", spf->prefix, ce->name);
1174 git_dir = read_gitfile(submodule_git_dir.buf);
1176 git_dir = submodule_git_dir.buf;
1177 if (is_directory(git_dir)) {
1178 child_process_init(cp);
1179 cp->dir = strbuf_detach(&submodule_path, NULL);
1180 prepare_submodule_repo_env(&cp->env_array);
1183 strbuf_addf(err, "Fetching submodule %s%s\n",
1184 spf->prefix, ce->name);
1185 argv_array_init(&cp->args);
1186 argv_array_pushv(&cp->args, spf->args.argv);
1187 argv_array_push(&cp->args, default_argv);
1188 argv_array_push(&cp->args, "--submodule-prefix");
1189 argv_array_push(&cp->args, submodule_prefix.buf);
1192 strbuf_release(&submodule_path);
1193 strbuf_release(&submodule_git_dir);
1194 strbuf_release(&submodule_prefix);
1203 static int fetch_start_failure(struct strbuf *err,
1204 void *cb, void *task_cb)
1206 struct submodule_parallel_fetch *spf = cb;
1213 static int fetch_finish(int retvalue, struct strbuf *err,
1214 void *cb, void *task_cb)
1216 struct submodule_parallel_fetch *spf = cb;
1224 int fetch_populated_submodules(const struct argv_array *options,
1225 const char *prefix, int command_line_option,
1227 int quiet, int max_parallel_jobs)
1230 struct submodule_parallel_fetch spf = SPF_INIT;
1232 spf.work_tree = get_git_work_tree();
1233 spf.command_line_option = command_line_option;
1234 spf.default_option = default_option;
1236 spf.prefix = prefix;
1241 if (read_cache() < 0)
1242 die("index file corrupt");
1244 argv_array_push(&spf.args, "fetch");
1245 for (i = 0; i < options->argc; i++)
1246 argv_array_push(&spf.args, options->argv[i]);
1247 argv_array_push(&spf.args, "--recurse-submodules-default");
1248 /* default value, "--submodule-prefix" and its value are added later */
1250 calculate_changed_submodule_paths();
1251 run_processes_parallel(max_parallel_jobs,
1253 fetch_start_failure,
1257 argv_array_clear(&spf.args);
1259 string_list_clear(&changed_submodule_paths, 1);
1263 unsigned is_submodule_modified(const char *path, int ignore_untracked)
1265 struct child_process cp = CHILD_PROCESS_INIT;
1266 struct strbuf buf = STRBUF_INIT;
1268 unsigned dirty_submodule = 0;
1269 const char *git_dir;
1270 int ignore_cp_exit_code = 0;
1272 strbuf_addf(&buf, "%s/.git", path);
1273 git_dir = read_gitfile(buf.buf);
1276 if (!is_git_directory(git_dir)) {
1277 if (is_directory(git_dir))
1278 die(_("'%s' not recognized as a git repository"), git_dir);
1279 strbuf_release(&buf);
1280 /* The submodule is not checked out, so it is not modified */
1285 argv_array_pushl(&cp.args, "status", "--porcelain=2", NULL);
1286 if (ignore_untracked)
1287 argv_array_push(&cp.args, "-uno");
1289 prepare_submodule_repo_env(&cp.env_array);
1294 if (start_command(&cp))
1295 die("Could not run 'git status --porcelain=2' in submodule %s", path);
1297 fp = xfdopen(cp.out, "r");
1298 while (strbuf_getwholeline(&buf, fp, '\n') != EOF) {
1299 /* regular untracked files */
1300 if (buf.buf[0] == '?')
1301 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
1303 if (buf.buf[0] == 'u' ||
1304 buf.buf[0] == '1' ||
1305 buf.buf[0] == '2') {
1306 /* T = line type, XY = status, SSSS = submodule state */
1307 if (buf.len < strlen("T XY SSSS"))
1308 die("BUG: invalid status --porcelain=2 line %s",
1311 if (buf.buf[5] == 'S' && buf.buf[8] == 'U')
1312 /* nested untracked file */
1313 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
1315 if (buf.buf[0] == 'u' ||
1316 buf.buf[0] == '2' ||
1317 memcmp(buf.buf + 5, "S..U", 4))
1319 dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
1322 if ((dirty_submodule & DIRTY_SUBMODULE_MODIFIED) &&
1323 ((dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ||
1324 ignore_untracked)) {
1326 * We're not interested in any further information from
1327 * the child any more, neither output nor its exit code.
1329 ignore_cp_exit_code = 1;
1335 if (finish_command(&cp) && !ignore_cp_exit_code)
1336 die("'git status --porcelain=2' failed in submodule %s", path);
1338 strbuf_release(&buf);
1339 return dirty_submodule;
1342 int submodule_uses_gitfile(const char *path)
1344 struct child_process cp = CHILD_PROCESS_INIT;
1345 const char *argv[] = {
1353 struct strbuf buf = STRBUF_INIT;
1354 const char *git_dir;
1356 strbuf_addf(&buf, "%s/.git", path);
1357 git_dir = read_gitfile(buf.buf);
1359 strbuf_release(&buf);
1362 strbuf_release(&buf);
1364 /* Now test that all nested submodules use a gitfile too */
1366 prepare_submodule_repo_env(&cp.env_array);
1372 if (run_command(&cp))
1379 * Check if it is a bad idea to remove a submodule, i.e. if we'd lose data
1382 * Return 1 if we'd lose data, return 0 if the removal is fine,
1383 * and negative values for errors.
1385 int bad_to_remove_submodule(const char *path, unsigned flags)
1388 struct child_process cp = CHILD_PROCESS_INIT;
1389 struct strbuf buf = STRBUF_INIT;
1392 if (!file_exists(path) || is_empty_dir(path))
1395 if (!submodule_uses_gitfile(path))
1398 argv_array_pushl(&cp.args, "status", "--porcelain",
1399 "--ignore-submodules=none", NULL);
1401 if (flags & SUBMODULE_REMOVAL_IGNORE_UNTRACKED)
1402 argv_array_push(&cp.args, "-uno");
1404 argv_array_push(&cp.args, "-uall");
1406 if (!(flags & SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED))
1407 argv_array_push(&cp.args, "--ignored");
1409 prepare_submodule_repo_env(&cp.env_array);
1414 if (start_command(&cp)) {
1415 if (flags & SUBMODULE_REMOVAL_DIE_ON_ERROR)
1416 die(_("could not start 'git status' in submodule '%s'"),
1422 len = strbuf_read(&buf, cp.out, 1024);
1427 if (finish_command(&cp)) {
1428 if (flags & SUBMODULE_REMOVAL_DIE_ON_ERROR)
1429 die(_("could not run 'git status' in submodule '%s'"),
1434 strbuf_release(&buf);
1438 static const char *get_super_prefix_or_empty(void)
1440 const char *s = get_super_prefix();
1446 static int submodule_has_dirty_index(const struct submodule *sub)
1448 struct child_process cp = CHILD_PROCESS_INIT;
1450 prepare_submodule_repo_env(&cp.env_array);
1453 argv_array_pushl(&cp.args, "diff-index", "--quiet",
1454 "--cached", "HEAD", NULL);
1458 if (start_command(&cp))
1459 die("could not recurse into submodule '%s'", sub->path);
1461 return finish_command(&cp);
1464 static void submodule_reset_index(const char *path)
1466 struct child_process cp = CHILD_PROCESS_INIT;
1467 prepare_submodule_repo_env(&cp.env_array);
1473 argv_array_pushf(&cp.args, "--super-prefix=%s%s/",
1474 get_super_prefix_or_empty(), path);
1475 argv_array_pushl(&cp.args, "read-tree", "-u", "--reset", NULL);
1477 argv_array_push(&cp.args, EMPTY_TREE_SHA1_HEX);
1479 if (run_command(&cp))
1480 die("could not reset submodule index");
1484 * Moves a submodule at a given path from a given head to another new head.
1485 * For edge cases (a submodule coming into existence or removing a submodule)
1486 * pass NULL for old or new respectively.
1488 int submodule_move_head(const char *path,
1494 struct child_process cp = CHILD_PROCESS_INIT;
1495 const struct submodule *sub;
1496 int *error_code_ptr, error_code;
1498 if (!is_submodule_active(the_repository, path))
1501 if (flags & SUBMODULE_MOVE_HEAD_FORCE)
1503 * Pass non NULL pointer to is_submodule_populated_gently
1504 * to prevent die()-ing. We'll use connect_work_tree_and_git_dir
1505 * to fixup the submodule in the force case later.
1507 error_code_ptr = &error_code;
1509 error_code_ptr = NULL;
1511 if (old && !is_submodule_populated_gently(path, error_code_ptr))
1514 sub = submodule_from_path(&null_oid, path);
1517 die("BUG: could not get submodule information for '%s'", path);
1519 if (old && !(flags & SUBMODULE_MOVE_HEAD_FORCE)) {
1520 /* Check if the submodule has a dirty index. */
1521 if (submodule_has_dirty_index(sub))
1522 return error(_("submodule '%s' has dirty index"), path);
1525 if (!(flags & SUBMODULE_MOVE_HEAD_DRY_RUN)) {
1527 if (!submodule_uses_gitfile(path))
1528 absorb_git_dir_into_superproject("", path,
1529 ABSORB_GITDIR_RECURSE_SUBMODULES);
1531 char *gitdir = xstrfmt("%s/modules/%s",
1532 get_git_common_dir(), sub->name);
1533 connect_work_tree_and_git_dir(path, gitdir);
1536 /* make sure the index is clean as well */
1537 submodule_reset_index(path);
1540 if (old && (flags & SUBMODULE_MOVE_HEAD_FORCE)) {
1541 char *gitdir = xstrfmt("%s/modules/%s",
1542 get_git_common_dir(), sub->name);
1543 connect_work_tree_and_git_dir(path, gitdir);
1548 prepare_submodule_repo_env(&cp.env_array);
1554 argv_array_pushf(&cp.args, "--super-prefix=%s%s/",
1555 get_super_prefix_or_empty(), path);
1556 argv_array_pushl(&cp.args, "read-tree", "--recurse-submodules", NULL);
1558 if (flags & SUBMODULE_MOVE_HEAD_DRY_RUN)
1559 argv_array_push(&cp.args, "-n");
1561 argv_array_push(&cp.args, "-u");
1563 if (flags & SUBMODULE_MOVE_HEAD_FORCE)
1564 argv_array_push(&cp.args, "--reset");
1566 argv_array_push(&cp.args, "-m");
1568 argv_array_push(&cp.args, old ? old : EMPTY_TREE_SHA1_HEX);
1569 argv_array_push(&cp.args, new ? new : EMPTY_TREE_SHA1_HEX);
1571 if (run_command(&cp)) {
1576 if (!(flags & SUBMODULE_MOVE_HEAD_DRY_RUN)) {
1578 child_process_init(&cp);
1579 /* also set the HEAD accordingly */
1584 prepare_submodule_repo_env(&cp.env_array);
1585 argv_array_pushl(&cp.args, "update-ref", "HEAD", new, NULL);
1587 if (run_command(&cp)) {
1592 struct strbuf sb = STRBUF_INIT;
1594 strbuf_addf(&sb, "%s/.git", path);
1595 unlink_or_warn(sb.buf);
1596 strbuf_release(&sb);
1598 if (is_empty_dir(path))
1599 rmdir_or_warn(path);
1606 static int find_first_merges(struct object_array *result, const char *path,
1607 struct commit *a, struct commit *b)
1610 struct object_array merges = OBJECT_ARRAY_INIT;
1611 struct commit *commit;
1612 int contains_another;
1614 char merged_revision[42];
1615 const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path",
1616 "--all", merged_revision, NULL };
1617 struct rev_info revs;
1618 struct setup_revision_opt rev_opts;
1620 memset(result, 0, sizeof(struct object_array));
1621 memset(&rev_opts, 0, sizeof(rev_opts));
1623 /* get all revisions that merge commit a */
1624 xsnprintf(merged_revision, sizeof(merged_revision), "^%s",
1625 oid_to_hex(&a->object.oid));
1626 init_revisions(&revs, NULL);
1627 rev_opts.submodule = path;
1628 setup_revisions(ARRAY_SIZE(rev_args)-1, rev_args, &revs, &rev_opts);
1630 /* save all revisions from the above list that contain b */
1631 if (prepare_revision_walk(&revs))
1632 die("revision walk setup failed");
1633 while ((commit = get_revision(&revs)) != NULL) {
1634 struct object *o = &(commit->object);
1635 if (in_merge_bases(b, commit))
1636 add_object_array(o, NULL, &merges);
1638 reset_revision_walk();
1640 /* Now we've got all merges that contain a and b. Prune all
1641 * merges that contain another found merge and save them in
1644 for (i = 0; i < merges.nr; i++) {
1645 struct commit *m1 = (struct commit *) merges.objects[i].item;
1647 contains_another = 0;
1648 for (j = 0; j < merges.nr; j++) {
1649 struct commit *m2 = (struct commit *) merges.objects[j].item;
1650 if (i != j && in_merge_bases(m2, m1)) {
1651 contains_another = 1;
1656 if (!contains_another)
1657 add_object_array(merges.objects[i].item, NULL, result);
1660 free(merges.objects);
1664 static void print_commit(struct commit *commit)
1666 struct strbuf sb = STRBUF_INIT;
1667 struct pretty_print_context ctx = {0};
1668 ctx.date_mode.type = DATE_NORMAL;
1669 format_commit_message(commit, " %h: %m %s", &sb, &ctx);
1670 fprintf(stderr, "%s\n", sb.buf);
1671 strbuf_release(&sb);
1674 #define MERGE_WARNING(path, msg) \
1675 warning("Failed to merge submodule %s (%s)", path, msg);
1677 int merge_submodule(struct object_id *result, const char *path,
1678 const struct object_id *base, const struct object_id *a,
1679 const struct object_id *b, int search)
1681 struct commit *commit_base, *commit_a, *commit_b;
1683 struct object_array merges;
1687 /* store a in result in case we fail */
1690 /* we can not handle deletion conflicts */
1691 if (is_null_oid(base))
1698 if (add_submodule_odb(path)) {
1699 MERGE_WARNING(path, "not checked out");
1703 if (!(commit_base = lookup_commit_reference(base)) ||
1704 !(commit_a = lookup_commit_reference(a)) ||
1705 !(commit_b = lookup_commit_reference(b))) {
1706 MERGE_WARNING(path, "commits not present");
1710 /* check whether both changes are forward */
1711 if (!in_merge_bases(commit_base, commit_a) ||
1712 !in_merge_bases(commit_base, commit_b)) {
1713 MERGE_WARNING(path, "commits don't follow merge-base");
1717 /* Case #1: a is contained in b or vice versa */
1718 if (in_merge_bases(commit_a, commit_b)) {
1722 if (in_merge_bases(commit_b, commit_a)) {
1728 * Case #2: There are one or more merges that contain a and b in
1729 * the submodule. If there is only one, then present it as a
1730 * suggestion to the user, but leave it marked unmerged so the
1731 * user needs to confirm the resolution.
1734 /* Skip the search if makes no sense to the calling context. */
1738 /* find commit which merges them */
1739 parent_count = find_first_merges(&merges, path, commit_a, commit_b);
1740 switch (parent_count) {
1742 MERGE_WARNING(path, "merge following commits not found");
1746 MERGE_WARNING(path, "not fast-forward");
1747 fprintf(stderr, "Found a possible merge resolution "
1748 "for the submodule:\n");
1749 print_commit((struct commit *) merges.objects[0].item);
1751 "If this is correct simply add it to the index "
1754 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
1755 "which will accept this suggestion.\n",
1756 oid_to_hex(&merges.objects[0].item->oid), path);
1760 MERGE_WARNING(path, "multiple merges found");
1761 for (i = 0; i < merges.nr; i++)
1762 print_commit((struct commit *) merges.objects[i].item);
1765 free(merges.objects);
1770 * Embeds a single submodules git directory into the superprojects git dir,
1773 static void relocate_single_git_dir_into_superproject(const char *prefix,
1776 char *old_git_dir = NULL, *real_old_git_dir = NULL, *real_new_git_dir = NULL;
1777 const char *new_git_dir;
1778 const struct submodule *sub;
1780 if (submodule_uses_worktrees(path))
1781 die(_("relocate_gitdir for submodule '%s' with "
1782 "more than one worktree not supported"), path);
1784 old_git_dir = xstrfmt("%s/.git", path);
1785 if (read_gitfile(old_git_dir))
1786 /* If it is an actual gitfile, it doesn't need migration. */
1789 real_old_git_dir = real_pathdup(old_git_dir, 1);
1791 sub = submodule_from_path(&null_oid, path);
1793 die(_("could not lookup name for submodule '%s'"), path);
1795 new_git_dir = git_path("modules/%s", sub->name);
1796 if (safe_create_leading_directories_const(new_git_dir) < 0)
1797 die(_("could not create directory '%s'"), new_git_dir);
1798 real_new_git_dir = real_pathdup(new_git_dir, 1);
1800 fprintf(stderr, _("Migrating git directory of '%s%s' from\n'%s' to\n'%s'\n"),
1801 get_super_prefix_or_empty(), path,
1802 real_old_git_dir, real_new_git_dir);
1804 relocate_gitdir(path, real_old_git_dir, real_new_git_dir);
1807 free(real_old_git_dir);
1808 free(real_new_git_dir);
1812 * Migrate the git directory of the submodule given by path from
1813 * having its git directory within the working tree to the git dir nested
1814 * in its superprojects git dir under modules/.
1816 void absorb_git_dir_into_superproject(const char *prefix,
1821 const char *sub_git_dir;
1822 struct strbuf gitdir = STRBUF_INIT;
1823 strbuf_addf(&gitdir, "%s/.git", path);
1824 sub_git_dir = resolve_gitdir_gently(gitdir.buf, &err_code);
1826 /* Not populated? */
1828 const struct submodule *sub;
1830 if (err_code == READ_GITFILE_ERR_STAT_FAILED) {
1831 /* unpopulated as expected */
1832 strbuf_release(&gitdir);
1836 if (err_code != READ_GITFILE_ERR_NOT_A_REPO)
1837 /* We don't know what broke here. */
1838 read_gitfile_error_die(err_code, path, NULL);
1841 * Maybe populated, but no git directory was found?
1842 * This can happen if the superproject is a submodule
1843 * itself and was just absorbed. The absorption of the
1844 * superproject did not rewrite the git file links yet,
1847 sub = submodule_from_path(&null_oid, path);
1849 die(_("could not lookup name for submodule '%s'"), path);
1850 connect_work_tree_and_git_dir(path,
1851 git_path("modules/%s", sub->name));
1853 /* Is it already absorbed into the superprojects git dir? */
1854 char *real_sub_git_dir = real_pathdup(sub_git_dir, 1);
1855 char *real_common_git_dir = real_pathdup(get_git_common_dir(), 1);
1857 if (!starts_with(real_sub_git_dir, real_common_git_dir))
1858 relocate_single_git_dir_into_superproject(prefix, path);
1860 free(real_sub_git_dir);
1861 free(real_common_git_dir);
1863 strbuf_release(&gitdir);
1865 if (flags & ABSORB_GITDIR_RECURSE_SUBMODULES) {
1866 struct child_process cp = CHILD_PROCESS_INIT;
1867 struct strbuf sb = STRBUF_INIT;
1869 if (flags & ~ABSORB_GITDIR_RECURSE_SUBMODULES)
1870 die("BUG: we don't know how to pass the flags down?");
1872 strbuf_addstr(&sb, get_super_prefix_or_empty());
1873 strbuf_addstr(&sb, path);
1874 strbuf_addch(&sb, '/');
1879 argv_array_pushl(&cp.args, "--super-prefix", sb.buf,
1880 "submodule--helper",
1881 "absorb-git-dirs", NULL);
1882 prepare_submodule_repo_env(&cp.env_array);
1883 if (run_command(&cp))
1884 die(_("could not recurse into submodule '%s'"), path);
1886 strbuf_release(&sb);
1890 const char *get_superproject_working_tree(void)
1892 struct child_process cp = CHILD_PROCESS_INIT;
1893 struct strbuf sb = STRBUF_INIT;
1894 const char *one_up = real_path_if_valid("../");
1895 const char *cwd = xgetcwd();
1896 const char *ret = NULL;
1897 const char *subpath;
1901 if (!is_inside_work_tree())
1904 * We might have a superproject, but it is harder
1912 subpath = relative_path(cwd, one_up, &sb);
1914 prepare_submodule_repo_env(&cp.env_array);
1915 argv_array_pop(&cp.env_array);
1917 argv_array_pushl(&cp.args, "--literal-pathspecs", "-C", "..",
1918 "ls-files", "-z", "--stage", "--full-name", "--",
1927 if (start_command(&cp))
1928 die(_("could not start ls-files in .."));
1930 len = strbuf_read(&sb, cp.out, PATH_MAX);
1933 if (starts_with(sb.buf, "160000")) {
1935 int cwd_len = strlen(cwd);
1936 char *super_sub, *super_wt;
1939 * There is a superproject having this repo as a submodule.
1940 * The format is <mode> SP <hash> SP <stage> TAB <full name> \0,
1941 * We're only interested in the name after the tab.
1943 super_sub = strchr(sb.buf, '\t') + 1;
1944 super_sub_len = sb.buf + sb.len - super_sub - 1;
1946 if (super_sub_len > cwd_len ||
1947 strcmp(&cwd[cwd_len - super_sub_len], super_sub))
1948 die (_("BUG: returned path string doesn't match cwd?"));
1950 super_wt = xstrdup(cwd);
1951 super_wt[cwd_len - super_sub_len] = '\0';
1953 ret = real_path(super_wt);
1956 strbuf_release(&sb);
1958 code = finish_command(&cp);
1961 /* '../' is not a git repository */
1963 if (code == 0 && len == 0)
1964 /* There is an unrelated git repository at '../' */
1967 die(_("ls-tree returned unexpected return code %d"), code);
1972 int submodule_to_gitdir(struct strbuf *buf, const char *submodule)
1974 const struct submodule *sub;
1975 const char *git_dir;
1979 strbuf_addstr(buf, submodule);
1980 strbuf_complete(buf, '/');
1981 strbuf_addstr(buf, ".git");
1983 git_dir = read_gitfile(buf->buf);
1986 strbuf_addstr(buf, git_dir);
1988 if (!is_git_directory(buf->buf)) {
1989 sub = submodule_from_path(&null_oid, submodule);
1995 strbuf_git_path(buf, "%s/%s", "modules", sub->name);