From: Jeff King Date: Tue, 5 Aug 2025 05:31:13 +0000 (-0700) Subject: remote: bail early from set_head() if missing remote name X-Git-Tag: v2.51.0-rc1~3^2~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=eb883b05da4489cef3fe7961a61faaa7533a9541;p=thirdparty%2Fgit.git remote: bail early from set_head() if missing remote name In "git remote set-head", we can take varying numbers of arguments depending on whether we saw the "-d" or "-a" options. But the first argument is always the remote name. The current code is somewhat awkward in that it conditionally handles the remote name up-front like this: if (argc) remote = ...from argv[0]... and then only later decides to bail if we do not have the right number of arguments for the options we saw. This makes it hard to figure out if "remote" is always set when it needs to be. Both for humans, but also for compilers; with -Og, gcc complains that "remote" can be accessed without being initialized (although this is not true, as we'd always die with a usage message in that case). Let's instead enforce the presence of the remote argument up front, which fixes the compiler warning and is easier to understand. It does mean duplicating the code to print a usage message, but it's a single line. Noticed-by: Denton Liu Signed-off-by: Jeff King Tested-by: Denton Liu Signed-off-by: Denton Liu Signed-off-by: Junio C Hamano --- diff --git a/builtin/remote.c b/builtin/remote.c index 0d6755bcb7..b6808bb28b 100644 --- a/builtin/remote.c +++ b/builtin/remote.c @@ -1457,10 +1457,13 @@ static int set_head(int argc, const char **argv, const char *prefix, }; argc = parse_options(argc, argv, prefix, options, builtin_remote_sethead_usage, 0); - if (argc) { - strbuf_addf(&b_head, "refs/remotes/%s/HEAD", argv[0]); - remote = remote_get(argv[0]); - } + + /* All modes require at least a remote name. */ + if (!argc) + usage_with_options(builtin_remote_sethead_usage, options); + + strbuf_addf(&b_head, "refs/remotes/%s/HEAD", argv[0]); + remote = remote_get(argv[0]); if (!opt_a && !opt_d && argc == 2) { head_name = xstrdup(argv[1]);