32
32
#include "advice.h"
33
33
#include "branch.h"
34
34
#include "list-objects-filter-options.h"
35
+ #include "wildmatch.h"
36
+ #include "strbuf.h"
35
37
36
38
#define OPT_QUIET (1 << 0)
37
39
#define OPT_CACHED (1 << 1)
@@ -3307,6 +3309,8 @@ static void configure_added_submodule(struct add_data *add_data)
3307
3309
char * key ;
3308
3310
struct child_process add_submod = CHILD_PROCESS_INIT ;
3309
3311
struct child_process add_gitmodules = CHILD_PROCESS_INIT ;
3312
+ const struct string_list * values ;
3313
+ int matched = 0 ;
3310
3314
3311
3315
key = xstrfmt ("submodule.%s.url" , add_data -> sm_name );
3312
3316
repo_config_set_gently (the_repository , key , add_data -> realrepo );
@@ -3349,20 +3353,28 @@ static void configure_added_submodule(struct add_data *add_data)
3349
3353
* is_submodule_active(), since that function needs to find
3350
3354
* out the value of "submodule.active" again anyway.
3351
3355
*/
3352
- if (!repo_config_get (the_repository , "submodule.active" )) {
3356
+ if (repo_config_get (the_repository , "submodule.active" ) || /* key absent */
3357
+ repo_config_get_string_multi (the_repository , "submodule.active" , & values )) {
3353
3358
/*
3354
3359
* If the submodule being added isn't already covered by the
3355
3360
* current configured pathspec, set the submodule's active flag
3356
3361
*/
3357
- if (!is_submodule_active (the_repository , add_data -> sm_path )) {
3362
+ key = xstrfmt ("submodule.%s.active" , add_data -> sm_name );
3363
+ repo_config_set_gently (the_repository , key , "true" );
3364
+ free (key );
3365
+ } else {
3366
+ for (size_t i = 0 ; i < values -> nr ; i ++ ) {
3367
+ const char * pat = values -> items [i ].string ;
3368
+ if (!wildmatch (pat , add_data -> sm_path , 0 )) { /* match found */
3369
+ matched = 1 ;
3370
+ break ;
3371
+ }
3372
+ }
3373
+ if (!matched ) { /* no pattern matched -> force-enable */
3358
3374
key = xstrfmt ("submodule.%s.active" , add_data -> sm_name );
3359
3375
repo_config_set_gently (the_repository , key , "true" );
3360
3376
free (key );
3361
3377
}
3362
- } else {
3363
- key = xstrfmt ("submodule.%s.active" , add_data -> sm_name );
3364
- repo_config_set_gently (the_repository , key , "true" );
3365
- free (key );
3366
3378
}
3367
3379
}
3368
3380
@@ -3423,6 +3435,9 @@ static int module_add(int argc, const char **argv, const char *prefix,
3423
3435
struct add_data add_data = ADD_DATA_INIT ;
3424
3436
const char * ref_storage_format = NULL ;
3425
3437
char * to_free = NULL ;
3438
+ const struct submodule * existing ;
3439
+ struct strbuf buf = STRBUF_INIT ;
3440
+ char * sm_name_to_free = NULL ;
3426
3441
struct option options [] = {
3427
3442
OPT_STRING ('b' , "branch" , & add_data .branch , N_ ("branch" ),
3428
3443
N_ ("branch of repository to add as submodule" )),
@@ -3525,6 +3540,28 @@ static int module_add(int argc, const char **argv, const char *prefix,
3525
3540
if (!add_data .sm_name )
3526
3541
add_data .sm_name = add_data .sm_path ;
3527
3542
3543
+ existing = submodule_from_name (the_repository ,
3544
+ null_oid (the_hash_algo ),
3545
+ add_data .sm_name );
3546
+
3547
+ if (existing && strcmp (existing -> path , add_data .sm_path )) {
3548
+ if (!force ) {
3549
+ die (_ ("submodule name '%s' already used for path '%s'" ),
3550
+ add_data .sm_name , existing -> path );
3551
+ }
3552
+ /* --force: build <name><n> until unique */
3553
+ for (int i = 1 ; ; i ++ ) {
3554
+ strbuf_reset (& buf );
3555
+ strbuf_addf (& buf , "%s%d" , add_data .sm_name , i );
3556
+ if (!submodule_from_name (the_repository ,
3557
+ null_oid (the_hash_algo ),
3558
+ buf .buf )) {
3559
+ break ;
3560
+ }
3561
+ }
3562
+ add_data .sm_name = sm_name_to_free = strbuf_detach (& buf , NULL );
3563
+ }
3564
+
3528
3565
if (check_submodule_name (add_data .sm_name ))
3529
3566
die (_ ("'%s' is not a valid submodule name" ), add_data .sm_name );
3530
3567
@@ -3540,6 +3577,7 @@ static int module_add(int argc, const char **argv, const char *prefix,
3540
3577
3541
3578
ret = 0 ;
3542
3579
cleanup :
3580
+ free (sm_name_to_free );
3543
3581
free (add_data .sm_path );
3544
3582
free (to_free );
3545
3583
strbuf_release (& sb );
0 commit comments