]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/check-ref-format.c
Merge branch 'nd/pretty-commit-log-message'
[thirdparty/git.git] / builtin / check-ref-format.c
CommitLineData
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 10static const char builtin_check_ref_format_usage[] =
a40e6fb6 11"git check-ref-format [--normalize] [options] <refname>\n"
6586b1f3
JN
12" or: git check-ref-format --branch <branchname-shorthand>";
13
1ba447b8 14/*
7f748c7c
MH
15 * Return a copy of refname but with leading slashes removed and runs
16 * of adjacent slashes replaced with single slashes.
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 */
7f748c7c 21static char *collapse_slashes(const char *refname)
1ba447b8 22{
7f748c7c 23 char *ret = xmalloc(strlen(refname) + 1);
1ba447b8 24 char ch;
2f633f41 25 char prev = '/';
7f748c7c 26 char *cp = ret;
1ba447b8 27
7f748c7c 28 while ((ch = *refname++) != '\0') {
1ba447b8
JN
29 if (prev == '/' && ch == prev)
30 continue;
31
7f748c7c 32 *cp++ = ch;
1ba447b8
JN
33 prev = ch;
34 }
7f748c7c
MH
35 *cp = '\0';
36 return ret;
1ba447b8
JN
37}
38
cfbe22f0
JN
39static int check_ref_format_branch(const char *arg)
40{
41 struct strbuf sb = STRBUF_INIT;
49cc460d 42 int nongit;
cfbe22f0 43
49cc460d 44 setup_git_directory_gently(&nongit);
cfbe22f0
JN
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);
48 return 0;
49}
50
a633fca0 51int cmd_check_ref_format(int argc, const char **argv, const char *prefix)
9370bae2 52{
e4ed6105 53 int i;
a40e6fb6 54 int normalize = 0;
e4ed6105 55 int flags = 0;
a5e4ec06 56 const char *refname;
e4ed6105 57
aeda85a8
JN
58 if (argc == 2 && !strcmp(argv[1], "-h"))
59 usage(builtin_check_ref_format_usage);
60
cfbe22f0
JN
61 if (argc == 3 && !strcmp(argv[1], "--branch"))
62 return check_ref_format_branch(argv[2]);
e4ed6105
MH
63
64 for (i = 1; i < argc && argv[i][0] == '-'; i++) {
a40e6fb6
MH
65 if (!strcmp(argv[i], "--normalize") || !strcmp(argv[i], "--print"))
66 normalize = 1;
e4ed6105
MH
67 else if (!strcmp(argv[i], "--allow-onelevel"))
68 flags |= REFNAME_ALLOW_ONELEVEL;
69 else if (!strcmp(argv[i], "--no-allow-onelevel"))
70 flags &= ~REFNAME_ALLOW_ONELEVEL;
71 else if (!strcmp(argv[i], "--refspec-pattern"))
72 flags |= REFNAME_REFSPEC_PATTERN;
73 else
74 usage(builtin_check_ref_format_usage);
75 }
76 if (! (i == argc - 1))
6586b1f3 77 usage(builtin_check_ref_format_usage);
e4ed6105 78
a5e4ec06 79 refname = argv[i];
a40e6fb6
MH
80 if (normalize)
81 refname = collapse_slashes(refname);
a5e4ec06 82 if (check_refname_format(refname, flags))
e4ed6105 83 return 1;
a40e6fb6 84 if (normalize)
a5e4ec06 85 printf("%s\n", refname);
e4ed6105
MH
86
87 return 0;
9370bae2 88}