config: Introduce functions to write non-standard file

Introduce two new functions corresponding to "git_config_set" and "git_config_set_multivar" to write a non-standard configuration file. Expose these new functions in cache.h for other git programs to use. Helped-by: Jeff King <peff@peff.net> Helped-by: Jonathan Nieder <jrnieder@gmail.com> Reviewed-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ramkumar Ramachandra committed Aug 4, 2011 at 16:09 UTC 5ec3118293931cf1fe8677f16449999cfbfc8b92
2 files changed +29 -9
cache.h
+2
@@ -1051,9 +1051,11 @@ extern int git_config_bool(const char *, const char *);
1051 extern int git_config_maybe_bool(const char *, const char *);
1052 extern int git_config_string(const char **, const char *, const char *);
1053 extern int git_config_pathname(const char **, const char *, const char *);
1054 +extern int git_config_set_in_file(const char *, const char *, const char *);
1055 extern int git_config_set(const char *, const char *);
1056 extern int git_config_parse_key(const char *, char **, int *);
1057 extern int git_config_set_multivar(const char *, const char *, const char *, int);
1058 +extern int git_config_set_multivar_in_file(const char *, const char *, const char *, const char *, int);
1059 extern int git_config_rename_section(const char *, const char *);
1060 extern const char *git_etc_gitconfig(void);
1061 extern int check_repository_format_version(const char *var, const char *value, void *cb);
config.c
+27 -9
@@ -1073,6 +1073,12 @@ contline:
1073 return offset;
1074 }
1075
1076 +int git_config_set_in_file(const char *config_filename,
1077 + const char *key, const char *value)
1078 +{
1079 + return git_config_set_multivar_in_file(config_filename, key, value, NULL, 0);
1080 +}
1081 +
1082 int git_config_set(const char *key, const char *value)
1083 {
1084 return git_config_set_multivar(key, value, NULL, 0);
@@ -1170,19 +1176,14 @@ out_free_ret_1:
1176 * - the config file is removed and the lock file rename()d to it.
1177 *
1178 */
1173 -int git_config_set_multivar(const char *key, const char *value,
1174 - const char *value_regex, int multi_replace)
1179 +int git_config_set_multivar_in_file(const char *config_filename,
1180 + const char *key, const char *value,
1181 + const char *value_regex, int multi_replace)
1182 {
1183 int fd = -1, in_fd;
1184 int ret;
1178 - char *config_filename;
1185 struct lock_file *lock = NULL;
1186
1181 - if (config_exclusive_filename)
1182 - config_filename = xstrdup(config_exclusive_filename);
1183 - else
1184 - config_filename = git_pathdup("config");
1185 -
1187 /* parse-key returns negative; flip the sign to feed exit(3) */
1188 ret = 0 - git_config_parse_key(key, &store.key, &store.baselen);
1189 if (ret)
@@ -1359,7 +1360,6 @@ int git_config_set_multivar(const char *key, const char *value,
1360 out_free:
1361 if (lock)
1362 rollback_lock_file(lock);
1362 - free(config_filename);
1363 return ret;
1364
1365 write_err_out:
@@ -1368,6 +1368,24 @@ write_err_out:
1368
1369 }
1370
1371 +int git_config_set_multivar(const char *key, const char *value,
1372 + const char *value_regex, int multi_replace)
1373 +{
1374 + const char *config_filename;
1375 + char *buf = NULL;
1376 + int ret;
1377 +
1378 + if (config_exclusive_filename)
1379 + config_filename = config_exclusive_filename;
1380 + else
1381 + config_filename = buf = git_pathdup("config");
1382 +
1383 + ret = git_config_set_multivar_in_file(config_filename, key, value,
1384 + value_regex, multi_replace);
1385 + free(buf);
1386 + return ret;
1387 +}
1388 +
1389 static int section_name_match (const char *buf, const char *name)
1390 {
1391 int i = 0, j = 0, dot = 0;