808
809
/*
810
* Holds relevant information for a changed submodule. Used as the .util
811
- * member of the changed submodule string_list_item.
811
+ * member of the changed submodule name string_list_item.
812
+ *
813
+ * (super_oid, path) allows the submodule config to be read from _some_
814
+ * .gitmodules file. We store this information the first time we find a
815
+ * superproject commit that points to the submodule, but this is
816
+ * arbitrary - we can choose any (super_oid, path) that matches the
817
+ * submodule's name.
818
+ *
819
+ * NEEDSWORK: Storing an arbitrary commit is undesirable because we can't
820
+ * guarantee that we're reading the commit that the user would expect. A better
821
+ * scheme would be to just fetch a submodule by its name. This requires two
822
+ * steps:
823
+ * - Create a function that behaves like repo_submodule_init(), but accepts a
824
+ * submodule name instead of treeish_name and path. This should be easy
825
+ * because repo_submodule_init() internally uses the submodule's name.
826
+ *
827
+ * - Replace most instances of 'struct submodule' (which is the .gitmodules
828
+ * config) with just the submodule name. This is OK because we expect
829
+ * submodule settings to be stored in .git/config (via "git submodule init"),
830
+ * not .gitmodules. This also lets us delete get_non_gitmodules_submodule(),
831
+ * which constructs a bogus 'struct submodule' for the sake of giving a
832
+ * placeholder name to a gitlink.
833
*/
834
struct changed_submodule_data {
835
+ /*
836
+ * The first superproject commit in the rev walk that points to
837
+ * the submodule.
838
+ */
839
+ const struct object_id *super_oid;
840
+ /*
841
+ * Path to the submodule in the superproject commit referenced
842
+ * by 'super_oid'.
843
+ */
844
+ char *path;
845
/* The submodule commits that have changed in the rev walk. */
846
struct oid_array new_commits;
847
};
849
static void changed_submodule_data_clear(struct changed_submodule_data *cs_data)
850
{
851
oid_array_clear(&cs_data->new_commits);
852
+ free(cs_data->path);
853
}
854
855
static void collect_changed_submodules_cb(struct diff_queue_struct *q,
894
continue;
895
896
item = string_list_insert(changed, name);
865
- if (!item->util)
897
+ if (item->util)
898
+ cs_data = item->util;
899
+ else {
900
item->util = xcalloc(1, sizeof(struct changed_submodule_data));
867
- cs_data = item->util;
901
+ cs_data = item->util;
902
+ cs_data->super_oid = commit_oid;
903
+ cs_data->path = xstrdup(p->two->path);
904
+ }
905
oid_array_append(&cs_data->new_commits, &p->two->oid);
906
}
907
}
1290
oid_array_append(&ref_tips_after_fetch, oid);
1291
}
1292
1293
+/*
1294
+ * Returns 1 if there is at least one submodule gitdir in
1295
+ * $GIT_DIR/modules and 0 otherwise. This follows
1296
+ * submodule_name_to_gitdir(), which looks for submodules in
1297
+ * $GIT_DIR/modules, not $GIT_COMMON_DIR.
1298
+ *
1299
+ * A submodule can be moved to $GIT_DIR/modules manually by running "git
1300
+ * submodule absorbgitdirs", or it may be initialized there by "git
1301
+ * submodule update".
1302
+ */
1303
+static int repo_has_absorbed_submodules(struct repository *r)
1304
+{
1305
+ int ret;
1306
+ struct strbuf buf = STRBUF_INIT;
1307
+
1308
+ strbuf_repo_git_path(&buf, r, "modules/");
1309
+ ret = file_exists(buf.buf) && !is_empty_dir(buf.buf);
1310
+ strbuf_release(&buf);
1311
+ return ret;
1312
+}
1313
+
1314
static void calculate_changed_submodule_paths(struct repository *r,
1315
struct string_list *changed_submodule_names)
1316
{
1317
struct strvec argv = STRVEC_INIT;
1318
struct string_list_item *name;
1319
1262
- /* No need to check if there are no submodules configured */
1263
- if (!submodule_from_path(r, NULL, NULL))
1320
+ /* No need to check if no submodules would be fetched */
1321
+ if (!submodule_from_path(r, NULL, NULL) &&
1322
+ !repo_has_absorbed_submodules(r))
1323
return;
1324
1325
strvec_push(&argv, "--"); /* argv[0] program name */
1392
}
1393
1394
struct submodule_parallel_fetch {
1336
- int count;
1395
+ /*
1396
+ * The index of the last index entry processed by
1397
+ * get_fetch_task_from_index().
1398
+ */
1399
+ int index_count;
1400
+ /*
1401
+ * The index of the last string_list entry processed by
1402
+ * get_fetch_task_from_changed().
1403
+ */
1404
+ int changed_count;
1405
struct strvec args;
1406
struct repository *r;
1407
const char *prefix;
1410
int quiet;
1411
int result;
1412
1413
+ /*
1414
+ * Names of submodules that have new commits. Generated by
1415
+ * walking the newly fetched superproject commits.
1416
+ */
1417
struct string_list changed_submodule_names;
1418
+ /*
1419
+ * Names of submodules that have already been processed. Lets us
1420
+ * avoid fetching the same submodule more than once.
1421
+ */
1422
+ struct string_list seen_submodule_names;
1423
1424
/* Pending fetches by OIDs */
1425
struct fetch_task **oid_fetch_tasks;
1430
#define SPF_INIT { \
1431
.args = STRVEC_INIT, \
1432
.changed_submodule_names = STRING_LIST_INIT_DUP, \
1433
+ .seen_submodule_names = STRING_LIST_INIT_DUP, \
1434
.submodules_with_errors = STRBUF_INIT, \
1435
}
1436
1468
const struct submodule *sub;
1469
unsigned free_sub : 1; /* Do we need to free the submodule? */
1470
const char *default_argv; /* The default fetch mode. */
1471
+ struct strvec git_args; /* Args for the child git process. */
1472
1473
struct oid_array *commits; /* Ensure these commits are fetched */
1474
};
1504
if (p->repo)
1505
repo_clear(p->repo);
1506
FREE_AND_NULL(p->repo);
1507
+
1508
+ strvec_clear(&p->git_args);
1509
}
1510
1511
static struct repository *get_submodule_repo_for(struct repository *r,
1544
task->free_sub = 1;
1545
}
1546
1547
+ if (string_list_lookup(&spf->seen_submodule_names, task->sub->name))
1548
+ goto cleanup;
1549
+
1550
switch (get_fetch_recurse_config(task->sub, spf))
1551
{
1552
default:
1577
}
1578
1579
static struct fetch_task *
1496
-get_fetch_task(struct submodule_parallel_fetch *spf, struct strbuf *err)
1580
+get_fetch_task_from_index(struct submodule_parallel_fetch *spf,
1581
+ struct strbuf *err)
1582
{
1498
- for (; spf->count < spf->r->index->cache_nr; spf->count++) {
1499
- const struct cache_entry *ce = spf->r->index->cache[spf->count];
1583
+ for (; spf->index_count < spf->r->index->cache_nr; spf->index_count++) {
1584
+ const struct cache_entry *ce =
1585
+ spf->r->index->cache[spf->index_count];
1586
struct fetch_task *task;
1587
1588
if (!S_ISGITLINK(ce->ce_mode))
1597
strbuf_addf(err, _("Fetching submodule %s%s\n"),
1598
spf->prefix, ce->name);
1599
1514
- spf->count++;
1600
+ spf->index_count++;
1601
return task;
1602
} else {
1603
struct strbuf empty_submodule_path = STRBUF_INIT;
1625
return NULL;
1626
}
1627
1628
+static struct fetch_task *
1629
+get_fetch_task_from_changed(struct submodule_parallel_fetch *spf,
1630
+ struct strbuf *err)
1631
+{
1632
+ for (; spf->changed_count < spf->changed_submodule_names.nr;
1633
+ spf->changed_count++) {
1634
+ struct string_list_item item =
1635
+ spf->changed_submodule_names.items[spf->changed_count];
1636
+ struct changed_submodule_data *cs_data = item.util;
1637
+ struct fetch_task *task;
1638
+
1639
+ if (!is_tree_submodule_active(spf->r, cs_data->super_oid,cs_data->path))
1640
+ continue;
1641
+
1642
+ task = fetch_task_create(spf, cs_data->path,
1643
+ cs_data->super_oid);
1644
+ if (!task)
1645
+ continue;
1646
+
1647
+ if (!task->repo) {
1648
+ strbuf_addf(err, _("Could not access submodule '%s' at commit %s\n"),
1649
+ cs_data->path,
1650
+ find_unique_abbrev(cs_data->super_oid, DEFAULT_ABBREV));
1651
+
1652
+ fetch_task_release(task);
1653
+ free(task);
1654
+ continue;
1655
+ }
1656
+
1657
+ if (!spf->quiet)
1658
+ strbuf_addf(err,
1659
+ _("Fetching submodule %s%s at commit %s\n"),
1660
+ spf->prefix, task->sub->path,
1661
+ find_unique_abbrev(cs_data->super_oid,
1662
+ DEFAULT_ABBREV));
1663
+
1664
+ spf->changed_count++;
1665
+ /*
1666
+ * NEEDSWORK: Submodules set/unset a value for
1667
+ * core.worktree when they are populated/unpopulated by
1668
+ * "git checkout" (and similar commands, see
1669
+ * submodule_move_head() and
1670
+ * connect_work_tree_and_git_dir()), but if the
1671
+ * submodule is unpopulated in another way (e.g. "git
1672
+ * rm", "rm -r"), core.worktree will still be set even
1673
+ * though the directory doesn't exist, and the child
1674
+ * process will crash while trying to chdir into the
1675
+ * nonexistent directory.
1676
+ *
1677
+ * In this case, we know that the submodule has no
1678
+ * working tree, so we can work around this by
1679
+ * setting "--work-tree=." (--bare does not work because
1680
+ * worktree settings take precedence over bare-ness).
1681
+ * However, this is not necessarily true in other cases,
1682
+ * so a generalized solution is still necessary.
1683
+ *
1684
+ * Possible solutions:
1685
+ * - teach "git [add|rm]" to unset core.worktree and
1686
+ * discourage users from removing submodules without
1687
+ * using a Git command.
1688
+ * - teach submodule child processes to ignore stale
1689
+ * core.worktree values.
1690
+ */
1691
+ strvec_push(&task->git_args, "--work-tree=.");
1692
+ return task;
1693
+ }
1694
+ return NULL;
1695
+}
1696
+
1697
static int get_next_submodule(struct child_process *cp, struct strbuf *err,
1698
void *data, void **task_cb)
1699
{
1700
struct submodule_parallel_fetch *spf = data;
1546
- struct fetch_task *task = get_fetch_task(spf, err);
1701
+ struct fetch_task *task =
1702
+ get_fetch_task_from_index(spf, err);
1703
+ if (!task)
1704
+ task = get_fetch_task_from_changed(spf, err);
1705
1706
if (task) {
1707
struct strbuf submodule_prefix = STRBUF_INIT;
1711
prepare_submodule_repo_env_in_gitdir(&cp->env_array);
1712
cp->git_cmd = 1;
1713
strvec_init(&cp->args);
1714
+ if (task->git_args.nr)
1715
+ strvec_pushv(&cp->args, task->git_args.v);
1716
strvec_pushv(&cp->args, spf->args.v);
1717
strvec_push(&cp->args, task->default_argv);
1718
strvec_push(&cp->args, "--submodule-prefix");
1724
*task_cb = task;
1725
1726
strbuf_release(&submodule_prefix);
1727
+ string_list_insert(&spf->seen_submodule_names, task->sub->name);
1728
return 1;
1729
}
1730
1839
return 0;
1840
}
1841
1681
-int fetch_populated_submodules(struct repository *r,
1682
- const struct strvec *options,
1683
- const char *prefix, int command_line_option,
1684
- int default_option,
1685
- int quiet, int max_parallel_jobs)
1842
+int fetch_submodules(struct repository *r,
1843
+ const struct strvec *options,
1844
+ const char *prefix, int command_line_option,
1845
+ int default_option,
1846
+ int quiet, int max_parallel_jobs)
1847
{
1848
int i;
1849
struct submodule_parallel_fetch spf = SPF_INIT;