Skip to content

Commit ced7010

Browse files
committed
Merge branch 'kj/renamed-submodule' into jch
The case where a new submodule takes a path where used to be a completely different subproject is now dealt a bit better than before. Comments? * kj/renamed-submodule: submodule: skip redundant active entries when pattern covers path submodule: prevent overwriting .gitmodules entry on path reuse
2 parents 4760f38 + 015518d commit ced7010

File tree

3 files changed

+87
-11
lines changed

3 files changed

+87
-11
lines changed

builtin/submodule--helper.c

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
#include "advice.h"
3333
#include "branch.h"
3434
#include "list-objects-filter-options.h"
35+
#include "wildmatch.h"
36+
#include "strbuf.h"
3537

3638
#define OPT_QUIET (1 << 0)
3739
#define OPT_CACHED (1 << 1)
@@ -3331,6 +3333,9 @@ static void configure_added_submodule(struct add_data *add_data)
33313333
char *key;
33323334
struct child_process add_submod = CHILD_PROCESS_INIT;
33333335
struct child_process add_gitmodules = CHILD_PROCESS_INIT;
3336+
const struct string_list *values;
3337+
size_t i;
3338+
int matched = 0;
33343339

33353340
key = xstrfmt("submodule.%s.url", add_data->sm_name);
33363341
git_config_set_gently(key, add_data->realrepo);
@@ -3373,20 +3378,25 @@ static void configure_added_submodule(struct add_data *add_data)
33733378
* is_submodule_active(), since that function needs to find
33743379
* out the value of "submodule.active" again anyway.
33753380
*/
3376-
if (!git_config_get("submodule.active")) {
3377-
/*
3378-
* If the submodule being added isn't already covered by the
3379-
* current configured pathspec, set the submodule's active flag
3380-
*/
3381-
if (!is_submodule_active(the_repository, add_data->sm_path)) {
3382-
key = xstrfmt("submodule.%s.active", add_data->sm_name);
3383-
git_config_set_gently(key, "true");
3384-
free(key);
3385-
}
3386-
} else {
3381+
if (git_config_get("submodule.active") || /* key absent */
3382+
git_config_get_string_multi("submodule.active", &values)) {
3383+
/* submodule.active is missing -> force-enable */
33873384
key = xstrfmt("submodule.%s.active", add_data->sm_name);
33883385
git_config_set_gently(key, "true");
33893386
free(key);
3387+
} else {
3388+
for (i = 0; i < values->nr; i++) {
3389+
const char *pat = values->items[i].string;
3390+
if (!wildmatch(pat, add_data->sm_path, 0)) { /* match found */
3391+
matched = 1;
3392+
break;
3393+
}
3394+
}
3395+
if (!matched) { /* no pattern matched -> force-enable */
3396+
key = xstrfmt("submodule.%s.active", add_data->sm_name);
3397+
git_config_set_gently(key, "true");
3398+
free(key);
3399+
}
33903400
}
33913401
}
33923402

@@ -3447,6 +3457,10 @@ static int module_add(int argc, const char **argv, const char *prefix,
34473457
struct add_data add_data = ADD_DATA_INIT;
34483458
const char *ref_storage_format = NULL;
34493459
char *to_free = NULL;
3460+
const struct submodule *existing;
3461+
struct strbuf buf = STRBUF_INIT;
3462+
int i;
3463+
char *sm_name_to_free = NULL;
34503464
struct option options[] = {
34513465
OPT_STRING('b', "branch", &add_data.branch, N_("branch"),
34523466
N_("branch of repository to add as submodule")),
@@ -3549,6 +3563,29 @@ static int module_add(int argc, const char **argv, const char *prefix,
35493563
if(!add_data.sm_name)
35503564
add_data.sm_name = add_data.sm_path;
35513565

3566+
existing = submodule_from_name(the_repository,
3567+
null_oid(the_hash_algo),
3568+
add_data.sm_name);
3569+
3570+
if (existing && strcmp(existing->path, add_data.sm_path)) {
3571+
if (!force) {
3572+
die(_("submodule name '%s' already used for path '%s'"),
3573+
add_data.sm_name, existing->path);
3574+
}
3575+
3576+
/* --force: build <name><n> until unique */
3577+
for (i = 1; ; i++) {
3578+
strbuf_reset(&buf);
3579+
strbuf_addf(&buf, "%s%d", add_data.sm_name, i);
3580+
if (!submodule_from_name(the_repository,
3581+
null_oid(the_hash_algo),
3582+
buf.buf)) {
3583+
break;
3584+
}
3585+
}
3586+
3587+
add_data.sm_name = sm_name_to_free = strbuf_detach(&buf, NULL);
3588+
}
35523589
if (check_submodule_name(add_data.sm_name))
35533590
die(_("'%s' is not a valid submodule name"), add_data.sm_name);
35543591

@@ -3564,6 +3601,7 @@ static int module_add(int argc, const char **argv, const char *prefix,
35643601

35653602
ret = 0;
35663603
cleanup:
3604+
free(sm_name_to_free);
35673605
free(add_data.sm_path);
35683606
free(to_free);
35693607
strbuf_release(&sb);

t/t7400-submodule-basic.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,4 +1482,27 @@ test_expect_success '`submodule init` and `init.templateDir`' '
14821482
)
14831483
'
14841484

1485+
test_expect_success 'submodule add fails when name is reused' '
1486+
git init test-submodule &&
1487+
(
1488+
cd test-submodule &&
1489+
git commit --allow-empty -m init &&
1490+
1491+
git init ../child-origin &&
1492+
git -C ../child-origin commit --allow-empty -m init &&
1493+
1494+
git submodule add ../child-origin child &&
1495+
git commit -m "Add submodule child" &&
1496+
1497+
git mv child child_old &&
1498+
git commit -m "Move child to child_old" &&
1499+
1500+
# Now adding a *new* repo at the old name must fail
1501+
git init ../child2-origin &&
1502+
git -C ../child2-origin commit --allow-empty -m init &&
1503+
test_must_fail git submodule add ../child2-origin child
1504+
)
1505+
'
1506+
1507+
14851508
test_done

t/t7413-submodule-is-active.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,19 @@ test_expect_success 'is-active, submodule.active and submodule add' '
124124
git -C super2 config --get submodule.mod.active
125125
'
126126

127+
test_expect_success 'submodule add skips redundant active entry' '
128+
git init repo &&
129+
(
130+
cd repo &&
131+
git config submodule.active "lib/*" &&
132+
git commit --allow-empty -m init &&
133+
134+
git init ../lib-origin &&
135+
git -C ../lib-origin commit --allow-empty -m init &&
136+
137+
git submodule add ../lib-origin lib/foo &&
138+
! git config --get submodule.lib/foo.active
139+
)
140+
'
141+
127142
test_done

0 commit comments

Comments
 (0)