add: use advise_if_enabled for ADVICE_ADD_EMBEDDED_REPO
[alt-git.git] / builtin / check-ref-format.c
blob5eb6bdc3f691e8a71e07fc2be80ee94e3599f522
1 /*
2 * GIT - The information manager from hell
3 */
5 #include "builtin.h"
6 #include "refs.h"
7 #include "setup.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 = xmallocz(strlen(refname));
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 const char *name;
43 int nongit;
45 setup_git_directory_gently(&nongit);
46 if (strbuf_check_branch_ref(&sb, arg) ||
47 !skip_prefix(sb.buf, "refs/heads/", &name))
48 die("'%s' is not a valid branch name", arg);
49 printf("%s\n", name);
50 strbuf_release(&sb);
51 return 0;
54 int cmd_check_ref_format(int argc, const char **argv, const char *prefix)
56 int i;
57 int normalize = 0;
58 int flags = 0;
59 const char *refname;
60 char *to_free = NULL;
61 int ret = 1;
63 BUG_ON_NON_EMPTY_PREFIX(prefix);
65 if (argc == 2 && !strcmp(argv[1], "-h"))
66 usage(builtin_check_ref_format_usage);
68 if (argc == 3 && !strcmp(argv[1], "--branch"))
69 return check_ref_format_branch(argv[2]);
71 for (i = 1; i < argc && argv[i][0] == '-'; i++) {
72 if (!strcmp(argv[i], "--normalize") || !strcmp(argv[i], "--print"))
73 normalize = 1;
74 else if (!strcmp(argv[i], "--allow-onelevel"))
75 flags |= REFNAME_ALLOW_ONELEVEL;
76 else if (!strcmp(argv[i], "--no-allow-onelevel"))
77 flags &= ~REFNAME_ALLOW_ONELEVEL;
78 else if (!strcmp(argv[i], "--refspec-pattern"))
79 flags |= REFNAME_REFSPEC_PATTERN;
80 else
81 usage(builtin_check_ref_format_usage);
83 if (! (i == argc - 1))
84 usage(builtin_check_ref_format_usage);
86 refname = argv[i];
87 if (normalize)
88 refname = to_free = collapse_slashes(refname);
89 if (check_refname_format(refname, flags))
90 goto cleanup;
91 if (normalize)
92 printf("%s\n", refname);
94 ret = 0;
95 cleanup:
96 free(to_free);
97 return ret;