blob: 5eb6bdc3f691e8a71e07fc2be80ee94e3599f522 [file] [log] [blame]
Lukas Sandström9370bae2006-05-18 14:15:55 +02001/*
2 * GIT - The information manager from hell
3 */
4
Lukas Sandström9370bae2006-05-18 14:15:55 +02005#include "builtin.h"
Elijah Newrenbc5c5ec2023-05-16 06:33:57 +00006#include "refs.h"
Elijah Newrene38da482023-03-21 06:26:05 +00007#include "setup.h"
Junio C Hamanoa31dca02009-03-21 14:19:53 -07008#include "strbuf.h"
Lukas Sandström9370bae2006-05-18 14:15:55 +02009
Jonathan Nieder6586b1f2009-11-09 09:04:47 -060010static const char builtin_check_ref_format_usage[] =
Alex Henrie9c9b4f22015-01-13 00:44:47 -070011"git check-ref-format [--normalize] [<options>] <refname>\n"
Jonathan Nieder6586b1f2009-11-09 09:04:47 -060012" or: git check-ref-format --branch <branchname-shorthand>";
13
Jonathan Nieder1ba447b2009-10-12 00:33:01 -050014/*
Michael Haggerty7f748c72011-09-15 23:10:28 +020015 * Return a copy of refname but with leading slashes removed and runs
16 * of adjacent slashes replaced with single slashes.
Jonathan Nieder1ba447b2009-10-12 00:33:01 -050017 *
18 * This function is similar to normalize_path_copy(), but stripped down
19 * to meet check_ref_format's simpler needs.
20 */
Michael Haggerty7f748c72011-09-15 23:10:28 +020021static char *collapse_slashes(const char *refname)
Jonathan Nieder1ba447b2009-10-12 00:33:01 -050022{
Jeff King3733e692016-02-22 17:44:28 -050023 char *ret = xmallocz(strlen(refname));
Jonathan Nieder1ba447b2009-10-12 00:33:01 -050024 char ch;
Michael Haggerty2f633f42011-08-25 21:19:24 +020025 char prev = '/';
Michael Haggerty7f748c72011-09-15 23:10:28 +020026 char *cp = ret;
Jonathan Nieder1ba447b2009-10-12 00:33:01 -050027
Michael Haggerty7f748c72011-09-15 23:10:28 +020028 while ((ch = *refname++) != '\0') {
Jonathan Nieder1ba447b2009-10-12 00:33:01 -050029 if (prev == '/' && ch == prev)
30 continue;
31
Michael Haggerty7f748c72011-09-15 23:10:28 +020032 *cp++ = ch;
Jonathan Nieder1ba447b2009-10-12 00:33:01 -050033 prev = ch;
34 }
Michael Haggerty7f748c72011-09-15 23:10:28 +020035 *cp = '\0';
36 return ret;
Jonathan Nieder1ba447b2009-10-12 00:33:01 -050037}
38
Jonathan Niedercfbe22f2010-08-05 22:36:07 -050039static int check_ref_format_branch(const char *arg)
40{
41 struct strbuf sb = STRBUF_INIT;
Junio C Hamano7ccc94f2017-10-17 00:10:00 -070042 const char *name;
Jonathan Nieder49cc4602010-08-05 22:39:22 -050043 int nongit;
Jonathan Niedercfbe22f2010-08-05 22:36:07 -050044
Jonathan Nieder49cc4602010-08-05 22:39:22 -050045 setup_git_directory_gently(&nongit);
Junio C Hamano7ccc94f2017-10-17 00:10:00 -070046 if (strbuf_check_branch_ref(&sb, arg) ||
47 !skip_prefix(sb.buf, "refs/heads/", &name))
Jonathan Niedercfbe22f2010-08-05 22:36:07 -050048 die("'%s' is not a valid branch name", arg);
Junio C Hamano7ccc94f2017-10-17 00:10:00 -070049 printf("%s\n", name);
Rene Scharfe861e6552017-08-30 19:49:35 +020050 strbuf_release(&sb);
Jonathan Niedercfbe22f2010-08-05 22:36:07 -050051 return 0;
52}
53
Linus Torvaldsa633fca2006-07-28 22:44:25 -070054int cmd_check_ref_format(int argc, const char **argv, const char *prefix)
Lukas Sandström9370bae2006-05-18 14:15:55 +020055{
Michael Haggertye4ed6102011-09-15 23:10:23 +020056 int i;
Michael Haggertya40e6fb2011-09-15 23:10:30 +020057 int normalize = 0;
Michael Haggertye4ed6102011-09-15 23:10:23 +020058 int flags = 0;
Michael Haggertya5e4ec02011-09-15 23:10:29 +020059 const char *refname;
Ævar Arnfjörð Bjarmason99b6c452022-07-01 12:42:50 +020060 char *to_free = NULL;
61 int ret = 1;
Michael Haggertye4ed6102011-09-15 23:10:23 +020062
Jeff King79156912023-03-28 16:55:17 -040063 BUG_ON_NON_EMPTY_PREFIX(prefix);
64
Jonathan Niederaeda85a2009-11-09 09:04:48 -060065 if (argc == 2 && !strcmp(argv[1], "-h"))
66 usage(builtin_check_ref_format_usage);
67
Jonathan Niedercfbe22f2010-08-05 22:36:07 -050068 if (argc == 3 && !strcmp(argv[1], "--branch"))
69 return check_ref_format_branch(argv[2]);
Michael Haggertye4ed6102011-09-15 23:10:23 +020070
71 for (i = 1; i < argc && argv[i][0] == '-'; i++) {
Michael Haggertya40e6fb2011-09-15 23:10:30 +020072 if (!strcmp(argv[i], "--normalize") || !strcmp(argv[i], "--print"))
73 normalize = 1;
Michael Haggertye4ed6102011-09-15 23:10:23 +020074 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);
82 }
83 if (! (i == argc - 1))
Jonathan Nieder6586b1f2009-11-09 09:04:47 -060084 usage(builtin_check_ref_format_usage);
Michael Haggertye4ed6102011-09-15 23:10:23 +020085
Michael Haggertya5e4ec02011-09-15 23:10:29 +020086 refname = argv[i];
Michael Haggertya40e6fb2011-09-15 23:10:30 +020087 if (normalize)
Ævar Arnfjörð Bjarmason99b6c452022-07-01 12:42:50 +020088 refname = to_free = collapse_slashes(refname);
Michael Haggertya5e4ec02011-09-15 23:10:29 +020089 if (check_refname_format(refname, flags))
Ævar Arnfjörð Bjarmason99b6c452022-07-01 12:42:50 +020090 goto cleanup;
Michael Haggertya40e6fb2011-09-15 23:10:30 +020091 if (normalize)
Michael Haggertya5e4ec02011-09-15 23:10:29 +020092 printf("%s\n", refname);
Michael Haggertye4ed6102011-09-15 23:10:23 +020093
Ævar Arnfjörð Bjarmason99b6c452022-07-01 12:42:50 +020094 ret = 0;
95cleanup:
96 free(to_free);
97 return ret;
Lukas Sandström9370bae2006-05-18 14:15:55 +020098}