Skip to content

Commit eb883b0

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Jeff King <[email protected]> Tested-by: Denton Liu <[email protected]> Signed-off-by: Denton Liu <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f368df4 commit eb883b0

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

builtin/remote.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,10 +1457,13 @@ static int set_head(int argc, const char **argv, const char *prefix,
14571457
};
14581458
argc = parse_options(argc, argv, prefix, options,
14591459
builtin_remote_sethead_usage, 0);
1460-
if (argc) {
1461-
strbuf_addf(&b_head, "refs/remotes/%s/HEAD", argv[0]);
1462-
remote = remote_get(argv[0]);
1463-
}
1460+
1461+
/* All modes require at least a remote name. */
1462+
if (!argc)
1463+
usage_with_options(builtin_remote_sethead_usage, options);
1464+
1465+
strbuf_addf(&b_head, "refs/remotes/%s/HEAD", argv[0]);
1466+
remote = remote_get(argv[0]);
14641467

14651468
if (!opt_a && !opt_d && argc == 2) {
14661469
head_name = xstrdup(argv[1]);

0 commit comments

Comments
 (0)