Skip to content

Commit 42bd39b

Browse files
peffgitster
authored andcommitted
config: teach git_config_rename_section a file argument
The other config-writing functions (git_config_set and git_config_set_multivar) each have an -"in_file" version to write a specific file. Let's add one for rename_section, with the eventual goal of moving away from the magic config_exclusive_filename global. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0a5f575 commit 42bd39b

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,7 @@ extern int git_config_parse_key(const char *, char **, int *);
11281128
extern int git_config_set_multivar(const char *, const char *, const char *, int);
11291129
extern int git_config_set_multivar_in_file(const char *, const char *, const char *, const char *, int);
11301130
extern int git_config_rename_section(const char *, const char *);
1131+
extern int git_config_rename_section_in_file(const char *, const char *, const char *);
11311132
extern const char *git_etc_gitconfig(void);
11321133
extern int check_repository_format_version(const char *var, const char *value, void *cb);
11331134
extern int git_env_bool(const char *, int);

config.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,19 +1470,19 @@ static int section_name_match (const char *buf, const char *name)
14701470
}
14711471

14721472
/* if new_name == NULL, the section is removed instead */
1473-
int git_config_rename_section(const char *old_name, const char *new_name)
1473+
int git_config_rename_section_in_file(const char *config_filename,
1474+
const char *old_name, const char *new_name)
14741475
{
14751476
int ret = 0, remove = 0;
1476-
char *config_filename;
1477+
char *filename_buf = NULL;
14771478
struct lock_file *lock = xcalloc(sizeof(struct lock_file), 1);
14781479
int out_fd;
14791480
char buf[1024];
14801481
FILE *config_file;
14811482

1482-
if (config_exclusive_filename)
1483-
config_filename = xstrdup(config_exclusive_filename);
1484-
else
1485-
config_filename = git_pathdup("config");
1483+
if (!config_filename)
1484+
config_filename = filename_buf = git_pathdup("config");
1485+
14861486
out_fd = hold_lock_file_for_update(lock, config_filename, 0);
14871487
if (out_fd < 0) {
14881488
ret = error("could not lock config file %s", config_filename);
@@ -1546,10 +1546,16 @@ int git_config_rename_section(const char *old_name, const char *new_name)
15461546
if (commit_lock_file(lock) < 0)
15471547
ret = error("could not commit config file %s", config_filename);
15481548
out:
1549-
free(config_filename);
1549+
free(filename_buf);
15501550
return ret;
15511551
}
15521552

1553+
int git_config_rename_section(const char *old_name, const char *new_name)
1554+
{
1555+
return git_config_rename_section_in_file(config_exclusive_filename,
1556+
old_name, new_name);
1557+
}
1558+
15531559
/*
15541560
* Call this to report error for your variable that should not
15551561
* get a boolean value (i.e. "[my] var" means "true").

0 commit comments

Comments
 (0)