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