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_names = 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(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 ie_match_stat(istate, istate->cache[pos], &st,
66 CE_MATCH_IGNORE_FSMONITOR) & DATA_CHANGED)
73 static int for_each_remote_ref_submodule(const char *submodule,
74 each_ref_fn fn, void *cb_data)
76 return refs_for_each_remote_ref(get_submodule_ref_store(submodule),
81 * Try to update the "path" entry in the "submodule.<name>" section of the
82 * .gitmodules file. Return 0 only if a .gitmodules file was found, a section
83 * with the correct path=<oldpath> setting was found and we could update it.
85 int update_path_in_gitmodules(const char *oldpath, const char *newpath)
87 struct strbuf entry = STRBUF_INIT;
88 const struct submodule *submodule;
90 if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */
93 if (is_gitmodules_unmerged(&the_index))
94 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
96 submodule = submodule_from_path(&null_oid, oldpath);
97 if (!submodule || !submodule->name) {
98 warning(_("Could not find section in .gitmodules where path=%s"), oldpath);
101 strbuf_addstr(&entry, "submodule.");
102 strbuf_addstr(&entry, submodule->name);
103 strbuf_addstr(&entry, ".path");
104 if (git_config_set_in_file_gently(GITMODULES_FILE, entry.buf, newpath) < 0) {
105 /* Maybe the user already did that, don't error out here */
106 warning(_("Could not update .gitmodules entry %s"), entry.buf);
107 strbuf_release(&entry);
110 strbuf_release(&entry);
115 * Try to remove the "submodule.<name>" section from .gitmodules where the given
116 * path is configured. Return 0 only if a .gitmodules file was found, a section
117 * with the correct path=<path> setting was found and we could remove it.
119 int remove_path_from_gitmodules(const char *path)
121 struct strbuf sect = STRBUF_INIT;
122 const struct submodule *submodule;
124 if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */
127 if (is_gitmodules_unmerged(&the_index))
128 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
130 submodule = submodule_from_path(&null_oid, path);
131 if (!submodule || !submodule->name) {
132 warning(_("Could not find section in .gitmodules where path=%s"), path);
135 strbuf_addstr(§, "submodule.");
136 strbuf_addstr(§, submodule->name);
137 if (git_config_rename_section_in_file(GITMODULES_FILE, sect.buf, NULL) < 0) {
138 /* Maybe the user already did that, don't error out here */
139 warning(_("Could not remove .gitmodules entry for %s"), path);
140 strbuf_release(§);
143 strbuf_release(§);
147 void stage_updated_gitmodules(struct index_state *istate)
149 if (add_file_to_index(istate, GITMODULES_FILE, 0))
150 die(_("staging updated .gitmodules failed"));
153 static int add_submodule_odb(const char *path)
155 struct strbuf objects_directory = STRBUF_INIT;
158 ret = strbuf_git_path_submodule(&objects_directory, path, "objects/");
161 if (!is_directory(objects_directory.buf)) {
165 add_to_alternates_memory(objects_directory.buf);
167 strbuf_release(&objects_directory);
171 void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
174 const struct submodule *submodule = submodule_from_path(&null_oid, path);
179 key = xstrfmt("submodule.%s.ignore", submodule->name);
180 if (repo_config_get_string_const(the_repository, key, &ignore))
181 ignore = submodule->ignore;
185 handle_ignore_submodules_arg(diffopt, ignore);
186 else if (is_gitmodules_unmerged(&the_index))
187 diffopt->flags.ignore_submodules = 1;
191 /* Cheap function that only determines if we're interested in submodules at all */
192 int git_default_submodule_config(const char *var, const char *value, void *cb)
194 if (!strcmp(var, "submodule.recurse")) {
195 int v = git_config_bool(var, value) ?
196 RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF;
197 config_update_recurse_submodules = v;
202 int option_parse_recurse_submodules_worktree_updater(const struct option *opt,
203 const char *arg, int unset)
206 config_update_recurse_submodules = RECURSE_SUBMODULES_OFF;
210 config_update_recurse_submodules =
211 parse_update_recurse_submodules_arg(opt->long_name,
214 config_update_recurse_submodules = RECURSE_SUBMODULES_ON;
220 * Determine if a submodule has been initialized at a given 'path'
222 int is_submodule_active(struct repository *repo, const char *path)
227 const struct string_list *sl;
228 const struct submodule *module;
230 module = submodule_from_cache(repo, &null_oid, path);
232 /* early return if there isn't a path->module mapping */
236 /* submodule.<name>.active is set */
237 key = xstrfmt("submodule.%s.active", module->name);
238 if (!repo_config_get_bool(repo, key, &ret)) {
244 /* submodule.active is set */
245 sl = repo_config_get_value_multi(repo, "submodule.active");
248 struct argv_array args = ARGV_ARRAY_INIT;
249 const struct string_list_item *item;
251 for_each_string_list_item(item, sl) {
252 argv_array_push(&args, item->string);
255 parse_pathspec(&ps, 0, 0, NULL, args.argv);
256 ret = match_pathspec(&ps, path, strlen(path), 0, NULL, 1);
258 argv_array_clear(&args);
263 /* fallback to checking if the URL is set */
264 key = xstrfmt("submodule.%s.url", module->name);
265 ret = !repo_config_get_string(repo, key, &value);
272 int is_submodule_populated_gently(const char *path, int *return_error_code)
275 char *gitdir = xstrfmt("%s/.git", path);
277 if (resolve_gitdir_gently(gitdir, return_error_code))
285 * Dies if the provided 'prefix' corresponds to an unpopulated submodule
287 void die_in_unpopulated_submodule(const struct index_state *istate,
295 prefixlen = strlen(prefix);
297 for (i = 0; i < istate->cache_nr; i++) {
298 struct cache_entry *ce = istate->cache[i];
299 int ce_len = ce_namelen(ce);
301 if (!S_ISGITLINK(ce->ce_mode))
303 if (prefixlen <= ce_len)
305 if (strncmp(ce->name, prefix, ce_len))
307 if (prefix[ce_len] != '/')
310 die(_("in unpopulated submodule '%s'"), ce->name);
315 * Dies if any paths in the provided pathspec descends into a submodule
317 void die_path_inside_submodule(const struct index_state *istate,
318 const struct pathspec *ps)
322 for (i = 0; i < istate->cache_nr; i++) {
323 struct cache_entry *ce = istate->cache[i];
324 int ce_len = ce_namelen(ce);
326 if (!S_ISGITLINK(ce->ce_mode))
329 for (j = 0; j < ps->nr ; j++) {
330 const struct pathspec_item *item = &ps->items[j];
332 if (item->len <= ce_len)
334 if (item->match[ce_len] != '/')
336 if (strncmp(ce->name, item->match, ce_len))
338 if (item->len == ce_len + 1)
341 die(_("Pathspec '%s' is in submodule '%.*s'"),
342 item->original, ce_len, ce->name);
347 enum submodule_update_type parse_submodule_update_type(const char *value)
349 if (!strcmp(value, "none"))
350 return SM_UPDATE_NONE;
351 else if (!strcmp(value, "checkout"))
352 return SM_UPDATE_CHECKOUT;
353 else if (!strcmp(value, "rebase"))
354 return SM_UPDATE_REBASE;
355 else if (!strcmp(value, "merge"))
356 return SM_UPDATE_MERGE;
357 else if (*value == '!')
358 return SM_UPDATE_COMMAND;
360 return SM_UPDATE_UNSPECIFIED;
363 int parse_submodule_update_strategy(const char *value,
364 struct submodule_update_strategy *dst)
366 enum submodule_update_type type;
368 free((void*)dst->command);
371 type = parse_submodule_update_type(value);
372 if (type == SM_UPDATE_UNSPECIFIED)
376 if (type == SM_UPDATE_COMMAND)
377 dst->command = xstrdup(value + 1);
382 const char *submodule_strategy_to_string(const struct submodule_update_strategy *s)
384 struct strbuf sb = STRBUF_INIT;
386 case SM_UPDATE_CHECKOUT:
388 case SM_UPDATE_MERGE:
390 case SM_UPDATE_REBASE:
394 case SM_UPDATE_UNSPECIFIED:
396 case SM_UPDATE_COMMAND:
397 strbuf_addf(&sb, "!%s", s->command);
398 return strbuf_detach(&sb, NULL);
403 void handle_ignore_submodules_arg(struct diff_options *diffopt,
406 diffopt->flags.ignore_submodules = 0;
407 diffopt->flags.ignore_untracked_in_submodules = 0;
408 diffopt->flags.ignore_dirty_submodules = 0;
410 if (!strcmp(arg, "all"))
411 diffopt->flags.ignore_submodules = 1;
412 else if (!strcmp(arg, "untracked"))
413 diffopt->flags.ignore_untracked_in_submodules = 1;
414 else if (!strcmp(arg, "dirty"))
415 diffopt->flags.ignore_dirty_submodules = 1;
416 else if (strcmp(arg, "none"))
417 die("bad --ignore-submodules argument: %s", arg);
420 static int prepare_submodule_summary(struct rev_info *rev, const char *path,
421 struct commit *left, struct commit *right,
422 struct commit_list *merge_bases)
424 struct commit_list *list;
426 init_revisions(rev, NULL);
427 setup_revisions(0, NULL, rev, NULL);
429 rev->first_parent_only = 1;
430 left->object.flags |= SYMMETRIC_LEFT;
431 add_pending_object(rev, &left->object, path);
432 add_pending_object(rev, &right->object, path);
433 for (list = merge_bases; list; list = list->next) {
434 list->item->object.flags |= UNINTERESTING;
435 add_pending_object(rev, &list->item->object,
436 oid_to_hex(&list->item->object.oid));
438 return prepare_revision_walk(rev);
441 static void print_submodule_summary(struct rev_info *rev, struct diff_options *o)
443 static const char format[] = " %m %s";
444 struct strbuf sb = STRBUF_INIT;
445 struct commit *commit;
447 while ((commit = get_revision(rev))) {
448 struct pretty_print_context ctx = {0};
449 ctx.date_mode = rev->date_mode;
450 ctx.output_encoding = get_log_output_encoding();
451 strbuf_setlen(&sb, 0);
452 format_commit_message(commit, format, &sb, &ctx);
453 strbuf_addch(&sb, '\n');
454 if (commit->object.flags & SYMMETRIC_LEFT)
455 diff_emit_submodule_del(o, sb.buf);
457 diff_emit_submodule_add(o, 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(struct diff_options *o, const char *path,
485 struct object_id *one, struct object_id *two,
486 unsigned dirty_submodule,
487 struct commit **left, struct commit **right,
488 struct commit_list **merge_bases)
490 const char *message = NULL;
491 struct strbuf sb = STRBUF_INIT;
492 int fast_forward = 0, fast_backward = 0;
494 if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
495 diff_emit_submodule_untracked(o, path);
497 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
498 diff_emit_submodule_modified(o, path);
500 if (is_null_oid(one))
501 message = "(new submodule)";
502 else if (is_null_oid(two))
503 message = "(submodule deleted)";
505 if (add_submodule_odb(path)) {
507 message = "(commits not present)";
512 * Attempt to lookup the commit references, and determine if this is
513 * a fast forward or fast backwards update.
515 *left = lookup_commit_reference(one);
516 *right = lookup_commit_reference(two);
519 * Warn about missing commits in the submodule project, but only if
522 if ((!is_null_oid(one) && !*left) ||
523 (!is_null_oid(two) && !*right))
524 message = "(commits not present)";
526 *merge_bases = get_merge_bases(*left, *right);
528 if ((*merge_bases)->item == *left)
530 else if ((*merge_bases)->item == *right)
534 if (!oidcmp(one, two)) {
540 strbuf_addf(&sb, "Submodule %s ", path);
541 strbuf_add_unique_abbrev(&sb, one->hash, DEFAULT_ABBREV);
542 strbuf_addstr(&sb, (fast_backward || fast_forward) ? ".." : "...");
543 strbuf_add_unique_abbrev(&sb, two->hash, DEFAULT_ABBREV);
545 strbuf_addf(&sb, " %s\n", message);
547 strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : "");
548 diff_emit_submodule_header(o, sb.buf);
553 void show_submodule_summary(struct diff_options *o, const char *path,
554 struct object_id *one, struct object_id *two,
555 unsigned dirty_submodule)
558 struct commit *left = NULL, *right = NULL;
559 struct commit_list *merge_bases = NULL;
561 show_submodule_header(o, path, one, two, dirty_submodule,
562 &left, &right, &merge_bases);
565 * If we don't have both a left and a right pointer, there is no
566 * reason to try and display a summary. The header line should contain
567 * all the information the user needs.
572 /* Treat revision walker failure the same as missing commits */
573 if (prepare_submodule_summary(&rev, path, left, right, merge_bases)) {
574 diff_emit_submodule_error(o, "(revision walker failed)\n");
578 print_submodule_summary(&rev, o);
582 free_commit_list(merge_bases);
583 clear_commit_marks(left, ~0);
584 clear_commit_marks(right, ~0);
587 void show_submodule_inline_diff(struct diff_options *o, const char *path,
588 struct object_id *one, struct object_id *two,
589 unsigned dirty_submodule)
591 const struct object_id *old = &empty_tree_oid, *new = &empty_tree_oid;
592 struct commit *left = NULL, *right = NULL;
593 struct commit_list *merge_bases = NULL;
594 struct child_process cp = CHILD_PROCESS_INIT;
595 struct strbuf sb = STRBUF_INIT;
597 show_submodule_header(o, path, one, two, dirty_submodule,
598 &left, &right, &merge_bases);
600 /* We need a valid left and right commit to display a difference */
601 if (!(left || is_null_oid(one)) ||
602 !(right || is_null_oid(two)))
615 /* TODO: other options may need to be passed here. */
616 argv_array_pushl(&cp.args, "diff", "--submodule=diff", NULL);
617 argv_array_pushf(&cp.args, "--color=%s", want_color(o->use_color) ?
620 if (o->flags.reverse_diff) {
621 argv_array_pushf(&cp.args, "--src-prefix=%s%s/",
623 argv_array_pushf(&cp.args, "--dst-prefix=%s%s/",
626 argv_array_pushf(&cp.args, "--src-prefix=%s%s/",
628 argv_array_pushf(&cp.args, "--dst-prefix=%s%s/",
631 argv_array_push(&cp.args, oid_to_hex(old));
633 * If the submodule has modified content, we will diff against the
634 * work tree, under the assumption that the user has asked for the
635 * diff format and wishes to actually see all differences even if they
636 * haven't yet been committed to the submodule yet.
638 if (!(dirty_submodule & DIRTY_SUBMODULE_MODIFIED))
639 argv_array_push(&cp.args, oid_to_hex(new));
641 prepare_submodule_repo_env(&cp.env_array);
642 if (start_command(&cp))
643 diff_emit_submodule_error(o, "(diff failed)\n");
645 while (strbuf_getwholeline_fd(&sb, cp.out, '\n') != EOF)
646 diff_emit_submodule_pipethrough(o, sb.buf, sb.len);
648 if (finish_command(&cp))
649 diff_emit_submodule_error(o, "(diff failed)\n");
654 free_commit_list(merge_bases);
656 clear_commit_marks(left, ~0);
658 clear_commit_marks(right, ~0);
661 int should_update_submodules(void)
663 return config_update_recurse_submodules == RECURSE_SUBMODULES_ON;
666 const struct submodule *submodule_from_ce(const struct cache_entry *ce)
668 if (!S_ISGITLINK(ce->ce_mode))
671 if (!should_update_submodules())
674 return submodule_from_path(&null_oid, ce->name);
677 static struct oid_array *submodule_commits(struct string_list *submodules,
680 struct string_list_item *item;
682 item = string_list_insert(submodules, name);
684 return (struct oid_array *) item->util;
686 /* NEEDSWORK: should we have oid_array_init()? */
687 item->util = xcalloc(1, sizeof(struct oid_array));
688 return (struct oid_array *) item->util;
691 struct collect_changed_submodules_cb_data {
692 struct string_list *changed;
693 const struct object_id *commit_oid;
697 * this would normally be two functions: default_name_from_path() and
698 * path_from_default_name(). Since the default name is the same as
699 * the submodule path we can get away with just one function which only
700 * checks whether there is a submodule in the working directory at that
703 static const char *default_name_or_path(const char *path_or_name)
707 if (!is_submodule_populated_gently(path_or_name, &error_code))
713 static void collect_changed_submodules_cb(struct diff_queue_struct *q,
714 struct diff_options *options,
717 struct collect_changed_submodules_cb_data *me = data;
718 struct string_list *changed = me->changed;
719 const struct object_id *commit_oid = me->commit_oid;
722 for (i = 0; i < q->nr; i++) {
723 struct diff_filepair *p = q->queue[i];
724 struct oid_array *commits;
725 const struct submodule *submodule;
728 if (!S_ISGITLINK(p->two->mode))
731 submodule = submodule_from_path(commit_oid, p->two->path);
733 name = submodule->name;
735 name = default_name_or_path(p->two->path);
736 /* make sure name does not collide with existing one */
737 submodule = submodule_from_name(commit_oid, name);
739 warning("Submodule in commit %s at path: "
740 "'%s' collides with a submodule named "
741 "the same. Skipping it.",
742 oid_to_hex(commit_oid), name);
750 commits = submodule_commits(changed, name);
751 oid_array_append(commits, &p->two->oid);
756 * Collect the paths of submodules in 'changed' which have changed based on
757 * the revisions as specified in 'argv'. Each entry in 'changed' will also
758 * have a corresponding 'struct oid_array' (in the 'util' field) which lists
759 * what the submodule pointers were updated to during the change.
761 static void collect_changed_submodules(struct string_list *changed,
762 struct argv_array *argv)
765 const struct commit *commit;
767 init_revisions(&rev, NULL);
768 setup_revisions(argv->argc, argv->argv, &rev, NULL);
769 if (prepare_revision_walk(&rev))
770 die("revision walk setup failed");
772 while ((commit = get_revision(&rev))) {
773 struct rev_info diff_rev;
774 struct collect_changed_submodules_cb_data data;
775 data.changed = changed;
776 data.commit_oid = &commit->object.oid;
778 init_revisions(&diff_rev, NULL);
779 diff_rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
780 diff_rev.diffopt.format_callback = collect_changed_submodules_cb;
781 diff_rev.diffopt.format_callback_data = &data;
782 diff_tree_combined_merge(commit, 1, &diff_rev);
785 reset_revision_walk();
788 static void free_submodules_oids(struct string_list *submodules)
790 struct string_list_item *item;
791 for_each_string_list_item(item, submodules)
792 oid_array_clear((struct oid_array *) item->util);
793 string_list_clear(submodules, 1);
796 static int has_remote(const char *refname, const struct object_id *oid,
797 int flags, void *cb_data)
802 static int append_oid_to_argv(const struct object_id *oid, void *data)
804 struct argv_array *argv = data;
805 argv_array_push(argv, oid_to_hex(oid));
809 struct has_commit_data {
814 static int check_has_commit(const struct object_id *oid, void *data)
816 struct has_commit_data *cb = data;
818 enum object_type type = sha1_object_info(oid->hash, NULL);
825 * Object is missing or invalid. If invalid, an error message
826 * has already been printed.
831 die(_("submodule entry '%s' (%s) is a %s, not a commit"),
832 cb->path, oid_to_hex(oid), typename(type));
836 static int submodule_has_commits(const char *path, struct oid_array *commits)
838 struct has_commit_data has_commit = { 1, path };
841 * Perform a cheap, but incorrect check for the existence of 'commits'.
842 * This is done by adding the submodule's object store to the in-core
843 * object store, and then querying for each commit's existence. If we
844 * do not have the commit object anywhere, there is no chance we have
845 * it in the object store of the correct submodule and have it
846 * reachable from a ref, so we can fail early without spawning rev-list
847 * which is expensive.
849 if (add_submodule_odb(path))
852 oid_array_for_each_unique(commits, check_has_commit, &has_commit);
854 if (has_commit.result) {
856 * Even if the submodule is checked out and the commit is
857 * present, make sure it exists in the submodule's object store
858 * and that it is reachable from a ref.
860 struct child_process cp = CHILD_PROCESS_INIT;
861 struct strbuf out = STRBUF_INIT;
863 argv_array_pushl(&cp.args, "rev-list", "-n", "1", NULL);
864 oid_array_for_each_unique(commits, append_oid_to_argv, &cp.args);
865 argv_array_pushl(&cp.args, "--not", "--all", NULL);
867 prepare_submodule_repo_env(&cp.env_array);
872 if (capture_command(&cp, &out, GIT_MAX_HEXSZ + 1) || out.len)
873 has_commit.result = 0;
875 strbuf_release(&out);
878 return has_commit.result;
881 static int submodule_needs_pushing(const char *path, struct oid_array *commits)
883 if (!submodule_has_commits(path, commits))
885 * NOTE: We do consider it safe to return "no" here. The
886 * correct answer would be "We do not know" instead of
887 * "No push needed", but it is quite hard to change
888 * the submodule pointer without having the submodule
889 * around. If a user did however change the submodules
890 * without having the submodule around, this indicates
891 * an expert who knows what they are doing or a
892 * maintainer integrating work from other people. In
893 * both cases it should be safe to skip this check.
897 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
898 struct child_process cp = CHILD_PROCESS_INIT;
899 struct strbuf buf = STRBUF_INIT;
900 int needs_pushing = 0;
902 argv_array_push(&cp.args, "rev-list");
903 oid_array_for_each_unique(commits, append_oid_to_argv, &cp.args);
904 argv_array_pushl(&cp.args, "--not", "--remotes", "-n", "1" , NULL);
906 prepare_submodule_repo_env(&cp.env_array);
911 if (start_command(&cp))
912 die("Could not run 'git rev-list <commits> --not --remotes -n 1' command in submodule %s",
914 if (strbuf_read(&buf, cp.out, 41))
918 strbuf_release(&buf);
919 return needs_pushing;
925 int find_unpushed_submodules(struct oid_array *commits,
926 const char *remotes_name, struct string_list *needs_pushing)
928 struct string_list submodules = STRING_LIST_INIT_DUP;
929 struct string_list_item *name;
930 struct argv_array argv = ARGV_ARRAY_INIT;
932 /* argv.argv[0] will be ignored by setup_revisions */
933 argv_array_push(&argv, "find_unpushed_submodules");
934 oid_array_for_each_unique(commits, append_oid_to_argv, &argv);
935 argv_array_push(&argv, "--not");
936 argv_array_pushf(&argv, "--remotes=%s", remotes_name);
938 collect_changed_submodules(&submodules, &argv);
940 for_each_string_list_item(name, &submodules) {
941 struct oid_array *commits = name->util;
942 const struct submodule *submodule;
943 const char *path = NULL;
945 submodule = submodule_from_name(&null_oid, name->string);
947 path = submodule->path;
949 path = default_name_or_path(name->string);
954 if (submodule_needs_pushing(path, commits))
955 string_list_insert(needs_pushing, path);
958 free_submodules_oids(&submodules);
959 argv_array_clear(&argv);
961 return needs_pushing->nr;
964 static int push_submodule(const char *path,
965 const struct remote *remote,
966 const char **refspec, int refspec_nr,
967 const struct string_list *push_options,
970 if (add_submodule_odb(path))
973 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
974 struct child_process cp = CHILD_PROCESS_INIT;
975 argv_array_push(&cp.args, "push");
977 argv_array_push(&cp.args, "--dry-run");
979 if (push_options && push_options->nr) {
980 const struct string_list_item *item;
981 for_each_string_list_item(item, push_options)
982 argv_array_pushf(&cp.args, "--push-option=%s",
986 if (remote->origin != REMOTE_UNCONFIGURED) {
988 argv_array_push(&cp.args, remote->name);
989 for (i = 0; i < refspec_nr; i++)
990 argv_array_push(&cp.args, refspec[i]);
993 prepare_submodule_repo_env(&cp.env_array);
997 if (run_command(&cp))
1006 * Perform a check in the submodule to see if the remote and refspec work.
1007 * Die if the submodule can't be pushed.
1009 static void submodule_push_check(const char *path, const char *head,
1010 const struct remote *remote,
1011 const char **refspec, int refspec_nr)
1013 struct child_process cp = CHILD_PROCESS_INIT;
1016 argv_array_push(&cp.args, "submodule--helper");
1017 argv_array_push(&cp.args, "push-check");
1018 argv_array_push(&cp.args, head);
1019 argv_array_push(&cp.args, remote->name);
1021 for (i = 0; i < refspec_nr; i++)
1022 argv_array_push(&cp.args, refspec[i]);
1024 prepare_submodule_repo_env(&cp.env_array);
1031 * Simply indicate if 'submodule--helper push-check' failed.
1032 * More detailed error information will be provided by the
1035 if (run_command(&cp))
1036 die("process for submodule '%s' failed", path);
1039 int push_unpushed_submodules(struct oid_array *commits,
1040 const struct remote *remote,
1041 const char **refspec, int refspec_nr,
1042 const struct string_list *push_options,
1046 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1048 if (!find_unpushed_submodules(commits, remote->name, &needs_pushing))
1052 * Verify that the remote and refspec can be propagated to all
1053 * submodules. This check can be skipped if the remote and refspec
1054 * won't be propagated due to the remote being unconfigured (e.g. a URL
1055 * instead of a remote name).
1057 if (remote->origin != REMOTE_UNCONFIGURED) {
1059 struct object_id head_oid;
1061 head = resolve_refdup("HEAD", 0, &head_oid, NULL);
1063 die(_("Failed to resolve HEAD as a valid ref."));
1065 for (i = 0; i < needs_pushing.nr; i++)
1066 submodule_push_check(needs_pushing.items[i].string,
1068 refspec, refspec_nr);
1072 /* Actually push the submodules */
1073 for (i = 0; i < needs_pushing.nr; i++) {
1074 const char *path = needs_pushing.items[i].string;
1075 fprintf(stderr, "Pushing submodule '%s'\n", path);
1076 if (!push_submodule(path, remote, refspec, refspec_nr,
1077 push_options, dry_run)) {
1078 fprintf(stderr, "Unable to push submodule '%s'\n", path);
1083 string_list_clear(&needs_pushing, 0);
1088 static int append_oid_to_array(const char *ref, const struct object_id *oid,
1089 int flags, void *data)
1091 struct oid_array *array = data;
1092 oid_array_append(array, oid);
1096 void check_for_new_submodule_commits(struct object_id *oid)
1098 if (!initialized_fetch_ref_tips) {
1099 for_each_ref(append_oid_to_array, &ref_tips_before_fetch);
1100 initialized_fetch_ref_tips = 1;
1103 oid_array_append(&ref_tips_after_fetch, oid);
1106 static void calculate_changed_submodule_paths(void)
1108 struct argv_array argv = ARGV_ARRAY_INIT;
1109 struct string_list changed_submodules = STRING_LIST_INIT_DUP;
1110 const struct string_list_item *name;
1112 /* No need to check if there are no submodules configured */
1113 if (!submodule_from_path(NULL, NULL))
1116 argv_array_push(&argv, "--"); /* argv[0] program name */
1117 oid_array_for_each_unique(&ref_tips_after_fetch,
1118 append_oid_to_argv, &argv);
1119 argv_array_push(&argv, "--not");
1120 oid_array_for_each_unique(&ref_tips_before_fetch,
1121 append_oid_to_argv, &argv);
1124 * Collect all submodules (whether checked out or not) for which new
1125 * commits have been recorded upstream in "changed_submodule_names".
1127 collect_changed_submodules(&changed_submodules, &argv);
1129 for_each_string_list_item(name, &changed_submodules) {
1130 struct oid_array *commits = name->util;
1131 const struct submodule *submodule;
1132 const char *path = NULL;
1134 submodule = submodule_from_name(&null_oid, name->string);
1136 path = submodule->path;
1138 path = default_name_or_path(name->string);
1143 if (!submodule_has_commits(path, commits))
1144 string_list_append(&changed_submodule_names, name->string);
1147 free_submodules_oids(&changed_submodules);
1148 argv_array_clear(&argv);
1149 oid_array_clear(&ref_tips_before_fetch);
1150 oid_array_clear(&ref_tips_after_fetch);
1151 initialized_fetch_ref_tips = 0;
1154 int submodule_touches_in_range(struct object_id *excl_oid,
1155 struct object_id *incl_oid)
1157 struct string_list subs = STRING_LIST_INIT_DUP;
1158 struct argv_array args = ARGV_ARRAY_INIT;
1161 /* No need to check if there are no submodules configured */
1162 if (!submodule_from_path(NULL, NULL))
1165 argv_array_push(&args, "--"); /* args[0] program name */
1166 argv_array_push(&args, oid_to_hex(incl_oid));
1167 argv_array_push(&args, "--not");
1168 argv_array_push(&args, oid_to_hex(excl_oid));
1170 collect_changed_submodules(&subs, &args);
1173 argv_array_clear(&args);
1175 free_submodules_oids(&subs);
1179 struct submodule_parallel_fetch {
1181 struct argv_array args;
1182 const char *work_tree;
1184 int command_line_option;
1189 #define SPF_INIT {0, ARGV_ARRAY_INIT, NULL, NULL, 0, 0, 0, 0}
1191 static int get_fetch_recurse_config(const struct submodule *submodule,
1192 struct submodule_parallel_fetch *spf)
1194 if (spf->command_line_option != RECURSE_SUBMODULES_DEFAULT)
1195 return spf->command_line_option;
1201 int fetch_recurse = submodule->fetch_recurse;
1202 key = xstrfmt("submodule.%s.fetchRecurseSubmodules", submodule->name);
1203 if (!repo_config_get_string_const(the_repository, key, &value)) {
1204 fetch_recurse = parse_fetch_recurse_submodules_arg(key, value);
1208 if (fetch_recurse != RECURSE_SUBMODULES_NONE)
1209 /* local config overrules everything except commandline */
1210 return fetch_recurse;
1213 return spf->default_option;
1216 static int get_next_submodule(struct child_process *cp,
1217 struct strbuf *err, void *data, void **task_cb)
1220 struct submodule_parallel_fetch *spf = data;
1222 for (; spf->count < active_nr; spf->count++) {
1223 struct strbuf submodule_path = STRBUF_INIT;
1224 struct strbuf submodule_git_dir = STRBUF_INIT;
1225 struct strbuf submodule_prefix = STRBUF_INIT;
1226 const struct cache_entry *ce = active_cache[spf->count];
1227 const char *git_dir, *default_argv;
1228 const struct submodule *submodule;
1229 struct submodule default_submodule = SUBMODULE_INIT;
1231 if (!S_ISGITLINK(ce->ce_mode))
1234 submodule = submodule_from_path(&null_oid, ce->name);
1236 const char *name = default_name_or_path(ce->name);
1238 default_submodule.path = default_submodule.name = name;
1239 submodule = &default_submodule;
1243 switch (get_fetch_recurse_config(submodule, spf))
1246 case RECURSE_SUBMODULES_DEFAULT:
1247 case RECURSE_SUBMODULES_ON_DEMAND:
1248 if (!submodule || !unsorted_string_list_lookup(&changed_submodule_names,
1251 default_argv = "on-demand";
1253 case RECURSE_SUBMODULES_ON:
1254 default_argv = "yes";
1256 case RECURSE_SUBMODULES_OFF:
1260 strbuf_addf(&submodule_path, "%s/%s", spf->work_tree, ce->name);
1261 strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf);
1262 strbuf_addf(&submodule_prefix, "%s%s/", spf->prefix, ce->name);
1263 git_dir = read_gitfile(submodule_git_dir.buf);
1265 git_dir = submodule_git_dir.buf;
1266 if (is_directory(git_dir)) {
1267 child_process_init(cp);
1268 cp->dir = strbuf_detach(&submodule_path, NULL);
1269 prepare_submodule_repo_env(&cp->env_array);
1272 strbuf_addf(err, "Fetching submodule %s%s\n",
1273 spf->prefix, ce->name);
1274 argv_array_init(&cp->args);
1275 argv_array_pushv(&cp->args, spf->args.argv);
1276 argv_array_push(&cp->args, default_argv);
1277 argv_array_push(&cp->args, "--submodule-prefix");
1278 argv_array_push(&cp->args, submodule_prefix.buf);
1281 strbuf_release(&submodule_path);
1282 strbuf_release(&submodule_git_dir);
1283 strbuf_release(&submodule_prefix);
1292 static int fetch_start_failure(struct strbuf *err,
1293 void *cb, void *task_cb)
1295 struct submodule_parallel_fetch *spf = cb;
1302 static int fetch_finish(int retvalue, struct strbuf *err,
1303 void *cb, void *task_cb)
1305 struct submodule_parallel_fetch *spf = cb;
1313 int fetch_populated_submodules(const struct argv_array *options,
1314 const char *prefix, int command_line_option,
1316 int quiet, int max_parallel_jobs)
1319 struct submodule_parallel_fetch spf = SPF_INIT;
1321 spf.work_tree = get_git_work_tree();
1322 spf.command_line_option = command_line_option;
1323 spf.default_option = default_option;
1325 spf.prefix = prefix;
1330 if (read_cache() < 0)
1331 die("index file corrupt");
1333 argv_array_push(&spf.args, "fetch");
1334 for (i = 0; i < options->argc; i++)
1335 argv_array_push(&spf.args, options->argv[i]);
1336 argv_array_push(&spf.args, "--recurse-submodules-default");
1337 /* default value, "--submodule-prefix" and its value are added later */
1339 calculate_changed_submodule_paths();
1340 run_processes_parallel(max_parallel_jobs,
1342 fetch_start_failure,
1346 argv_array_clear(&spf.args);
1348 string_list_clear(&changed_submodule_names, 1);
1352 unsigned is_submodule_modified(const char *path, int ignore_untracked)
1354 struct child_process cp = CHILD_PROCESS_INIT;
1355 struct strbuf buf = STRBUF_INIT;
1357 unsigned dirty_submodule = 0;
1358 const char *git_dir;
1359 int ignore_cp_exit_code = 0;
1361 strbuf_addf(&buf, "%s/.git", path);
1362 git_dir = read_gitfile(buf.buf);
1365 if (!is_git_directory(git_dir)) {
1366 if (is_directory(git_dir))
1367 die(_("'%s' not recognized as a git repository"), git_dir);
1368 strbuf_release(&buf);
1369 /* The submodule is not checked out, so it is not modified */
1374 argv_array_pushl(&cp.args, "status", "--porcelain=2", NULL);
1375 if (ignore_untracked)
1376 argv_array_push(&cp.args, "-uno");
1378 prepare_submodule_repo_env(&cp.env_array);
1383 if (start_command(&cp))
1384 die("Could not run 'git status --porcelain=2' in submodule %s", path);
1386 fp = xfdopen(cp.out, "r");
1387 while (strbuf_getwholeline(&buf, fp, '\n') != EOF) {
1388 /* regular untracked files */
1389 if (buf.buf[0] == '?')
1390 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
1392 if (buf.buf[0] == 'u' ||
1393 buf.buf[0] == '1' ||
1394 buf.buf[0] == '2') {
1395 /* T = line type, XY = status, SSSS = submodule state */
1396 if (buf.len < strlen("T XY SSSS"))
1397 die("BUG: invalid status --porcelain=2 line %s",
1400 if (buf.buf[5] == 'S' && buf.buf[8] == 'U')
1401 /* nested untracked file */
1402 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
1404 if (buf.buf[0] == 'u' ||
1405 buf.buf[0] == '2' ||
1406 memcmp(buf.buf + 5, "S..U", 4))
1408 dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
1411 if ((dirty_submodule & DIRTY_SUBMODULE_MODIFIED) &&
1412 ((dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ||
1413 ignore_untracked)) {
1415 * We're not interested in any further information from
1416 * the child any more, neither output nor its exit code.
1418 ignore_cp_exit_code = 1;
1424 if (finish_command(&cp) && !ignore_cp_exit_code)
1425 die("'git status --porcelain=2' failed in submodule %s", path);
1427 strbuf_release(&buf);
1428 return dirty_submodule;
1431 int submodule_uses_gitfile(const char *path)
1433 struct child_process cp = CHILD_PROCESS_INIT;
1434 const char *argv[] = {
1442 struct strbuf buf = STRBUF_INIT;
1443 const char *git_dir;
1445 strbuf_addf(&buf, "%s/.git", path);
1446 git_dir = read_gitfile(buf.buf);
1448 strbuf_release(&buf);
1451 strbuf_release(&buf);
1453 /* Now test that all nested submodules use a gitfile too */
1455 prepare_submodule_repo_env(&cp.env_array);
1461 if (run_command(&cp))
1468 * Check if it is a bad idea to remove a submodule, i.e. if we'd lose data
1471 * Return 1 if we'd lose data, return 0 if the removal is fine,
1472 * and negative values for errors.
1474 int bad_to_remove_submodule(const char *path, unsigned flags)
1477 struct child_process cp = CHILD_PROCESS_INIT;
1478 struct strbuf buf = STRBUF_INIT;
1481 if (!file_exists(path) || is_empty_dir(path))
1484 if (!submodule_uses_gitfile(path))
1487 argv_array_pushl(&cp.args, "status", "--porcelain",
1488 "--ignore-submodules=none", NULL);
1490 if (flags & SUBMODULE_REMOVAL_IGNORE_UNTRACKED)
1491 argv_array_push(&cp.args, "-uno");
1493 argv_array_push(&cp.args, "-uall");
1495 if (!(flags & SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED))
1496 argv_array_push(&cp.args, "--ignored");
1498 prepare_submodule_repo_env(&cp.env_array);
1503 if (start_command(&cp)) {
1504 if (flags & SUBMODULE_REMOVAL_DIE_ON_ERROR)
1505 die(_("could not start 'git status' in submodule '%s'"),
1511 len = strbuf_read(&buf, cp.out, 1024);
1516 if (finish_command(&cp)) {
1517 if (flags & SUBMODULE_REMOVAL_DIE_ON_ERROR)
1518 die(_("could not run 'git status' in submodule '%s'"),
1523 strbuf_release(&buf);
1527 static const char *get_super_prefix_or_empty(void)
1529 const char *s = get_super_prefix();
1535 static int submodule_has_dirty_index(const struct submodule *sub)
1537 struct child_process cp = CHILD_PROCESS_INIT;
1539 prepare_submodule_repo_env(&cp.env_array);
1542 argv_array_pushl(&cp.args, "diff-index", "--quiet",
1543 "--cached", "HEAD", NULL);
1547 if (start_command(&cp))
1548 die("could not recurse into submodule '%s'", sub->path);
1550 return finish_command(&cp);
1553 static void submodule_reset_index(const char *path)
1555 struct child_process cp = CHILD_PROCESS_INIT;
1556 prepare_submodule_repo_env(&cp.env_array);
1562 argv_array_pushf(&cp.args, "--super-prefix=%s%s/",
1563 get_super_prefix_or_empty(), path);
1564 argv_array_pushl(&cp.args, "read-tree", "-u", "--reset", NULL);
1566 argv_array_push(&cp.args, EMPTY_TREE_SHA1_HEX);
1568 if (run_command(&cp))
1569 die("could not reset submodule index");
1573 * Moves a submodule at a given path from a given head to another new head.
1574 * For edge cases (a submodule coming into existence or removing a submodule)
1575 * pass NULL for old or new respectively.
1577 int submodule_move_head(const char *path,
1583 struct child_process cp = CHILD_PROCESS_INIT;
1584 const struct submodule *sub;
1585 int *error_code_ptr, error_code;
1587 if (!is_submodule_active(the_repository, path))
1590 if (flags & SUBMODULE_MOVE_HEAD_FORCE)
1592 * Pass non NULL pointer to is_submodule_populated_gently
1593 * to prevent die()-ing. We'll use connect_work_tree_and_git_dir
1594 * to fixup the submodule in the force case later.
1596 error_code_ptr = &error_code;
1598 error_code_ptr = NULL;
1600 if (old && !is_submodule_populated_gently(path, error_code_ptr))
1603 sub = submodule_from_path(&null_oid, path);
1606 die("BUG: could not get submodule information for '%s'", path);
1608 if (old && !(flags & SUBMODULE_MOVE_HEAD_FORCE)) {
1609 /* Check if the submodule has a dirty index. */
1610 if (submodule_has_dirty_index(sub))
1611 return error(_("submodule '%s' has dirty index"), path);
1614 if (!(flags & SUBMODULE_MOVE_HEAD_DRY_RUN)) {
1616 if (!submodule_uses_gitfile(path))
1617 absorb_git_dir_into_superproject("", path,
1618 ABSORB_GITDIR_RECURSE_SUBMODULES);
1620 char *gitdir = xstrfmt("%s/modules/%s",
1621 get_git_common_dir(), sub->name);
1622 connect_work_tree_and_git_dir(path, gitdir);
1625 /* make sure the index is clean as well */
1626 submodule_reset_index(path);
1629 if (old && (flags & SUBMODULE_MOVE_HEAD_FORCE)) {
1630 char *gitdir = xstrfmt("%s/modules/%s",
1631 get_git_common_dir(), sub->name);
1632 connect_work_tree_and_git_dir(path, gitdir);
1637 prepare_submodule_repo_env(&cp.env_array);
1643 argv_array_pushf(&cp.args, "--super-prefix=%s%s/",
1644 get_super_prefix_or_empty(), path);
1645 argv_array_pushl(&cp.args, "read-tree", "--recurse-submodules", NULL);
1647 if (flags & SUBMODULE_MOVE_HEAD_DRY_RUN)
1648 argv_array_push(&cp.args, "-n");
1650 argv_array_push(&cp.args, "-u");
1652 if (flags & SUBMODULE_MOVE_HEAD_FORCE)
1653 argv_array_push(&cp.args, "--reset");
1655 argv_array_push(&cp.args, "-m");
1657 argv_array_push(&cp.args, old ? old : EMPTY_TREE_SHA1_HEX);
1658 argv_array_push(&cp.args, new ? new : EMPTY_TREE_SHA1_HEX);
1660 if (run_command(&cp)) {
1665 if (!(flags & SUBMODULE_MOVE_HEAD_DRY_RUN)) {
1667 child_process_init(&cp);
1668 /* also set the HEAD accordingly */
1673 prepare_submodule_repo_env(&cp.env_array);
1674 argv_array_pushl(&cp.args, "update-ref", "HEAD",
1675 "--no-deref", new, NULL);
1677 if (run_command(&cp)) {
1682 struct strbuf sb = STRBUF_INIT;
1684 strbuf_addf(&sb, "%s/.git", path);
1685 unlink_or_warn(sb.buf);
1686 strbuf_release(&sb);
1688 if (is_empty_dir(path))
1689 rmdir_or_warn(path);
1696 static int find_first_merges(struct object_array *result, const char *path,
1697 struct commit *a, struct commit *b)
1700 struct object_array merges = OBJECT_ARRAY_INIT;
1701 struct commit *commit;
1702 int contains_another;
1704 char merged_revision[42];
1705 const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path",
1706 "--all", merged_revision, NULL };
1707 struct rev_info revs;
1708 struct setup_revision_opt rev_opts;
1710 memset(result, 0, sizeof(struct object_array));
1711 memset(&rev_opts, 0, sizeof(rev_opts));
1713 /* get all revisions that merge commit a */
1714 xsnprintf(merged_revision, sizeof(merged_revision), "^%s",
1715 oid_to_hex(&a->object.oid));
1716 init_revisions(&revs, NULL);
1717 rev_opts.submodule = path;
1718 /* FIXME: can't handle linked worktrees in submodules yet */
1719 revs.single_worktree = path != NULL;
1720 setup_revisions(ARRAY_SIZE(rev_args)-1, rev_args, &revs, &rev_opts);
1722 /* save all revisions from the above list that contain b */
1723 if (prepare_revision_walk(&revs))
1724 die("revision walk setup failed");
1725 while ((commit = get_revision(&revs)) != NULL) {
1726 struct object *o = &(commit->object);
1727 if (in_merge_bases(b, commit))
1728 add_object_array(o, NULL, &merges);
1730 reset_revision_walk();
1732 /* Now we've got all merges that contain a and b. Prune all
1733 * merges that contain another found merge and save them in
1736 for (i = 0; i < merges.nr; i++) {
1737 struct commit *m1 = (struct commit *) merges.objects[i].item;
1739 contains_another = 0;
1740 for (j = 0; j < merges.nr; j++) {
1741 struct commit *m2 = (struct commit *) merges.objects[j].item;
1742 if (i != j && in_merge_bases(m2, m1)) {
1743 contains_another = 1;
1748 if (!contains_another)
1749 add_object_array(merges.objects[i].item, NULL, result);
1752 object_array_clear(&merges);
1756 static void print_commit(struct commit *commit)
1758 struct strbuf sb = STRBUF_INIT;
1759 struct pretty_print_context ctx = {0};
1760 ctx.date_mode.type = DATE_NORMAL;
1761 format_commit_message(commit, " %h: %m %s", &sb, &ctx);
1762 fprintf(stderr, "%s\n", sb.buf);
1763 strbuf_release(&sb);
1766 #define MERGE_WARNING(path, msg) \
1767 warning("Failed to merge submodule %s (%s)", path, msg);
1769 int merge_submodule(struct object_id *result, const char *path,
1770 const struct object_id *base, const struct object_id *a,
1771 const struct object_id *b, int search)
1773 struct commit *commit_base, *commit_a, *commit_b;
1775 struct object_array merges;
1779 /* store a in result in case we fail */
1782 /* we can not handle deletion conflicts */
1783 if (is_null_oid(base))
1790 if (add_submodule_odb(path)) {
1791 MERGE_WARNING(path, "not checked out");
1795 if (!(commit_base = lookup_commit_reference(base)) ||
1796 !(commit_a = lookup_commit_reference(a)) ||
1797 !(commit_b = lookup_commit_reference(b))) {
1798 MERGE_WARNING(path, "commits not present");
1802 /* check whether both changes are forward */
1803 if (!in_merge_bases(commit_base, commit_a) ||
1804 !in_merge_bases(commit_base, commit_b)) {
1805 MERGE_WARNING(path, "commits don't follow merge-base");
1809 /* Case #1: a is contained in b or vice versa */
1810 if (in_merge_bases(commit_a, commit_b)) {
1814 if (in_merge_bases(commit_b, commit_a)) {
1820 * Case #2: There are one or more merges that contain a and b in
1821 * the submodule. If there is only one, then present it as a
1822 * suggestion to the user, but leave it marked unmerged so the
1823 * user needs to confirm the resolution.
1826 /* Skip the search if makes no sense to the calling context. */
1830 /* find commit which merges them */
1831 parent_count = find_first_merges(&merges, path, commit_a, commit_b);
1832 switch (parent_count) {
1834 MERGE_WARNING(path, "merge following commits not found");
1838 MERGE_WARNING(path, "not fast-forward");
1839 fprintf(stderr, "Found a possible merge resolution "
1840 "for the submodule:\n");
1841 print_commit((struct commit *) merges.objects[0].item);
1843 "If this is correct simply add it to the index "
1846 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
1847 "which will accept this suggestion.\n",
1848 oid_to_hex(&merges.objects[0].item->oid), path);
1852 MERGE_WARNING(path, "multiple merges found");
1853 for (i = 0; i < merges.nr; i++)
1854 print_commit((struct commit *) merges.objects[i].item);
1857 object_array_clear(&merges);
1862 * Embeds a single submodules git directory into the superprojects git dir,
1865 static void relocate_single_git_dir_into_superproject(const char *prefix,
1868 char *old_git_dir = NULL, *real_old_git_dir = NULL, *real_new_git_dir = NULL;
1869 const char *new_git_dir;
1870 const struct submodule *sub;
1872 if (submodule_uses_worktrees(path))
1873 die(_("relocate_gitdir for submodule '%s' with "
1874 "more than one worktree not supported"), path);
1876 old_git_dir = xstrfmt("%s/.git", path);
1877 if (read_gitfile(old_git_dir))
1878 /* If it is an actual gitfile, it doesn't need migration. */
1881 real_old_git_dir = real_pathdup(old_git_dir, 1);
1883 sub = submodule_from_path(&null_oid, path);
1885 die(_("could not lookup name for submodule '%s'"), path);
1887 new_git_dir = git_path("modules/%s", sub->name);
1888 if (safe_create_leading_directories_const(new_git_dir) < 0)
1889 die(_("could not create directory '%s'"), new_git_dir);
1890 real_new_git_dir = real_pathdup(new_git_dir, 1);
1892 fprintf(stderr, _("Migrating git directory of '%s%s' from\n'%s' to\n'%s'\n"),
1893 get_super_prefix_or_empty(), path,
1894 real_old_git_dir, real_new_git_dir);
1896 relocate_gitdir(path, real_old_git_dir, real_new_git_dir);
1899 free(real_old_git_dir);
1900 free(real_new_git_dir);
1904 * Migrate the git directory of the submodule given by path from
1905 * having its git directory within the working tree to the git dir nested
1906 * in its superprojects git dir under modules/.
1908 void absorb_git_dir_into_superproject(const char *prefix,
1913 const char *sub_git_dir;
1914 struct strbuf gitdir = STRBUF_INIT;
1915 strbuf_addf(&gitdir, "%s/.git", path);
1916 sub_git_dir = resolve_gitdir_gently(gitdir.buf, &err_code);
1918 /* Not populated? */
1920 const struct submodule *sub;
1922 if (err_code == READ_GITFILE_ERR_STAT_FAILED) {
1923 /* unpopulated as expected */
1924 strbuf_release(&gitdir);
1928 if (err_code != READ_GITFILE_ERR_NOT_A_REPO)
1929 /* We don't know what broke here. */
1930 read_gitfile_error_die(err_code, path, NULL);
1933 * Maybe populated, but no git directory was found?
1934 * This can happen if the superproject is a submodule
1935 * itself and was just absorbed. The absorption of the
1936 * superproject did not rewrite the git file links yet,
1939 sub = submodule_from_path(&null_oid, path);
1941 die(_("could not lookup name for submodule '%s'"), path);
1942 connect_work_tree_and_git_dir(path,
1943 git_path("modules/%s", sub->name));
1945 /* Is it already absorbed into the superprojects git dir? */
1946 char *real_sub_git_dir = real_pathdup(sub_git_dir, 1);
1947 char *real_common_git_dir = real_pathdup(get_git_common_dir(), 1);
1949 if (!starts_with(real_sub_git_dir, real_common_git_dir))
1950 relocate_single_git_dir_into_superproject(prefix, path);
1952 free(real_sub_git_dir);
1953 free(real_common_git_dir);
1955 strbuf_release(&gitdir);
1957 if (flags & ABSORB_GITDIR_RECURSE_SUBMODULES) {
1958 struct child_process cp = CHILD_PROCESS_INIT;
1959 struct strbuf sb = STRBUF_INIT;
1961 if (flags & ~ABSORB_GITDIR_RECURSE_SUBMODULES)
1962 die("BUG: we don't know how to pass the flags down?");
1964 strbuf_addstr(&sb, get_super_prefix_or_empty());
1965 strbuf_addstr(&sb, path);
1966 strbuf_addch(&sb, '/');
1971 argv_array_pushl(&cp.args, "--super-prefix", sb.buf,
1972 "submodule--helper",
1973 "absorb-git-dirs", NULL);
1974 prepare_submodule_repo_env(&cp.env_array);
1975 if (run_command(&cp))
1976 die(_("could not recurse into submodule '%s'"), path);
1978 strbuf_release(&sb);
1982 const char *get_superproject_working_tree(void)
1984 struct child_process cp = CHILD_PROCESS_INIT;
1985 struct strbuf sb = STRBUF_INIT;
1986 const char *one_up = real_path_if_valid("../");
1987 const char *cwd = xgetcwd();
1988 const char *ret = NULL;
1989 const char *subpath;
1993 if (!is_inside_work_tree())
1996 * We might have a superproject, but it is harder
2004 subpath = relative_path(cwd, one_up, &sb);
2006 prepare_submodule_repo_env(&cp.env_array);
2007 argv_array_pop(&cp.env_array);
2009 argv_array_pushl(&cp.args, "--literal-pathspecs", "-C", "..",
2010 "ls-files", "-z", "--stage", "--full-name", "--",
2019 if (start_command(&cp))
2020 die(_("could not start ls-files in .."));
2022 len = strbuf_read(&sb, cp.out, PATH_MAX);
2025 if (starts_with(sb.buf, "160000")) {
2027 int cwd_len = strlen(cwd);
2028 char *super_sub, *super_wt;
2031 * There is a superproject having this repo as a submodule.
2032 * The format is <mode> SP <hash> SP <stage> TAB <full name> \0,
2033 * We're only interested in the name after the tab.
2035 super_sub = strchr(sb.buf, '\t') + 1;
2036 super_sub_len = sb.buf + sb.len - super_sub - 1;
2038 if (super_sub_len > cwd_len ||
2039 strcmp(&cwd[cwd_len - super_sub_len], super_sub))
2040 die (_("BUG: returned path string doesn't match cwd?"));
2042 super_wt = xstrdup(cwd);
2043 super_wt[cwd_len - super_sub_len] = '\0';
2045 ret = real_path(super_wt);
2048 strbuf_release(&sb);
2050 code = finish_command(&cp);
2053 /* '../' is not a git repository */
2055 if (code == 0 && len == 0)
2056 /* There is an unrelated git repository at '../' */
2059 die(_("ls-tree returned unexpected return code %d"), code);
2065 * Put the gitdir for a submodule (given relative to the main
2066 * repository worktree) into `buf`, or return -1 on error.
2068 int submodule_to_gitdir(struct strbuf *buf, const char *submodule)
2070 const struct submodule *sub;
2071 const char *git_dir;
2075 strbuf_addstr(buf, submodule);
2076 strbuf_complete(buf, '/');
2077 strbuf_addstr(buf, ".git");
2079 git_dir = read_gitfile(buf->buf);
2082 strbuf_addstr(buf, git_dir);
2084 if (!is_git_directory(buf->buf)) {
2085 sub = submodule_from_path(&null_oid, submodule);
2091 strbuf_git_path(buf, "%s/%s", "modules", sub->name);