Lukas Sandström | 9370bae | 2006-05-18 14:15:55 +0200 | [diff] [blame] | 1 | /* |
| 2 | * GIT - The information manager from hell |
| 3 | */ |
| 4 | |
Lukas Sandström | 9370bae | 2006-05-18 14:15:55 +0200 | [diff] [blame] | 5 | #include "builtin.h" |
Elijah Newren | bc5c5ec | 2023-05-16 06:33:57 +0000 | [diff] [blame] | 6 | #include "refs.h" |
Elijah Newren | e38da48 | 2023-03-21 06:26:05 +0000 | [diff] [blame] | 7 | #include "setup.h" |
Junio C Hamano | a31dca0 | 2009-03-21 14:19:53 -0700 | [diff] [blame] | 8 | #include "strbuf.h" |
Lukas Sandström | 9370bae | 2006-05-18 14:15:55 +0200 | [diff] [blame] | 9 | |
Jonathan Nieder | 6586b1f | 2009-11-09 09:04:47 -0600 | [diff] [blame] | 10 | static const char builtin_check_ref_format_usage[] = |
Alex Henrie | 9c9b4f2 | 2015-01-13 00:44:47 -0700 | [diff] [blame] | 11 | "git check-ref-format [--normalize] [<options>] <refname>\n" |
Jonathan Nieder | 6586b1f | 2009-11-09 09:04:47 -0600 | [diff] [blame] | 12 | " or: git check-ref-format --branch <branchname-shorthand>"; |
| 13 | |
Jonathan Nieder | 1ba447b | 2009-10-12 00:33:01 -0500 | [diff] [blame] | 14 | /* |
Michael Haggerty | 7f748c7 | 2011-09-15 23:10:28 +0200 | [diff] [blame] | 15 | * Return a copy of refname but with leading slashes removed and runs |
| 16 | * of adjacent slashes replaced with single slashes. |
Jonathan Nieder | 1ba447b | 2009-10-12 00:33:01 -0500 | [diff] [blame] | 17 | * |
| 18 | * This function is similar to normalize_path_copy(), but stripped down |
| 19 | * to meet check_ref_format's simpler needs. |
| 20 | */ |
Michael Haggerty | 7f748c7 | 2011-09-15 23:10:28 +0200 | [diff] [blame] | 21 | static char *collapse_slashes(const char *refname) |
Jonathan Nieder | 1ba447b | 2009-10-12 00:33:01 -0500 | [diff] [blame] | 22 | { |
Jeff King | 3733e69 | 2016-02-22 17:44:28 -0500 | [diff] [blame] | 23 | char *ret = xmallocz(strlen(refname)); |
Jonathan Nieder | 1ba447b | 2009-10-12 00:33:01 -0500 | [diff] [blame] | 24 | char ch; |
Michael Haggerty | 2f633f4 | 2011-08-25 21:19:24 +0200 | [diff] [blame] | 25 | char prev = '/'; |
Michael Haggerty | 7f748c7 | 2011-09-15 23:10:28 +0200 | [diff] [blame] | 26 | char *cp = ret; |
Jonathan Nieder | 1ba447b | 2009-10-12 00:33:01 -0500 | [diff] [blame] | 27 | |
Michael Haggerty | 7f748c7 | 2011-09-15 23:10:28 +0200 | [diff] [blame] | 28 | while ((ch = *refname++) != '\0') { |
Jonathan Nieder | 1ba447b | 2009-10-12 00:33:01 -0500 | [diff] [blame] | 29 | if (prev == '/' && ch == prev) |
| 30 | continue; |
| 31 | |
Michael Haggerty | 7f748c7 | 2011-09-15 23:10:28 +0200 | [diff] [blame] | 32 | *cp++ = ch; |
Jonathan Nieder | 1ba447b | 2009-10-12 00:33:01 -0500 | [diff] [blame] | 33 | prev = ch; |
| 34 | } |
Michael Haggerty | 7f748c7 | 2011-09-15 23:10:28 +0200 | [diff] [blame] | 35 | *cp = '\0'; |
| 36 | return ret; |
Jonathan Nieder | 1ba447b | 2009-10-12 00:33:01 -0500 | [diff] [blame] | 37 | } |
| 38 | |
Jonathan Nieder | cfbe22f | 2010-08-05 22:36:07 -0500 | [diff] [blame] | 39 | static int check_ref_format_branch(const char *arg) |
| 40 | { |
| 41 | struct strbuf sb = STRBUF_INIT; |
Junio C Hamano | 7ccc94f | 2017-10-17 00:10:00 -0700 | [diff] [blame] | 42 | const char *name; |
Jonathan Nieder | 49cc460 | 2010-08-05 22:39:22 -0500 | [diff] [blame] | 43 | int nongit; |
Jonathan Nieder | cfbe22f | 2010-08-05 22:36:07 -0500 | [diff] [blame] | 44 | |
Jonathan Nieder | 49cc460 | 2010-08-05 22:39:22 -0500 | [diff] [blame] | 45 | setup_git_directory_gently(&nongit); |
Junio C Hamano | 7ccc94f | 2017-10-17 00:10:00 -0700 | [diff] [blame] | 46 | if (strbuf_check_branch_ref(&sb, arg) || |
| 47 | !skip_prefix(sb.buf, "refs/heads/", &name)) |
Jonathan Nieder | cfbe22f | 2010-08-05 22:36:07 -0500 | [diff] [blame] | 48 | die("'%s' is not a valid branch name", arg); |
Junio C Hamano | 7ccc94f | 2017-10-17 00:10:00 -0700 | [diff] [blame] | 49 | printf("%s\n", name); |
Rene Scharfe | 861e655 | 2017-08-30 19:49:35 +0200 | [diff] [blame] | 50 | strbuf_release(&sb); |
Jonathan Nieder | cfbe22f | 2010-08-05 22:36:07 -0500 | [diff] [blame] | 51 | return 0; |
| 52 | } |
| 53 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 54 | int cmd_check_ref_format(int argc, const char **argv, const char *prefix) |
Lukas Sandström | 9370bae | 2006-05-18 14:15:55 +0200 | [diff] [blame] | 55 | { |
Michael Haggerty | e4ed610 | 2011-09-15 23:10:23 +0200 | [diff] [blame] | 56 | int i; |
Michael Haggerty | a40e6fb | 2011-09-15 23:10:30 +0200 | [diff] [blame] | 57 | int normalize = 0; |
Michael Haggerty | e4ed610 | 2011-09-15 23:10:23 +0200 | [diff] [blame] | 58 | int flags = 0; |
Michael Haggerty | a5e4ec0 | 2011-09-15 23:10:29 +0200 | [diff] [blame] | 59 | const char *refname; |
Ævar Arnfjörð Bjarmason | 99b6c45 | 2022-07-01 12:42:50 +0200 | [diff] [blame] | 60 | char *to_free = NULL; |
| 61 | int ret = 1; |
Michael Haggerty | e4ed610 | 2011-09-15 23:10:23 +0200 | [diff] [blame] | 62 | |
Jeff King | 7915691 | 2023-03-28 16:55:17 -0400 | [diff] [blame] | 63 | BUG_ON_NON_EMPTY_PREFIX(prefix); |
| 64 | |
Jonathan Nieder | aeda85a | 2009-11-09 09:04:48 -0600 | [diff] [blame] | 65 | if (argc == 2 && !strcmp(argv[1], "-h")) |
| 66 | usage(builtin_check_ref_format_usage); |
| 67 | |
Jonathan Nieder | cfbe22f | 2010-08-05 22:36:07 -0500 | [diff] [blame] | 68 | if (argc == 3 && !strcmp(argv[1], "--branch")) |
| 69 | return check_ref_format_branch(argv[2]); |
Michael Haggerty | e4ed610 | 2011-09-15 23:10:23 +0200 | [diff] [blame] | 70 | |
| 71 | for (i = 1; i < argc && argv[i][0] == '-'; i++) { |
Michael Haggerty | a40e6fb | 2011-09-15 23:10:30 +0200 | [diff] [blame] | 72 | if (!strcmp(argv[i], "--normalize") || !strcmp(argv[i], "--print")) |
| 73 | normalize = 1; |
Michael Haggerty | e4ed610 | 2011-09-15 23:10:23 +0200 | [diff] [blame] | 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); |
| 82 | } |
| 83 | if (! (i == argc - 1)) |
Jonathan Nieder | 6586b1f | 2009-11-09 09:04:47 -0600 | [diff] [blame] | 84 | usage(builtin_check_ref_format_usage); |
Michael Haggerty | e4ed610 | 2011-09-15 23:10:23 +0200 | [diff] [blame] | 85 | |
Michael Haggerty | a5e4ec0 | 2011-09-15 23:10:29 +0200 | [diff] [blame] | 86 | refname = argv[i]; |
Michael Haggerty | a40e6fb | 2011-09-15 23:10:30 +0200 | [diff] [blame] | 87 | if (normalize) |
Ævar Arnfjörð Bjarmason | 99b6c45 | 2022-07-01 12:42:50 +0200 | [diff] [blame] | 88 | refname = to_free = collapse_slashes(refname); |
Michael Haggerty | a5e4ec0 | 2011-09-15 23:10:29 +0200 | [diff] [blame] | 89 | if (check_refname_format(refname, flags)) |
Ævar Arnfjörð Bjarmason | 99b6c45 | 2022-07-01 12:42:50 +0200 | [diff] [blame] | 90 | goto cleanup; |
Michael Haggerty | a40e6fb | 2011-09-15 23:10:30 +0200 | [diff] [blame] | 91 | if (normalize) |
Michael Haggerty | a5e4ec0 | 2011-09-15 23:10:29 +0200 | [diff] [blame] | 92 | printf("%s\n", refname); |
Michael Haggerty | e4ed610 | 2011-09-15 23:10:23 +0200 | [diff] [blame] | 93 | |
Ævar Arnfjörð Bjarmason | 99b6c45 | 2022-07-01 12:42:50 +0200 | [diff] [blame] | 94 | ret = 0; |
| 95 | cleanup: |
| 96 | free(to_free); |
| 97 | return ret; |
Lukas Sandström | 9370bae | 2006-05-18 14:15:55 +0200 | [diff] [blame] | 98 | } |