Merge branch 'gj/push-more-verbose-advice'
[git/mingw.git] / builtin / check-ref-format.c
blob28a7320271a9555356170bfdb06ffb4b07bc9b92
1 /*
2 * GIT - The information manager from hell
3 */
5 #include "cache.h"
6 #include "refs.h"
7 #include "builtin.h"
8 #include "strbuf.h"
10 static const char builtin_check_ref_format_usage[] =
11 "git check-ref-format [--normalize] [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);
24 char ch;
25 char prev = '/';
26 char *cp = ret;
28 while ((ch = *refname++) != '\0') {
29 if (prev == '/' && ch == prev)
30 continue;
32 *cp++ = ch;
33 prev = ch;
35 *cp = '\0';
36 return ret;
39 static int check_ref_format_branch(const char *arg)
41 struct strbuf sb = STRBUF_INIT;
42 int nongit;
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);
48 return 0;
51 int cmd_check_ref_format(int argc, const char **argv, const char *prefix)
53 int i;
54 int normalize = 0;
55 int flags = 0;
56 const char *refname;
58 if (argc == 2 && !strcmp(argv[1], "-h"))
59 usage(builtin_check_ref_format_usage);
61 if (argc == 3 && !strcmp(argv[1], "--branch"))
62 return check_ref_format_branch(argv[2]);
64 for (i = 1; i < argc && argv[i][0] == '-'; i++) {
65 if (!strcmp(argv[i], "--normalize") || !strcmp(argv[i], "--print"))
66 normalize = 1;
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);
76 if (! (i == argc - 1))
77 usage(builtin_check_ref_format_usage);
79 refname = argv[i];
80 if (normalize)
81 refname = collapse_slashes(refname);
82 if (check_refname_format(refname, flags))
83 return 1;
84 if (normalize)
85 printf("%s\n", refname);
87 return 0;