2 * GIT - The information manager from hell
10 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>";
15 * Remove leading slashes and replace each run of adjacent slashes in
16 * src with a single slash, 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)
26 while ((ch = *src++) != '\0') {
27 if (prev == '/' && ch == prev)
36 static int check_ref_format_branch(const char *arg)
38 struct strbuf sb = STRBUF_INIT;
41 setup_git_directory_gently(&nongit);
42 if (strbuf_check_branch_ref(&sb, arg))
43 die("'%s' is not a valid branch name", arg);
44 printf("%s\n", sb.buf + 11);
48 static void refname_format_print(const char *arg)
50 char *refname = xmalloc(strlen(arg) + 1);
52 collapse_slashes(refname, arg);
53 printf("%s\n", refname);
56 int cmd_check_ref_format(int argc, const char **argv, const char *prefix)
62 if (argc == 2 && !strcmp(argv[1], "-h"))
63 usage(builtin_check_ref_format_usage);
65 if (argc == 3 && !strcmp(argv[1], "--branch"))
66 return check_ref_format_branch(argv[2]);
68 for (i = 1; i < argc && argv[i][0] == '-'; i++) {
69 if (!strcmp(argv[i], "--print"))
71 else if (!strcmp(argv[i], "--allow-onelevel"))
72 flags |= REFNAME_ALLOW_ONELEVEL;
73 else if (!strcmp(argv[i], "--no-allow-onelevel"))
74 flags &= ~REFNAME_ALLOW_ONELEVEL;
75 else if (!strcmp(argv[i], "--refspec-pattern"))
76 flags |= REFNAME_REFSPEC_PATTERN;
78 usage(builtin_check_ref_format_usage);
80 if (! (i == argc - 1))
81 usage(builtin_check_ref_format_usage);
83 if (check_refname_format(argv[i], flags))
87 refname_format_print(argv[i]);