Code

Merge branch 'tr/describe-advice'
[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 == 3 && !strcmp(argv[1], "--branch")) {
35                 struct strbuf sb = STRBUF_INIT;
37                 if (strbuf_check_branch_ref(&sb, argv[2]))
38                         die("'%s' is not a valid branch name", argv[2]);
39                 printf("%s\n", sb.buf + 11);
40                 exit(0);
41         }
42         if (argc == 3 && !strcmp(argv[1], "--print")) {
43                 char *refname = xmalloc(strlen(argv[2]) + 1);
45                 if (check_ref_format(argv[2]))
46                         exit(1);
47                 collapse_slashes(refname, argv[2]);
48                 printf("%s\n", refname);
49                 exit(0);
50         }
51         if (argc != 2)
52                 usage("git check-ref-format refname");
53         return !!check_ref_format(argv[1]);
54 }