git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 1/2] remote: bail early from set_head() if missing remote name

From
Denton Liu <liu.denton@gmail.com>
Date
Aug 5, 2025, 05:31 UTC
Message-ID
<458ec588b7fe4d40f834d535b8f7d83684f9935b.1754371650.git.liu.denton@gmail.com>
In-Reply-To
<cover.1754371649.git.liu.denton@gmail.com>
From: Jeff King <peff@peff.net>

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 <liu.denton@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Tested-by: Denton Liu <liu.denton@gmail.com>
Signed-off-by: Denton Liu <liu.denton@gmail.com>
---
Thanks Peff for writing this patch up
 builtin/remote.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
diff --git a/builtin/remote.c b/builtin/remote.c
index 5dd6cbbaee..f0e49a5681 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -1474,10 +1474,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]);
-- 
2.50.1
Previous: Denton LiuNext: Denton Liu
Message 6 of 10 in “fix -Wmaybe-uninitialized with -Og”
  1. fix -Wmaybe-uninitialized with -OgDenton Liu, Aug 4, 2025
  2. Jeff KingAug 4, 2025
  3. Junio C HamanoAug 4, 2025
  4. Jeff KingAug 4, 2025
  5. 0/2 fix -Wmaybe-uninitialized with -OgDenton Liu, Aug 5, 2025
  6. 1/2 remote: bail early from set_head() if missing remote nameDenton Liu, Aug 5, 2025
  7. 2/2 t/unit-tests/clar: fix -Wmaybe-uninitialized with -OgDenton Liu, Aug 5, 2025
  8. Patrick SteinhardtAug 8, 2025
  9. Denton LiuAug 8, 2025
  10. Patrick SteinhardtAug 11, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.