Merge branch 'jk/maint-push-tracking-wo-remote' into maint
[tgit.git] / builtin-check-ref-format.c
blobb106c65d80dfc8fe794d46e34ea1ff78b04f3056
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 static const char builtin_check_ref_format_usage[] =
11 "git check-ref-format [--print] <refname>\n"
12 " or: git check-ref-format --branch <branchname-shorthand>";
15 * Replace each run of adjacent slashes in src with a single slash,
16 * and write the result to dst.
18 * This function is similar to normalize_path_copy(), but stripped down
19 * to meet check_ref_format's simpler needs.
21 static void collapse_slashes(char *dst, const char *src)
23 char ch;
24 char prev = '\0';
26 while ((ch = *src++) != '\0') {
27 if (prev == '/' && ch == prev)
28 continue;
30 *dst++ = ch;
31 prev = ch;
33 *dst = '\0';
36 int cmd_check_ref_format(int argc, const char **argv, const char *prefix)
38 if (argc == 2 && !strcmp(argv[1], "-h"))
39 usage(builtin_check_ref_format_usage);
41 if (argc == 3 && !strcmp(argv[1], "--branch")) {
42 struct strbuf sb = STRBUF_INIT;
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);
49 if (argc == 3 && !strcmp(argv[1], "--print")) {
50 char *refname = xmalloc(strlen(argv[2]) + 1);
52 if (check_ref_format(argv[2]))
53 exit(1);
54 collapse_slashes(refname, argv[2]);
55 printf("%s\n", refname);
56 exit(0);
58 if (argc != 2)
59 usage(builtin_check_ref_format_usage);
60 return !!check_ref_format(argv[1]);