push: drop confusing configset/callback redundancy

We parse push config by calling git_config() with our git_push_config() callback. But inside that callback, when we see "push.gpgsign", we ignore the value passed into the callback and instead make a new call to git_config_get_value(). This is unnecessary at best, and slightly wrong at worst (if there are multiple instances, get_value() only returns one; both methods end up with last-one-wins, but we'd fail to report errors if earlier incarnations were bogus). The call was added by 68c757f219 (push: add a config option push.gpgSign for default signed pushes, 2015-08-19). That commit doesn't give any reason to deviate from the usual strategy here; it was probably just somebody unfamiliar with our config API and its conventions. It also added identical code to builtin/send-pack.c, which also handles push.gpgsign. And then the same issue spread to its neighbor in b33a15b081 (push: add recurseSubmodules config option, 2015-11-17), presumably via cargo-culting. This patch fixes all three to just directly use the value provided to the callback. While I was adjusting the code to do so, I noticed that push.gpgsign is overly careful about a NULL value. After git_parse_maybe_bool() has returned anything besides 1, we know that the value cannot be NULL (if it were, it would be an implicit "true", and many callers of maybe_bool rely on that). Here that lets us shorten "if (v && !strcasecmp(v, ...))" to just "if (!strcasecmp(v, ...))". Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Dec 7, 2023 at 02:26 UTC 37e8a341ea7719cd91d1b6172c171d2ffe2d6374
2 files changed +25 -33
builtin/push.c
+13 -18
@@ -526,26 +526,21 @@ static int git_push_config(const char *k, const char *v,
526 *flags |= TRANSPORT_PUSH_AUTO_UPSTREAM;
527 return 0;
528 } else if (!strcmp(k, "push.gpgsign")) {
529 - const char *value;
530 - if (!git_config_get_value("push.gpgsign", &value)) {
531 - switch (git_parse_maybe_bool(value)) {
532 - case 0:
533 - set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_NEVER);
534 - break;
535 - case 1:
536 - set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_ALWAYS);
537 - break;
538 - default:
539 - if (value && !strcasecmp(value, "if-asked"))
540 - set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_IF_ASKED);
541 - else
542 - return error(_("invalid value for '%s'"), k);
543 - }
529 + switch (git_parse_maybe_bool(v)) {
530 + case 0:
531 + set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_NEVER);
532 + break;
533 + case 1:
534 + set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_ALWAYS);
535 + break;
536 + default:
537 + if (!strcasecmp(v, "if-asked"))
538 + set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_IF_ASKED);
539 + else
540 + return error(_("invalid value for '%s'"), k);
541 }
542 } else if (!strcmp(k, "push.recursesubmodules")) {
546 - const char *value;
547 - if (!git_config_get_value("push.recursesubmodules", &value))
548 - recurse_submodules = parse_push_recurse_submodules_arg(k, value);
543 + recurse_submodules = parse_push_recurse_submodules_arg(k, v);
544 } else if (!strcmp(k, "submodule.recurse")) {
545 int val = git_config_bool(k, v) ?
546 RECURSE_SUBMODULES_ON_DEMAND : RECURSE_SUBMODULES_OFF;
builtin/send-pack.c
+12 -15
@@ -135,21 +135,18 @@ static int send_pack_config(const char *k, const char *v,
135 const struct config_context *ctx, void *cb)
136 {
137 if (!strcmp(k, "push.gpgsign")) {
138 - const char *value;
139 - if (!git_config_get_value("push.gpgsign", &value)) {
140 - switch (git_parse_maybe_bool(value)) {
141 - case 0:
142 - args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
143 - break;
144 - case 1:
145 - args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
146 - break;
147 - default:
148 - if (value && !strcasecmp(value, "if-asked"))
149 - args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
150 - else
151 - return error(_("invalid value for '%s'"), k);
152 - }
138 + switch (git_parse_maybe_bool(v)) {
139 + case 0:
140 + args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
141 + break;
142 + case 1:
143 + args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
144 + break;
145 + default:
146 + if (!strcasecmp(v, "if-asked"))
147 + args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
148 + else
149 + return error(_("invalid value for '%s'"), k);
150 }
151 }
152 return git_default_config(k, v, ctx, cb);