]>
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 | 10 | static const char builtin_check_ref_format_usage[] = |
e4ed6105 | 11 | "git check-ref-format [--print] [options] <refname>\n" |
6586b1f3 JN |
12 | " or: git check-ref-format --branch <branchname-shorthand>"; |
13 | ||
1ba447b8 | 14 | /* |
2f633f41 MH |
15 | * Remove leading slashes and replace each run of adjacent slashes in |
16 | * src with a single slash, and write the result to dst. | |
1ba447b8 JN |
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; | |
2f633f41 | 24 | char prev = '/'; |
1ba447b8 JN |
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 | ||
e4ed6105 | 48 | static void refname_format_print(const char *arg) |
cfbe22f0 JN |
49 | { |
50 | char *refname = xmalloc(strlen(arg) + 1); | |
51 | ||
cfbe22f0 JN |
52 | collapse_slashes(refname, arg); |
53 | printf("%s\n", refname); | |
cfbe22f0 JN |
54 | } |
55 | ||
a633fca0 | 56 | int cmd_check_ref_format(int argc, const char **argv, const char *prefix) |
9370bae2 | 57 | { |
e4ed6105 MH |
58 | int i; |
59 | int print = 0; | |
60 | int flags = 0; | |
61 | ||
aeda85a8 JN |
62 | if (argc == 2 && !strcmp(argv[1], "-h")) |
63 | usage(builtin_check_ref_format_usage); | |
64 | ||
cfbe22f0 JN |
65 | if (argc == 3 && !strcmp(argv[1], "--branch")) |
66 | return check_ref_format_branch(argv[2]); | |
e4ed6105 MH |
67 | |
68 | for (i = 1; i < argc && argv[i][0] == '-'; i++) { | |
69 | if (!strcmp(argv[i], "--print")) | |
70 | print = 1; | |
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; | |
77 | else | |
78 | usage(builtin_check_ref_format_usage); | |
79 | } | |
80 | if (! (i == argc - 1)) | |
6586b1f3 | 81 | usage(builtin_check_ref_format_usage); |
e4ed6105 | 82 | |
8d9c5010 | 83 | if (check_refname_format(argv[i], flags)) |
e4ed6105 | 84 | return 1; |
e4ed6105 MH |
85 | |
86 | if (print) | |
87 | refname_format_print(argv[i]); | |
88 | ||
89 | return 0; | |
9370bae2 | 90 | } |