Code

fetch: avoid quadratic loop checking for updated submodules
[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"
11 #include "sha1-array.h"
13 static struct string_list config_name_for_path;
14 static struct string_list config_fetch_recurse_submodules_for_name;
15 static struct string_list config_ignore_for_name;
16 static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND;
17 static struct string_list changed_submodule_paths;
18 static int initialized_fetch_ref_tips;
19 static struct sha1_array ref_tips_before_fetch;
20 static struct sha1_array ref_tips_after_fetch;
22 /*
23  * The following flag is set if the .gitmodules file is unmerged. We then
24  * disable recursion for all submodules where .git/config doesn't have a
25  * matching config entry because we can't guess what might be configured in
26  * .gitmodules unless the user resolves the conflict. When a command line
27  * option is given (which always overrides configuration) this flag will be
28  * ignored.
29  */
30 static int gitmodules_is_unmerged;
32 static int add_submodule_odb(const char *path)
33 {
34         struct strbuf objects_directory = STRBUF_INIT;
35         struct alternate_object_database *alt_odb;
36         int ret = 0;
37         const char *git_dir;
39         strbuf_addf(&objects_directory, "%s/.git", path);
40         git_dir = read_gitfile_gently(objects_directory.buf);
41         if (git_dir) {
42                 strbuf_reset(&objects_directory);
43                 strbuf_addstr(&objects_directory, git_dir);
44         }
45         strbuf_addstr(&objects_directory, "/objects/");
46         if (!is_directory(objects_directory.buf)) {
47                 ret = -1;
48                 goto done;
49         }
50         /* avoid adding it twice */
51         for (alt_odb = alt_odb_list; alt_odb; alt_odb = alt_odb->next)
52                 if (alt_odb->name - alt_odb->base == objects_directory.len &&
53                                 !strncmp(alt_odb->base, objects_directory.buf,
54                                         objects_directory.len))
55                         goto done;
57         alt_odb = xmalloc(objects_directory.len + 42 + sizeof(*alt_odb));
58         alt_odb->next = alt_odb_list;
59         strcpy(alt_odb->base, objects_directory.buf);
60         alt_odb->name = alt_odb->base + objects_directory.len;
61         alt_odb->name[2] = '/';
62         alt_odb->name[40] = '\0';
63         alt_odb->name[41] = '\0';
64         alt_odb_list = alt_odb;
65         prepare_alt_odb();
66 done:
67         strbuf_release(&objects_directory);
68         return ret;
69 }
71 void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
72                                              const char *path)
73 {
74         struct string_list_item *path_option, *ignore_option;
75         path_option = unsorted_string_list_lookup(&config_name_for_path, path);
76         if (path_option) {
77                 ignore_option = unsorted_string_list_lookup(&config_ignore_for_name, path_option->util);
78                 if (ignore_option)
79                         handle_ignore_submodules_arg(diffopt, ignore_option->util);
80                 else if (gitmodules_is_unmerged)
81                         DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
82         }
83 }
85 int submodule_config(const char *var, const char *value, void *cb)
86 {
87         if (!prefixcmp(var, "submodule."))
88                 return parse_submodule_config_option(var, value);
89         else if (!strcmp(var, "fetch.recursesubmodules")) {
90                 config_fetch_recurse_submodules = parse_fetch_recurse_submodules_arg(var, value);
91                 return 0;
92         }
93         return 0;
94 }
96 void gitmodules_config(void)
97 {
98         const char *work_tree = get_git_work_tree();
99         if (work_tree) {
100                 struct strbuf gitmodules_path = STRBUF_INIT;
101                 int pos;
102                 strbuf_addstr(&gitmodules_path, work_tree);
103                 strbuf_addstr(&gitmodules_path, "/.gitmodules");
104                 if (read_cache() < 0)
105                         die("index file corrupt");
106                 pos = cache_name_pos(".gitmodules", 11);
107                 if (pos < 0) { /* .gitmodules not found or isn't merged */
108                         pos = -1 - pos;
109                         if (active_nr > pos) {  /* there is a .gitmodules */
110                                 const struct cache_entry *ce = active_cache[pos];
111                                 if (ce_namelen(ce) == 11 &&
112                                     !memcmp(ce->name, ".gitmodules", 11))
113                                         gitmodules_is_unmerged = 1;
114                         }
115                 }
117                 if (!gitmodules_is_unmerged)
118                         git_config_from_file(submodule_config, gitmodules_path.buf, NULL);
119                 strbuf_release(&gitmodules_path);
120         }
123 int parse_submodule_config_option(const char *var, const char *value)
125         int len;
126         struct string_list_item *config;
127         struct strbuf submodname = STRBUF_INIT;
129         var += 10;              /* Skip "submodule." */
131         len = strlen(var);
132         if ((len > 5) && !strcmp(var + len - 5, ".path")) {
133                 strbuf_add(&submodname, var, len - 5);
134                 config = unsorted_string_list_lookup(&config_name_for_path, value);
135                 if (config)
136                         free(config->util);
137                 else
138                         config = string_list_append(&config_name_for_path, xstrdup(value));
139                 config->util = strbuf_detach(&submodname, NULL);
140                 strbuf_release(&submodname);
141         } else if ((len > 23) && !strcmp(var + len - 23, ".fetchrecursesubmodules")) {
142                 strbuf_add(&submodname, var, len - 23);
143                 config = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, submodname.buf);
144                 if (!config)
145                         config = string_list_append(&config_fetch_recurse_submodules_for_name,
146                                                     strbuf_detach(&submodname, NULL));
147                 config->util = (void *)(intptr_t)parse_fetch_recurse_submodules_arg(var, value);
148                 strbuf_release(&submodname);
149         } else if ((len > 7) && !strcmp(var + len - 7, ".ignore")) {
150                 if (strcmp(value, "untracked") && strcmp(value, "dirty") &&
151                     strcmp(value, "all") && strcmp(value, "none")) {
152                         warning("Invalid parameter \"%s\" for config option \"submodule.%s.ignore\"", value, var);
153                         return 0;
154                 }
156                 strbuf_add(&submodname, var, len - 7);
157                 config = unsorted_string_list_lookup(&config_ignore_for_name, submodname.buf);
158                 if (config)
159                         free(config->util);
160                 else
161                         config = string_list_append(&config_ignore_for_name,
162                                                     strbuf_detach(&submodname, NULL));
163                 strbuf_release(&submodname);
164                 config->util = xstrdup(value);
165                 return 0;
166         }
167         return 0;
170 void handle_ignore_submodules_arg(struct diff_options *diffopt,
171                                   const char *arg)
173         DIFF_OPT_CLR(diffopt, IGNORE_SUBMODULES);
174         DIFF_OPT_CLR(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
175         DIFF_OPT_CLR(diffopt, IGNORE_DIRTY_SUBMODULES);
177         if (!strcmp(arg, "all"))
178                 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
179         else if (!strcmp(arg, "untracked"))
180                 DIFF_OPT_SET(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
181         else if (!strcmp(arg, "dirty"))
182                 DIFF_OPT_SET(diffopt, IGNORE_DIRTY_SUBMODULES);
183         else if (strcmp(arg, "none"))
184                 die("bad --ignore-submodules argument: %s", arg);
187 static int prepare_submodule_summary(struct rev_info *rev, const char *path,
188                 struct commit *left, struct commit *right,
189                 int *fast_forward, int *fast_backward)
191         struct commit_list *merge_bases, *list;
193         init_revisions(rev, NULL);
194         setup_revisions(0, NULL, rev, NULL);
195         rev->left_right = 1;
196         rev->first_parent_only = 1;
197         left->object.flags |= SYMMETRIC_LEFT;
198         add_pending_object(rev, &left->object, path);
199         add_pending_object(rev, &right->object, path);
200         merge_bases = get_merge_bases(left, right, 1);
201         if (merge_bases) {
202                 if (merge_bases->item == left)
203                         *fast_forward = 1;
204                 else if (merge_bases->item == right)
205                         *fast_backward = 1;
206         }
207         for (list = merge_bases; list; list = list->next) {
208                 list->item->object.flags |= UNINTERESTING;
209                 add_pending_object(rev, &list->item->object,
210                         sha1_to_hex(list->item->object.sha1));
211         }
212         return prepare_revision_walk(rev);
215 static void print_submodule_summary(struct rev_info *rev, FILE *f,
216                 const char *del, const char *add, const char *reset)
218         static const char format[] = "  %m %s";
219         struct strbuf sb = STRBUF_INIT;
220         struct commit *commit;
222         while ((commit = get_revision(rev))) {
223                 struct pretty_print_context ctx = {0};
224                 ctx.date_mode = rev->date_mode;
225                 strbuf_setlen(&sb, 0);
226                 if (commit->object.flags & SYMMETRIC_LEFT) {
227                         if (del)
228                                 strbuf_addstr(&sb, del);
229                 }
230                 else if (add)
231                         strbuf_addstr(&sb, add);
232                 format_commit_message(commit, format, &sb, &ctx);
233                 if (reset)
234                         strbuf_addstr(&sb, reset);
235                 strbuf_addch(&sb, '\n');
236                 fprintf(f, "%s", sb.buf);
237         }
238         strbuf_release(&sb);
241 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
243         switch (git_config_maybe_bool(opt, arg)) {
244         case 1:
245                 return RECURSE_SUBMODULES_ON;
246         case 0:
247                 return RECURSE_SUBMODULES_OFF;
248         default:
249                 if (!strcmp(arg, "on-demand"))
250                         return RECURSE_SUBMODULES_ON_DEMAND;
251                 die("bad %s argument: %s", opt, arg);
252         }
255 void show_submodule_summary(FILE *f, const char *path,
256                 unsigned char one[20], unsigned char two[20],
257                 unsigned dirty_submodule,
258                 const char *del, const char *add, const char *reset)
260         struct rev_info rev;
261         struct commit *left = left, *right = right;
262         const char *message = NULL;
263         struct strbuf sb = STRBUF_INIT;
264         int fast_forward = 0, fast_backward = 0;
266         if (is_null_sha1(two))
267                 message = "(submodule deleted)";
268         else if (add_submodule_odb(path))
269                 message = "(not checked out)";
270         else if (is_null_sha1(one))
271                 message = "(new submodule)";
272         else if (!(left = lookup_commit_reference(one)) ||
273                  !(right = lookup_commit_reference(two)))
274                 message = "(commits not present)";
276         if (!message &&
277             prepare_submodule_summary(&rev, path, left, right,
278                                         &fast_forward, &fast_backward))
279                 message = "(revision walker failed)";
281         if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
282                 fprintf(f, "Submodule %s contains untracked content\n", path);
283         if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
284                 fprintf(f, "Submodule %s contains modified content\n", path);
286         if (!hashcmp(one, two)) {
287                 strbuf_release(&sb);
288                 return;
289         }
291         strbuf_addf(&sb, "Submodule %s %s..", path,
292                         find_unique_abbrev(one, DEFAULT_ABBREV));
293         if (!fast_backward && !fast_forward)
294                 strbuf_addch(&sb, '.');
295         strbuf_addf(&sb, "%s", find_unique_abbrev(two, DEFAULT_ABBREV));
296         if (message)
297                 strbuf_addf(&sb, " %s\n", message);
298         else
299                 strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : "");
300         fwrite(sb.buf, sb.len, 1, f);
302         if (!message) {
303                 print_submodule_summary(&rev, f, del, add, reset);
304                 clear_commit_marks(left, ~0);
305                 clear_commit_marks(right, ~0);
306         }
308         strbuf_release(&sb);
311 void set_config_fetch_recurse_submodules(int value)
313         config_fetch_recurse_submodules = value;
316 static int is_submodule_commit_present(const char *path, unsigned char sha1[20])
318         int is_present = 0;
319         if (!add_submodule_odb(path) && lookup_commit_reference(sha1)) {
320                 /* Even if the submodule is checked out and the commit is
321                  * present, make sure it is reachable from a ref. */
322                 struct child_process cp;
323                 const char *argv[] = {"rev-list", "-n", "1", NULL, "--not", "--all", NULL};
324                 struct strbuf buf = STRBUF_INIT;
326                 argv[3] = sha1_to_hex(sha1);
327                 memset(&cp, 0, sizeof(cp));
328                 cp.argv = argv;
329                 cp.env = local_repo_env;
330                 cp.git_cmd = 1;
331                 cp.no_stdin = 1;
332                 cp.out = -1;
333                 cp.dir = path;
334                 if (!run_command(&cp) && !strbuf_read(&buf, cp.out, 1024))
335                         is_present = 1;
337                 close(cp.out);
338                 strbuf_release(&buf);
339         }
340         return is_present;
343 static void submodule_collect_changed_cb(struct diff_queue_struct *q,
344                                          struct diff_options *options,
345                                          void *data)
347         int i;
348         for (i = 0; i < q->nr; i++) {
349                 struct diff_filepair *p = q->queue[i];
350                 if (!S_ISGITLINK(p->two->mode))
351                         continue;
353                 if (S_ISGITLINK(p->one->mode)) {
354                         /* NEEDSWORK: We should honor the name configured in
355                          * the .gitmodules file of the commit we are examining
356                          * here to be able to correctly follow submodules
357                          * being moved around. */
358                         struct string_list_item *path;
359                         path = unsorted_string_list_lookup(&changed_submodule_paths, p->two->path);
360                         if (!path && !is_submodule_commit_present(p->two->path, p->two->sha1))
361                                 string_list_append(&changed_submodule_paths, xstrdup(p->two->path));
362                 } else {
363                         /* Submodule is new or was moved here */
364                         /* NEEDSWORK: When the .git directories of submodules
365                          * live inside the superprojects .git directory some
366                          * day we should fetch new submodules directly into
367                          * that location too when config or options request
368                          * that so they can be checked out from there. */
369                         continue;
370                 }
371         }
374 static int add_sha1_to_array(const char *ref, const unsigned char *sha1,
375                              int flags, void *data)
377         sha1_array_append(data, sha1);
378         return 0;
381 void check_for_new_submodule_commits(unsigned char new_sha1[20])
383         if (!initialized_fetch_ref_tips) {
384                 for_each_ref(add_sha1_to_array, &ref_tips_before_fetch);
385                 initialized_fetch_ref_tips = 1;
386         }
388         sha1_array_append(&ref_tips_after_fetch, new_sha1);
391 struct argv_array {
392         const char **argv;
393         unsigned int argc;
394         unsigned int alloc;
395 };
397 static void init_argv(struct argv_array *array)
399         array->argv = NULL;
400         array->argc = 0;
401         array->alloc = 0;
404 static void push_argv(struct argv_array *array, const char *value)
406         ALLOC_GROW(array->argv, array->argc + 2, array->alloc);
407         array->argv[array->argc++] = xstrdup(value);
408         array->argv[array->argc] = NULL;
411 static void clear_argv(struct argv_array *array)
413         int i;
414         for (i = 0; i < array->argc; i++)
415                 free((char **)array->argv[i]);
416         free(array->argv);
417         init_argv(array);
420 static void add_sha1_to_argv(const unsigned char sha1[20], void *data)
422         push_argv(data, sha1_to_hex(sha1));
425 static void calculate_changed_submodule_paths(void)
427         struct rev_info rev;
428         struct commit *commit;
429         struct argv_array argv;
431         init_revisions(&rev, NULL);
432         init_argv(&argv);
433         push_argv(&argv, "--"); /* argv[0] program name */
434         sha1_array_for_each_unique(&ref_tips_after_fetch,
435                                    add_sha1_to_argv, &argv);
436         push_argv(&argv, "--not");
437         sha1_array_for_each_unique(&ref_tips_before_fetch,
438                                    add_sha1_to_argv, &argv);
439         setup_revisions(argv.argc, argv.argv, &rev, NULL);
440         if (prepare_revision_walk(&rev))
441                 die("revision walk setup failed");
443         /*
444          * Collect all submodules (whether checked out or not) for which new
445          * commits have been recorded upstream in "changed_submodule_paths".
446          */
447         while ((commit = get_revision(&rev))) {
448                 struct commit_list *parent = commit->parents;
449                 while (parent) {
450                         struct diff_options diff_opts;
451                         diff_setup(&diff_opts);
452                         diff_opts.output_format |= DIFF_FORMAT_CALLBACK;
453                         diff_opts.format_callback = submodule_collect_changed_cb;
454                         if (diff_setup_done(&diff_opts) < 0)
455                                 die("diff_setup_done failed");
456                         diff_tree_sha1(parent->item->object.sha1, commit->object.sha1, "", &diff_opts);
457                         diffcore_std(&diff_opts);
458                         diff_flush(&diff_opts);
459                         parent = parent->next;
460                 }
461         }
463         clear_argv(&argv);
464         sha1_array_clear(&ref_tips_before_fetch);
465         sha1_array_clear(&ref_tips_after_fetch);
466         initialized_fetch_ref_tips = 0;
469 int fetch_populated_submodules(int num_options, const char **options,
470                                const char *prefix, int command_line_option,
471                                int quiet)
473         int i, result = 0, argc = 0, default_argc;
474         struct child_process cp;
475         const char **argv;
476         struct string_list_item *name_for_path;
477         const char *work_tree = get_git_work_tree();
478         if (!work_tree)
479                 goto out;
481         if (!the_index.initialized)
482                 if (read_cache() < 0)
483                         die("index file corrupt");
485         /* 6: "fetch" (options) --recurse-submodules-default default "--submodule-prefix" prefix NULL */
486         argv = xcalloc(num_options + 6, sizeof(const char *));
487         argv[argc++] = "fetch";
488         for (i = 0; i < num_options; i++)
489                 argv[argc++] = options[i];
490         argv[argc++] = "--recurse-submodules-default";
491         default_argc = argc++;
492         argv[argc++] = "--submodule-prefix";
494         memset(&cp, 0, sizeof(cp));
495         cp.argv = argv;
496         cp.env = local_repo_env;
497         cp.git_cmd = 1;
498         cp.no_stdin = 1;
500         calculate_changed_submodule_paths();
502         for (i = 0; i < active_nr; i++) {
503                 struct strbuf submodule_path = STRBUF_INIT;
504                 struct strbuf submodule_git_dir = STRBUF_INIT;
505                 struct strbuf submodule_prefix = STRBUF_INIT;
506                 struct cache_entry *ce = active_cache[i];
507                 const char *git_dir, *name, *default_argv;
509                 if (!S_ISGITLINK(ce->ce_mode))
510                         continue;
512                 name = ce->name;
513                 name_for_path = unsorted_string_list_lookup(&config_name_for_path, ce->name);
514                 if (name_for_path)
515                         name = name_for_path->util;
517                 default_argv = "yes";
518                 if (command_line_option == RECURSE_SUBMODULES_DEFAULT) {
519                         struct string_list_item *fetch_recurse_submodules_option;
520                         fetch_recurse_submodules_option = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name);
521                         if (fetch_recurse_submodules_option) {
522                                 if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_OFF)
523                                         continue;
524                                 if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_ON_DEMAND) {
525                                         if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
526                                                 continue;
527                                         default_argv = "on-demand";
528                                 }
529                         } else {
530                                 if ((config_fetch_recurse_submodules == RECURSE_SUBMODULES_OFF) ||
531                                     gitmodules_is_unmerged)
532                                         continue;
533                                 if (config_fetch_recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) {
534                                         if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
535                                                 continue;
536                                         default_argv = "on-demand";
537                                 }
538                         }
539                 } else if (command_line_option == RECURSE_SUBMODULES_ON_DEMAND) {
540                         if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
541                                 continue;
542                         default_argv = "on-demand";
543                 }
545                 strbuf_addf(&submodule_path, "%s/%s", work_tree, ce->name);
546                 strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf);
547                 strbuf_addf(&submodule_prefix, "%s%s/", prefix, ce->name);
548                 git_dir = read_gitfile_gently(submodule_git_dir.buf);
549                 if (!git_dir)
550                         git_dir = submodule_git_dir.buf;
551                 if (is_directory(git_dir)) {
552                         if (!quiet)
553                                 printf("Fetching submodule %s%s\n", prefix, ce->name);
554                         cp.dir = submodule_path.buf;
555                         argv[default_argc] = default_argv;
556                         argv[argc] = submodule_prefix.buf;
557                         if (run_command(&cp))
558                                 result = 1;
559                 }
560                 strbuf_release(&submodule_path);
561                 strbuf_release(&submodule_git_dir);
562                 strbuf_release(&submodule_prefix);
563         }
564         free(argv);
565 out:
566         string_list_clear(&changed_submodule_paths, 1);
567         return result;
570 unsigned is_submodule_modified(const char *path, int ignore_untracked)
572         ssize_t len;
573         struct child_process cp;
574         const char *argv[] = {
575                 "status",
576                 "--porcelain",
577                 NULL,
578                 NULL,
579         };
580         struct strbuf buf = STRBUF_INIT;
581         unsigned dirty_submodule = 0;
582         const char *line, *next_line;
583         const char *git_dir;
585         strbuf_addf(&buf, "%s/.git", path);
586         git_dir = read_gitfile_gently(buf.buf);
587         if (!git_dir)
588                 git_dir = buf.buf;
589         if (!is_directory(git_dir)) {
590                 strbuf_release(&buf);
591                 /* The submodule is not checked out, so it is not modified */
592                 return 0;
594         }
595         strbuf_reset(&buf);
597         if (ignore_untracked)
598                 argv[2] = "-uno";
600         memset(&cp, 0, sizeof(cp));
601         cp.argv = argv;
602         cp.env = local_repo_env;
603         cp.git_cmd = 1;
604         cp.no_stdin = 1;
605         cp.out = -1;
606         cp.dir = path;
607         if (start_command(&cp))
608                 die("Could not run git status --porcelain");
610         len = strbuf_read(&buf, cp.out, 1024);
611         line = buf.buf;
612         while (len > 2) {
613                 if ((line[0] == '?') && (line[1] == '?')) {
614                         dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
615                         if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
616                                 break;
617                 } else {
618                         dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
619                         if (ignore_untracked ||
620                             (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED))
621                                 break;
622                 }
623                 next_line = strchr(line, '\n');
624                 if (!next_line)
625                         break;
626                 next_line++;
627                 len -= (next_line - line);
628                 line = next_line;
629         }
630         close(cp.out);
632         if (finish_command(&cp))
633                 die("git status --porcelain failed");
635         strbuf_release(&buf);
636         return dirty_submodule;
639 static int find_first_merges(struct object_array *result, const char *path,
640                 struct commit *a, struct commit *b)
642         int i, j;
643         struct object_array merges;
644         struct commit *commit;
645         int contains_another;
647         char merged_revision[42];
648         const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path",
649                                    "--all", merged_revision, NULL };
650         struct rev_info revs;
651         struct setup_revision_opt rev_opts;
653         memset(&merges, 0, sizeof(merges));
654         memset(result, 0, sizeof(struct object_array));
655         memset(&rev_opts, 0, sizeof(rev_opts));
657         /* get all revisions that merge commit a */
658         snprintf(merged_revision, sizeof(merged_revision), "^%s",
659                         sha1_to_hex(a->object.sha1));
660         init_revisions(&revs, NULL);
661         rev_opts.submodule = path;
662         setup_revisions(sizeof(rev_args)/sizeof(char *)-1, rev_args, &revs, &rev_opts);
664         /* save all revisions from the above list that contain b */
665         if (prepare_revision_walk(&revs))
666                 die("revision walk setup failed");
667         while ((commit = get_revision(&revs)) != NULL) {
668                 struct object *o = &(commit->object);
669                 if (in_merge_bases(b, &commit, 1))
670                         add_object_array(o, NULL, &merges);
671         }
673         /* Now we've got all merges that contain a and b. Prune all
674          * merges that contain another found merge and save them in
675          * result.
676          */
677         for (i = 0; i < merges.nr; i++) {
678                 struct commit *m1 = (struct commit *) merges.objects[i].item;
680                 contains_another = 0;
681                 for (j = 0; j < merges.nr; j++) {
682                         struct commit *m2 = (struct commit *) merges.objects[j].item;
683                         if (i != j && in_merge_bases(m2, &m1, 1)) {
684                                 contains_another = 1;
685                                 break;
686                         }
687                 }
689                 if (!contains_another)
690                         add_object_array(merges.objects[i].item,
691                                          merges.objects[i].name, result);
692         }
694         free(merges.objects);
695         return result->nr;
698 static void print_commit(struct commit *commit)
700         struct strbuf sb = STRBUF_INIT;
701         struct pretty_print_context ctx = {0};
702         ctx.date_mode = DATE_NORMAL;
703         format_commit_message(commit, " %h: %m %s", &sb, &ctx);
704         fprintf(stderr, "%s\n", sb.buf);
705         strbuf_release(&sb);
708 #define MERGE_WARNING(path, msg) \
709         warning("Failed to merge submodule %s (%s)", path, msg);
711 int merge_submodule(unsigned char result[20], const char *path,
712                     const unsigned char base[20], const unsigned char a[20],
713                     const unsigned char b[20])
715         struct commit *commit_base, *commit_a, *commit_b;
716         int parent_count;
717         struct object_array merges;
719         int i;
721         /* store a in result in case we fail */
722         hashcpy(result, a);
724         /* we can not handle deletion conflicts */
725         if (is_null_sha1(base))
726                 return 0;
727         if (is_null_sha1(a))
728                 return 0;
729         if (is_null_sha1(b))
730                 return 0;
732         if (add_submodule_odb(path)) {
733                 MERGE_WARNING(path, "not checked out");
734                 return 0;
735         }
737         if (!(commit_base = lookup_commit_reference(base)) ||
738             !(commit_a = lookup_commit_reference(a)) ||
739             !(commit_b = lookup_commit_reference(b))) {
740                 MERGE_WARNING(path, "commits not present");
741                 return 0;
742         }
744         /* check whether both changes are forward */
745         if (!in_merge_bases(commit_base, &commit_a, 1) ||
746             !in_merge_bases(commit_base, &commit_b, 1)) {
747                 MERGE_WARNING(path, "commits don't follow merge-base");
748                 return 0;
749         }
751         /* Case #1: a is contained in b or vice versa */
752         if (in_merge_bases(commit_a, &commit_b, 1)) {
753                 hashcpy(result, b);
754                 return 1;
755         }
756         if (in_merge_bases(commit_b, &commit_a, 1)) {
757                 hashcpy(result, a);
758                 return 1;
759         }
761         /*
762          * Case #2: There are one or more merges that contain a and b in
763          * the submodule. If there is only one, then present it as a
764          * suggestion to the user, but leave it marked unmerged so the
765          * user needs to confirm the resolution.
766          */
768         /* find commit which merges them */
769         parent_count = find_first_merges(&merges, path, commit_a, commit_b);
770         switch (parent_count) {
771         case 0:
772                 MERGE_WARNING(path, "merge following commits not found");
773                 break;
775         case 1:
776                 MERGE_WARNING(path, "not fast-forward");
777                 fprintf(stderr, "Found a possible merge resolution "
778                                 "for the submodule:\n");
779                 print_commit((struct commit *) merges.objects[0].item);
780                 fprintf(stderr,
781                         "If this is correct simply add it to the index "
782                         "for example\n"
783                         "by using:\n\n"
784                         "  git update-index --cacheinfo 160000 %s \"%s\"\n\n"
785                         "which will accept this suggestion.\n",
786                         sha1_to_hex(merges.objects[0].item->sha1), path);
787                 break;
789         default:
790                 MERGE_WARNING(path, "multiple merges found");
791                 for (i = 0; i < merges.nr; i++)
792                         print_commit((struct commit *) merges.objects[i].item);
793         }
795         free(merges.objects);
796         return 0;