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] [options] <refname>\n"
12" or: git check-ref-format --branch <branchname-shorthand>";
1314
/*
15* Remove leading slashes and replace each run of adjacent slashes in
16* src with a single slash, 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 = '/';
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;
39int nongit;
4041
setup_git_directory_gently(&nongit);
42if (strbuf_check_branch_ref(&sb, arg))
43die("'%s' is not a valid branch name", arg);
44printf("%s\n", sb.buf + 11);
45return 0;
46}
4748
static void refname_format_print(const char *arg)
49{
50char *refname = xmalloc(strlen(arg) + 1);
5152
collapse_slashes(refname, arg);
53printf("%s\n", refname);
54}
5556
int cmd_check_ref_format(int argc, const char **argv, const char *prefix)
57{
58int i;
59int print = 0;
60int flags = 0;
6162
if (argc == 2 && !strcmp(argv[1], "-h"))
63usage(builtin_check_ref_format_usage);
6465
if (argc == 3 && !strcmp(argv[1], "--branch"))
66return check_ref_format_branch(argv[2]);
6768
for (i = 1; i < argc && argv[i][0] == '-'; i++) {
69if (!strcmp(argv[i], "--print"))
70print = 1;
71else if (!strcmp(argv[i], "--allow-onelevel"))
72flags |= REFNAME_ALLOW_ONELEVEL;
73else if (!strcmp(argv[i], "--no-allow-onelevel"))
74flags &= ~REFNAME_ALLOW_ONELEVEL;
75else if (!strcmp(argv[i], "--refspec-pattern"))
76flags |= REFNAME_REFSPEC_PATTERN;
77else
78usage(builtin_check_ref_format_usage);
79}
80if (! (i == argc - 1))
81usage(builtin_check_ref_format_usage);
8283
if (check_refname_format(argv[i], flags))
84return 1;
8586
if (print)
87refname_format_print(argv[i]);
8889
return 0;
90}