]>
Commit | Line | Data |
---|---|---|
9370bae2 LS |
1 | /* |
2 | * GIT - The information manager from hell | |
3 | */ | |
4 | ||
5 | #include "cache.h" | |
6 | #include "refs.h" | |
7 | #include "builtin.h" | |
a31dca03 | 8 | #include "strbuf.h" |
9370bae2 | 9 | |
6586b1f3 JN |
10 | 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>"; | |
13 | ||
1ba447b8 JN |
14 | /* |
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 | */ | |
21 | static void collapse_slashes(char *dst, const char *src) | |
22 | { | |
23 | char ch; | |
24 | char prev = '\0'; | |
25 | ||
26 | while ((ch = *src++) != '\0') { | |
27 | if (prev == '/' && ch == prev) | |
28 | continue; | |
29 | ||
30 | *dst++ = ch; | |
31 | prev = ch; | |
32 | } | |
33 | *dst = '\0'; | |
34 | } | |
35 | ||
cfbe22f0 JN |
36 | static int check_ref_format_branch(const char *arg) |
37 | { | |
38 | struct strbuf sb = STRBUF_INIT; | |
49cc460d | 39 | int nongit; |
cfbe22f0 | 40 | |
49cc460d | 41 | setup_git_directory_gently(&nongit); |
cfbe22f0 JN |
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); | |
45 | return 0; | |
46 | } | |
47 | ||
48 | static int check_ref_format_print(const char *arg) | |
49 | { | |
50 | char *refname = xmalloc(strlen(arg) + 1); | |
51 | ||
52 | if (check_ref_format(arg)) | |
53 | return 1; | |
54 | collapse_slashes(refname, arg); | |
55 | printf("%s\n", refname); | |
56 | return 0; | |
57 | } | |
58 | ||
a633fca0 | 59 | int cmd_check_ref_format(int argc, const char **argv, const char *prefix) |
9370bae2 | 60 | { |
aeda85a8 JN |
61 | if (argc == 2 && !strcmp(argv[1], "-h")) |
62 | usage(builtin_check_ref_format_usage); | |
63 | ||
cfbe22f0 JN |
64 | if (argc == 3 && !strcmp(argv[1], "--branch")) |
65 | return check_ref_format_branch(argv[2]); | |
66 | if (argc == 3 && !strcmp(argv[1], "--print")) | |
67 | return check_ref_format_print(argv[2]); | |
9370bae2 | 68 | if (argc != 2) |
6586b1f3 | 69 | usage(builtin_check_ref_format_usage); |
9370bae2 LS |
70 | return !!check_ref_format(argv[1]); |
71 | } |