Source file crs_ignore.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
let filename = Fsegment.v ".crs-ignore"
module Entry = struct
type t =
{ pattern : Re.re
; original_pattern : string Loc.Txt.t
}
end
let strip_location ~file_contents ~file_cache ~loc =
let rec strip_left start stop =
if start >= stop
then start
else (
let char = file_contents.[start] in
match char with
| ' ' -> strip_left (start + 1) stop
| _ -> start)
in
let rec find_stop index stop =
if index >= stop
then stop
else (
let char = file_contents.[index] in
match char with
| '#' -> index
| _ -> find_stop (index + 1) stop)
in
let rec strip_right start stop =
if start >= stop
then stop
else (
let char = file_contents.[stop - 1] in
match char with
| ' ' -> strip_right start (stop - 1)
| _ -> stop)
in
let start = Loc.start_offset loc in
let stop = find_stop start (Loc.stop_offset loc) in
let start = strip_left start stop in
let stop = strip_right start stop in
let str = String.sub file_contents ~pos:start ~len:(stop - start) in
{ Loc.Txt.txt = str
; loc =
Loc.of_lexbuf_loc
{ start = Loc.Offset.to_position start ~file_cache
; stop = Loc.Offset.to_position stop ~file_cache
}
}
;;
let parse_and_compile_pattern
~file_contents
~file_cache
~line
~whole_line
~invalid_patterns_are_errors
~emit_github_annotations
=
let whole_line_loc = Loc.of_file_line ~file_cache ~line in
let ({ Loc.Txt.txt; loc } as original_pattern) =
strip_location ~file_contents ~file_cache ~loc:whole_line_loc
in
let () =
let str =
let str =
match String.lsplit2 whole_line ~on:'#' with
| None -> whole_line
| Some (pat, ) -> pat
in
String.strip str ~drop:(fun char -> Char.equal char ' ')
in
if not (String.equal str txt)
then
Err.raise
~loc
[ Pp.textf "Invalid computation of position - pattern = %S" str ] [@coverage off]
in
if String.is_empty txt
then None
else (
try
let pattern =
let pathname = true
and anchored = true in
let pattern_str =
if Sys.win32
then
String.concat
~sep:"/"
(String.split_on_chars txt ~on:[ '\\' ]) [@coverage off]
else txt
in
Re.(
Glob.glob ~pathname ~anchored ~match_backslashes:Sys.win32 pattern_str
|> compile)
in
Some { Entry.pattern; original_pattern }
with
| Re.Glob.Parse_error ->
(if invalid_patterns_are_errors then User_message.error else User_message.warning)
~loc
~emit_github_annotations
[ Pp.text "Invalid glob pattern:"; Pp.text txt ];
None)
;;
module File = struct
type t =
{ directory : Relative_path.t
; entries : Entry.t list
}
let parse_exn
~repo_root
~path
~file_contents
~invalid_patterns_are_errors
~emit_github_annotations
=
let directory =
Vcs.Path_in_repo.to_relative_path path
|> Relative_path.parent
|> Option.value_exn ~here:[%here]
|> Relative_path.to_dir_path
in
let ignore_file_path = Vcs.Repo_root.append repo_root path in
let file_contents = file_contents |> Vcs.File_contents.to_string in
let file_cache =
Loc.File_cache.create ~path:(ignore_file_path :> Fpath.t) ~file_contents
in
let entries =
List.filter_mapi (String.split_lines file_contents) ~f:(fun i whole_line ->
parse_and_compile_pattern
~file_contents
~file_cache
~line:(i + 1)
~whole_line
~invalid_patterns_are_errors
~emit_github_annotations)
in
{ directory; entries }
;;
let load_exn ~repo_root ~path ~invalid_patterns_are_errors ~emit_github_annotations =
let ignore_file_path = Vcs.Repo_root.append repo_root path in
let ignore_file_str = Absolute_path.to_string ignore_file_path in
let file_contents = In_channel.read_all ignore_file_str |> Vcs.File_contents.create in
parse_exn
~repo_root
~path
~file_contents
~invalid_patterns_are_errors
~emit_github_annotations
;;
end
let better_chop_prefix ~prefix path =
if Relative_path.equal prefix Relative_path.empty
then Some path
else Relative_path.chop_prefix ~prefix path
;;
module Rules = struct
module Marked_entry = struct
type t =
{ entry : Entry.t
; mutable used : bool
}
let create entry = { entry; used = false }
end
module One_file = struct
type t =
{ directory : Relative_path.t
; entries : Marked_entry.t list
}
let is_file_ignored t ~path =
match better_chop_prefix ~prefix:t.directory path with
| None ->
false
[@coverage off]
| Some path ->
let path = Relative_path.to_string path in
List.exists t.entries ~f:(fun entry ->
if Re.execp entry.entry.pattern path
then (
entry.used <- true;
true)
else false)
;;
let create ({ directory; entries } : File.t) =
{ directory; entries = List.map entries ~f:Marked_entry.create }
;;
end
type t = { files_by_dir : One_file.t Hashtbl.M(Relative_path).t }
let create files =
let files_by_dir =
List.map files ~f:(fun (t : File.t) -> t.directory, One_file.create t)
|> Hashtbl.of_alist_exn (module Relative_path)
in
{ files_by_dir }
;;
let aux_dir (t : t) ~dir ~path =
match Hashtbl.find t.files_by_dir dir with
| None -> false
| Some file -> One_file.is_file_ignored file ~path
;;
let strict_parent dir =
if Relative_path.equal dir Relative_path.empty then None else Relative_path.parent dir
;;
let is_file_ignored (t : t) ~path =
let path = Vcs.Path_in_repo.to_relative_path path in
let rec aux dir =
match strict_parent dir with
| None -> aux_dir t ~dir:Relative_path.empty ~path
| Some dir -> aux_dir t ~dir ~path || aux dir
in
aux path
;;
let unused_patterns t =
Hashtbl.data t.files_by_dir
|> List.map ~f:(fun { One_file.directory; entries } ->
let entries = List.filter entries ~f:(fun entry -> not entry.used) in
{ One_file.directory; entries })
|> List.sort ~compare:(fun (f1 : One_file.t) f2 ->
Relative_path.compare f1.directory f2.directory)
|> List.concat_map ~f:(fun (f : One_file.t) ->
List.map f.entries ~f:(fun entry -> entry.entry.original_pattern))
;;
end
module Private = struct
module File = File
end