Skip to content

Commit cfbe22f

Browse files
jrngitster
authored andcommitted
check-ref-format: handle subcommands in separate functions
The code for each subcommand should be easier to read and manipulate this way. Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 2429e8d commit cfbe22f

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

builtin/check-ref-format.c

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,36 @@ static void collapse_slashes(char *dst, const char *src)
3333
*dst = '\0';
3434
}
3535

36+
static int check_ref_format_branch(const char *arg)
37+
{
38+
struct strbuf sb = STRBUF_INIT;
39+
40+
if (strbuf_check_branch_ref(&sb, arg))
41+
die("'%s' is not a valid branch name", arg);
42+
printf("%s\n", sb.buf + 11);
43+
return 0;
44+
}
45+
46+
static int check_ref_format_print(const char *arg)
47+
{
48+
char *refname = xmalloc(strlen(arg) + 1);
49+
50+
if (check_ref_format(arg))
51+
return 1;
52+
collapse_slashes(refname, arg);
53+
printf("%s\n", refname);
54+
return 0;
55+
}
56+
3657
int cmd_check_ref_format(int argc, const char **argv, const char *prefix)
3758
{
3859
if (argc == 2 && !strcmp(argv[1], "-h"))
3960
usage(builtin_check_ref_format_usage);
4061

41-
if (argc == 3 && !strcmp(argv[1], "--branch")) {
42-
struct strbuf sb = STRBUF_INIT;
43-
44-
if (strbuf_check_branch_ref(&sb, argv[2]))
45-
die("'%s' is not a valid branch name", argv[2]);
46-
printf("%s\n", sb.buf + 11);
47-
exit(0);
48-
}
49-
if (argc == 3 && !strcmp(argv[1], "--print")) {
50-
char *refname = xmalloc(strlen(argv[2]) + 1);
51-
52-
if (check_ref_format(argv[2]))
53-
exit(1);
54-
collapse_slashes(refname, argv[2]);
55-
printf("%s\n", refname);
56-
exit(0);
57-
}
62+
if (argc == 3 && !strcmp(argv[1], "--branch"))
63+
return check_ref_format_branch(argv[2]);
64+
if (argc == 3 && !strcmp(argv[1], "--print"))
65+
return check_ref_format_print(argv[2]);
5866
if (argc != 2)
5967
usage(builtin_check_ref_format_usage);
6068
return !!check_ref_format(argv[1]);

0 commit comments

Comments
 (0)