Code

0a576afd0e51f166f46493d86f4a0914ed8a744b
[git.git] / builtin-check-ref-format.c
1 /*
2  * GIT - The information manager from hell
3  */
5 #include "cache.h"
6 #include "refs.h"
7 #include "builtin.h"
8 #include "strbuf.h"
10 /*
11  * Replace each run of adjacent slashes in src with a single slash,
12  * and write the result to dst.
13  *
14  * This function is similar to normalize_path_copy(), but stripped down
15  * to meet check_ref_format's simpler needs.
16  */
17 static void collapse_slashes(char *dst, const char *src)
18 {
19         char ch;
20         char prev = '\0';
22         while ((ch = *src++) != '\0') {
23                 if (prev == '/' && ch == prev)
24                         continue;
26                 *dst++ = ch;
27                 prev = ch;
28         }
29         *dst = '\0';
30 }
32 int cmd_check_ref_format(int argc, const char **argv, const char *prefix)
33 {
34         if (argc == 2 && !strcmp(argv[1], "-h"))
35                 usage(builtin_check_ref_format_usage);
37         if (argc == 3 && !strcmp(argv[1], "--branch")) {
38                 struct strbuf sb = STRBUF_INIT;
40                 if (strbuf_check_branch_ref(&sb, argv[2]))
41                         die("'%s' is not a valid branch name", argv[2]);
42                 printf("%s\n", sb.buf + 11);
43                 exit(0);
44         }
45         if (argc == 3 && !strcmp(argv[1], "--print")) {
46                 char *refname = xmalloc(strlen(argv[2]) + 1);
48                 if (check_ref_format(argv[2]))
49                         exit(1);
50                 collapse_slashes(refname, argv[2]);
51                 printf("%s\n", refname);
52                 exit(0);
53         }
54         if (argc != 2)
55                 usage("git check-ref-format refname");
56         return !!check_ref_format(argv[1]);
57 }