分支名称中哪些字符是非法的?

发布于 2024-09-18 01:27:53 字数 21 浏览 3 评论 0原文

分支名称中哪些字符是非法的?

Which characters are illegal within a branch name?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

将军与妓 2024-09-25 01:28:05

另外,如果您考虑使用美元符号 $ 字符,则必须小心。

gitbranch pew$ign 将创建 pew

为了创建一个包含 $ 的分支,整个名称应该用 引号 括起来,使其成为字符串文字:gitbranch 'pew$ign'。理想情况下,您应该避免使用任何符号。

As an addition, care must be taken if you consider to use the dollar sign $ character.

git branch pew$ign will create pew.

In order to create a branch that has $ within it the whole name should be wrapped in quotes that make it a string literal: git branch 'pew$ign'. Ideally you should avoid to use the symbol whatsoever.

我为君王 2024-09-25 01:28:03

要完成 Manoj Govindan 的主要答案:

  • @ 是有效的分支名称(在 git 版本 git version 2.24.3 (Apple Git-128) 上)
  • HEAD 不是有效的分支名称(显然是队长!)
  • 长度有限(由操作系统决定,在 Mac OS 10.15.7 上,最多 250 个字符,ascii 或非 ascii)

To complete the main answer by Manoj Govindan :

  • @ is a valid branch name (on git version git version 2.24.3 (Apple Git-128))
  • HEAD is not a valid branch name (by captain obvious!)
  • length is limited (by the OS, on Mac OS 10.15.7, 250 characters is the maximum, either ascii or not-ascii)
夏有森光若流苏 2024-09-25 01:28:01

接受的答案和 手册页 已经解释了哪些规则适用于 Git 分支名称。

在Git源代码中,通过 refname_disposition 数组来确定如何处理refnames中的各种字符。
数组中的索引对应于 ASCII 代码,并且值指示如何处理 ASCII 字符。

/*
 * How to handle various characters in refnames:
 * 0: An acceptable character for refs
 * 1: End-of-component
 * 2: ., look for a preceding . to reject .. in refs
 * 3: {, look for a preceding @ to reject @{ in refs
 * 4: A bad character: ASCII control characters, and
 *    ":", "?", "[", "\", "^", "~", SP, or TAB
 * 5: *, reject unless REFNAME_REFSPEC_PATTERN is set
 */
static unsigned char refname_disposition[256] = {
    1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
    4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
    4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 2, 1,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 4,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 4, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 4, 4
};

由于 4 表示分支名称中不允许使用相应的 ASCII 字符,因此有 39 个不允许的字符。
不允许的字符是 ASCII 控制字符(ASCII 代码 < 32)、可打印字符 : ? [ \ ^ ~ 和空格/制表符。

另外 3 个字符需要满足一些条件(请参阅文档注释):

  • .:禁止使用两个连续的点。
  • {:禁止使用子字符串@{
  • *:除非设置 REFNAME_REFSPEC_PATTERN,否则拒绝。

空字节终止分支名称,并且 / 为分支创建新的目录层次结构。
因此,分支名称不能以斜杠结尾。
例如git checkout -b 'a/b/c'会在.git/refs/heads下创建相应的目录结构,

注意UTF-8 字符可以在分支名称中使用:

$ git checkout -b 
\xCE\xA9'
Switched to a new branch 'Ω'

The accepted answer and the man page already explain which rules apply to Git branch names.

In the Git source code, the refname_disposition array is used to determine how to handle various characters in refnames.
The indexes in the array correspond to ASCII codes and the values indicate how the ASCII characters are handled.

/*
 * How to handle various characters in refnames:
 * 0: An acceptable character for refs
 * 1: End-of-component
 * 2: ., look for a preceding . to reject .. in refs
 * 3: {, look for a preceding @ to reject @{ in refs
 * 4: A bad character: ASCII control characters, and
 *    ":", "?", "[", "\", "^", "~", SP, or TAB
 * 5: *, reject unless REFNAME_REFSPEC_PATTERN is set
 */
static unsigned char refname_disposition[256] = {
    1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
    4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
    4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 2, 1,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 4,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 4, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 4, 4
};

Since 4 means that the corresponding ASCII character is not allowed inside branch names, there are 39 disallowed characters.
The disallowed characters are ASCII control characters (ASCII codes < 32), the printable characters : ? [ \ ^ ~ and the whitespace/tab character.

3 more characters require some conditions to be met (see doc comment):

  • .: Two subsequent dots are forbidden.
  • {: The substring @{ is forbidden.
  • *: Reject unless REFNAME_REFSPEC_PATTERN is set.

The null byte terminates the branch name and / creates a new directory hierarchy for the branch.
Therefore, branch names cannot end with a slash.
For example git checkout -b 'a/b/c' will create the corresponding directory structure under .git/refs/heads

Note that UTF-8 characters can be used in branch names:

$ git checkout -b 
\xCE\xA9'
Switched to a new branch 'Ω'
拥抱我好吗 2024-09-25 01:27:59

refname的命名规则:

Git 对引用的命名方式施加了以下规则:

  1. 它们可以包含用于分层(目录)分组的斜杠 /,但斜杠分隔的组件不能以点 . 开头或以序列 <代码>.lock.

  2. 它们必须至少包含一个 /。这强制存在诸如 heads/tags/ 等类别,但实际名称不受限制。如果使用 --allow-onelevel 选项,则放弃此规则。

  3. 任何地方都不能有两个连续的点 ..

  4. 它们不能有 ASCII 控制字符(即值低于 \040\177 DEL 的字节),空格、波形符 ~、脱字号 ^ 或冒号 : 任意位置。

  5. 任何地方都不能有问号 ?、星号 * 或开括号 [。有关此规则的例外情况,请参阅下面的 --refspec-pattern 选项。

  6. 它们不能以斜杠 / 开头或结尾,也不能包含多个连续的斜杠(有关此规则的例外情况,请参阅下面的 --normalize 选项)< /p>

  7. 它们不能以点 结尾。

  8. 它们不能包含序列 @{

  9. 它们不能是单个字符@

  10. 它们不能包含 \

最重要的是,分支名称的附加规则:

  1. 不能以短划线开头 -

感谢 Jakub Narębskigit check-ref-format 的“https://git-scm.com/docs/git-check-ref-format” rel="noreferrer">手册页 有更多详细信息。

Naming rules for refname:

Git imposes the following rules on how references are named:

  1. They can include slash / for hierarchical (directory) grouping, but no slash-separated component can begin with a dot . or end with the sequence .lock.

  2. They must contain at least one /. This enforces the presence of a category like heads/, tags/ etc. but the actual names are not restricted. If the --allow-onelevel option is used, this rule is waived.

  3. They cannot have two consecutive dots .. anywhere.

  4. They cannot have ASCII control characters (i.e. bytes whose values are lower than \040, or \177 DEL), space, tilde ~, caret ^, or colon : anywhere.

  5. They cannot have question-mark ?, asterisk *, or open bracket [ anywhere. See the --refspec-pattern option below for an exception to this rule.

  6. They cannot begin or end with a slash / or contain multiple consecutive slashes (see the --normalize option below for an exception to this rule)

  7. They cannot end with a dot .

  8. They cannot contain a sequence @{.

  9. They cannot be the single character @.

  10. They cannot contain a \.

On top of that, additional rule for branch name:

  1. They cannot start with a dash -

Thanks to Jakub Narębski, the man page for git check-ref-format has more details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文