]> git.ipfire.org Git - thirdparty/git.git/commitdiff
config: pass repo to functions that rename or copy sections
authorPatrick Steinhardt <ps@pks.im>
Tue, 13 Aug 2024 09:14:12 +0000 (11:14 +0200)
committerJunio C Hamano <gitster@pobox.com>
Tue, 13 Aug 2024 17:01:04 +0000 (10:01 -0700)
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>
builtin/branch.c
builtin/config.c
builtin/remote.c
builtin/submodule--helper.c
config.c
config.h
submodule.c

index 48cac74f97fc4a9257a8c5a493e949ccb4ebbe92..3f870741bfd88a8da565b7571256056a393d3b4e 100644 (file)
@@ -210,7 +210,7 @@ static void delete_branch_config(const char *branchname)
 {
        struct strbuf buf = STRBUF_INIT;
        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"));
        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(&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"));
-       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"));
        strbuf_release(&oldref);
        strbuf_release(&newref);
index 20a0b64090eab56d3b6a54073e338fdbf5db4448..e00d983596b8db413ab7eba457bbbaafb26c5f33 100644 (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);
        check_write(&location_opts.source);
 
-       ret = git_config_rename_section_in_file(location_opts.source.file,
-                                               argv[0], argv[1]);
+       ret = repo_config_rename_section_in_file(the_repository, location_opts.source.file,
+                                                argv[0], argv[1]);
        if (ret < 0)
                goto out;
        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);
        check_write(&location_opts.source);
 
-       ret = git_config_rename_section_in_file(location_opts.source.file,
-                                               argv[0], NULL);
+       ret = repo_config_rename_section_in_file(the_repository, location_opts.source.file,
+                                                argv[0], NULL);
        if (ret < 0)
                goto out;
        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) {
                check_write(&location_opts.source);
                check_argc(argc, 2, 2);
-               ret = git_config_rename_section_in_file(location_opts.source.file,
-                                                       argv[0], argv[1]);
+               ret = repo_config_rename_section_in_file(the_repository, location_opts.source.file,
+                                                        argv[0], argv[1]);
                if (ret < 0)
                        goto out;
                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) {
                check_write(&location_opts.source);
                check_argc(argc, 1, 1);
-               ret = git_config_rename_section_in_file(location_opts.source.file,
-                                                       argv[0], NULL);
+               ret = repo_config_rename_section_in_file(the_repository, location_opts.source.file,
+                                                        argv[0], NULL);
                if (ret < 0)
                        goto out;
                else if (!ret)
index 08292498bdebcafb603f140933166d8b1af12b6f..fef3026ef4054ea6dcdb90e27b3517dad343ad00 100644 (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(&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'"),
                                buf.buf, buf2.buf);
 
@@ -944,7 +944,7 @@ static int rm(int argc, const char **argv, const char *prefix)
 
        if (!result) {
                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);
 
                handle_push_default(remote->name, NULL);
index f1218a19957aa526c20065e23076711183510b07..662c87c06c657333fd7936a9b6fe718b5cc12732 100644 (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
                 * 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))
                        printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
                                 sub->name, sub->url, displaypath);
index e35fc902382fbed1ccbba67ec5ce6aa475771527..ed39922dbb31c25ba19fbed97918bf6beabb267e 100644 (file)
--- a/config.c
+++ b/config.c
@@ -3697,9 +3697,11 @@ static int section_name_is_ok(const char *name)
 #define GIT_CONFIG_MAX_LINE_LEN (512 * 1024)
 
 /* if new_name == NULL, the section is removed instead */
-static int git_config_copy_or_rename_section_in_file(const char *config_filename,
-                                     const char *old_name,
-                                     const char *new_name, int copy)
+static int repo_config_copy_or_rename_section_in_file(
+       struct repository *r,
+       const char *config_filename,
+       const char *old_name,
+       const char *new_name, int copy)
 {
        int ret = 0, remove = 0;
        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)
-               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);
        if (out_fd < 0) {
@@ -3863,28 +3865,28 @@ out_no_rollback:
        return ret;
 }
 
-int git_config_rename_section_in_file(const char *config_filename,
-                                     const char *old_name, const char *new_name)
+int repo_config_rename_section_in_file(struct repository *r, const char *config_filename,
+                                      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);
 }
 
-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,
-                                     const char *old_name, const char *new_name)
+int repo_config_copy_section_in_file(struct repository *r, const char *config_filename,
+                                    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);
 }
 
-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);
 }
 
 /*
index 947307c591b57a5bd81021ed00593e259241fcd8..589d6dae2681c589534d156e55884195f4c90f01 100644 (file)
--- a/config.h
+++ b/config.h
@@ -392,11 +392,11 @@ void repo_config_set_multivar_in_file(struct repository *r,
  * If NULL is passed through `new_name` parameter,
  * 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 git_config_copy_section(const char *, const char *);
-int git_config_copy_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 repo_config_copy_section(struct repository *, 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 config_error_nonbool(const char *);
 #if defined(__GNUC__)
index ab99a30253083bc9f4d53120ed27b53d1caca2a1..bc04fc8284f5dd6afc3251ec20ce1181d867d19c 100644 (file)
@@ -159,7 +159,7 @@ int remove_path_from_gitmodules(const char *path)
        }
        strbuf_addstr(&sect, "submodule.");
        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 */
                warning(_("Could not remove .gitmodules entry for %s"), path);
                strbuf_release(&sect);