7 #include "run-command.h"
10 #include "string-list.h"
11 #include "transport.h"
13 typedef int (*needs_push_func_t)(const char *path, const unsigned char sha1[20],
15 static struct string_list config_name_for_path;
16 static struct string_list config_fetch_recurse_submodules_for_name;
17 static struct string_list config_ignore_for_name;
18 static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND;
19 static struct string_list changed_submodule_paths;
21 * The following flag is set if the .gitmodules file is unmerged. We then
22 * disable recursion for all submodules where .git/config doesn't have a
23 * matching config entry because we can't guess what might be configured in
24 * .gitmodules unless the user resolves the conflict. When a command line
25 * option is given (which always overrides configuration) this flag will be
28 static int gitmodules_is_unmerged;
30 static int add_submodule_odb(const char *path)
32 struct strbuf objects_directory = STRBUF_INIT;
33 struct alternate_object_database *alt_odb;
37 strbuf_addf(&objects_directory, "%s/.git", path);
38 git_dir = read_gitfile(objects_directory.buf);
40 strbuf_reset(&objects_directory);
41 strbuf_addstr(&objects_directory, git_dir);
43 strbuf_addstr(&objects_directory, "/objects/");
44 if (!is_directory(objects_directory.buf)) {
48 /* avoid adding it twice */
49 for (alt_odb = alt_odb_list; alt_odb; alt_odb = alt_odb->next)
50 if (alt_odb->name - alt_odb->base == objects_directory.len &&
51 !strncmp(alt_odb->base, objects_directory.buf,
52 objects_directory.len))
55 alt_odb = xmalloc(objects_directory.len + 42 + sizeof(*alt_odb));
56 alt_odb->next = alt_odb_list;
57 strcpy(alt_odb->base, objects_directory.buf);
58 alt_odb->name = alt_odb->base + objects_directory.len;
59 alt_odb->name[2] = '/';
60 alt_odb->name[40] = '\0';
61 alt_odb->name[41] = '\0';
62 alt_odb_list = alt_odb;
65 strbuf_release(&objects_directory);
69 void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
72 struct string_list_item *path_option, *ignore_option;
73 path_option = unsorted_string_list_lookup(&config_name_for_path, path);
75 ignore_option = unsorted_string_list_lookup(&config_ignore_for_name, path_option->util);
77 handle_ignore_submodules_arg(diffopt, ignore_option->util);
78 else if (gitmodules_is_unmerged)
79 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
83 int submodule_config(const char *var, const char *value, void *cb)
85 if (!prefixcmp(var, "submodule."))
86 return parse_submodule_config_option(var, value);
87 else if (!strcmp(var, "fetch.recursesubmodules")) {
88 config_fetch_recurse_submodules = parse_fetch_recurse_submodules_arg(var, value);
94 void gitmodules_config(void)
96 const char *work_tree = get_git_work_tree();
98 struct strbuf gitmodules_path = STRBUF_INIT;
100 strbuf_addstr(&gitmodules_path, work_tree);
101 strbuf_addstr(&gitmodules_path, "/.gitmodules");
102 if (read_cache() < 0)
103 die("index file corrupt");
104 pos = cache_name_pos(".gitmodules", 11);
105 if (pos < 0) { /* .gitmodules not found or isn't merged */
107 if (active_nr > pos) { /* there is a .gitmodules */
108 const struct cache_entry *ce = active_cache[pos];
109 if (ce_namelen(ce) == 11 &&
110 !memcmp(ce->name, ".gitmodules", 11))
111 gitmodules_is_unmerged = 1;
115 if (!gitmodules_is_unmerged)
116 git_config_from_file(submodule_config, gitmodules_path.buf, NULL);
117 strbuf_release(&gitmodules_path);
121 int parse_submodule_config_option(const char *var, const char *value)
124 struct string_list_item *config;
125 struct strbuf submodname = STRBUF_INIT;
127 var += 10; /* Skip "submodule." */
130 if ((len > 5) && !strcmp(var + len - 5, ".path")) {
131 strbuf_add(&submodname, var, len - 5);
132 config = unsorted_string_list_lookup(&config_name_for_path, value);
136 config = string_list_append(&config_name_for_path, xstrdup(value));
137 config->util = strbuf_detach(&submodname, NULL);
138 strbuf_release(&submodname);
139 } else if ((len > 23) && !strcmp(var + len - 23, ".fetchrecursesubmodules")) {
140 strbuf_add(&submodname, var, len - 23);
141 config = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, submodname.buf);
143 config = string_list_append(&config_fetch_recurse_submodules_for_name,
144 strbuf_detach(&submodname, NULL));
145 config->util = (void *)(intptr_t)parse_fetch_recurse_submodules_arg(var, value);
146 strbuf_release(&submodname);
147 } else if ((len > 7) && !strcmp(var + len - 7, ".ignore")) {
148 if (strcmp(value, "untracked") && strcmp(value, "dirty") &&
149 strcmp(value, "all") && strcmp(value, "none")) {
150 warning("Invalid parameter \"%s\" for config option \"submodule.%s.ignore\"", value, var);
154 strbuf_add(&submodname, var, len - 7);
155 config = unsorted_string_list_lookup(&config_ignore_for_name, submodname.buf);
159 config = string_list_append(&config_ignore_for_name,
160 strbuf_detach(&submodname, NULL));
161 strbuf_release(&submodname);
162 config->util = xstrdup(value);
168 void handle_ignore_submodules_arg(struct diff_options *diffopt,
171 DIFF_OPT_CLR(diffopt, IGNORE_SUBMODULES);
172 DIFF_OPT_CLR(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
173 DIFF_OPT_CLR(diffopt, IGNORE_DIRTY_SUBMODULES);
175 if (!strcmp(arg, "all"))
176 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
177 else if (!strcmp(arg, "untracked"))
178 DIFF_OPT_SET(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
179 else if (!strcmp(arg, "dirty"))
180 DIFF_OPT_SET(diffopt, IGNORE_DIRTY_SUBMODULES);
181 else if (strcmp(arg, "none"))
182 die("bad --ignore-submodules argument: %s", arg);
185 static int prepare_submodule_summary(struct rev_info *rev, const char *path,
186 struct commit *left, struct commit *right,
187 int *fast_forward, int *fast_backward)
189 struct commit_list *merge_bases, *list;
191 init_revisions(rev, NULL);
192 setup_revisions(0, NULL, rev, NULL);
194 rev->first_parent_only = 1;
195 left->object.flags |= SYMMETRIC_LEFT;
196 add_pending_object(rev, &left->object, path);
197 add_pending_object(rev, &right->object, path);
198 merge_bases = get_merge_bases(left, right, 1);
200 if (merge_bases->item == left)
202 else if (merge_bases->item == right)
205 for (list = merge_bases; list; list = list->next) {
206 list->item->object.flags |= UNINTERESTING;
207 add_pending_object(rev, &list->item->object,
208 sha1_to_hex(list->item->object.sha1));
210 return prepare_revision_walk(rev);
213 static void print_submodule_summary(struct rev_info *rev, FILE *f,
214 const char *del, const char *add, const char *reset)
216 static const char format[] = " %m %s";
217 struct strbuf sb = STRBUF_INIT;
218 struct commit *commit;
220 while ((commit = get_revision(rev))) {
221 struct pretty_print_context ctx = {0};
222 ctx.date_mode = rev->date_mode;
223 strbuf_setlen(&sb, 0);
224 if (commit->object.flags & SYMMETRIC_LEFT) {
226 strbuf_addstr(&sb, del);
229 strbuf_addstr(&sb, add);
230 format_commit_message(commit, format, &sb, &ctx);
232 strbuf_addstr(&sb, reset);
233 strbuf_addch(&sb, '\n');
234 fprintf(f, "%s", sb.buf);
239 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
241 switch (git_config_maybe_bool(opt, arg)) {
243 return RECURSE_SUBMODULES_ON;
245 return RECURSE_SUBMODULES_OFF;
247 if (!strcmp(arg, "on-demand"))
248 return RECURSE_SUBMODULES_ON_DEMAND;
249 die("bad %s argument: %s", opt, arg);
253 void show_submodule_summary(FILE *f, const char *path,
254 unsigned char one[20], unsigned char two[20],
255 unsigned dirty_submodule,
256 const char *del, const char *add, const char *reset)
259 struct commit *left = left, *right = right;
260 const char *message = NULL;
261 struct strbuf sb = STRBUF_INIT;
262 int fast_forward = 0, fast_backward = 0;
264 if (is_null_sha1(two))
265 message = "(submodule deleted)";
266 else if (add_submodule_odb(path))
267 message = "(not checked out)";
268 else if (is_null_sha1(one))
269 message = "(new submodule)";
270 else if (!(left = lookup_commit_reference(one)) ||
271 !(right = lookup_commit_reference(two)))
272 message = "(commits not present)";
275 prepare_submodule_summary(&rev, path, left, right,
276 &fast_forward, &fast_backward))
277 message = "(revision walker failed)";
279 if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
280 fprintf(f, "Submodule %s contains untracked content\n", path);
281 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
282 fprintf(f, "Submodule %s contains modified content\n", path);
284 if (!hashcmp(one, two)) {
289 strbuf_addf(&sb, "Submodule %s %s..", path,
290 find_unique_abbrev(one, DEFAULT_ABBREV));
291 if (!fast_backward && !fast_forward)
292 strbuf_addch(&sb, '.');
293 strbuf_addf(&sb, "%s", find_unique_abbrev(two, DEFAULT_ABBREV));
295 strbuf_addf(&sb, " %s\n", message);
297 strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : "");
298 fwrite(sb.buf, sb.len, 1, f);
301 print_submodule_summary(&rev, f, del, add, reset);
302 clear_commit_marks(left, ~0);
303 clear_commit_marks(right, ~0);
309 void set_config_fetch_recurse_submodules(int value)
311 config_fetch_recurse_submodules = value;
314 typedef int (*module_func_t)(const char *path, const unsigned char sha1[20], void *data);
316 static int has_remote(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
321 int submodule_needs_pushing(const char *path, const unsigned char sha1[20], void *data)
323 int *needs_pushing = data;
325 if (add_submodule_odb(path) || !lookup_commit_reference(sha1))
328 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
329 struct child_process cp;
330 const char *argv[] = {"rev-list", NULL, "--not", "--remotes", "-n", "1" , NULL};
331 struct strbuf buf = STRBUF_INIT;
333 argv[1] = sha1_to_hex(sha1);
334 memset(&cp, 0, sizeof(cp));
336 cp.env = local_repo_env;
341 if (start_command(&cp))
342 die("Could not run 'git rev-list %s --not --remotes -n 1' command in submodule %s",
343 sha1_to_hex(sha1), path);
344 if (strbuf_read(&buf, cp.out, 41))
348 strbuf_release(&buf);
349 return !*needs_pushing;
355 int push_submodule(const char *path, const unsigned char sha1[20], void *data)
357 if (add_submodule_odb(path) || !lookup_commit_reference(sha1))
360 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
361 struct child_process cp;
362 const char *argv[] = {"push", NULL};
364 memset(&cp, 0, sizeof(cp));
366 cp.env = local_repo_env;
371 if (run_command(&cp))
372 die("Could not run 'git push' command in submodule %s", path);
379 struct collect_submodules_data {
385 static void collect_submodules_from_diff(struct diff_queue_struct *q,
386 struct diff_options *options,
390 struct collect_submodules_data *me = data;
392 for (i = 0; i < q->nr; i++) {
393 struct diff_filepair *p = q->queue[i];
394 if (!S_ISGITLINK(p->two->mode))
396 if (!(me->ret = me->func(p->two->path, p->two->sha1, me->data)))
401 static int commit_need_pushing(struct commit *commit, struct commit_list *parent,
402 module_func_t func, void *data)
404 const unsigned char (*parents)[20];
408 struct collect_submodules_data cb;
413 n = commit_list_count(parent);
414 parents = xmalloc(n * sizeof(*parents));
416 for (i = 0; i < n; i++) {
417 hashcpy((unsigned char *)(parents + i), parent->item->object.sha1);
418 parent = parent->next;
421 init_revisions(&rev, NULL);
422 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
423 rev.diffopt.format_callback = collect_submodules_from_diff;
424 rev.diffopt.format_callback_data = &cb;
425 diff_tree_combined(commit->object.sha1, parents, n, 1, &rev);
431 static int inspect_superproject_commits(unsigned char new_sha1[20], const char *remotes_name,
432 module_func_t func, void *data)
435 struct commit *commit;
436 const char *argv[] = {NULL, NULL, "--not", "NULL", NULL};
437 int argc = ARRAY_SIZE(argv) - 1;
439 struct strbuf remotes_arg = STRBUF_INIT;
442 strbuf_addf(&remotes_arg, "--remotes=%s", remotes_name);
443 init_revisions(&rev, NULL);
444 sha1_copy = xstrdup(sha1_to_hex(new_sha1));
446 argv[3] = remotes_arg.buf;
447 setup_revisions(argc, argv, &rev, NULL);
448 if (prepare_revision_walk(&rev))
449 die("revision walk setup failed");
451 while ((commit = get_revision(&rev)) && do_continue)
452 do_continue = commit_need_pushing(commit, commit->parents, func, data);
455 strbuf_release(&remotes_arg);
460 int check_submodule_needs_pushing(unsigned char new_sha1[20], const char *remotes_name)
463 inspect_superproject_commits(new_sha1, remotes_name, submodule_needs_pushing, &needs_push);
467 void push_unpushed_submodules(unsigned char new_sha1[20], const char *remotes_name)
469 inspect_superproject_commits(new_sha1, remotes_name, push_submodule, NULL);
472 static int is_submodule_commit_present(const char *path, unsigned char sha1[20])
475 if (!add_submodule_odb(path) && lookup_commit_reference(sha1)) {
476 /* Even if the submodule is checked out and the commit is
477 * present, make sure it is reachable from a ref. */
478 struct child_process cp;
479 const char *argv[] = {"rev-list", "-n", "1", NULL, "--not", "--all", NULL};
480 struct strbuf buf = STRBUF_INIT;
482 argv[3] = sha1_to_hex(sha1);
483 memset(&cp, 0, sizeof(cp));
485 cp.env = local_repo_env;
490 if (!run_command(&cp) && !strbuf_read(&buf, cp.out, 1024))
494 strbuf_release(&buf);
499 static void submodule_collect_changed_cb(struct diff_queue_struct *q,
500 struct diff_options *options,
504 for (i = 0; i < q->nr; i++) {
505 struct diff_filepair *p = q->queue[i];
506 if (!S_ISGITLINK(p->two->mode))
509 if (S_ISGITLINK(p->one->mode)) {
510 /* NEEDSWORK: We should honor the name configured in
511 * the .gitmodules file of the commit we are examining
512 * here to be able to correctly follow submodules
513 * being moved around. */
514 struct string_list_item *path;
515 path = unsorted_string_list_lookup(&changed_submodule_paths, p->two->path);
516 if (!path && !is_submodule_commit_present(p->two->path, p->two->sha1))
517 string_list_append(&changed_submodule_paths, xstrdup(p->two->path));
519 /* Submodule is new or was moved here */
520 /* NEEDSWORK: When the .git directories of submodules
521 * live inside the superprojects .git directory some
522 * day we should fetch new submodules directly into
523 * that location too when config or options request
524 * that so they can be checked out from there. */
530 void check_for_new_submodule_commits(unsigned char new_sha1[20])
533 struct commit *commit;
534 const char *argv[] = {NULL, NULL, "--not", "--all", NULL};
535 int argc = ARRAY_SIZE(argv) - 1;
537 init_revisions(&rev, NULL);
538 argv[1] = xstrdup(sha1_to_hex(new_sha1));
539 setup_revisions(argc, argv, &rev, NULL);
540 if (prepare_revision_walk(&rev))
541 die("revision walk setup failed");
544 * Collect all submodules (whether checked out or not) for which new
545 * commits have been recorded upstream in "changed_submodule_paths".
547 while ((commit = get_revision(&rev))) {
548 struct commit_list *parent = commit->parents;
550 struct diff_options diff_opts;
551 diff_setup(&diff_opts);
552 DIFF_OPT_SET(&diff_opts, RECURSIVE);
553 diff_opts.output_format |= DIFF_FORMAT_CALLBACK;
554 diff_opts.format_callback = submodule_collect_changed_cb;
555 if (diff_setup_done(&diff_opts) < 0)
556 die("diff_setup_done failed");
557 diff_tree_sha1(parent->item->object.sha1, commit->object.sha1, "", &diff_opts);
558 diffcore_std(&diff_opts);
559 diff_flush(&diff_opts);
560 parent = parent->next;
563 free((char *)argv[1]);
566 int fetch_populated_submodules(int num_options, const char **options,
567 const char *prefix, int command_line_option,
570 int i, result = 0, argc = 0, default_argc;
571 struct child_process cp;
573 struct string_list_item *name_for_path;
574 const char *work_tree = get_git_work_tree();
578 if (!the_index.initialized)
579 if (read_cache() < 0)
580 die("index file corrupt");
582 /* 6: "fetch" (options) --recurse-submodules-default default "--submodule-prefix" prefix NULL */
583 argv = xcalloc(num_options + 6, sizeof(const char *));
584 argv[argc++] = "fetch";
585 for (i = 0; i < num_options; i++)
586 argv[argc++] = options[i];
587 argv[argc++] = "--recurse-submodules-default";
588 default_argc = argc++;
589 argv[argc++] = "--submodule-prefix";
591 memset(&cp, 0, sizeof(cp));
593 cp.env = local_repo_env;
597 for (i = 0; i < active_nr; i++) {
598 struct strbuf submodule_path = STRBUF_INIT;
599 struct strbuf submodule_git_dir = STRBUF_INIT;
600 struct strbuf submodule_prefix = STRBUF_INIT;
601 struct cache_entry *ce = active_cache[i];
602 const char *git_dir, *name, *default_argv;
604 if (!S_ISGITLINK(ce->ce_mode))
608 name_for_path = unsorted_string_list_lookup(&config_name_for_path, ce->name);
610 name = name_for_path->util;
612 default_argv = "yes";
613 if (command_line_option == RECURSE_SUBMODULES_DEFAULT) {
614 struct string_list_item *fetch_recurse_submodules_option;
615 fetch_recurse_submodules_option = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name);
616 if (fetch_recurse_submodules_option) {
617 if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_OFF)
619 if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_ON_DEMAND) {
620 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
622 default_argv = "on-demand";
625 if ((config_fetch_recurse_submodules == RECURSE_SUBMODULES_OFF) ||
626 gitmodules_is_unmerged)
628 if (config_fetch_recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) {
629 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
631 default_argv = "on-demand";
634 } else if (command_line_option == RECURSE_SUBMODULES_ON_DEMAND) {
635 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
637 default_argv = "on-demand";
640 strbuf_addf(&submodule_path, "%s/%s", work_tree, ce->name);
641 strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf);
642 strbuf_addf(&submodule_prefix, "%s%s/", prefix, ce->name);
643 git_dir = read_gitfile(submodule_git_dir.buf);
645 git_dir = submodule_git_dir.buf;
646 if (is_directory(git_dir)) {
648 printf("Fetching submodule %s%s\n", prefix, ce->name);
649 cp.dir = submodule_path.buf;
650 argv[default_argc] = default_argv;
651 argv[argc] = submodule_prefix.buf;
652 if (run_command(&cp))
655 strbuf_release(&submodule_path);
656 strbuf_release(&submodule_git_dir);
657 strbuf_release(&submodule_prefix);
661 string_list_clear(&changed_submodule_paths, 1);
665 unsigned is_submodule_modified(const char *path, int ignore_untracked)
668 struct child_process cp;
669 const char *argv[] = {
675 struct strbuf buf = STRBUF_INIT;
676 unsigned dirty_submodule = 0;
677 const char *line, *next_line;
680 strbuf_addf(&buf, "%s/.git", path);
681 git_dir = read_gitfile(buf.buf);
684 if (!is_directory(git_dir)) {
685 strbuf_release(&buf);
686 /* The submodule is not checked out, so it is not modified */
692 if (ignore_untracked)
695 memset(&cp, 0, sizeof(cp));
697 cp.env = local_repo_env;
702 if (start_command(&cp))
703 die("Could not run git status --porcelain");
705 len = strbuf_read(&buf, cp.out, 1024);
708 if ((line[0] == '?') && (line[1] == '?')) {
709 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
710 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
713 dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
714 if (ignore_untracked ||
715 (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED))
718 next_line = strchr(line, '\n');
722 len -= (next_line - line);
727 if (finish_command(&cp))
728 die("git status --porcelain failed");
730 strbuf_release(&buf);
731 return dirty_submodule;
734 static int find_first_merges(struct object_array *result, const char *path,
735 struct commit *a, struct commit *b)
738 struct object_array merges;
739 struct commit *commit;
740 int contains_another;
742 char merged_revision[42];
743 const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path",
744 "--all", merged_revision, NULL };
745 struct rev_info revs;
746 struct setup_revision_opt rev_opts;
748 memset(&merges, 0, sizeof(merges));
749 memset(result, 0, sizeof(struct object_array));
750 memset(&rev_opts, 0, sizeof(rev_opts));
752 /* get all revisions that merge commit a */
753 snprintf(merged_revision, sizeof(merged_revision), "^%s",
754 sha1_to_hex(a->object.sha1));
755 init_revisions(&revs, NULL);
756 rev_opts.submodule = path;
757 setup_revisions(sizeof(rev_args)/sizeof(char *)-1, rev_args, &revs, &rev_opts);
759 /* save all revisions from the above list that contain b */
760 if (prepare_revision_walk(&revs))
761 die("revision walk setup failed");
762 while ((commit = get_revision(&revs)) != NULL) {
763 struct object *o = &(commit->object);
764 if (in_merge_bases(b, &commit, 1))
765 add_object_array(o, NULL, &merges);
768 /* Now we've got all merges that contain a and b. Prune all
769 * merges that contain another found merge and save them in
772 for (i = 0; i < merges.nr; i++) {
773 struct commit *m1 = (struct commit *) merges.objects[i].item;
775 contains_another = 0;
776 for (j = 0; j < merges.nr; j++) {
777 struct commit *m2 = (struct commit *) merges.objects[j].item;
778 if (i != j && in_merge_bases(m2, &m1, 1)) {
779 contains_another = 1;
784 if (!contains_another)
785 add_object_array(merges.objects[i].item,
786 merges.objects[i].name, result);
789 free(merges.objects);
793 static void print_commit(struct commit *commit)
795 struct strbuf sb = STRBUF_INIT;
796 struct pretty_print_context ctx = {0};
797 ctx.date_mode = DATE_NORMAL;
798 format_commit_message(commit, " %h: %m %s", &sb, &ctx);
799 fprintf(stderr, "%s\n", sb.buf);
803 #define MERGE_WARNING(path, msg) \
804 warning("Failed to merge submodule %s (%s)", path, msg);
806 int merge_submodule(unsigned char result[20], const char *path,
807 const unsigned char base[20], const unsigned char a[20],
808 const unsigned char b[20])
810 struct commit *commit_base, *commit_a, *commit_b;
812 struct object_array merges;
816 /* store a in result in case we fail */
819 /* we can not handle deletion conflicts */
820 if (is_null_sha1(base))
827 if (add_submodule_odb(path)) {
828 MERGE_WARNING(path, "not checked out");
832 if (!(commit_base = lookup_commit_reference(base)) ||
833 !(commit_a = lookup_commit_reference(a)) ||
834 !(commit_b = lookup_commit_reference(b))) {
835 MERGE_WARNING(path, "commits not present");
839 /* check whether both changes are forward */
840 if (!in_merge_bases(commit_base, &commit_a, 1) ||
841 !in_merge_bases(commit_base, &commit_b, 1)) {
842 MERGE_WARNING(path, "commits don't follow merge-base");
846 /* Case #1: a is contained in b or vice versa */
847 if (in_merge_bases(commit_a, &commit_b, 1)) {
851 if (in_merge_bases(commit_b, &commit_a, 1)) {
857 * Case #2: There are one or more merges that contain a and b in
858 * the submodule. If there is only one, then present it as a
859 * suggestion to the user, but leave it marked unmerged so the
860 * user needs to confirm the resolution.
863 /* find commit which merges them */
864 parent_count = find_first_merges(&merges, path, commit_a, commit_b);
865 switch (parent_count) {
867 MERGE_WARNING(path, "merge following commits not found");
871 MERGE_WARNING(path, "not fast-forward");
872 fprintf(stderr, "Found a possible merge resolution "
873 "for the submodule:\n");
874 print_commit((struct commit *) merges.objects[0].item);
876 "If this is correct simply add it to the index "
879 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
880 "which will accept this suggestion.\n",
881 sha1_to_hex(merges.objects[0].item->sha1), path);
885 MERGE_WARNING(path, "multiple merges found");
886 for (i = 0; i < merges.nr; i++)
887 print_commit((struct commit *) merges.objects[i].item);
890 free(merges.objects);