Skip to content

Commit 9a53219

Browse files
peffgitster
authored andcommitted
config: drop git_config_get_string_const()
As evidenced by the leak fixes in the previous commit, the "const" in git_config_get_string_const() clearly misleads people into thinking that it does not allocate a copy of the string. We can fix this by renaming it, but it's easier still to just drop it. Of the four remaining callers: - The one in git_config_parse_expiry() still needs to allocate, since that's what its callers expect. We can just use the non-const version and cast our pointer. Slightly ugly, but the damage is contained in one spot. - The two in apply are writing to global "const char *" variables, and need to continue allocating. We often mark these as const because we assign default string literals to them. But in this case we don't do that, so we can just declare them as real "char *" pointers and use the non-const version. - The call in checkout doesn't actually need a copy; it can just use the non-allocating "tmp" version of the function. The function is also mentioned in the MyFirstContribution document. We can swap that call out for the non-allocating "tmp" variant, which fits well in the example given. We'll drop the "configset" and "repo" variants, as well (which are unused). Note that this frees up the "const" name, so we could rename the "tmp" variant back to that. But let's give some time for topics in flight to adapt to the new code before doing so (if we do it too soon, the function semantics will change but the compiler won't alert us). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent f1de981 commit 9a53219

File tree

7 files changed

+16
-43
lines changed

7 files changed

+16
-43
lines changed

Documentation/MyFirstContribution.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,14 +319,14 @@ function body:
319319
...
320320

321321
git_config(git_default_config, NULL);
322-
if (git_config_get_string_const("user.name", &cfg_name) > 0)
322+
if (git_config_get_string_tmp("user.name", &cfg_name) > 0)
323323
printf(_("No name is found in config\n"));
324324
else
325325
printf(_("Your name: %s\n"), cfg_name);
326326
----
327327

328328
`git_config()` will grab the configuration from config files known to Git and
329-
apply standard precedence rules. `git_config_get_string_const()` will look up
329+
apply standard precedence rules. `git_config_get_string_tmp()` will look up
330330
a specific key ("user.name") and give you the value. There are a number of
331331
single-key lookup functions like this one; you can see them all (and more info
332332
about how to use `git_config()`) in `Documentation/technical/api-config.txt`.

apply.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ struct gitdiff_data {
3030

3131
static void git_apply_config(void)
3232
{
33-
git_config_get_string_const("apply.whitespace", &apply_default_whitespace);
34-
git_config_get_string_const("apply.ignorewhitespace", &apply_default_ignorewhitespace);
33+
git_config_get_string("apply.whitespace", &apply_default_whitespace);
34+
git_config_get_string("apply.ignorewhitespace", &apply_default_ignorewhitespace);
3535
git_config(git_xmerge_config, NULL);
3636
}
3737

cache.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -921,8 +921,8 @@ extern int assume_unchanged;
921921
extern int prefer_symlink_refs;
922922
extern int warn_ambiguous_refs;
923923
extern int warn_on_object_refname_ambiguity;
924-
extern const char *apply_default_whitespace;
925-
extern const char *apply_default_ignorewhitespace;
924+
extern char *apply_default_whitespace;
925+
extern char *apply_default_ignorewhitespace;
926926
extern const char *git_attributes_file;
927927
extern const char *git_hooks_path;
928928
extern int zlib_compression_level;

checkout.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,14 @@ const char *unique_tracking_name(const char *name, struct object_id *oid,
4747
{
4848
struct tracking_name_data cb_data = TRACKING_NAME_DATA_INIT;
4949
const char *default_remote = NULL;
50-
if (!git_config_get_string_const("checkout.defaultremote", &default_remote))
50+
if (!git_config_get_string_tmp("checkout.defaultremote", &default_remote))
5151
cb_data.default_remote = default_remote;
5252
cb_data.src_ref = xstrfmt("refs/heads/%s", name);
5353
cb_data.dst_oid = oid;
5454
for_each_remote(check_tracking_name, &cb_data);
5555
if (dwim_remotes_matched)
5656
*dwim_remotes_matched = cb_data.num_matches;
5757
free(cb_data.src_ref);
58-
free((char *)default_remote);
5958
if (cb_data.num_matches == 1) {
6059
free(cb_data.default_dst_ref);
6160
free(cb_data.default_dst_oid);

config.c

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2006,20 +2006,15 @@ const struct string_list *git_configset_get_value_multi(struct config_set *cs, c
20062006
return e ? &e->value_list : NULL;
20072007
}
20082008

2009-
int git_configset_get_string_const(struct config_set *cs, const char *key, const char **dest)
2009+
int git_configset_get_string(struct config_set *cs, const char *key, char **dest)
20102010
{
20112011
const char *value;
20122012
if (!git_configset_get_value(cs, key, &value))
2013-
return git_config_string(dest, key, value);
2013+
return git_config_string((const char **)dest, key, value);
20142014
else
20152015
return 1;
20162016
}
20172017

2018-
int git_configset_get_string(struct config_set *cs, const char *key, char **dest)
2019-
{
2020-
return git_configset_get_string_const(cs, key, (const char **)dest);
2021-
}
2022-
20232018
int git_configset_get_string_tmp(struct config_set *cs, const char *key,
20242019
const char **dest)
20252020
{
@@ -2161,24 +2156,17 @@ const struct string_list *repo_config_get_value_multi(struct repository *repo,
21612156
return git_configset_get_value_multi(repo->config, key);
21622157
}
21632158

2164-
int repo_config_get_string_const(struct repository *repo,
2165-
const char *key, const char **dest)
2159+
int repo_config_get_string(struct repository *repo,
2160+
const char *key, char **dest)
21662161
{
21672162
int ret;
21682163
git_config_check_init(repo);
2169-
ret = git_configset_get_string_const(repo->config, key, dest);
2164+
ret = git_configset_get_string(repo->config, key, dest);
21702165
if (ret < 0)
21712166
git_die_config(key, NULL);
21722167
return ret;
21732168
}
21742169

2175-
int repo_config_get_string(struct repository *repo,
2176-
const char *key, char **dest)
2177-
{
2178-
git_config_check_init(repo);
2179-
return repo_config_get_string_const(repo, key, (const char **)dest);
2180-
}
2181-
21822170
int repo_config_get_string_tmp(struct repository *repo,
21832171
const char *key, const char **dest)
21842172
{
@@ -2257,11 +2245,6 @@ const struct string_list *git_config_get_value_multi(const char *key)
22572245
return repo_config_get_value_multi(the_repository, key);
22582246
}
22592247

2260-
int git_config_get_string_const(const char *key, const char **dest)
2261-
{
2262-
return repo_config_get_string_const(the_repository, key, dest);
2263-
}
2264-
22652248
int git_config_get_string(const char *key, char **dest)
22662249
{
22672250
return repo_config_get_string(the_repository, key, dest);
@@ -2304,7 +2287,7 @@ int git_config_get_pathname(const char *key, const char **dest)
23042287

23052288
int git_config_get_expiry(const char *key, const char **output)
23062289
{
2307-
int ret = git_config_get_string_const(key, output);
2290+
int ret = git_config_get_string(key, (char **)output);
23082291
if (ret)
23092292
return ret;
23102293
if (strcmp(*output, "now")) {

config.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,6 @@ void git_configset_clear(struct config_set *cs);
458458
*/
459459
int git_configset_get_value(struct config_set *cs, const char *key, const char **dest);
460460

461-
int git_configset_get_string_const(struct config_set *cs, const char *key, const char **dest);
462461
int git_configset_get_string(struct config_set *cs, const char *key, char **dest);
463462
int git_configset_get_string_tmp(struct config_set *cs, const char *key, const char **dest);
464463
int git_configset_get_int(struct config_set *cs, const char *key, int *dest);
@@ -475,8 +474,6 @@ int repo_config_get_value(struct repository *repo,
475474
const char *key, const char **value);
476475
const struct string_list *repo_config_get_value_multi(struct repository *repo,
477476
const char *key);
478-
int repo_config_get_string_const(struct repository *repo,
479-
const char *key, const char **dest);
480477
int repo_config_get_string(struct repository *repo,
481478
const char *key, char **dest);
482479
int repo_config_get_string_tmp(struct repository *repo,
@@ -532,16 +529,10 @@ void git_config_clear(void);
532529
* error message and returns -1. When the configuration variable `key` is
533530
* not found, returns 1 without touching `dest`.
534531
*/
535-
int git_config_get_string_const(const char *key, const char **dest);
536-
537-
/**
538-
* Similar to `git_config_get_string_const`, except that retrieved value
539-
* copied into the `dest` parameter is a mutable string.
540-
*/
541532
int git_config_get_string(const char *key, char **dest);
542533

543534
/**
544-
* Similar to `git_config_get_string_const`, but does not allocate any new
535+
* Similar to `git_config_get_string`, but does not allocate any new
545536
* memory; on success `dest` will point to memory owned by the config
546537
* machinery, which could be invalidated if it is discarded and reloaded.
547538
*/

environment.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ int repository_format_precious_objects;
3535
int repository_format_worktree_config;
3636
const char *git_commit_encoding;
3737
const char *git_log_output_encoding;
38-
const char *apply_default_whitespace;
39-
const char *apply_default_ignorewhitespace;
38+
char *apply_default_whitespace;
39+
char *apply_default_ignorewhitespace;
4040
const char *git_attributes_file;
4141
const char *git_hooks_path;
4242
int zlib_compression_level = Z_BEST_SPEED;

0 commit comments

Comments
 (0)