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