submodule.con commit fetch/pull: Don't recurse into a submodule when commits are already present (c16c3e4)
   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
  12struct string_list config_name_for_path;
  13struct string_list config_fetch_recurse_submodules_for_name;
  14struct string_list config_ignore_for_name;
  15static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND;
  16struct string_list changed_submodule_paths;
  17
  18static int add_submodule_odb(const char *path)
  19{
  20        struct strbuf objects_directory = STRBUF_INIT;
  21        struct alternate_object_database *alt_odb;
  22        int ret = 0;
  23        const char *git_dir;
  24
  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/");
  32        if (!is_directory(objects_directory.buf)) {
  33                ret = -1;
  34                goto done;
  35        }
  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))
  41                        goto done;
  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();
  52done:
  53        strbuf_release(&objects_directory);
  54        return ret;
  55}
  56
  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
  69int submodule_config(const char *var, const char *value, void *cb)
  70{
  71        if (!prefixcmp(var, "submodule."))
  72                return parse_submodule_config_option(var, value);
  73        else if (!strcmp(var, "fetch.recursesubmodules")) {
  74                config_fetch_recurse_submodules = parse_fetch_recurse_submodules_arg(var, value);
  75                return 0;
  76        }
  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
  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);
 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));
 116                config->util = (void *)(intptr_t)parse_fetch_recurse_submodules_arg(var, value);
 117                strbuf_release(&submodname);
 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
 139void handle_ignore_submodules_arg(struct diff_options *diffopt,
 140                                  const char *arg)
 141{
 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
 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);
 152        else if (strcmp(arg, "none"))
 153                die("bad --ignore-submodules argument: %s", arg);
 154}
 155
 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
 170void show_submodule_summary(FILE *f, const char *path,
 171                unsigned char one[20], unsigned char two[20],
 172                unsigned dirty_submodule,
 173                const char *del, const char *add, const char *reset)
 174{
 175        struct rev_info rev;
 176        struct commit *commit, *left = left, *right = right;
 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
 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
 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))) {
 240                        struct pretty_print_context ctx = {0};
 241                        ctx.date_mode = rev.date_mode;
 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);
 249                        format_commit_message(commit, format, &sb, &ctx);
 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}
 260
 261void set_config_fetch_recurse_submodules(int value)
 262{
 263        config_fetch_recurse_submodules = value;
 264}
 265
 266static int is_submodule_commit_present(const char *path, unsigned char sha1[20])
 267{
 268        int is_present = 0;
 269        if (!add_submodule_odb(path) && lookup_commit_reference(sha1)) {
 270                /* Even if the submodule is checked out and the commit is
 271                 * present, make sure it is reachable from a ref. */
 272                struct child_process cp;
 273                const char *argv[] = {"rev-list", "-n", "1", NULL, "--not", "--all", NULL};
 274                struct strbuf buf = STRBUF_INIT;
 275
 276                argv[3] = sha1_to_hex(sha1);
 277                memset(&cp, 0, sizeof(cp));
 278                cp.argv = argv;
 279                cp.env = local_repo_env;
 280                cp.git_cmd = 1;
 281                cp.no_stdin = 1;
 282                cp.out = -1;
 283                cp.dir = path;
 284                if (!run_command(&cp) && !strbuf_read(&buf, cp.out, 1024))
 285                        is_present = 1;
 286
 287                close(cp.out);
 288                strbuf_release(&buf);
 289        }
 290        return is_present;
 291}
 292
 293static void submodule_collect_changed_cb(struct diff_queue_struct *q,
 294                                         struct diff_options *options,
 295                                         void *data)
 296{
 297        int i;
 298        for (i = 0; i < q->nr; i++) {
 299                struct diff_filepair *p = q->queue[i];
 300                if (!S_ISGITLINK(p->two->mode))
 301                        continue;
 302
 303                if (S_ISGITLINK(p->one->mode)) {
 304                        /* NEEDSWORK: We should honor the name configured in
 305                         * the .gitmodules file of the commit we are examining
 306                         * here to be able to correctly follow submodules
 307                         * being moved around. */
 308                        struct string_list_item *path;
 309                        path = unsorted_string_list_lookup(&changed_submodule_paths, p->two->path);
 310                        if (!path && !is_submodule_commit_present(p->two->path, p->two->sha1))
 311                                string_list_append(&changed_submodule_paths, xstrdup(p->two->path));
 312                } else {
 313                        /* Submodule is new or was moved here */
 314                        /* NEEDSWORK: When the .git directories of submodules
 315                         * live inside the superprojects .git directory some
 316                         * day we should fetch new submodules directly into
 317                         * that location too when config or options request
 318                         * that so they can be checked out from there. */
 319                        continue;
 320                }
 321        }
 322}
 323
 324void check_for_new_submodule_commits(unsigned char new_sha1[20])
 325{
 326        struct rev_info rev;
 327        struct commit *commit;
 328        const char *argv[] = {NULL, NULL, "--not", "--all", NULL};
 329        int argc = ARRAY_SIZE(argv) - 1;
 330
 331        init_revisions(&rev, NULL);
 332        argv[1] = xstrdup(sha1_to_hex(new_sha1));
 333        setup_revisions(argc, argv, &rev, NULL);
 334        if (prepare_revision_walk(&rev))
 335                die("revision walk setup failed");
 336
 337        /*
 338         * Collect all submodules (whether checked out or not) for which new
 339         * commits have been recorded upstream in "changed_submodule_paths".
 340         */
 341        while ((commit = get_revision(&rev))) {
 342                struct commit_list *parent = commit->parents;
 343                while (parent) {
 344                        struct diff_options diff_opts;
 345                        diff_setup(&diff_opts);
 346                        diff_opts.output_format |= DIFF_FORMAT_CALLBACK;
 347                        diff_opts.format_callback = submodule_collect_changed_cb;
 348                        if (diff_setup_done(&diff_opts) < 0)
 349                                die("diff_setup_done failed");
 350                        diff_tree_sha1(parent->item->object.sha1, commit->object.sha1, "", &diff_opts);
 351                        diffcore_std(&diff_opts);
 352                        diff_flush(&diff_opts);
 353                        parent = parent->next;
 354                }
 355        }
 356        free((char *)argv[1]);
 357}
 358
 359int fetch_populated_submodules(int num_options, const char **options,
 360                               const char *prefix, int command_line_option,
 361                               int quiet)
 362{
 363        int i, result = 0, argc = 0, default_argc;
 364        struct child_process cp;
 365        const char **argv;
 366        struct string_list_item *name_for_path;
 367        const char *work_tree = get_git_work_tree();
 368        if (!work_tree)
 369                goto out;
 370
 371        if (!the_index.initialized)
 372                if (read_cache() < 0)
 373                        die("index file corrupt");
 374
 375        /* 6: "fetch" (options) --recurse-submodules-default default "--submodule-prefix" prefix NULL */
 376        argv = xcalloc(num_options + 6, sizeof(const char *));
 377        argv[argc++] = "fetch";
 378        for (i = 0; i < num_options; i++)
 379                argv[argc++] = options[i];
 380        argv[argc++] = "--recurse-submodules-default";
 381        default_argc = argc++;
 382        argv[argc++] = "--submodule-prefix";
 383
 384        memset(&cp, 0, sizeof(cp));
 385        cp.argv = argv;
 386        cp.env = local_repo_env;
 387        cp.git_cmd = 1;
 388        cp.no_stdin = 1;
 389
 390        for (i = 0; i < active_nr; i++) {
 391                struct strbuf submodule_path = STRBUF_INIT;
 392                struct strbuf submodule_git_dir = STRBUF_INIT;
 393                struct strbuf submodule_prefix = STRBUF_INIT;
 394                struct cache_entry *ce = active_cache[i];
 395                const char *git_dir, *name, *default_argv;
 396
 397                if (!S_ISGITLINK(ce->ce_mode))
 398                        continue;
 399
 400                name = ce->name;
 401                name_for_path = unsorted_string_list_lookup(&config_name_for_path, ce->name);
 402                if (name_for_path)
 403                        name = name_for_path->util;
 404
 405                default_argv = "yes";
 406                if (command_line_option == RECURSE_SUBMODULES_DEFAULT) {
 407                        struct string_list_item *fetch_recurse_submodules_option;
 408                        fetch_recurse_submodules_option = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name);
 409                        if (fetch_recurse_submodules_option) {
 410                                if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_OFF)
 411                                        continue;
 412                                if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_ON_DEMAND) {
 413                                        if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
 414                                                continue;
 415                                        default_argv = "on-demand";
 416                                }
 417                        } else {
 418                                if (config_fetch_recurse_submodules == RECURSE_SUBMODULES_OFF)
 419                                        continue;
 420                                if (config_fetch_recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) {
 421                                        if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
 422                                                continue;
 423                                        default_argv = "on-demand";
 424                                }
 425                        }
 426                } else if (command_line_option == RECURSE_SUBMODULES_ON_DEMAND) {
 427                        if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
 428                                continue;
 429                        default_argv = "on-demand";
 430                }
 431
 432                strbuf_addf(&submodule_path, "%s/%s", work_tree, ce->name);
 433                strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf);
 434                strbuf_addf(&submodule_prefix, "%s%s/", prefix, ce->name);
 435                git_dir = read_gitfile_gently(submodule_git_dir.buf);
 436                if (!git_dir)
 437                        git_dir = submodule_git_dir.buf;
 438                if (is_directory(git_dir)) {
 439                        if (!quiet)
 440                                printf("Fetching submodule %s%s\n", prefix, ce->name);
 441                        cp.dir = submodule_path.buf;
 442                        argv[default_argc] = default_argv;
 443                        argv[argc] = submodule_prefix.buf;
 444                        if (run_command(&cp))
 445                                result = 1;
 446                }
 447                strbuf_release(&submodule_path);
 448                strbuf_release(&submodule_git_dir);
 449                strbuf_release(&submodule_prefix);
 450        }
 451        free(argv);
 452out:
 453        string_list_clear(&changed_submodule_paths, 1);
 454        return result;
 455}
 456
 457unsigned is_submodule_modified(const char *path, int ignore_untracked)
 458{
 459        ssize_t len;
 460        struct child_process cp;
 461        const char *argv[] = {
 462                "status",
 463                "--porcelain",
 464                NULL,
 465                NULL,
 466        };
 467        struct strbuf buf = STRBUF_INIT;
 468        unsigned dirty_submodule = 0;
 469        const char *line, *next_line;
 470        const char *git_dir;
 471
 472        strbuf_addf(&buf, "%s/.git", path);
 473        git_dir = read_gitfile_gently(buf.buf);
 474        if (!git_dir)
 475                git_dir = buf.buf;
 476        if (!is_directory(git_dir)) {
 477                strbuf_release(&buf);
 478                /* The submodule is not checked out, so it is not modified */
 479                return 0;
 480
 481        }
 482        strbuf_reset(&buf);
 483
 484        if (ignore_untracked)
 485                argv[2] = "-uno";
 486
 487        memset(&cp, 0, sizeof(cp));
 488        cp.argv = argv;
 489        cp.env = local_repo_env;
 490        cp.git_cmd = 1;
 491        cp.no_stdin = 1;
 492        cp.out = -1;
 493        cp.dir = path;
 494        if (start_command(&cp))
 495                die("Could not run git status --porcelain");
 496
 497        len = strbuf_read(&buf, cp.out, 1024);
 498        line = buf.buf;
 499        while (len > 2) {
 500                if ((line[0] == '?') && (line[1] == '?')) {
 501                        dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
 502                        if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
 503                                break;
 504                } else {
 505                        dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
 506                        if (ignore_untracked ||
 507                            (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED))
 508                                break;
 509                }
 510                next_line = strchr(line, '\n');
 511                if (!next_line)
 512                        break;
 513                next_line++;
 514                len -= (next_line - line);
 515                line = next_line;
 516        }
 517        close(cp.out);
 518
 519        if (finish_command(&cp))
 520                die("git status --porcelain failed");
 521
 522        strbuf_release(&buf);
 523        return dirty_submodule;
 524}
 525
 526static int find_first_merges(struct object_array *result, const char *path,
 527                struct commit *a, struct commit *b)
 528{
 529        int i, j;
 530        struct object_array merges;
 531        struct commit *commit;
 532        int contains_another;
 533
 534        char merged_revision[42];
 535        const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path",
 536                                   "--all", merged_revision, NULL };
 537        struct rev_info revs;
 538        struct setup_revision_opt rev_opts;
 539
 540        memset(&merges, 0, sizeof(merges));
 541        memset(result, 0, sizeof(struct object_array));
 542        memset(&rev_opts, 0, sizeof(rev_opts));
 543
 544        /* get all revisions that merge commit a */
 545        snprintf(merged_revision, sizeof(merged_revision), "^%s",
 546                        sha1_to_hex(a->object.sha1));
 547        init_revisions(&revs, NULL);
 548        rev_opts.submodule = path;
 549        setup_revisions(sizeof(rev_args)/sizeof(char *)-1, rev_args, &revs, &rev_opts);
 550
 551        /* save all revisions from the above list that contain b */
 552        if (prepare_revision_walk(&revs))
 553                die("revision walk setup failed");
 554        while ((commit = get_revision(&revs)) != NULL) {
 555                struct object *o = &(commit->object);
 556                if (in_merge_bases(b, &commit, 1))
 557                        add_object_array(o, NULL, &merges);
 558        }
 559
 560        /* Now we've got all merges that contain a and b. Prune all
 561         * merges that contain another found merge and save them in
 562         * result.
 563         */
 564        for (i = 0; i < merges.nr; i++) {
 565                struct commit *m1 = (struct commit *) merges.objects[i].item;
 566
 567                contains_another = 0;
 568                for (j = 0; j < merges.nr; j++) {
 569                        struct commit *m2 = (struct commit *) merges.objects[j].item;
 570                        if (i != j && in_merge_bases(m2, &m1, 1)) {
 571                                contains_another = 1;
 572                                break;
 573                        }
 574                }
 575
 576                if (!contains_another)
 577                        add_object_array(merges.objects[i].item,
 578                                         merges.objects[i].name, result);
 579        }
 580
 581        free(merges.objects);
 582        return result->nr;
 583}
 584
 585static void print_commit(struct commit *commit)
 586{
 587        struct strbuf sb = STRBUF_INIT;
 588        struct pretty_print_context ctx = {0};
 589        ctx.date_mode = DATE_NORMAL;
 590        format_commit_message(commit, " %h: %m %s", &sb, &ctx);
 591        fprintf(stderr, "%s\n", sb.buf);
 592        strbuf_release(&sb);
 593}
 594
 595#define MERGE_WARNING(path, msg) \
 596        warning("Failed to merge submodule %s (%s)", path, msg);
 597
 598int merge_submodule(unsigned char result[20], const char *path,
 599                    const unsigned char base[20], const unsigned char a[20],
 600                    const unsigned char b[20])
 601{
 602        struct commit *commit_base, *commit_a, *commit_b;
 603        int parent_count;
 604        struct object_array merges;
 605
 606        int i;
 607
 608        /* store a in result in case we fail */
 609        hashcpy(result, a);
 610
 611        /* we can not handle deletion conflicts */
 612        if (is_null_sha1(base))
 613                return 0;
 614        if (is_null_sha1(a))
 615                return 0;
 616        if (is_null_sha1(b))
 617                return 0;
 618
 619        if (add_submodule_odb(path)) {
 620                MERGE_WARNING(path, "not checked out");
 621                return 0;
 622        }
 623
 624        if (!(commit_base = lookup_commit_reference(base)) ||
 625            !(commit_a = lookup_commit_reference(a)) ||
 626            !(commit_b = lookup_commit_reference(b))) {
 627                MERGE_WARNING(path, "commits not present");
 628                return 0;
 629        }
 630
 631        /* check whether both changes are forward */
 632        if (!in_merge_bases(commit_base, &commit_a, 1) ||
 633            !in_merge_bases(commit_base, &commit_b, 1)) {
 634                MERGE_WARNING(path, "commits don't follow merge-base");
 635                return 0;
 636        }
 637
 638        /* Case #1: a is contained in b or vice versa */
 639        if (in_merge_bases(commit_a, &commit_b, 1)) {
 640                hashcpy(result, b);
 641                return 1;
 642        }
 643        if (in_merge_bases(commit_b, &commit_a, 1)) {
 644                hashcpy(result, a);
 645                return 1;
 646        }
 647
 648        /*
 649         * Case #2: There are one or more merges that contain a and b in
 650         * the submodule. If there is only one, then present it as a
 651         * suggestion to the user, but leave it marked unmerged so the
 652         * user needs to confirm the resolution.
 653         */
 654
 655        /* find commit which merges them */
 656        parent_count = find_first_merges(&merges, path, commit_a, commit_b);
 657        switch (parent_count) {
 658        case 0:
 659                MERGE_WARNING(path, "merge following commits not found");
 660                break;
 661
 662        case 1:
 663                MERGE_WARNING(path, "not fast-forward");
 664                fprintf(stderr, "Found a possible merge resolution "
 665                                "for the submodule:\n");
 666                print_commit((struct commit *) merges.objects[0].item);
 667                fprintf(stderr,
 668                        "If this is correct simply add it to the index "
 669                        "for example\n"
 670                        "by using:\n\n"
 671                        "  git update-index --cacheinfo 160000 %s \"%s\"\n\n"
 672                        "which will accept this suggestion.\n",
 673                        sha1_to_hex(merges.objects[0].item->sha1), path);
 674                break;
 675
 676        default:
 677                MERGE_WARNING(path, "multiple merges found");
 678                for (i = 0; i < merges.nr; i++)
 679                        print_commit((struct commit *) merges.objects[i].item);
 680        }
 681
 682        free(merges.objects);
 683        return 0;
 684}