@@ -48,7 +48,8 @@ def globs_to_owner(files)
48
48
. map ( &:cleanpath )
49
49
. each_with_object ( { } ) do |pathname , res |
50
50
owner = owner_for_codeowners_file ( pathname )
51
- res [ pathname . dirname . cleanpath . join ( '**/**' ) . to_s ] = owner
51
+ glob = glob_for_codeowners_file ( pathname )
52
+ res [ glob ] = owner
52
53
end
53
54
end
54
55
@@ -77,7 +78,7 @@ def owner_for_codeowners_file(codeowners_file)
77
78
# Takes a file and finds the relevant `.codeowner` file by walking up the directory
78
79
# structure. Example, given `a/b/c.rb`, this looks for `a/b/.codeowner`, `a/.codeowner`,
79
80
# and `.codeowner` in that order, stopping at the first file to actually exist.
80
- # If the parovided file is a directory, it will look for `.codeowner` in that directory and then upwards.
81
+ # If the provided file is a directory, it will look for `.codeowner` in that directory and then upwards.
81
82
# We do additional caching so that we don't have to check for file existence every time.
82
83
sig { params ( file : String ) . returns ( T . nilable ( CodeTeams ::Team ) ) }
83
84
def map_file_to_relevant_owner ( file )
@@ -123,6 +124,26 @@ def get_team_from_codeowners_file_within_directory(directory)
123
124
124
125
team
125
126
end
127
+
128
+ sig { params ( codeowners_file : Pathname ) . returns ( String ) }
129
+ def glob_for_codeowners_file ( codeowners_file )
130
+ unescaped = codeowners_file . dirname . cleanpath . join ( '**/**' ) . to_s
131
+
132
+ # Globs can contain certain regex characters, like "[" and "]".
133
+ # However, when we are generating a glob from a .codeowner file, we
134
+ # need to escape bracket characters and interpret them literally.
135
+ # Otherwise the resulting glob will not actually match the directory
136
+ # containing the .codeowner file.
137
+ #
138
+ # Example
139
+ # file: "/some/[dir]/.codeowner"
140
+ # unescaped: "/some/[dir]/**/**"
141
+ # matches: "/some/d/file"
142
+ # matches: "/some/i/file"
143
+ # matches: "/some/r/file"
144
+ # does not match!: "/some/[dir]/file"
145
+ unescaped . gsub ( /[\[ \] ]/ ) { |x | "\\ #{ x } " }
146
+ end
126
147
end
127
148
end
128
149
end
0 commit comments