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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Feb 16, 2012 at 03:04 UTC 42bd39b57fee80a9fd136f89c7728e640d13964a
2 files changed +14 -7
cache.h
+1
@@ -1128,6 +1128,7 @@ extern int git_config_parse_key(const char *, char **, int *);
1128 extern int git_config_set_multivar(const char *, const char *, const char *, int);
1129 extern int git_config_set_multivar_in_file(const char *, const char *, const char *, const char *, int);
1130 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 *);
1132 extern const char *git_etc_gitconfig(void);
1133 extern int check_repository_format_version(const char *var, const char *value, void *cb);
1134 extern int git_env_bool(const char *, int);
config.c
+13 -7
@@ -1470,19 +1470,19 @@ static int section_name_match (const char *buf, const char *name)
1470 }
1471
1472 /* 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)
1475 {
1476 int ret = 0, remove = 0;
1476 - char *config_filename;
1477 + char *filename_buf = NULL;
1478 struct lock_file *lock = xcalloc(sizeof(struct lock_file), 1);
1479 int out_fd;
1480 char buf[1024];
1481 FILE *config_file;
1482
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 +
1486 out_fd = hold_lock_file_for_update(lock, config_filename, 0);
1487 if (out_fd < 0) {
1488 ret = error("could not lock config file %s", config_filename);
@@ -1546,10 +1546,16 @@ unlock_and_out:
1546 if (commit_lock_file(lock) < 0)
1547 ret = error("could not commit config file %s", config_filename);
1548 out:
1549 - free(config_filename);
1549 + free(filename_buf);
1550 return ret;
1551 }
1552
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 +
1559 /*
1560 * Call this to report error for your variable that should not
1561 * get a boolean value (i.e. "[my] var" means "true").