@matheusbsbs / matheus-bagliano / commits / cfbe22f03f
Fork 0

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>

Jonathan Nieder committed Aug 5, 2010 at 22:36 UTC cfbe22f03f9be1f3bbbd33240dcb127099d4346e
1 file changed +25 -17
builtin/check-ref-format.c
+25 -17
@@ -33,28 +33,36 @@ static void collapse_slashes(char *dst, const char *src)
33 *dst = '\0';
34 }
35
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 +
57 int cmd_check_ref_format(int argc, const char **argv, const char *prefix)
58 {
59 if (argc == 2 && !strcmp(argv[1], "-h"))
60 usage(builtin_check_ref_format_usage);
61
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]);
66 if (argc != 2)
67 usage(builtin_check_ref_format_usage);
68 return !!check_ref_format(argv[1]);