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)
@@ -3331,6 +3333,9 @@ static void configure_added_submodule(struct add_data *add_data)
3331
3333
char * key ;
3332
3334
struct child_process add_submod = CHILD_PROCESS_INIT ;
3333
3335
struct child_process add_gitmodules = CHILD_PROCESS_INIT ;
3336
+ const struct string_list * values ;
3337
+ size_t i ;
3338
+ int matched = 0 ;
3334
3339
3335
3340
key = xstrfmt ("submodule.%s.url" , add_data -> sm_name );
3336
3341
git_config_set_gently (key , add_data -> realrepo );
@@ -3373,20 +3378,25 @@ static void configure_added_submodule(struct add_data *add_data)
3373
3378
* is_submodule_active(), since that function needs to find
3374
3379
* out the value of "submodule.active" again anyway.
3375
3380
*/
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 */
3387
3384
key = xstrfmt ("submodule.%s.active" , add_data -> sm_name );
3388
3385
git_config_set_gently (key , "true" );
3389
3386
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
+ }
3390
3400
}
3391
3401
}
3392
3402
@@ -3447,6 +3457,10 @@ static int module_add(int argc, const char **argv, const char *prefix,
3447
3457
struct add_data add_data = ADD_DATA_INIT ;
3448
3458
const char * ref_storage_format = NULL ;
3449
3459
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 ;
3450
3464
struct option options [] = {
3451
3465
OPT_STRING ('b' , "branch" , & add_data .branch , N_ ("branch" ),
3452
3466
N_ ("branch of repository to add as submodule" )),
@@ -3549,6 +3563,29 @@ static int module_add(int argc, const char **argv, const char *prefix,
3549
3563
if (!add_data .sm_name )
3550
3564
add_data .sm_name = add_data .sm_path ;
3551
3565
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
+ }
3552
3589
if (check_submodule_name (add_data .sm_name ))
3553
3590
die (_ ("'%s' is not a valid submodule name" ), add_data .sm_name );
3554
3591
@@ -3564,6 +3601,7 @@ static int module_add(int argc, const char **argv, const char *prefix,
3564
3601
3565
3602
ret = 0 ;
3566
3603
cleanup :
3604
+ free (sm_name_to_free );
3567
3605
free (add_data .sm_path );
3568
3606
free (to_free );
3569
3607
strbuf_release (& sb );
0 commit comments