如何检查有效的Git分支名称? [英] How do I check for valid Git branch names?

查看:94
本文介绍了如何检查有效的Git分支名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python开发 git post-receive钩子.数据在stdin上以类似于

I'm developing a git post-receive hook in Python. Data is supplied on stdin with lines similar to

ef4d4037f8568e386629457d4d960915a85da2ae 61a4033ccf9159ae69f951f709d9c987d3c9f580 refs/heads/master

第一个哈希是旧引用,第二个是新引用,第三列是要更新的引用.

The first hash is the old-ref, the second the new-ref and the third column is the reference being updated.

我想将其分为3个变量,同时还要验证输入. 如何验证分支名称?

I want to split this into 3 variables, whilst also validating input. How do I validate the branch name?

我当前正在使用以下正则表达式

I am currently using the following regular expression

^([0-9a-f]{40}) ([0-9a-f]{40}) refs/heads/([0-9a-zA-Z]+)$

这不接受所有可能的分支名称,如

This doesn't accept all possible branch names, as set out by man git-check-ref-format. For example, it excludes a branch by the name of build-master, which is valid.

我实际上想排除任何以"build-"开头的分支.可以在同一个正则表达式中完成吗?

I actually want to exclude any branch that starts with "build-". Can this be done in the same regex?

鉴于以下出色的答案,我编写了一些测试,可以在以下位置找到 https://github.com/alexchamberlain/githooks/blob/master/其他/git-branch-re-test.py .

Given the great answers below, I wrote some tests, which can be found at https://github.com/alexchamberlain/githooks/blob/master/miscellaneous/git-branch-re-test.py.

状态:以下所有正则表达式均无法编译.这可能表明我的脚本有问题或语法不兼容.

推荐答案

让我们剖析各种规则并从中构建正则表达式部分:

Let's dissect the various rules and build regex parts from them:

  1. 对于层次结构(目录)分组,它们可以包括斜杠/,但是斜杠分隔的组件不能以点.开头或以序列.lock结尾.

  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.

 # must not contain /.
 (?!.*/\.)
 # must not end with .lock
 (?<!\.lock)$

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

  • 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.

     .+/.+  # may get more precise later
    

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

     (?!.*\.\.)
    

  • 它们在任何地方都不能具有ASCII控制字符(即值小于\040\177 DEL的字节),空格,代字号~,插入符^或冒号:. /p>

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

     [^\000-\037\177 ~^:]+   # pattern for allowed characters
    

  • 他们在任何地方都不能带有问号?,星号*或右括号[.有关此规则的例外,请参见下面的--refspec-pattern选项.

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

     [^\000-\037\177 ~^:?*[]+   # new pattern for allowed characters
    

  • 它们不能以斜杠/开头或结尾,也不能包含多个连续的斜杠(有关此规则的例外,请参见下面的--normalize选项)

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

     ^(?!/)
     (?<!/)$
     (?!.*//)
    

  • 它们不能以点号.结尾.

     (?<!\.)$
    

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

     (?!.*@\{)
    

  • 它们不能包含\.

     (?!.*\\)
    

  • 将所有部分拼凑在一起,我们得出以下怪物:

    Piecing it all together we arrive at the following monstrosity:

    ^(?!.*/\.)(?!.*\.\.)(?!/)(?!.*//)(?!.*@\{)(?!.*\\)[^\000-\037\177 ~^:?*[]+/[^\000-\037\177 ~^:?*[]+(?<!\.lock)(?<!/)(?<!\.)$
    

    如果您要排除以build-开头的内容,则只需添加另一个前行即可:

    And if you want to exclude those that start with build- then just add another lookahead:

    ^(?!build-)(?!.*/\.)(?!.*\.\.)(?!/)(?!.*//)(?!.*@\{)(?!.*\\)[^\000-\037\177 ~^:?*[]+/[^\000-\037\177 ~^:?*[]+(?<!\.lock)(?<!/)(?<!\.)$
    

    也可以通过合并一些寻找通用模式的东西来对其进行优化:

    This can be optimized a bit as well by conflating a few things that look for common patterns:

    ^(?!@$|build-|/|.*([/.]\.|//|@\{|\\))[^\000-\037\177 ~^:?*[]+/[^\000-\037\177 ~^:?*[]+(?<!\.lock|[/.])$
    

    这篇关于如何检查有效的Git分支名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆