Skip to content

Commit f9546a1

Browse files
committed
Merge branch 'kj/renamed-submodule' into seen
* kj/renamed-submodule: submodule: prevent overwriting .gitmodules entry on path reuse
2 parents 82e1b3f + 630d3b6 commit f9546a1

File tree

2 files changed

+72
-10
lines changed

2 files changed

+72
-10
lines changed

builtin/submodule--helper.c

Lines changed: 49 additions & 10 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 "strbuf.h"
36+
#include "wildmatch.h"
3537

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

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

@@ -3447,7 +3457,11 @@ static int module_add(int argc, const char **argv, const char *prefix,
34473457
int force = 0, quiet = 0, progress = 0, dissociate = 0;
34483458
struct add_data add_data = ADD_DATA_INIT;
34493459
const char *ref_storage_format = NULL;
3460+
const struct submodule *existing;
34503461
char *to_free = NULL;
3462+
struct strbuf buf = STRBUF_INIT;
3463+
int i;
3464+
char *sm_name_to_free = NULL;
34513465
struct option options[] = {
34523466
OPT_STRING('b', "branch", &add_data.branch, N_("branch"),
34533467
N_("branch of repository to add as submodule")),
@@ -3550,6 +3564,30 @@ static int module_add(int argc, const char **argv, const char *prefix,
35503564
if(!add_data.sm_name)
35513565
add_data.sm_name = add_data.sm_path;
35523566

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

@@ -3565,6 +3603,7 @@ static int module_add(int argc, const char **argv, const char *prefix,
35653603

35663604
ret = 0;
35673605
cleanup:
3606+
free(sm_name_to_free);
35683607
free(add_data.sm_path);
35693608
free(to_free);
35703609
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 "initial commit" &&
1490+
1491+
git init ../child-origin &&
1492+
git -C ../child-origin commit --allow-empty -m "initial commit" &&
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+
# Create another submodule repo
1501+
git init ../child2-origin &&
1502+
git -C ../child2-origin commit --allow-empty -m "initial commit" &&
1503+
1504+
test_must_fail git submodule add ../child2-origin child
1505+
)
1506+
'
1507+
14851508
test_done

0 commit comments

Comments
 (0)