Code

fetch: skip on-demand checking when no submodules are configured
[git.git] / submodule.c
1 #include "cache.h"
2 #include "submodule.h"
3 #include "dir.h"
4 #include "diff.h"
5 #include "commit.h"
6 #include "revision.h"
7 #include "run-command.h"
8 #include "diffcore.h"
9 #include "refs.h"
10 #include "string-list.h"
12 static struct string_list config_name_for_path;
13 static struct string_list config_fetch_recurse_submodules_for_name;
14 static struct string_list config_ignore_for_name;
15 static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND;
16 static struct string_list changed_submodule_paths;
17 /*
18  * The following flag is set if the .gitmodules file is unmerged. We then
19  * disable recursion for all submodules where .git/config doesn't have a
20  * matching config entry because we can't guess what might be configured in
21  * .gitmodules unless the user resolves the conflict. When a command line
22  * option is given (which always overrides configuration) this flag will be
23  * ignored.
24  */
25 static int gitmodules_is_unmerged;
27 static int add_submodule_odb(const char *path)
28 {
29         struct strbuf objects_directory = STRBUF_INIT;
30         struct alternate_object_database *alt_odb;
31         int ret = 0;
32         const char *git_dir;
34         strbuf_addf(&objects_directory, "%s/.git", path);
35         git_dir = read_gitfile_gently(objects_directory.buf);
36         if (git_dir) {
37                 strbuf_reset(&objects_directory);
38                 strbuf_addstr(&objects_directory, git_dir);
39         }
40         strbuf_addstr(&objects_directory, "/objects/");
41         if (!is_directory(objects_directory.buf)) {
42                 ret = -1;
43                 goto done;
44         }
45         /* avoid adding it twice */
46         for (alt_odb = alt_odb_list; alt_odb; alt_odb = alt_odb->next)
47                 if (alt_odb->name - alt_odb->base == objects_directory.len &&
48                                 !strncmp(alt_odb->base, objects_directory.buf,
49                                         objects_directory.len))
50                         goto done;
52         alt_odb = xmalloc(objects_directory.len + 42 + sizeof(*alt_odb));
53         alt_odb->next = alt_odb_list;
54         strcpy(alt_odb->base, objects_directory.buf);
55         alt_odb->name = alt_odb->base + objects_directory.len;
56         alt_odb->name[2] = '/';
57         alt_odb->name[40] = '\0';
58         alt_odb->name[41] = '\0';
59         alt_odb_list = alt_odb;
60         prepare_alt_odb();
61 done:
62         strbuf_release(&objects_directory);
63         return ret;
64 }
66 void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
67                                              const char *path)
68 {
69         struct string_list_item *path_option, *ignore_option;
70         path_option = unsorted_string_list_lookup(&config_name_for_path, path);
71         if (path_option) {
72                 ignore_option = unsorted_string_list_lookup(&config_ignore_for_name, path_option->util);
73                 if (ignore_option)
74                         handle_ignore_submodules_arg(diffopt, ignore_option->util);
75                 else if (gitmodules_is_unmerged)
76                         DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
77         }
78 }
80 int submodule_config(const char *var, const char *value, void *cb)
81 {
82         if (!prefixcmp(var, "submodule."))
83                 return parse_submodule_config_option(var, value);
84         else if (!strcmp(var, "fetch.recursesubmodules")) {
85                 config_fetch_recurse_submodules = parse_fetch_recurse_submodules_arg(var, value);
86                 return 0;
87         }
88         return 0;
89 }
91 void gitmodules_config(void)
92 {
93         const char *work_tree = get_git_work_tree();
94         if (work_tree) {
95                 struct strbuf gitmodules_path = STRBUF_INIT;
96                 int pos;
97                 strbuf_addstr(&gitmodules_path, work_tree);
98                 strbuf_addstr(&gitmodules_path, "/.gitmodules");
99                 if (read_cache() < 0)
100                         die("index file corrupt");
101                 pos = cache_name_pos(".gitmodules", 11);
102                 if (pos < 0) { /* .gitmodules not found or isn't merged */
103                         pos = -1 - pos;
104                         if (active_nr > pos) {  /* there is a .gitmodules */
105                                 const struct cache_entry *ce = active_cache[pos];
106                                 if (ce_namelen(ce) == 11 &&
107                                     !memcmp(ce->name, ".gitmodules", 11))
108                                         gitmodules_is_unmerged = 1;
109                         }
110                 }
112                 if (!gitmodules_is_unmerged)
113                         git_config_from_file(submodule_config, gitmodules_path.buf, NULL);
114                 strbuf_release(&gitmodules_path);
115         }
118 int parse_submodule_config_option(const char *var, const char *value)
120         int len;
121         struct string_list_item *config;
122         struct strbuf submodname = STRBUF_INIT;
124         var += 10;              /* Skip "submodule." */
126         len = strlen(var);
127         if ((len > 5) && !strcmp(var + len - 5, ".path")) {
128                 strbuf_add(&submodname, var, len - 5);
129                 config = unsorted_string_list_lookup(&config_name_for_path, value);
130                 if (config)
131                         free(config->util);
132                 else
133                         config = string_list_append(&config_name_for_path, xstrdup(value));
134                 config->util = strbuf_detach(&submodname, NULL);
135                 strbuf_release(&submodname);
136         } else if ((len > 23) && !strcmp(var + len - 23, ".fetchrecursesubmodules")) {
137                 strbuf_add(&submodname, var, len - 23);
138                 config = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, submodname.buf);
139                 if (!config)
140                         config = string_list_append(&config_fetch_recurse_submodules_for_name,
141                                                     strbuf_detach(&submodname, NULL));
142                 config->util = (void *)(intptr_t)parse_fetch_recurse_submodules_arg(var, value);
143                 strbuf_release(&submodname);
144         } else if ((len > 7) && !strcmp(var + len - 7, ".ignore")) {
145                 if (strcmp(value, "untracked") && strcmp(value, "dirty") &&
146                     strcmp(value, "all") && strcmp(value, "none")) {
147                         warning("Invalid parameter \"%s\" for config option \"submodule.%s.ignore\"", value, var);
148                         return 0;
149                 }
151                 strbuf_add(&submodname, var, len - 7);
152                 config = unsorted_string_list_lookup(&config_ignore_for_name, submodname.buf);
153                 if (config)
154                         free(config->util);
155                 else
156                         config = string_list_append(&config_ignore_for_name,
157                                                     strbuf_detach(&submodname, NULL));
158                 strbuf_release(&submodname);
159                 config->util = xstrdup(value);
160                 return 0;
161         }
162         return 0;
165 void handle_ignore_submodules_arg(struct diff_options *diffopt,
166                                   const char *arg)
168         DIFF_OPT_CLR(diffopt, IGNORE_SUBMODULES);
169         DIFF_OPT_CLR(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
170         DIFF_OPT_CLR(diffopt, IGNORE_DIRTY_SUBMODULES);
172         if (!strcmp(arg, "all"))
173                 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
174         else if (!strcmp(arg, "untracked"))
175                 DIFF_OPT_SET(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
176         else if (!strcmp(arg, "dirty"))
177                 DIFF_OPT_SET(diffopt, IGNORE_DIRTY_SUBMODULES);
178         else if (strcmp(arg, "none"))
179                 die("bad --ignore-submodules argument: %s", arg);
182 static int prepare_submodule_summary(struct rev_info *rev, const char *path,
183                 struct commit *left, struct commit *right,
184                 int *fast_forward, int *fast_backward)
186         struct commit_list *merge_bases, *list;
188         init_revisions(rev, NULL);
189         setup_revisions(0, NULL, rev, NULL);
190         rev->left_right = 1;
191         rev->first_parent_only = 1;
192         left->object.flags |= SYMMETRIC_LEFT;
193         add_pending_object(rev, &left->object, path);
194         add_pending_object(rev, &right->object, path);
195         merge_bases = get_merge_bases(left, right, 1);
196         if (merge_bases) {
197                 if (merge_bases->item == left)
198                         *fast_forward = 1;
199                 else if (merge_bases->item == right)
200                         *fast_backward = 1;
201         }
202         for (list = merge_bases; list; list = list->next) {
203                 list->item->object.flags |= UNINTERESTING;
204                 add_pending_object(rev, &list->item->object,
205                         sha1_to_hex(list->item->object.sha1));
206         }
207         return prepare_revision_walk(rev);
210 static void print_submodule_summary(struct rev_info *rev, FILE *f,
211                 const char *del, const char *add, const char *reset)
213         static const char format[] = "  %m %s";
214         struct strbuf sb = STRBUF_INIT;
215         struct commit *commit;
217         while ((commit = get_revision(rev))) {
218                 struct pretty_print_context ctx = {0};
219                 ctx.date_mode = rev->date_mode;
220                 strbuf_setlen(&sb, 0);
221                 if (commit->object.flags & SYMMETRIC_LEFT) {
222                         if (del)
223                                 strbuf_addstr(&sb, del);
224                 }
225                 else if (add)
226                         strbuf_addstr(&sb, add);
227                 format_commit_message(commit, format, &sb, &ctx);
228                 if (reset)
229                         strbuf_addstr(&sb, reset);
230                 strbuf_addch(&sb, '\n');
231                 fprintf(f, "%s", sb.buf);
232         }
233         strbuf_release(&sb);
236 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
238         switch (git_config_maybe_bool(opt, arg)) {
239         case 1:
240                 return RECURSE_SUBMODULES_ON;
241         case 0:
242                 return RECURSE_SUBMODULES_OFF;
243         default:
244                 if (!strcmp(arg, "on-demand"))
245                         return RECURSE_SUBMODULES_ON_DEMAND;
246                 die("bad %s argument: %s", opt, arg);
247         }
250 void show_submodule_summary(FILE *f, const char *path,
251                 unsigned char one[20], unsigned char two[20],
252                 unsigned dirty_submodule,
253                 const char *del, const char *add, const char *reset)
255         struct rev_info rev;
256         struct commit *left = left, *right = right;
257         const char *message = NULL;
258         struct strbuf sb = STRBUF_INIT;
259         int fast_forward = 0, fast_backward = 0;
261         if (is_null_sha1(two))
262                 message = "(submodule deleted)";
263         else if (add_submodule_odb(path))
264                 message = "(not checked out)";
265         else if (is_null_sha1(one))
266                 message = "(new submodule)";
267         else if (!(left = lookup_commit_reference(one)) ||
268                  !(right = lookup_commit_reference(two)))
269                 message = "(commits not present)";
271         if (!message &&
272             prepare_submodule_summary(&rev, path, left, right,
273                                         &fast_forward, &fast_backward))
274                 message = "(revision walker failed)";
276         if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
277                 fprintf(f, "Submodule %s contains untracked content\n", path);
278         if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
279                 fprintf(f, "Submodule %s contains modified content\n", path);
281         if (!hashcmp(one, two)) {
282                 strbuf_release(&sb);
283                 return;
284         }
286         strbuf_addf(&sb, "Submodule %s %s..", path,
287                         find_unique_abbrev(one, DEFAULT_ABBREV));
288         if (!fast_backward && !fast_forward)
289                 strbuf_addch(&sb, '.');
290         strbuf_addf(&sb, "%s", find_unique_abbrev(two, DEFAULT_ABBREV));
291         if (message)
292                 strbuf_addf(&sb, " %s\n", message);
293         else
294                 strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : "");
295         fwrite(sb.buf, sb.len, 1, f);
297         if (!message) {
298                 print_submodule_summary(&rev, f, del, add, reset);
299                 clear_commit_marks(left, ~0);
300                 clear_commit_marks(right, ~0);
301         }
303         strbuf_release(&sb);
306 void set_config_fetch_recurse_submodules(int value)
308         config_fetch_recurse_submodules = value;
311 static int is_submodule_commit_present(const char *path, unsigned char sha1[20])
313         int is_present = 0;
314         if (!add_submodule_odb(path) && lookup_commit_reference(sha1)) {
315                 /* Even if the submodule is checked out and the commit is
316                  * present, make sure it is reachable from a ref. */
317                 struct child_process cp;
318                 const char *argv[] = {"rev-list", "-n", "1", NULL, "--not", "--all", NULL};
319                 struct strbuf buf = STRBUF_INIT;
321                 argv[3] = sha1_to_hex(sha1);
322                 memset(&cp, 0, sizeof(cp));
323                 cp.argv = argv;
324                 cp.env = local_repo_env;
325                 cp.git_cmd = 1;
326                 cp.no_stdin = 1;
327                 cp.out = -1;
328                 cp.dir = path;
329                 if (!run_command(&cp) && !strbuf_read(&buf, cp.out, 1024))
330                         is_present = 1;
332                 close(cp.out);
333                 strbuf_release(&buf);
334         }
335         return is_present;
338 static void submodule_collect_changed_cb(struct diff_queue_struct *q,
339                                          struct diff_options *options,
340                                          void *data)
342         int i;
343         for (i = 0; i < q->nr; i++) {
344                 struct diff_filepair *p = q->queue[i];
345                 if (!S_ISGITLINK(p->two->mode))
346                         continue;
348                 if (S_ISGITLINK(p->one->mode)) {
349                         /* NEEDSWORK: We should honor the name configured in
350                          * the .gitmodules file of the commit we are examining
351                          * here to be able to correctly follow submodules
352                          * being moved around. */
353                         struct string_list_item *path;
354                         path = unsorted_string_list_lookup(&changed_submodule_paths, p->two->path);
355                         if (!path && !is_submodule_commit_present(p->two->path, p->two->sha1))
356                                 string_list_append(&changed_submodule_paths, xstrdup(p->two->path));
357                 } else {
358                         /* Submodule is new or was moved here */
359                         /* NEEDSWORK: When the .git directories of submodules
360                          * live inside the superprojects .git directory some
361                          * day we should fetch new submodules directly into
362                          * that location too when config or options request
363                          * that so they can be checked out from there. */
364                         continue;
365                 }
366         }
369 void check_for_new_submodule_commits(unsigned char new_sha1[20])
371         struct rev_info rev;
372         struct commit *commit;
373         const char *argv[] = {NULL, NULL, "--not", "--all", NULL};
374         int argc = ARRAY_SIZE(argv) - 1;
376         /* No need to check if there are no submodules configured */
377         if (!config_name_for_path.nr)
378                 return;
380         init_revisions(&rev, NULL);
381         argv[1] = xstrdup(sha1_to_hex(new_sha1));
382         setup_revisions(argc, argv, &rev, NULL);
383         if (prepare_revision_walk(&rev))
384                 die("revision walk setup failed");
386         /*
387          * Collect all submodules (whether checked out or not) for which new
388          * commits have been recorded upstream in "changed_submodule_paths".
389          */
390         while ((commit = get_revision(&rev))) {
391                 struct commit_list *parent = commit->parents;
392                 while (parent) {
393                         struct diff_options diff_opts;
394                         diff_setup(&diff_opts);
395                         diff_opts.output_format |= DIFF_FORMAT_CALLBACK;
396                         diff_opts.format_callback = submodule_collect_changed_cb;
397                         if (diff_setup_done(&diff_opts) < 0)
398                                 die("diff_setup_done failed");
399                         diff_tree_sha1(parent->item->object.sha1, commit->object.sha1, "", &diff_opts);
400                         diffcore_std(&diff_opts);
401                         diff_flush(&diff_opts);
402                         parent = parent->next;
403                 }
404         }
405         free((char *)argv[1]);
408 int fetch_populated_submodules(int num_options, const char **options,
409                                const char *prefix, int command_line_option,
410                                int quiet)
412         int i, result = 0, argc = 0, default_argc;
413         struct child_process cp;
414         const char **argv;
415         struct string_list_item *name_for_path;
416         const char *work_tree = get_git_work_tree();
417         if (!work_tree)
418                 goto out;
420         if (!the_index.initialized)
421                 if (read_cache() < 0)
422                         die("index file corrupt");
424         /* 6: "fetch" (options) --recurse-submodules-default default "--submodule-prefix" prefix NULL */
425         argv = xcalloc(num_options + 6, sizeof(const char *));
426         argv[argc++] = "fetch";
427         for (i = 0; i < num_options; i++)
428                 argv[argc++] = options[i];
429         argv[argc++] = "--recurse-submodules-default";
430         default_argc = argc++;
431         argv[argc++] = "--submodule-prefix";
433         memset(&cp, 0, sizeof(cp));
434         cp.argv = argv;
435         cp.env = local_repo_env;
436         cp.git_cmd = 1;
437         cp.no_stdin = 1;
439         for (i = 0; i < active_nr; i++) {
440                 struct strbuf submodule_path = STRBUF_INIT;
441                 struct strbuf submodule_git_dir = STRBUF_INIT;
442                 struct strbuf submodule_prefix = STRBUF_INIT;
443                 struct cache_entry *ce = active_cache[i];
444                 const char *git_dir, *name, *default_argv;
446                 if (!S_ISGITLINK(ce->ce_mode))
447                         continue;
449                 name = ce->name;
450                 name_for_path = unsorted_string_list_lookup(&config_name_for_path, ce->name);
451                 if (name_for_path)
452                         name = name_for_path->util;
454                 default_argv = "yes";
455                 if (command_line_option == RECURSE_SUBMODULES_DEFAULT) {
456                         struct string_list_item *fetch_recurse_submodules_option;
457                         fetch_recurse_submodules_option = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name);
458                         if (fetch_recurse_submodules_option) {
459                                 if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_OFF)
460                                         continue;
461                                 if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_ON_DEMAND) {
462                                         if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
463                                                 continue;
464                                         default_argv = "on-demand";
465                                 }
466                         } else {
467                                 if ((config_fetch_recurse_submodules == RECURSE_SUBMODULES_OFF) ||
468                                     gitmodules_is_unmerged)
469                                         continue;
470                                 if (config_fetch_recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) {
471                                         if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
472                                                 continue;
473                                         default_argv = "on-demand";
474                                 }
475                         }
476                 } else if (command_line_option == RECURSE_SUBMODULES_ON_DEMAND) {
477                         if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
478                                 continue;
479                         default_argv = "on-demand";
480                 }
482                 strbuf_addf(&submodule_path, "%s/%s", work_tree, ce->name);
483                 strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf);
484                 strbuf_addf(&submodule_prefix, "%s%s/", prefix, ce->name);
485                 git_dir = read_gitfile_gently(submodule_git_dir.buf);
486                 if (!git_dir)
487                         git_dir = submodule_git_dir.buf;
488                 if (is_directory(git_dir)) {
489                         if (!quiet)
490                                 printf("Fetching submodule %s%s\n", prefix, ce->name);
491                         cp.dir = submodule_path.buf;
492                         argv[default_argc] = default_argv;
493                         argv[argc] = submodule_prefix.buf;
494                         if (run_command(&cp))
495                                 result = 1;
496                 }
497                 strbuf_release(&submodule_path);
498                 strbuf_release(&submodule_git_dir);
499                 strbuf_release(&submodule_prefix);
500         }
501         free(argv);
502 out:
503         string_list_clear(&changed_submodule_paths, 1);
504         return result;
507 unsigned is_submodule_modified(const char *path, int ignore_untracked)
509         ssize_t len;
510         struct child_process cp;
511         const char *argv[] = {
512                 "status",
513                 "--porcelain",
514                 NULL,
515                 NULL,
516         };
517         struct strbuf buf = STRBUF_INIT;
518         unsigned dirty_submodule = 0;
519         const char *line, *next_line;
520         const char *git_dir;
522         strbuf_addf(&buf, "%s/.git", path);
523         git_dir = read_gitfile_gently(buf.buf);
524         if (!git_dir)
525                 git_dir = buf.buf;
526         if (!is_directory(git_dir)) {
527                 strbuf_release(&buf);
528                 /* The submodule is not checked out, so it is not modified */
529                 return 0;
531         }
532         strbuf_reset(&buf);
534         if (ignore_untracked)
535                 argv[2] = "-uno";
537         memset(&cp, 0, sizeof(cp));
538         cp.argv = argv;
539         cp.env = local_repo_env;
540         cp.git_cmd = 1;
541         cp.no_stdin = 1;
542         cp.out = -1;
543         cp.dir = path;
544         if (start_command(&cp))
545                 die("Could not run git status --porcelain");
547         len = strbuf_read(&buf, cp.out, 1024);
548         line = buf.buf;
549         while (len > 2) {
550                 if ((line[0] == '?') && (line[1] == '?')) {
551                         dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
552                         if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
553                                 break;
554                 } else {
555                         dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
556                         if (ignore_untracked ||
557                             (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED))
558                                 break;
559                 }
560                 next_line = strchr(line, '\n');
561                 if (!next_line)
562                         break;
563                 next_line++;
564                 len -= (next_line - line);
565                 line = next_line;
566         }
567         close(cp.out);
569         if (finish_command(&cp))
570                 die("git status --porcelain failed");
572         strbuf_release(&buf);
573         return dirty_submodule;
576 static int find_first_merges(struct object_array *result, const char *path,
577                 struct commit *a, struct commit *b)
579         int i, j;
580         struct object_array merges;
581         struct commit *commit;
582         int contains_another;
584         char merged_revision[42];
585         const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path",
586                                    "--all", merged_revision, NULL };
587         struct rev_info revs;
588         struct setup_revision_opt rev_opts;
590         memset(&merges, 0, sizeof(merges));
591         memset(result, 0, sizeof(struct object_array));
592         memset(&rev_opts, 0, sizeof(rev_opts));
594         /* get all revisions that merge commit a */
595         snprintf(merged_revision, sizeof(merged_revision), "^%s",
596                         sha1_to_hex(a->object.sha1));
597         init_revisions(&revs, NULL);
598         rev_opts.submodule = path;
599         setup_revisions(sizeof(rev_args)/sizeof(char *)-1, rev_args, &revs, &rev_opts);
601         /* save all revisions from the above list that contain b */
602         if (prepare_revision_walk(&revs))
603                 die("revision walk setup failed");
604         while ((commit = get_revision(&revs)) != NULL) {
605                 struct object *o = &(commit->object);
606                 if (in_merge_bases(b, &commit, 1))
607                         add_object_array(o, NULL, &merges);
608         }
610         /* Now we've got all merges that contain a and b. Prune all
611          * merges that contain another found merge and save them in
612          * result.
613          */
614         for (i = 0; i < merges.nr; i++) {
615                 struct commit *m1 = (struct commit *) merges.objects[i].item;
617                 contains_another = 0;
618                 for (j = 0; j < merges.nr; j++) {
619                         struct commit *m2 = (struct commit *) merges.objects[j].item;
620                         if (i != j && in_merge_bases(m2, &m1, 1)) {
621                                 contains_another = 1;
622                                 break;
623                         }
624                 }
626                 if (!contains_another)
627                         add_object_array(merges.objects[i].item,
628                                          merges.objects[i].name, result);
629         }
631         free(merges.objects);
632         return result->nr;
635 static void print_commit(struct commit *commit)
637         struct strbuf sb = STRBUF_INIT;
638         struct pretty_print_context ctx = {0};
639         ctx.date_mode = DATE_NORMAL;
640         format_commit_message(commit, " %h: %m %s", &sb, &ctx);
641         fprintf(stderr, "%s\n", sb.buf);
642         strbuf_release(&sb);
645 #define MERGE_WARNING(path, msg) \
646         warning("Failed to merge submodule %s (%s)", path, msg);
648 int merge_submodule(unsigned char result[20], const char *path,
649                     const unsigned char base[20], const unsigned char a[20],
650                     const unsigned char b[20])
652         struct commit *commit_base, *commit_a, *commit_b;
653         int parent_count;
654         struct object_array merges;
656         int i;
658         /* store a in result in case we fail */
659         hashcpy(result, a);
661         /* we can not handle deletion conflicts */
662         if (is_null_sha1(base))
663                 return 0;
664         if (is_null_sha1(a))
665                 return 0;
666         if (is_null_sha1(b))
667                 return 0;
669         if (add_submodule_odb(path)) {
670                 MERGE_WARNING(path, "not checked out");
671                 return 0;
672         }
674         if (!(commit_base = lookup_commit_reference(base)) ||
675             !(commit_a = lookup_commit_reference(a)) ||
676             !(commit_b = lookup_commit_reference(b))) {
677                 MERGE_WARNING(path, "commits not present");
678                 return 0;
679         }
681         /* check whether both changes are forward */
682         if (!in_merge_bases(commit_base, &commit_a, 1) ||
683             !in_merge_bases(commit_base, &commit_b, 1)) {
684                 MERGE_WARNING(path, "commits don't follow merge-base");
685                 return 0;
686         }
688         /* Case #1: a is contained in b or vice versa */
689         if (in_merge_bases(commit_a, &commit_b, 1)) {
690                 hashcpy(result, b);
691                 return 1;
692         }
693         if (in_merge_bases(commit_b, &commit_a, 1)) {
694                 hashcpy(result, a);
695                 return 1;
696         }
698         /*
699          * Case #2: There are one or more merges that contain a and b in
700          * the submodule. If there is only one, then present it as a
701          * suggestion to the user, but leave it marked unmerged so the
702          * user needs to confirm the resolution.
703          */
705         /* find commit which merges them */
706         parent_count = find_first_merges(&merges, path, commit_a, commit_b);
707         switch (parent_count) {
708         case 0:
709                 MERGE_WARNING(path, "merge following commits not found");
710                 break;
712         case 1:
713                 MERGE_WARNING(path, "not fast-forward");
714                 fprintf(stderr, "Found a possible merge resolution "
715                                 "for the submodule:\n");
716                 print_commit((struct commit *) merges.objects[0].item);
717                 fprintf(stderr,
718                         "If this is correct simply add it to the index "
719                         "for example\n"
720                         "by using:\n\n"
721                         "  git update-index --cacheinfo 160000 %s \"%s\"\n\n"
722                         "which will accept this suggestion.\n",
723                         sha1_to_hex(merges.objects[0].item->sha1), path);
724                 break;
726         default:
727                 MERGE_WARNING(path, "multiple merges found");
728                 for (i = 0; i < merges.nr; i++)
729                         print_commit((struct commit *) merges.objects[i].item);
730         }
732         free(merges.objects);
733         return 0;