How do I check for valid Git branch names?

前端 未结 6 1734
粉色の甜心
粉色の甜心 2021-01-03 23:40

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

ef4d4037f8568e386629457d4d960915a85da2ae 61a4         


        
6条回答
  •  感动是毒
    2021-01-04 00:24

    There's no need to write monstrosities in Perl. Just use /x:

    # RegExp rules based on git-check-ref-format
    my $valid_ref_name = qr%
       ^
       (?!
          # begins with
          /|                # (from #6)   cannot begin with /
          # contains
          .*(?:
             [/.]\.|        # (from #1,3) cannot contain /. or ..
             //|            # (from #6)   cannot contain multiple consecutive slashes
             @\{|           # (from #8)   cannot contain a sequence @{
             \\             # (from #9)   cannot contain a \
          )
       )
                            # (from #2)   (waiving this rule; too strict)
       [^\040\177 ~^:?*[]+  # (from #4-5) valid character rules
    
       # ends with
       (? ".($branch =~ $valid_ref_name)."\n";
    }
    

    Joey++ for some of the code, though I made some corrections.

提交回复
热议问题