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>
This commit is contained in:
Patrick Steinhardt
2024-08-13 11:14:12 +02:00
committed by Junio C Hamano
parent 0c2c37d16b
commit 76fc9906f2
7 changed files with 36 additions and 33 deletions

View File

@ -210,7 +210,7 @@ static void delete_branch_config(const char *branchname)
{ {
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
strbuf_addf(&buf, "branch.%s", branchname); strbuf_addf(&buf, "branch.%s", branchname);
if (git_config_rename_section(buf.buf, NULL) < 0) if (repo_config_rename_section(the_repository, buf.buf, NULL) < 0)
warning(_("update of config-file failed")); warning(_("update of config-file failed"));
strbuf_release(&buf); strbuf_release(&buf);
} }
@ -659,9 +659,10 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
strbuf_addf(&oldsection, "branch.%s", interpreted_oldname); strbuf_addf(&oldsection, "branch.%s", interpreted_oldname);
strbuf_addf(&newsection, "branch.%s", interpreted_newname); strbuf_addf(&newsection, "branch.%s", interpreted_newname);
if (!copy && git_config_rename_section(oldsection.buf, newsection.buf) < 0) if (!copy && repo_config_rename_section(the_repository, oldsection.buf, newsection.buf) < 0)
die(_("branch is renamed, but update of config-file failed")); die(_("branch is renamed, but update of config-file failed"));
if (copy && strcmp(interpreted_oldname, interpreted_newname) && git_config_copy_section(oldsection.buf, newsection.buf) < 0) if (copy && strcmp(interpreted_oldname, interpreted_newname) &&
repo_config_copy_section(the_repository, oldsection.buf, newsection.buf) < 0)
die(_("branch is copied, but update of config-file failed")); die(_("branch is copied, but update of config-file failed"));
strbuf_release(&oldref); strbuf_release(&oldref);
strbuf_release(&newref); strbuf_release(&newref);

View File

@ -1026,8 +1026,8 @@ static int cmd_config_rename_section(int argc, const char **argv, const char *pr
location_options_init(&location_opts, prefix); location_options_init(&location_opts, prefix);
check_write(&location_opts.source); check_write(&location_opts.source);
ret = git_config_rename_section_in_file(location_opts.source.file, ret = repo_config_rename_section_in_file(the_repository, location_opts.source.file,
argv[0], argv[1]); argv[0], argv[1]);
if (ret < 0) if (ret < 0)
goto out; goto out;
else if (!ret) else if (!ret)
@ -1055,8 +1055,8 @@ static int cmd_config_remove_section(int argc, const char **argv, const char *pr
location_options_init(&location_opts, prefix); location_options_init(&location_opts, prefix);
check_write(&location_opts.source); check_write(&location_opts.source);
ret = git_config_rename_section_in_file(location_opts.source.file, ret = repo_config_rename_section_in_file(the_repository, location_opts.source.file,
argv[0], NULL); argv[0], NULL);
if (ret < 0) if (ret < 0)
goto out; goto out;
else if (!ret) else if (!ret)
@ -1353,8 +1353,8 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
else if (actions == ACTION_RENAME_SECTION) { else if (actions == ACTION_RENAME_SECTION) {
check_write(&location_opts.source); check_write(&location_opts.source);
check_argc(argc, 2, 2); check_argc(argc, 2, 2);
ret = git_config_rename_section_in_file(location_opts.source.file, ret = repo_config_rename_section_in_file(the_repository, location_opts.source.file,
argv[0], argv[1]); argv[0], argv[1]);
if (ret < 0) if (ret < 0)
goto out; goto out;
else if (!ret) else if (!ret)
@ -1365,8 +1365,8 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
else if (actions == ACTION_REMOVE_SECTION) { else if (actions == ACTION_REMOVE_SECTION) {
check_write(&location_opts.source); check_write(&location_opts.source);
check_argc(argc, 1, 1); check_argc(argc, 1, 1);
ret = git_config_rename_section_in_file(location_opts.source.file, ret = repo_config_rename_section_in_file(the_repository, location_opts.source.file,
argv[0], NULL); argv[0], NULL);
if (ret < 0) if (ret < 0)
goto out; goto out;
else if (!ret) else if (!ret)

View File

@ -736,7 +736,7 @@ static int mv(int argc, const char **argv, const char *prefix)
strbuf_addf(&buf, "remote.%s", rename.old_name); strbuf_addf(&buf, "remote.%s", rename.old_name);
strbuf_addf(&buf2, "remote.%s", rename.new_name); strbuf_addf(&buf2, "remote.%s", rename.new_name);
if (git_config_rename_section(buf.buf, buf2.buf) < 1) if (repo_config_rename_section(the_repository, buf.buf, buf2.buf) < 1)
return error(_("Could not rename config section '%s' to '%s'"), return error(_("Could not rename config section '%s' to '%s'"),
buf.buf, buf2.buf); buf.buf, buf2.buf);
@ -944,7 +944,7 @@ static int rm(int argc, const char **argv, const char *prefix)
if (!result) { if (!result) {
strbuf_addf(&buf, "remote.%s", remote->name); strbuf_addf(&buf, "remote.%s", remote->name);
if (git_config_rename_section(buf.buf, NULL) < 1) if (repo_config_rename_section(the_repository, buf.buf, NULL) < 1)
return error(_("Could not remove config section '%s'"), buf.buf); return error(_("Could not remove config section '%s'"), buf.buf);
handle_push_default(remote->name, NULL); handle_push_default(remote->name, NULL);

View File

@ -1455,7 +1455,7 @@ static void deinit_submodule(const char *path, const char *prefix,
* remove the whole section so we have a clean state when * remove the whole section so we have a clean state when
* the user later decides to init this submodule again * the user later decides to init this submodule again
*/ */
git_config_rename_section_in_file(NULL, sub_key, NULL); repo_config_rename_section_in_file(the_repository, NULL, sub_key, NULL);
if (!(flags & OPT_QUIET)) if (!(flags & OPT_QUIET))
printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"), printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
sub->name, sub->url, displaypath); sub->name, sub->url, displaypath);

View File

@ -3697,9 +3697,11 @@ static int section_name_is_ok(const char *name)
#define GIT_CONFIG_MAX_LINE_LEN (512 * 1024) #define GIT_CONFIG_MAX_LINE_LEN (512 * 1024)
/* if new_name == NULL, the section is removed instead */ /* if new_name == NULL, the section is removed instead */
static int git_config_copy_or_rename_section_in_file(const char *config_filename, static int repo_config_copy_or_rename_section_in_file(
const char *old_name, struct repository *r,
const char *new_name, int copy) const char *config_filename,
const char *old_name,
const char *new_name, int copy)
{ {
int ret = 0, remove = 0; int ret = 0, remove = 0;
char *filename_buf = NULL; char *filename_buf = NULL;
@ -3720,7 +3722,7 @@ static int git_config_copy_or_rename_section_in_file(const char *config_filename
} }
if (!config_filename) if (!config_filename)
config_filename = filename_buf = git_pathdup("config"); config_filename = filename_buf = repo_git_path(r, "config");
out_fd = hold_lock_file_for_update(&lock, config_filename, 0); out_fd = hold_lock_file_for_update(&lock, config_filename, 0);
if (out_fd < 0) { if (out_fd < 0) {
@ -3863,28 +3865,28 @@ out_no_rollback:
return ret; return ret;
} }
int git_config_rename_section_in_file(const char *config_filename, int repo_config_rename_section_in_file(struct repository *r, const char *config_filename,
const char *old_name, const char *new_name) const char *old_name, const char *new_name)
{ {
return git_config_copy_or_rename_section_in_file(config_filename, return repo_config_copy_or_rename_section_in_file(r, config_filename,
old_name, new_name, 0); old_name, new_name, 0);
} }
int git_config_rename_section(const char *old_name, const char *new_name) int repo_config_rename_section(struct repository *r, const char *old_name, const char *new_name)
{ {
return git_config_rename_section_in_file(NULL, old_name, new_name); return repo_config_rename_section_in_file(r, NULL, old_name, new_name);
} }
int git_config_copy_section_in_file(const char *config_filename, int repo_config_copy_section_in_file(struct repository *r, const char *config_filename,
const char *old_name, const char *new_name) const char *old_name, const char *new_name)
{ {
return git_config_copy_or_rename_section_in_file(config_filename, return repo_config_copy_or_rename_section_in_file(r, config_filename,
old_name, new_name, 1); old_name, new_name, 1);
} }
int git_config_copy_section(const char *old_name, const char *new_name) int repo_config_copy_section(struct repository *r, const char *old_name, const char *new_name)
{ {
return git_config_copy_section_in_file(NULL, old_name, new_name); return repo_config_copy_section_in_file(r, NULL, old_name, new_name);
} }
/* /*

View File

@ -392,11 +392,11 @@ void repo_config_set_multivar_in_file(struct repository *r,
* If NULL is passed through `new_name` parameter, * If NULL is passed through `new_name` parameter,
* the section will be removed from the config file. * the section will be removed from the config file.
*/ */
int git_config_rename_section(const char *, const char *); int repo_config_rename_section(struct repository *, const char *, const char *);
int git_config_rename_section_in_file(const char *, const char *, const char *); int repo_config_rename_section_in_file(struct repository *, const char *, const char *, const char *);
int git_config_copy_section(const char *, const char *); int repo_config_copy_section(struct repository *, const char *, const char *);
int git_config_copy_section_in_file(const char *, const char *, const char *); int repo_config_copy_section_in_file(struct repository *, const char *, const char *, const char *);
int git_config_system(void); int git_config_system(void);
int config_error_nonbool(const char *); int config_error_nonbool(const char *);
#if defined(__GNUC__) #if defined(__GNUC__)

View File

@ -159,7 +159,7 @@ int remove_path_from_gitmodules(const char *path)
} }
strbuf_addstr(&sect, "submodule."); strbuf_addstr(&sect, "submodule.");
strbuf_addstr(&sect, submodule->name); strbuf_addstr(&sect, submodule->name);
if (git_config_rename_section_in_file(GITMODULES_FILE, sect.buf, NULL) < 0) { if (repo_config_rename_section_in_file(the_repository, GITMODULES_FILE, sect.buf, NULL) < 0) {
/* Maybe the user already did that, don't error out here */ /* Maybe the user already did that, don't error out here */
warning(_("Could not remove .gitmodules entry for %s"), path); warning(_("Could not remove .gitmodules entry for %s"), path);
strbuf_release(&sect); strbuf_release(&sect);