config: pass repo to functions that rename or copy sections

Refactor functions that rename or copy config sections to accept a `struct repository` such that we can get rid of the implicit dependency on `the_repository`. Rename the functions accordingly. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Aug 13, 2024 at 11:14 UTC 76fc9906f2121dd4abd42a4c6c2d864c40736b3a
7 files changed +36 -33
builtin/branch.c
+4 -3
@@ -210,7 +210,7 @@ static void delete_branch_config(const char *branchname)
210 {
211 struct strbuf buf = STRBUF_INIT;
212 strbuf_addf(&buf, "branch.%s", branchname);
213 - if (git_config_rename_section(buf.buf, NULL) < 0)
213 + if (repo_config_rename_section(the_repository, buf.buf, NULL) < 0)
214 warning(_("update of config-file failed"));
215 strbuf_release(&buf);
216 }
@@ -659,9 +659,10 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
659
660 strbuf_addf(&oldsection, "branch.%s", interpreted_oldname);
661 strbuf_addf(&newsection, "branch.%s", interpreted_newname);
662 - if (!copy && git_config_rename_section(oldsection.buf, newsection.buf) < 0)
662 + if (!copy && repo_config_rename_section(the_repository, oldsection.buf, newsection.buf) < 0)
663 die(_("branch is renamed, but update of config-file failed"));
664 - if (copy && strcmp(interpreted_oldname, interpreted_newname) && git_config_copy_section(oldsection.buf, newsection.buf) < 0)
664 + if (copy && strcmp(interpreted_oldname, interpreted_newname) &&
665 + repo_config_copy_section(the_repository, oldsection.buf, newsection.buf) < 0)
666 die(_("branch is copied, but update of config-file failed"));
667 strbuf_release(&oldref);
668 strbuf_release(&newref);
builtin/config.c
+8 -8
@@ -1026,8 +1026,8 @@ static int cmd_config_rename_section(int argc, const char **argv, const char *pr
1026 location_options_init(&location_opts, prefix);
1027 check_write(&location_opts.source);
1028
1029 - ret = git_config_rename_section_in_file(location_opts.source.file,
1030 - argv[0], argv[1]);
1029 + ret = repo_config_rename_section_in_file(the_repository, location_opts.source.file,
1030 + argv[0], argv[1]);
1031 if (ret < 0)
1032 goto out;
1033 else if (!ret)
@@ -1055,8 +1055,8 @@ static int cmd_config_remove_section(int argc, const char **argv, const char *pr
1055 location_options_init(&location_opts, prefix);
1056 check_write(&location_opts.source);
1057
1058 - ret = git_config_rename_section_in_file(location_opts.source.file,
1059 - argv[0], NULL);
1058 + ret = repo_config_rename_section_in_file(the_repository, location_opts.source.file,
1059 + argv[0], NULL);
1060 if (ret < 0)
1061 goto out;
1062 else if (!ret)
@@ -1353,8 +1353,8 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
1353 else if (actions == ACTION_RENAME_SECTION) {
1354 check_write(&location_opts.source);
1355 check_argc(argc, 2, 2);
1356 - ret = git_config_rename_section_in_file(location_opts.source.file,
1357 - argv[0], argv[1]);
1356 + ret = repo_config_rename_section_in_file(the_repository, location_opts.source.file,
1357 + argv[0], argv[1]);
1358 if (ret < 0)
1359 goto out;
1360 else if (!ret)
@@ -1365,8 +1365,8 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
1365 else if (actions == ACTION_REMOVE_SECTION) {
1366 check_write(&location_opts.source);
1367 check_argc(argc, 1, 1);
1368 - ret = git_config_rename_section_in_file(location_opts.source.file,
1369 - argv[0], NULL);
1368 + ret = repo_config_rename_section_in_file(the_repository, location_opts.source.file,
1369 + argv[0], NULL);
1370 if (ret < 0)
1371 goto out;
1372 else if (!ret)
builtin/remote.c
+2 -2
@@ -736,7 +736,7 @@ static int mv(int argc, const char **argv, const char *prefix)
736
737 strbuf_addf(&buf, "remote.%s", rename.old_name);
738 strbuf_addf(&buf2, "remote.%s", rename.new_name);
739 - if (git_config_rename_section(buf.buf, buf2.buf) < 1)
739 + if (repo_config_rename_section(the_repository, buf.buf, buf2.buf) < 1)
740 return error(_("Could not rename config section '%s' to '%s'"),
741 buf.buf, buf2.buf);
742
@@ -944,7 +944,7 @@ static int rm(int argc, const char **argv, const char *prefix)
944
945 if (!result) {
946 strbuf_addf(&buf, "remote.%s", remote->name);
947 - if (git_config_rename_section(buf.buf, NULL) < 1)
947 + if (repo_config_rename_section(the_repository, buf.buf, NULL) < 1)
948 return error(_("Could not remove config section '%s'"), buf.buf);
949
950 handle_push_default(remote->name, NULL);
builtin/submodule--helper.c
+1 -1
@@ -1455,7 +1455,7 @@ static void deinit_submodule(const char *path, const char *prefix,
1455 * remove the whole section so we have a clean state when
1456 * the user later decides to init this submodule again
1457 */
1458 - git_config_rename_section_in_file(NULL, sub_key, NULL);
1458 + repo_config_rename_section_in_file(the_repository, NULL, sub_key, NULL);
1459 if (!(flags & OPT_QUIET))
1460 printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1461 sub->name, sub->url, displaypath);
config.c
+16 -14
@@ -3697,9 +3697,11 @@ static int section_name_is_ok(const char *name)
3697 #define GIT_CONFIG_MAX_LINE_LEN (512 * 1024)
3698
3699 /* if new_name == NULL, the section is removed instead */
3700 -static int git_config_copy_or_rename_section_in_file(const char *config_filename,
3701 - const char *old_name,
3702 - const char *new_name, int copy)
3700 +static int repo_config_copy_or_rename_section_in_file(
3701 + struct repository *r,
3702 + const char *config_filename,
3703 + const char *old_name,
3704 + const char *new_name, int copy)
3705 {
3706 int ret = 0, remove = 0;
3707 char *filename_buf = NULL;
@@ -3720,7 +3722,7 @@ static int git_config_copy_or_rename_section_in_file(const char *config_filename
3722 }
3723
3724 if (!config_filename)
3723 - config_filename = filename_buf = git_pathdup("config");
3725 + config_filename = filename_buf = repo_git_path(r, "config");
3726
3727 out_fd = hold_lock_file_for_update(&lock, config_filename, 0);
3728 if (out_fd < 0) {
@@ -3863,28 +3865,28 @@ out_no_rollback:
3865 return ret;
3866 }
3867
3866 -int git_config_rename_section_in_file(const char *config_filename,
3867 - const char *old_name, const char *new_name)
3868 +int repo_config_rename_section_in_file(struct repository *r, const char *config_filename,
3869 + const char *old_name, const char *new_name)
3870 {
3869 - return git_config_copy_or_rename_section_in_file(config_filename,
3871 + return repo_config_copy_or_rename_section_in_file(r, config_filename,
3872 old_name, new_name, 0);
3873 }
3874
3873 -int git_config_rename_section(const char *old_name, const char *new_name)
3875 +int repo_config_rename_section(struct repository *r, const char *old_name, const char *new_name)
3876 {
3875 - return git_config_rename_section_in_file(NULL, old_name, new_name);
3877 + return repo_config_rename_section_in_file(r, NULL, old_name, new_name);
3878 }
3879
3878 -int git_config_copy_section_in_file(const char *config_filename,
3879 - const char *old_name, const char *new_name)
3880 +int repo_config_copy_section_in_file(struct repository *r, const char *config_filename,
3881 + const char *old_name, const char *new_name)
3882 {
3881 - return git_config_copy_or_rename_section_in_file(config_filename,
3883 + return repo_config_copy_or_rename_section_in_file(r, config_filename,
3884 old_name, new_name, 1);
3885 }
3886
3885 -int git_config_copy_section(const char *old_name, const char *new_name)
3887 +int repo_config_copy_section(struct repository *r, const char *old_name, const char *new_name)
3888 {
3887 - return git_config_copy_section_in_file(NULL, old_name, new_name);
3889 + return repo_config_copy_section_in_file(r, NULL, old_name, new_name);
3890 }
3891
3892 /*
config.h
+4 -4
@@ -392,11 +392,11 @@ void repo_config_set_multivar_in_file(struct repository *r,
392 * If NULL is passed through `new_name` parameter,
393 * the section will be removed from the config file.
394 */
395 -int git_config_rename_section(const char *, const char *);
395 +int repo_config_rename_section(struct repository *, const char *, const char *);
396
397 -int git_config_rename_section_in_file(const char *, const char *, const char *);
398 -int git_config_copy_section(const char *, const char *);
399 -int git_config_copy_section_in_file(const char *, const char *, const char *);
397 +int repo_config_rename_section_in_file(struct repository *, const char *, const char *, const char *);
398 +int repo_config_copy_section(struct repository *, const char *, const char *);
399 +int repo_config_copy_section_in_file(struct repository *, const char *, const char *, const char *);
400 int git_config_system(void);
401 int config_error_nonbool(const char *);
402 #if defined(__GNUC__)
submodule.c
+1 -1
@@ -159,7 +159,7 @@ int remove_path_from_gitmodules(const char *path)
159 }
160 strbuf_addstr(&sect, "submodule.");
161 strbuf_addstr(&sect, submodule->name);
162 - if (git_config_rename_section_in_file(GITMODULES_FILE, sect.buf, NULL) < 0) {
162 + if (repo_config_rename_section_in_file(the_repository, GITMODULES_FILE, sect.buf, NULL) < 0) {
163 /* Maybe the user already did that, don't error out here */
164 warning(_("Could not remove .gitmodules entry for %s"), path);
165 strbuf_release(&sect);