submodule--helper: use submodule_from_path in set-{url,branch}

The commands need a path to a submodule but treated it as the name when modifying the .gitmodules file, leading to confusion when a submodule's name does not match its path. Because calling submodule_from_path initializes the submodule cache, we need to manually trigger a reread before syncing, as the cache is missing the config change we just made. Signed-off-by: Jan Alexander Steffens (heftig) <heftig@archlinux.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jan Alexander Steffens (heftig) committed Oct 3, 2023 at 20:50 UTC 6327085aa027b7d5936912bed596a3cf86953439
1 file changed +18 -4
builtin/submodule--helper.c
+18 -4
@@ -2901,19 +2901,26 @@ static int module_set_url(int argc, const char **argv, const char *prefix)
2901 N_("git submodule set-url [--quiet] <path> <newurl>"),
2902 NULL
2903 };
2904 + const struct submodule *sub;
2905
2906 argc = parse_options(argc, argv, prefix, options, usage, 0);
2907
2908 if (argc != 2 || !(path = argv[0]) || !(newurl = argv[1]))
2909 usage_with_options(usage, options);
2910
2910 - config_name = xstrfmt("submodule.%s.url", path);
2911 + sub = submodule_from_path(the_repository, null_oid(), path);
2912
2913 + if (!sub)
2914 + die(_("no submodule mapping found in .gitmodules for path '%s'"),
2915 + path);
2916 +
2917 + config_name = xstrfmt("submodule.%s.url", sub->name);
2918 config_set_in_gitmodules_file_gently(config_name, newurl);
2913 - sync_submodule(path, prefix, NULL, quiet ? OPT_QUIET : 0);
2919
2915 - free(config_name);
2920 + repo_read_gitmodules (the_repository, 0);
2921 + sync_submodule(sub->path, prefix, NULL, quiet ? OPT_QUIET : 0);
2922
2923 + free(config_name);
2924 return 0;
2925 }
2926
@@ -2941,6 +2948,7 @@ static int module_set_branch(int argc, const char **argv, const char *prefix)
2948 N_("git submodule set-branch [-q|--quiet] (-b|--branch) <branch> <path>"),
2949 NULL
2950 };
2951 + const struct submodule *sub;
2952
2953 argc = parse_options(argc, argv, prefix, options, usage, 0);
2954
@@ -2953,7 +2961,13 @@ static int module_set_branch(int argc, const char **argv, const char *prefix)
2961 if (argc != 1 || !(path = argv[0]))
2962 usage_with_options(usage, options);
2963
2956 - config_name = xstrfmt("submodule.%s.branch", path);
2964 + sub = submodule_from_path(the_repository, null_oid(), path);
2965 +
2966 + if (!sub)
2967 + die(_("no submodule mapping found in .gitmodules for path '%s'"),
2968 + path);
2969 +
2970 + config_name = xstrfmt("submodule.%s.branch", sub->name);
2971 ret = config_set_in_gitmodules_file_gently(config_name, opt_branch);
2972
2973 free(config_name);