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 * Return a copy of refname but with leading slashes removed and runs
16 * of adjacent slashes replaced with single slashes.
18 * This function is similar to normalize_path_copy(), but stripped down
19 * to meet check_ref_format's simpler needs.
21 static char *collapse_slashes(const char *refname)
23 char *ret = xmalloc(strlen(refname) + 1);
28 while ((ch = *refname++) != '\0') {
29 if (prev == '/' && ch == prev)
39 static int check_ref_format_branch(const char *arg)
41 struct strbuf sb = STRBUF_INIT;
44 setup_git_directory_gently(&nongit);
45 if (strbuf_check_branch_ref(&sb, arg))
46 die("'%s' is not a valid branch name", arg);
47 printf("%s\n", sb.buf + 11);
51 static void refname_format_print(const char *arg)
53 char *refname = collapse_slashes(arg);
54 printf("%s\n", refname);
57 int cmd_check_ref_format(int argc, const char **argv, const char *prefix)
63 if (argc == 2 && !strcmp(argv[1], "-h"))
64 usage(builtin_check_ref_format_usage);
66 if (argc == 3 && !strcmp(argv[1], "--branch"))
67 return check_ref_format_branch(argv[2]);
69 for (i = 1; i < argc && argv[i][0] == '-'; i++) {
70 if (!strcmp(argv[i], "--print"))
72 else if (!strcmp(argv[i], "--allow-onelevel"))
73 flags |= REFNAME_ALLOW_ONELEVEL;
74 else if (!strcmp(argv[i], "--no-allow-onelevel"))
75 flags &= ~REFNAME_ALLOW_ONELEVEL;
76 else if (!strcmp(argv[i], "--refspec-pattern"))
77 flags |= REFNAME_REFSPEC_PATTERN;
79 usage(builtin_check_ref_format_usage);
81 if (! (i == argc - 1))
82 usage(builtin_check_ref_format_usage);
84 if (check_refname_format(argv[i], flags))
88 refname_format_print(argv[i]);