2 * GIT - The information manager from hell
11 static const char builtin_check_ref_format_usage
[] =
12 "git check-ref-format [--normalize] [<options>] <refname>\n"
13 " or: git check-ref-format --branch <branchname-shorthand>";
16 * Return a copy of refname but with leading slashes removed and runs
17 * of adjacent slashes replaced with single slashes.
19 * This function is similar to normalize_path_copy(), but stripped down
20 * to meet check_ref_format's simpler needs.
22 static char *collapse_slashes(const char *refname
)
24 char *ret
= xmallocz(strlen(refname
));
29 while ((ch
= *refname
++) != '\0') {
30 if (prev
== '/' && ch
== prev
)
40 static int check_ref_format_branch(const char *arg
)
42 struct strbuf sb
= STRBUF_INIT
;
46 setup_git_directory_gently(&nongit
);
47 if (strbuf_check_branch_ref(&sb
, arg
) ||
48 !skip_prefix(sb
.buf
, "refs/heads/", &name
))
49 die("'%s' is not a valid branch name", arg
);
55 int cmd_check_ref_format(int argc
, const char **argv
, const char *prefix
)
64 if (argc
== 2 && !strcmp(argv
[1], "-h"))
65 usage(builtin_check_ref_format_usage
);
67 if (argc
== 3 && !strcmp(argv
[1], "--branch"))
68 return check_ref_format_branch(argv
[2]);
70 for (i
= 1; i
< argc
&& argv
[i
][0] == '-'; i
++) {
71 if (!strcmp(argv
[i
], "--normalize") || !strcmp(argv
[i
], "--print"))
73 else if (!strcmp(argv
[i
], "--allow-onelevel"))
74 flags
|= REFNAME_ALLOW_ONELEVEL
;
75 else if (!strcmp(argv
[i
], "--no-allow-onelevel"))
76 flags
&= ~REFNAME_ALLOW_ONELEVEL
;
77 else if (!strcmp(argv
[i
], "--refspec-pattern"))
78 flags
|= REFNAME_REFSPEC_PATTERN
;
80 usage(builtin_check_ref_format_usage
);
82 if (! (i
== argc
- 1))
83 usage(builtin_check_ref_format_usage
);
87 refname
= to_free
= collapse_slashes(refname
);
88 if (check_refname_format(refname
, flags
))
91 printf("%s\n", refname
);