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)
@@ -3327,6 +3329,24 @@ static int config_submodule_in_gitmodules(const char *name, const char *var, con
3327
3329
return ret ;
3328
3330
}
3329
3331
3332
+ static int submodule_active_matches_path (const char * path )
3333
+ {
3334
+ const struct string_list * values ;
3335
+ size_t i ;
3336
+
3337
+ if (git_config_get_string_multi ("submodule.active" , & values ))
3338
+ return 0 ;
3339
+
3340
+ for (i = 0 ; i < values -> nr ; i ++ ) {
3341
+ const char * pat = values -> items [i ].string ;
3342
+ if (!wildmatch (pat , path , 0 ))
3343
+ return 1 ;
3344
+ }
3345
+
3346
+ return 0 ;
3347
+ }
3348
+
3349
+
3330
3350
static void configure_added_submodule (struct add_data * add_data )
3331
3351
{
3332
3352
char * key ;
@@ -3374,17 +3394,7 @@ static void configure_added_submodule(struct add_data *add_data)
3374
3394
* is_submodule_active(), since that function needs to find
3375
3395
* out the value of "submodule.active" again anyway.
3376
3396
*/
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 )) {
3383
- key = xstrfmt ("submodule.%s.active" , add_data -> sm_name );
3384
- git_config_set_gently (key , "true" );
3385
- free (key );
3386
- }
3387
- } else {
3397
+ if (!submodule_active_matches_path (add_data -> sm_path )) {
3388
3398
key = xstrfmt ("submodule.%s.active" , add_data -> sm_name );
3389
3399
git_config_set_gently (key , "true" );
3390
3400
free (key );
@@ -3448,6 +3458,10 @@ static int module_add(int argc, const char **argv, const char *prefix,
3448
3458
struct add_data add_data = ADD_DATA_INIT ;
3449
3459
const char * ref_storage_format = NULL ;
3450
3460
char * to_free = NULL ;
3461
+ const struct submodule * existing ;
3462
+ struct strbuf buf = STRBUF_INIT ;
3463
+ int i ;
3464
+ char * sm_name_to_free = NULL ;
3451
3465
struct option options [] = {
3452
3466
OPT_STRING ('b' , "branch" , & add_data .branch , N_ ("branch" ),
3453
3467
N_ ("branch of repository to add as submodule" )),
@@ -3550,6 +3564,29 @@ static int module_add(int argc, const char **argv, const char *prefix,
3550
3564
if (!add_data .sm_name )
3551
3565
add_data .sm_name = add_data .sm_path ;
3552
3566
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
+ }
3553
3590
if (check_submodule_name (add_data .sm_name ))
3554
3591
die (_ ("'%s' is not a valid submodule name" ), add_data .sm_name );
3555
3592
@@ -3565,6 +3602,7 @@ static int module_add(int argc, const char **argv, const char *prefix,
3565
3602
3566
3603
ret = 0 ;
3567
3604
cleanup :
3605
+ free (sm_name_to_free );
3568
3606
free (add_data .sm_path );
3569
3607
free (to_free );
3570
3608
strbuf_release (& sb );
0 commit comments