config: fix constness of out parameter for `git_config_get_expiry()`

The type of the out parameter of `git_config_get_expiry()` is a pointer to a constant string, which creates the impression that ownership of the returned data wasn't transferred to the caller. This isn't true though and thus quite misleading. Adapt the parameter to be of type `char **` and adjust callers accordingly. While at it, refactor `get_shared_index_expire_date()` to drop the static `shared_index_expire` variable. It is only used in that function, and furthermore we would only hit the code where we parse the expiry date a single time because we already use a static `prepared` variable to track whether we did parse it. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Aug 16, 2024 at 12:44 UTC a70a9bf6eec832b808a805de318601684b19f18e
4 files changed +15 -9
builtin/gc.c
+3 -3
@@ -167,9 +167,9 @@ static void gc_config(void)
167 git_config_get_bool("gc.autodetach", &detach_auto);
168 git_config_get_bool("gc.cruftpacks", &cruft_packs);
169 git_config_get_ulong("gc.maxcruftsize", &max_cruft_size);
170 - git_config_get_expiry("gc.pruneexpire", &prune_expire);
171 - git_config_get_expiry("gc.worktreepruneexpire", &prune_worktrees_expire);
172 - git_config_get_expiry("gc.logexpiry", &gc_log_expire);
170 + git_config_get_expiry("gc.pruneexpire", (char **) &prune_expire);
171 + git_config_get_expiry("gc.worktreepruneexpire", (char **) &prune_worktrees_expire);
172 + git_config_get_expiry("gc.logexpiry", (char **) &gc_log_expire);
173
174 git_config_get_ulong("gc.bigpackthreshold", &big_pack_threshold);
175 git_config_get_ulong("pack.deltacachesize", &max_delta_cache_size);
config.c
+2 -2
@@ -2766,9 +2766,9 @@ int git_config_get_pathname(const char *key, char **dest)
2766 return repo_config_get_pathname(the_repository, key, dest);
2767 }
2768
2769 -int git_config_get_expiry(const char *key, const char **output)
2769 +int git_config_get_expiry(const char *key, char **output)
2770 {
2771 - int ret = git_config_get_string(key, (char **)output);
2771 + int ret = git_config_get_string(key, output);
2772 if (ret)
2773 return ret;
2774 if (strcmp(*output, "now")) {
config.h
+1 -1
@@ -701,7 +701,7 @@ int git_config_get_split_index(void);
701 int git_config_get_max_percent_split_change(void);
702
703 /* This dies if the configured or default date is in the future */
704 -int git_config_get_expiry(const char *key, const char **output);
704 +int git_config_get_expiry(const char *key, char **output);
705
706 /* parse either "this many days" integer, or "5.days.ago" approxidate */
707 int git_config_get_expiry_in_days(const char *key, timestamp_t *, timestamp_t now);
read-cache.c
+9 -3
@@ -3176,18 +3176,24 @@ static int write_split_index(struct index_state *istate,
3176 return ret;
3177 }
3178
3179 -static const char *shared_index_expire = "2.weeks.ago";
3180 -
3179 static unsigned long get_shared_index_expire_date(void)
3180 {
3181 static unsigned long shared_index_expire_date;
3182 static int shared_index_expire_date_prepared;
3183
3184 if (!shared_index_expire_date_prepared) {
3185 + const char *shared_index_expire = "2.weeks.ago";
3186 + char *value = NULL;
3187 +
3188 git_config_get_expiry("splitindex.sharedindexexpire",
3188 - &shared_index_expire);
3189 + &value);
3190 + if (value)
3191 + shared_index_expire = value;
3192 +
3193 shared_index_expire_date = approxidate(shared_index_expire);
3194 shared_index_expire_date_prepared = 1;
3195 +
3196 + free(value);
3197 }
3198
3199 return shared_index_expire_date;