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