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)
@@ -3307,6 +3309,8 @@ static void configure_added_submodule(struct add_data *add_data)
33073309 char * key ;
33083310 struct child_process add_submod = CHILD_PROCESS_INIT ;
33093311 struct child_process add_gitmodules = CHILD_PROCESS_INIT ;
3312+ const struct string_list * values ;
3313+ int matched = 0 ;
33103314
33113315 key = xstrfmt ("submodule.%s.url" , add_data -> sm_name );
33123316 repo_config_set_gently (the_repository , key , add_data -> realrepo );
@@ -3349,20 +3353,28 @@ static void configure_added_submodule(struct add_data *add_data)
33493353 * is_submodule_active(), since that function needs to find
33503354 * out the value of "submodule.active" again anyway.
33513355 */
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 )) {
33533358 /*
33543359 * If the submodule being added isn't already covered by the
33553360 * current configured pathspec, set the submodule's active flag
33563361 */
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 */
33583374 key = xstrfmt ("submodule.%s.active" , add_data -> sm_name );
33593375 repo_config_set_gently (the_repository , key , "true" );
33603376 free (key );
33613377 }
3362- } else {
3363- key = xstrfmt ("submodule.%s.active" , add_data -> sm_name );
3364- repo_config_set_gently (the_repository , key , "true" );
3365- free (key );
33663378 }
33673379}
33683380
@@ -3423,6 +3435,9 @@ static int module_add(int argc, const char **argv, const char *prefix,
34233435 struct add_data add_data = ADD_DATA_INIT ;
34243436 const char * ref_storage_format = NULL ;
34253437 char * to_free = NULL ;
3438+ const struct submodule * existing ;
3439+ struct strbuf buf = STRBUF_INIT ;
3440+ char * sm_name_to_free = NULL ;
34263441 struct option options [] = {
34273442 OPT_STRING ('b' , "branch" , & add_data .branch , N_ ("branch" ),
34283443 N_ ("branch of repository to add as submodule" )),
@@ -3525,6 +3540,28 @@ static int module_add(int argc, const char **argv, const char *prefix,
35253540 if (!add_data .sm_name )
35263541 add_data .sm_name = add_data .sm_path ;
35273542
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+
35283565 if (check_submodule_name (add_data .sm_name ))
35293566 die (_ ("'%s' is not a valid submodule name" ), add_data .sm_name );
35303567
@@ -3540,6 +3577,7 @@ static int module_add(int argc, const char **argv, const char *prefix,
35403577
35413578 ret = 0 ;
35423579cleanup :
3580+ free (sm_name_to_free );
35433581 free (add_data .sm_path );
35443582 free (to_free );
35453583 strbuf_release (& sb );
0 commit comments