submodule.con commit documentation: trivial style cleanups (0460ed2)
   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"
  12#include "argv-array.h"
  13
  14static struct string_list config_name_for_path;
  15static struct string_list config_fetch_recurse_submodules_for_name;
  16static struct string_list config_ignore_for_name;
  17static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND;
  18static struct string_list changed_submodule_paths;
  19static int initialized_fetch_ref_tips;
  20static struct sha1_array ref_tips_before_fetch;
  21static struct sha1_array ref_tips_after_fetch;
  22
  23/*
  24 * The following flag is set if the .gitmodules file is unmerged. We then
  25 * disable recursion for all submodules where .git/config doesn't have a
  26 * matching config entry because we can't guess what might be configured in
  27 * .gitmodules unless the user resolves the conflict. When a command line
  28 * option is given (which always overrides configuration) this flag will be
  29 * ignored.
  30 */
  31static int gitmodules_is_unmerged;
  32
  33static int add_submodule_odb(const char *path)
  34{
  35        struct strbuf objects_directory = STRBUF_INIT;
  36        struct alternate_object_database *alt_odb;
  37        int ret = 0;
  38        const char *git_dir;
  39
  40        strbuf_addf(&objects_directory, "%s/.git", path);
  41        git_dir = read_gitfile(objects_directory.buf);
  42        if (git_dir) {
  43                strbuf_reset(&objects_directory);
  44                strbuf_addstr(&objects_directory, git_dir);
  45        }
  46        strbuf_addstr(&objects_directory, "/objects/");
  47        if (!is_directory(objects_directory.buf)) {
  48                ret = -1;
  49                goto done;
  50        }
  51        /* avoid adding it twice */
  52        for (alt_odb = alt_odb_list; alt_odb; alt_odb = alt_odb->next)
  53                if (alt_odb->name - alt_odb->base == objects_directory.len &&
  54                                !strncmp(alt_odb->base, objects_directory.buf,
  55                                        objects_directory.len))
  56                        goto done;
  57
  58        alt_odb = xmalloc(objects_directory.len + 42 + sizeof(*alt_odb));
  59        alt_odb->next = alt_odb_list;
  60        strcpy(alt_odb->base, objects_directory.buf);
  61        alt_odb->name = alt_odb->base + objects_directory.len;
  62        alt_odb->name[2] = '/';
  63        alt_odb->name[40] = '\0';
  64        alt_odb->name[41] = '\0';
  65        alt_odb_list = alt_odb;
  66
  67        /* add possible alternates from the submodule */
  68        read_info_alternates(objects_directory.buf, 0);
  69        prepare_alt_odb();
  70done:
  71        strbuf_release(&objects_directory);
  72        return ret;
  73}
  74
  75void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
  76                                             const char *path)
  77{
  78        struct string_list_item *path_option, *ignore_option;
  79        path_option = unsorted_string_list_lookup(&config_name_for_path, path);
  80        if (path_option) {
  81                ignore_option = unsorted_string_list_lookup(&config_ignore_for_name, path_option->util);
  82                if (ignore_option)
  83                        handle_ignore_submodules_arg(diffopt, ignore_option->util);
  84                else if (gitmodules_is_unmerged)
  85                        DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
  86        }
  87}
  88
  89int submodule_config(const char *var, const char *value, void *cb)
  90{
  91        if (!prefixcmp(var, "submodule."))
  92                return parse_submodule_config_option(var, value);
  93        else if (!strcmp(var, "fetch.recursesubmodules")) {
  94                config_fetch_recurse_submodules = parse_fetch_recurse_submodules_arg(var, value);
  95                return 0;
  96        }
  97        return 0;
  98}
  99
 100void gitmodules_config(void)
 101{
 102        const char *work_tree = get_git_work_tree();
 103        if (work_tree) {
 104                struct strbuf gitmodules_path = STRBUF_INIT;
 105                int pos;
 106                strbuf_addstr(&gitmodules_path, work_tree);
 107                strbuf_addstr(&gitmodules_path, "/.gitmodules");
 108                if (read_cache() < 0)
 109                        die("index file corrupt");
 110                pos = cache_name_pos(".gitmodules", 11);
 111                if (pos < 0) { /* .gitmodules not found or isn't merged */
 112                        pos = -1 - pos;
 113                        if (active_nr > pos) {  /* there is a .gitmodules */
 114                                const struct cache_entry *ce = active_cache[pos];
 115                                if (ce_namelen(ce) == 11 &&
 116                                    !memcmp(ce->name, ".gitmodules", 11))
 117                                        gitmodules_is_unmerged = 1;
 118                        }
 119                }
 120
 121                if (!gitmodules_is_unmerged)
 122                        git_config_from_file(submodule_config, gitmodules_path.buf, NULL);
 123                strbuf_release(&gitmodules_path);
 124        }
 125}
 126
 127int parse_submodule_config_option(const char *var, const char *value)
 128{
 129        struct string_list_item *config;
 130        const char *name, *key;
 131        int namelen;
 132
 133        if (parse_config_key(var, "submodule", &name, &namelen, &key) < 0 || !name)
 134                return 0;
 135
 136        if (!strcmp(key, "path")) {
 137                config = unsorted_string_list_lookup(&config_name_for_path, value);
 138                if (config)
 139                        free(config->util);
 140                else
 141                        config = string_list_append(&config_name_for_path, xstrdup(value));
 142                config->util = xmemdupz(name, namelen);
 143        } else if (!strcmp(key, "fetchrecursesubmodules")) {
 144                char *name_cstr = xmemdupz(name, namelen);
 145                config = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name_cstr);
 146                if (!config)
 147                        config = string_list_append(&config_fetch_recurse_submodules_for_name, name_cstr);
 148                else
 149                        free(name_cstr);
 150                config->util = (void *)(intptr_t)parse_fetch_recurse_submodules_arg(var, value);
 151        } else if (!strcmp(key, "ignore")) {
 152                char *name_cstr;
 153
 154                if (strcmp(value, "untracked") && strcmp(value, "dirty") &&
 155                    strcmp(value, "all") && strcmp(value, "none")) {
 156                        warning("Invalid parameter \"%s\" for config option \"submodule.%s.ignore\"", value, var);
 157                        return 0;
 158                }
 159
 160                name_cstr = xmemdupz(name, namelen);
 161                config = unsorted_string_list_lookup(&config_ignore_for_name, name_cstr);
 162                if (config) {
 163                        free(config->util);
 164                        free(name_cstr);
 165                } else
 166                        config = string_list_append(&config_ignore_for_name, name_cstr);
 167                config->util = xstrdup(value);
 168                return 0;
 169        }
 170        return 0;
 171}
 172
 173void handle_ignore_submodules_arg(struct diff_options *diffopt,
 174                                  const char *arg)
 175{
 176        DIFF_OPT_CLR(diffopt, IGNORE_SUBMODULES);
 177        DIFF_OPT_CLR(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
 178        DIFF_OPT_CLR(diffopt, IGNORE_DIRTY_SUBMODULES);
 179
 180        if (!strcmp(arg, "all"))
 181                DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
 182        else if (!strcmp(arg, "untracked"))
 183                DIFF_OPT_SET(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
 184        else if (!strcmp(arg, "dirty"))
 185                DIFF_OPT_SET(diffopt, IGNORE_DIRTY_SUBMODULES);
 186        else if (strcmp(arg, "none"))
 187                die("bad --ignore-submodules argument: %s", arg);
 188}
 189
 190static int prepare_submodule_summary(struct rev_info *rev, const char *path,
 191                struct commit *left, struct commit *right,
 192                int *fast_forward, int *fast_backward)
 193{
 194        struct commit_list *merge_bases, *list;
 195
 196        init_revisions(rev, NULL);
 197        setup_revisions(0, NULL, rev, NULL);
 198        rev->left_right = 1;
 199        rev->first_parent_only = 1;
 200        left->object.flags |= SYMMETRIC_LEFT;
 201        add_pending_object(rev, &left->object, path);
 202        add_pending_object(rev, &right->object, path);
 203        merge_bases = get_merge_bases(left, right, 1);
 204        if (merge_bases) {
 205                if (merge_bases->item == left)
 206                        *fast_forward = 1;
 207                else if (merge_bases->item == right)
 208                        *fast_backward = 1;
 209        }
 210        for (list = merge_bases; list; list = list->next) {
 211                list->item->object.flags |= UNINTERESTING;
 212                add_pending_object(rev, &list->item->object,
 213                        sha1_to_hex(list->item->object.sha1));
 214        }
 215        return prepare_revision_walk(rev);
 216}
 217
 218static void print_submodule_summary(struct rev_info *rev, FILE *f,
 219                const char *line_prefix,
 220                const char *del, const char *add, const char *reset)
 221{
 222        static const char format[] = "  %m %s";
 223        struct strbuf sb = STRBUF_INIT;
 224        struct commit *commit;
 225
 226        while ((commit = get_revision(rev))) {
 227                struct pretty_print_context ctx = {0};
 228                ctx.date_mode = rev->date_mode;
 229                strbuf_setlen(&sb, 0);
 230                strbuf_addstr(&sb, line_prefix);
 231                if (commit->object.flags & SYMMETRIC_LEFT) {
 232                        if (del)
 233                                strbuf_addstr(&sb, del);
 234                }
 235                else if (add)
 236                        strbuf_addstr(&sb, add);
 237                format_commit_message(commit, format, &sb, &ctx);
 238                if (reset)
 239                        strbuf_addstr(&sb, reset);
 240                strbuf_addch(&sb, '\n');
 241                fprintf(f, "%s", sb.buf);
 242        }
 243        strbuf_release(&sb);
 244}
 245
 246int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
 247{
 248        switch (git_config_maybe_bool(opt, arg)) {
 249        case 1:
 250                return RECURSE_SUBMODULES_ON;
 251        case 0:
 252                return RECURSE_SUBMODULES_OFF;
 253        default:
 254                if (!strcmp(arg, "on-demand"))
 255                        return RECURSE_SUBMODULES_ON_DEMAND;
 256                die("bad %s argument: %s", opt, arg);
 257        }
 258}
 259
 260void show_submodule_summary(FILE *f, const char *path,
 261                const char *line_prefix,
 262                unsigned char one[20], unsigned char two[20],
 263                unsigned dirty_submodule, const char *meta,
 264                const char *del, const char *add, const char *reset)
 265{
 266        struct rev_info rev;
 267        struct commit *left = NULL, *right = NULL;
 268        const char *message = NULL;
 269        struct strbuf sb = STRBUF_INIT;
 270        int fast_forward = 0, fast_backward = 0;
 271
 272        if (is_null_sha1(two))
 273                message = "(submodule deleted)";
 274        else if (add_submodule_odb(path))
 275                message = "(not checked out)";
 276        else if (is_null_sha1(one))
 277                message = "(new submodule)";
 278        else if (!(left = lookup_commit_reference(one)) ||
 279                 !(right = lookup_commit_reference(two)))
 280                message = "(commits not present)";
 281        else if (prepare_submodule_summary(&rev, path, left, right,
 282                                           &fast_forward, &fast_backward))
 283                message = "(revision walker failed)";
 284
 285        if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
 286                fprintf(f, "%sSubmodule %s contains untracked content\n",
 287                        line_prefix, path);
 288        if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
 289                fprintf(f, "%sSubmodule %s contains modified content\n",
 290                        line_prefix, path);
 291
 292        if (!hashcmp(one, two)) {
 293                strbuf_release(&sb);
 294                return;
 295        }
 296
 297        strbuf_addf(&sb, "%s%sSubmodule %s %s..", line_prefix, meta, path,
 298                        find_unique_abbrev(one, DEFAULT_ABBREV));
 299        if (!fast_backward && !fast_forward)
 300                strbuf_addch(&sb, '.');
 301        strbuf_addf(&sb, "%s", find_unique_abbrev(two, DEFAULT_ABBREV));
 302        if (message)
 303                strbuf_addf(&sb, " %s%s\n", message, reset);
 304        else
 305                strbuf_addf(&sb, "%s:%s\n", fast_backward ? " (rewind)" : "", reset);
 306        fwrite(sb.buf, sb.len, 1, f);
 307
 308        if (!message) /* only NULL if we succeeded in setting up the walk */
 309                print_submodule_summary(&rev, f, line_prefix, del, add, reset);
 310        if (left)
 311                clear_commit_marks(left, ~0);
 312        if (right)
 313                clear_commit_marks(right, ~0);
 314
 315        strbuf_release(&sb);
 316}
 317
 318void set_config_fetch_recurse_submodules(int value)
 319{
 320        config_fetch_recurse_submodules = value;
 321}
 322
 323static int has_remote(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
 324{
 325        return 1;
 326}
 327
 328static int submodule_needs_pushing(const char *path, const unsigned char sha1[20])
 329{
 330        if (add_submodule_odb(path) || !lookup_commit_reference(sha1))
 331                return 0;
 332
 333        if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
 334                struct child_process cp;
 335                const char *argv[] = {"rev-list", NULL, "--not", "--remotes", "-n", "1" , NULL};
 336                struct strbuf buf = STRBUF_INIT;
 337                int needs_pushing = 0;
 338
 339                argv[1] = sha1_to_hex(sha1);
 340                memset(&cp, 0, sizeof(cp));
 341                cp.argv = argv;
 342                cp.env = local_repo_env;
 343                cp.git_cmd = 1;
 344                cp.no_stdin = 1;
 345                cp.out = -1;
 346                cp.dir = path;
 347                if (start_command(&cp))
 348                        die("Could not run 'git rev-list %s --not --remotes -n 1' command in submodule %s",
 349                                sha1_to_hex(sha1), path);
 350                if (strbuf_read(&buf, cp.out, 41))
 351                        needs_pushing = 1;
 352                finish_command(&cp);
 353                close(cp.out);
 354                strbuf_release(&buf);
 355                return needs_pushing;
 356        }
 357
 358        return 0;
 359}
 360
 361static void collect_submodules_from_diff(struct diff_queue_struct *q,
 362                                         struct diff_options *options,
 363                                         void *data)
 364{
 365        int i;
 366        struct string_list *needs_pushing = data;
 367
 368        for (i = 0; i < q->nr; i++) {
 369                struct diff_filepair *p = q->queue[i];
 370                if (!S_ISGITLINK(p->two->mode))
 371                        continue;
 372                if (submodule_needs_pushing(p->two->path, p->two->sha1))
 373                        string_list_insert(needs_pushing, p->two->path);
 374        }
 375}
 376
 377static void find_unpushed_submodule_commits(struct commit *commit,
 378                struct string_list *needs_pushing)
 379{
 380        struct rev_info rev;
 381
 382        init_revisions(&rev, NULL);
 383        rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
 384        rev.diffopt.format_callback = collect_submodules_from_diff;
 385        rev.diffopt.format_callback_data = needs_pushing;
 386        diff_tree_combined_merge(commit, 1, &rev);
 387}
 388
 389int find_unpushed_submodules(unsigned char new_sha1[20],
 390                const char *remotes_name, struct string_list *needs_pushing)
 391{
 392        struct rev_info rev;
 393        struct commit *commit;
 394        const char *argv[] = {NULL, NULL, "--not", "NULL", NULL};
 395        int argc = ARRAY_SIZE(argv) - 1;
 396        char *sha1_copy;
 397
 398        struct strbuf remotes_arg = STRBUF_INIT;
 399
 400        strbuf_addf(&remotes_arg, "--remotes=%s", remotes_name);
 401        init_revisions(&rev, NULL);
 402        sha1_copy = xstrdup(sha1_to_hex(new_sha1));
 403        argv[1] = sha1_copy;
 404        argv[3] = remotes_arg.buf;
 405        setup_revisions(argc, argv, &rev, NULL);
 406        if (prepare_revision_walk(&rev))
 407                die("revision walk setup failed");
 408
 409        while ((commit = get_revision(&rev)) != NULL)
 410                find_unpushed_submodule_commits(commit, needs_pushing);
 411
 412        reset_revision_walk();
 413        free(sha1_copy);
 414        strbuf_release(&remotes_arg);
 415
 416        return needs_pushing->nr;
 417}
 418
 419static int push_submodule(const char *path)
 420{
 421        if (add_submodule_odb(path))
 422                return 1;
 423
 424        if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
 425                struct child_process cp;
 426                const char *argv[] = {"push", NULL};
 427
 428                memset(&cp, 0, sizeof(cp));
 429                cp.argv = argv;
 430                cp.env = local_repo_env;
 431                cp.git_cmd = 1;
 432                cp.no_stdin = 1;
 433                cp.dir = path;
 434                if (run_command(&cp))
 435                        return 0;
 436                close(cp.out);
 437        }
 438
 439        return 1;
 440}
 441
 442int push_unpushed_submodules(unsigned char new_sha1[20], const char *remotes_name)
 443{
 444        int i, ret = 1;
 445        struct string_list needs_pushing;
 446
 447        memset(&needs_pushing, 0, sizeof(struct string_list));
 448        needs_pushing.strdup_strings = 1;
 449
 450        if (!find_unpushed_submodules(new_sha1, remotes_name, &needs_pushing))
 451                return 1;
 452
 453        for (i = 0; i < needs_pushing.nr; i++) {
 454                const char *path = needs_pushing.items[i].string;
 455                fprintf(stderr, "Pushing submodule '%s'\n", path);
 456                if (!push_submodule(path)) {
 457                        fprintf(stderr, "Unable to push submodule '%s'\n", path);
 458                        ret = 0;
 459                }
 460        }
 461
 462        string_list_clear(&needs_pushing, 0);
 463
 464        return ret;
 465}
 466
 467static int is_submodule_commit_present(const char *path, unsigned char sha1[20])
 468{
 469        int is_present = 0;
 470        if (!add_submodule_odb(path) && lookup_commit_reference(sha1)) {
 471                /* Even if the submodule is checked out and the commit is
 472                 * present, make sure it is reachable from a ref. */
 473                struct child_process cp;
 474                const char *argv[] = {"rev-list", "-n", "1", NULL, "--not", "--all", NULL};
 475                struct strbuf buf = STRBUF_INIT;
 476
 477                argv[3] = sha1_to_hex(sha1);
 478                memset(&cp, 0, sizeof(cp));
 479                cp.argv = argv;
 480                cp.env = local_repo_env;
 481                cp.git_cmd = 1;
 482                cp.no_stdin = 1;
 483                cp.out = -1;
 484                cp.dir = path;
 485                if (!run_command(&cp) && !strbuf_read(&buf, cp.out, 1024))
 486                        is_present = 1;
 487
 488                close(cp.out);
 489                strbuf_release(&buf);
 490        }
 491        return is_present;
 492}
 493
 494static void submodule_collect_changed_cb(struct diff_queue_struct *q,
 495                                         struct diff_options *options,
 496                                         void *data)
 497{
 498        int i;
 499        for (i = 0; i < q->nr; i++) {
 500                struct diff_filepair *p = q->queue[i];
 501                if (!S_ISGITLINK(p->two->mode))
 502                        continue;
 503
 504                if (S_ISGITLINK(p->one->mode)) {
 505                        /* NEEDSWORK: We should honor the name configured in
 506                         * the .gitmodules file of the commit we are examining
 507                         * here to be able to correctly follow submodules
 508                         * being moved around. */
 509                        struct string_list_item *path;
 510                        path = unsorted_string_list_lookup(&changed_submodule_paths, p->two->path);
 511                        if (!path && !is_submodule_commit_present(p->two->path, p->two->sha1))
 512                                string_list_append(&changed_submodule_paths, xstrdup(p->two->path));
 513                } else {
 514                        /* Submodule is new or was moved here */
 515                        /* NEEDSWORK: When the .git directories of submodules
 516                         * live inside the superprojects .git directory some
 517                         * day we should fetch new submodules directly into
 518                         * that location too when config or options request
 519                         * that so they can be checked out from there. */
 520                        continue;
 521                }
 522        }
 523}
 524
 525static int add_sha1_to_array(const char *ref, const unsigned char *sha1,
 526                             int flags, void *data)
 527{
 528        sha1_array_append(data, sha1);
 529        return 0;
 530}
 531
 532void check_for_new_submodule_commits(unsigned char new_sha1[20])
 533{
 534        if (!initialized_fetch_ref_tips) {
 535                for_each_ref(add_sha1_to_array, &ref_tips_before_fetch);
 536                initialized_fetch_ref_tips = 1;
 537        }
 538
 539        sha1_array_append(&ref_tips_after_fetch, new_sha1);
 540}
 541
 542static void add_sha1_to_argv(const unsigned char sha1[20], void *data)
 543{
 544        argv_array_push(data, sha1_to_hex(sha1));
 545}
 546
 547static void calculate_changed_submodule_paths(void)
 548{
 549        struct rev_info rev;
 550        struct commit *commit;
 551        struct argv_array argv = ARGV_ARRAY_INIT;
 552
 553        /* No need to check if there are no submodules configured */
 554        if (!config_name_for_path.nr)
 555                return;
 556
 557        init_revisions(&rev, NULL);
 558        argv_array_push(&argv, "--"); /* argv[0] program name */
 559        sha1_array_for_each_unique(&ref_tips_after_fetch,
 560                                   add_sha1_to_argv, &argv);
 561        argv_array_push(&argv, "--not");
 562        sha1_array_for_each_unique(&ref_tips_before_fetch,
 563                                   add_sha1_to_argv, &argv);
 564        setup_revisions(argv.argc, argv.argv, &rev, NULL);
 565        if (prepare_revision_walk(&rev))
 566                die("revision walk setup failed");
 567
 568        /*
 569         * Collect all submodules (whether checked out or not) for which new
 570         * commits have been recorded upstream in "changed_submodule_paths".
 571         */
 572        while ((commit = get_revision(&rev))) {
 573                struct commit_list *parent = commit->parents;
 574                while (parent) {
 575                        struct diff_options diff_opts;
 576                        diff_setup(&diff_opts);
 577                        DIFF_OPT_SET(&diff_opts, RECURSIVE);
 578                        diff_opts.output_format |= DIFF_FORMAT_CALLBACK;
 579                        diff_opts.format_callback = submodule_collect_changed_cb;
 580                        diff_setup_done(&diff_opts);
 581                        diff_tree_sha1(parent->item->object.sha1, commit->object.sha1, "", &diff_opts);
 582                        diffcore_std(&diff_opts);
 583                        diff_flush(&diff_opts);
 584                        parent = parent->next;
 585                }
 586        }
 587
 588        argv_array_clear(&argv);
 589        sha1_array_clear(&ref_tips_before_fetch);
 590        sha1_array_clear(&ref_tips_after_fetch);
 591        initialized_fetch_ref_tips = 0;
 592}
 593
 594int fetch_populated_submodules(const struct argv_array *options,
 595                               const char *prefix, int command_line_option,
 596                               int quiet)
 597{
 598        int i, result = 0;
 599        struct child_process cp;
 600        struct argv_array argv = ARGV_ARRAY_INIT;
 601        struct string_list_item *name_for_path;
 602        const char *work_tree = get_git_work_tree();
 603        if (!work_tree)
 604                goto out;
 605
 606        if (!the_index.initialized)
 607                if (read_cache() < 0)
 608                        die("index file corrupt");
 609
 610        argv_array_push(&argv, "fetch");
 611        for (i = 0; i < options->argc; i++)
 612                argv_array_push(&argv, options->argv[i]);
 613        argv_array_push(&argv, "--recurse-submodules-default");
 614        /* default value, "--submodule-prefix" and its value are added later */
 615
 616        memset(&cp, 0, sizeof(cp));
 617        cp.env = local_repo_env;
 618        cp.git_cmd = 1;
 619        cp.no_stdin = 1;
 620
 621        calculate_changed_submodule_paths();
 622
 623        for (i = 0; i < active_nr; i++) {
 624                struct strbuf submodule_path = STRBUF_INIT;
 625                struct strbuf submodule_git_dir = STRBUF_INIT;
 626                struct strbuf submodule_prefix = STRBUF_INIT;
 627                struct cache_entry *ce = active_cache[i];
 628                const char *git_dir, *name, *default_argv;
 629
 630                if (!S_ISGITLINK(ce->ce_mode))
 631                        continue;
 632
 633                name = ce->name;
 634                name_for_path = unsorted_string_list_lookup(&config_name_for_path, ce->name);
 635                if (name_for_path)
 636                        name = name_for_path->util;
 637
 638                default_argv = "yes";
 639                if (command_line_option == RECURSE_SUBMODULES_DEFAULT) {
 640                        struct string_list_item *fetch_recurse_submodules_option;
 641                        fetch_recurse_submodules_option = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name);
 642                        if (fetch_recurse_submodules_option) {
 643                                if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_OFF)
 644                                        continue;
 645                                if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_ON_DEMAND) {
 646                                        if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
 647                                                continue;
 648                                        default_argv = "on-demand";
 649                                }
 650                        } else {
 651                                if ((config_fetch_recurse_submodules == RECURSE_SUBMODULES_OFF) ||
 652                                    gitmodules_is_unmerged)
 653                                        continue;
 654                                if (config_fetch_recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) {
 655                                        if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
 656                                                continue;
 657                                        default_argv = "on-demand";
 658                                }
 659                        }
 660                } else if (command_line_option == RECURSE_SUBMODULES_ON_DEMAND) {
 661                        if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
 662                                continue;
 663                        default_argv = "on-demand";
 664                }
 665
 666                strbuf_addf(&submodule_path, "%s/%s", work_tree, ce->name);
 667                strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf);
 668                strbuf_addf(&submodule_prefix, "%s%s/", prefix, ce->name);
 669                git_dir = read_gitfile(submodule_git_dir.buf);
 670                if (!git_dir)
 671                        git_dir = submodule_git_dir.buf;
 672                if (is_directory(git_dir)) {
 673                        if (!quiet)
 674                                printf("Fetching submodule %s%s\n", prefix, ce->name);
 675                        cp.dir = submodule_path.buf;
 676                        argv_array_push(&argv, default_argv);
 677                        argv_array_push(&argv, "--submodule-prefix");
 678                        argv_array_push(&argv, submodule_prefix.buf);
 679                        cp.argv = argv.argv;
 680                        if (run_command(&cp))
 681                                result = 1;
 682                        argv_array_pop(&argv);
 683                        argv_array_pop(&argv);
 684                        argv_array_pop(&argv);
 685                }
 686                strbuf_release(&submodule_path);
 687                strbuf_release(&submodule_git_dir);
 688                strbuf_release(&submodule_prefix);
 689        }
 690        argv_array_clear(&argv);
 691out:
 692        string_list_clear(&changed_submodule_paths, 1);
 693        return result;
 694}
 695
 696unsigned is_submodule_modified(const char *path, int ignore_untracked)
 697{
 698        ssize_t len;
 699        struct child_process cp;
 700        const char *argv[] = {
 701                "status",
 702                "--porcelain",
 703                NULL,
 704                NULL,
 705        };
 706        struct strbuf buf = STRBUF_INIT;
 707        unsigned dirty_submodule = 0;
 708        const char *line, *next_line;
 709        const char *git_dir;
 710
 711        strbuf_addf(&buf, "%s/.git", path);
 712        git_dir = read_gitfile(buf.buf);
 713        if (!git_dir)
 714                git_dir = buf.buf;
 715        if (!is_directory(git_dir)) {
 716                strbuf_release(&buf);
 717                /* The submodule is not checked out, so it is not modified */
 718                return 0;
 719
 720        }
 721        strbuf_reset(&buf);
 722
 723        if (ignore_untracked)
 724                argv[2] = "-uno";
 725
 726        memset(&cp, 0, sizeof(cp));
 727        cp.argv = argv;
 728        cp.env = local_repo_env;
 729        cp.git_cmd = 1;
 730        cp.no_stdin = 1;
 731        cp.out = -1;
 732        cp.dir = path;
 733        if (start_command(&cp))
 734                die("Could not run 'git status --porcelain' in submodule %s", path);
 735
 736        len = strbuf_read(&buf, cp.out, 1024);
 737        line = buf.buf;
 738        while (len > 2) {
 739                if ((line[0] == '?') && (line[1] == '?')) {
 740                        dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
 741                        if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
 742                                break;
 743                } else {
 744                        dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
 745                        if (ignore_untracked ||
 746                            (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED))
 747                                break;
 748                }
 749                next_line = strchr(line, '\n');
 750                if (!next_line)
 751                        break;
 752                next_line++;
 753                len -= (next_line - line);
 754                line = next_line;
 755        }
 756        close(cp.out);
 757
 758        if (finish_command(&cp))
 759                die("'git status --porcelain' failed in submodule %s", path);
 760
 761        strbuf_release(&buf);
 762        return dirty_submodule;
 763}
 764
 765int submodule_uses_gitfile(const char *path)
 766{
 767        struct child_process cp;
 768        const char *argv[] = {
 769                "submodule",
 770                "foreach",
 771                "--quiet",
 772                "--recursive",
 773                "test -f .git",
 774                NULL,
 775        };
 776        struct strbuf buf = STRBUF_INIT;
 777        const char *git_dir;
 778
 779        strbuf_addf(&buf, "%s/.git", path);
 780        git_dir = read_gitfile(buf.buf);
 781        if (!git_dir) {
 782                strbuf_release(&buf);
 783                return 0;
 784        }
 785        strbuf_release(&buf);
 786
 787        /* Now test that all nested submodules use a gitfile too */
 788        memset(&cp, 0, sizeof(cp));
 789        cp.argv = argv;
 790        cp.env = local_repo_env;
 791        cp.git_cmd = 1;
 792        cp.no_stdin = 1;
 793        cp.no_stderr = 1;
 794        cp.no_stdout = 1;
 795        cp.dir = path;
 796        if (run_command(&cp))
 797                return 0;
 798
 799        return 1;
 800}
 801
 802int ok_to_remove_submodule(const char *path)
 803{
 804        struct stat st;
 805        ssize_t len;
 806        struct child_process cp;
 807        const char *argv[] = {
 808                "status",
 809                "--porcelain",
 810                "-u",
 811                "--ignore-submodules=none",
 812                NULL,
 813        };
 814        struct strbuf buf = STRBUF_INIT;
 815        int ok_to_remove = 1;
 816
 817        if ((lstat(path, &st) < 0) || is_empty_dir(path))
 818                return 1;
 819
 820        if (!submodule_uses_gitfile(path))
 821                return 0;
 822
 823        memset(&cp, 0, sizeof(cp));
 824        cp.argv = argv;
 825        cp.env = local_repo_env;
 826        cp.git_cmd = 1;
 827        cp.no_stdin = 1;
 828        cp.out = -1;
 829        cp.dir = path;
 830        if (start_command(&cp))
 831                die("Could not run 'git status --porcelain -uall --ignore-submodules=none' in submodule %s", path);
 832
 833        len = strbuf_read(&buf, cp.out, 1024);
 834        if (len > 2)
 835                ok_to_remove = 0;
 836        close(cp.out);
 837
 838        if (finish_command(&cp))
 839                die("'git status --porcelain -uall --ignore-submodules=none' failed in submodule %s", path);
 840
 841        strbuf_release(&buf);
 842        return ok_to_remove;
 843}
 844
 845static int find_first_merges(struct object_array *result, const char *path,
 846                struct commit *a, struct commit *b)
 847{
 848        int i, j;
 849        struct object_array merges;
 850        struct commit *commit;
 851        int contains_another;
 852
 853        char merged_revision[42];
 854        const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path",
 855                                   "--all", merged_revision, NULL };
 856        struct rev_info revs;
 857        struct setup_revision_opt rev_opts;
 858
 859        memset(&merges, 0, sizeof(merges));
 860        memset(result, 0, sizeof(struct object_array));
 861        memset(&rev_opts, 0, sizeof(rev_opts));
 862
 863        /* get all revisions that merge commit a */
 864        snprintf(merged_revision, sizeof(merged_revision), "^%s",
 865                        sha1_to_hex(a->object.sha1));
 866        init_revisions(&revs, NULL);
 867        rev_opts.submodule = path;
 868        setup_revisions(sizeof(rev_args)/sizeof(char *)-1, rev_args, &revs, &rev_opts);
 869
 870        /* save all revisions from the above list that contain b */
 871        if (prepare_revision_walk(&revs))
 872                die("revision walk setup failed");
 873        while ((commit = get_revision(&revs)) != NULL) {
 874                struct object *o = &(commit->object);
 875                if (in_merge_bases(b, commit))
 876                        add_object_array(o, NULL, &merges);
 877        }
 878        reset_revision_walk();
 879
 880        /* Now we've got all merges that contain a and b. Prune all
 881         * merges that contain another found merge and save them in
 882         * result.
 883         */
 884        for (i = 0; i < merges.nr; i++) {
 885                struct commit *m1 = (struct commit *) merges.objects[i].item;
 886
 887                contains_another = 0;
 888                for (j = 0; j < merges.nr; j++) {
 889                        struct commit *m2 = (struct commit *) merges.objects[j].item;
 890                        if (i != j && in_merge_bases(m2, m1)) {
 891                                contains_another = 1;
 892                                break;
 893                        }
 894                }
 895
 896                if (!contains_another)
 897                        add_object_array(merges.objects[i].item,
 898                                         merges.objects[i].name, result);
 899        }
 900
 901        free(merges.objects);
 902        return result->nr;
 903}
 904
 905static void print_commit(struct commit *commit)
 906{
 907        struct strbuf sb = STRBUF_INIT;
 908        struct pretty_print_context ctx = {0};
 909        ctx.date_mode = DATE_NORMAL;
 910        format_commit_message(commit, " %h: %m %s", &sb, &ctx);
 911        fprintf(stderr, "%s\n", sb.buf);
 912        strbuf_release(&sb);
 913}
 914
 915#define MERGE_WARNING(path, msg) \
 916        warning("Failed to merge submodule %s (%s)", path, msg);
 917
 918int merge_submodule(unsigned char result[20], const char *path,
 919                    const unsigned char base[20], const unsigned char a[20],
 920                    const unsigned char b[20], int search)
 921{
 922        struct commit *commit_base, *commit_a, *commit_b;
 923        int parent_count;
 924        struct object_array merges;
 925
 926        int i;
 927
 928        /* store a in result in case we fail */
 929        hashcpy(result, a);
 930
 931        /* we can not handle deletion conflicts */
 932        if (is_null_sha1(base))
 933                return 0;
 934        if (is_null_sha1(a))
 935                return 0;
 936        if (is_null_sha1(b))
 937                return 0;
 938
 939        if (add_submodule_odb(path)) {
 940                MERGE_WARNING(path, "not checked out");
 941                return 0;
 942        }
 943
 944        if (!(commit_base = lookup_commit_reference(base)) ||
 945            !(commit_a = lookup_commit_reference(a)) ||
 946            !(commit_b = lookup_commit_reference(b))) {
 947                MERGE_WARNING(path, "commits not present");
 948                return 0;
 949        }
 950
 951        /* check whether both changes are forward */
 952        if (!in_merge_bases(commit_base, commit_a) ||
 953            !in_merge_bases(commit_base, commit_b)) {
 954                MERGE_WARNING(path, "commits don't follow merge-base");
 955                return 0;
 956        }
 957
 958        /* Case #1: a is contained in b or vice versa */
 959        if (in_merge_bases(commit_a, commit_b)) {
 960                hashcpy(result, b);
 961                return 1;
 962        }
 963        if (in_merge_bases(commit_b, commit_a)) {
 964                hashcpy(result, a);
 965                return 1;
 966        }
 967
 968        /*
 969         * Case #2: There are one or more merges that contain a and b in
 970         * the submodule. If there is only one, then present it as a
 971         * suggestion to the user, but leave it marked unmerged so the
 972         * user needs to confirm the resolution.
 973         */
 974
 975        /* Skip the search if makes no sense to the calling context.  */
 976        if (!search)
 977                return 0;
 978
 979        /* find commit which merges them */
 980        parent_count = find_first_merges(&merges, path, commit_a, commit_b);
 981        switch (parent_count) {
 982        case 0:
 983                MERGE_WARNING(path, "merge following commits not found");
 984                break;
 985
 986        case 1:
 987                MERGE_WARNING(path, "not fast-forward");
 988                fprintf(stderr, "Found a possible merge resolution "
 989                                "for the submodule:\n");
 990                print_commit((struct commit *) merges.objects[0].item);
 991                fprintf(stderr,
 992                        "If this is correct simply add it to the index "
 993                        "for example\n"
 994                        "by using:\n\n"
 995                        "  git update-index --cacheinfo 160000 %s \"%s\"\n\n"
 996                        "which will accept this suggestion.\n",
 997                        sha1_to_hex(merges.objects[0].item->sha1), path);
 998                break;
 999
1000        default:
1001                MERGE_WARNING(path, "multiple merges found");
1002                for (i = 0; i < merges.nr; i++)
1003                        print_commit((struct commit *) merges.objects[i].item);
1004        }
1005
1006        free(merges.objects);
1007        return 0;
1008}