1/*
2* GIT - The information manager from hell
3*/
45
#include "cache.h"
6#include "refs.h"
7#include "builtin.h"
8#include "strbuf.h"
910
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>";
1314
/*
15* Replace each run of adjacent slashes in src with a single slash,
16* and write the result to dst.
17*
18* This function is similar to normalize_path_copy(), but stripped down
19* to meet check_ref_format's simpler needs.
20*/
21static void collapse_slashes(char *dst, const char *src)
22{
23char ch;
24char prev = '\0';
2526
while ((ch = *src++) != '\0') {
27if (prev == '/' && ch == prev)
28continue;
2930
*dst++ = ch;
31prev = ch;
32}
33*dst = '\0';
34}
3536
static int check_ref_format_branch(const char *arg)
37{
38struct strbuf sb = STRBUF_INIT;
3940
if (strbuf_check_branch_ref(&sb, arg))
41die("'%s' is not a valid branch name", arg);
42printf("%s\n", sb.buf + 11);
43return 0;
44}
4546
static int check_ref_format_print(const char *arg)
47{
48char *refname = xmalloc(strlen(arg) + 1);
4950
if (check_ref_format(arg))
51return 1;
52collapse_slashes(refname, arg);
53printf("%s\n", refname);
54return 0;
55}
5657
int cmd_check_ref_format(int argc, const char **argv, const char *prefix)
58{
59if (argc == 2 && !strcmp(argv[1], "-h"))
60usage(builtin_check_ref_format_usage);
6162
if (argc == 3 && !strcmp(argv[1], "--branch"))
63return check_ref_format_branch(argv[2]);
64if (argc == 3 && !strcmp(argv[1], "--print"))
65return check_ref_format_print(argv[2]);
66if (argc != 2)
67usage(builtin_check_ref_format_usage);
68return !!check_ref_format(argv[1]);
69}