Source file crs_parser.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
module Unix = UnixLabels
let cr_pattern_egrep = File_parser.cr_pattern_egrep
let parse_file = File_parser.parse_file
let rec waitpid_non_intr pid =
try Unix.waitpid ~mode:[] pid with
| Unix.Unix_error (EINTR, _, _) -> waitpid_non_intr pid [@coverage off]
;;
let read_all_from_fd fd =
let out = In_channel.input_all (Unix.in_channel_of_descr fd) in
Unix.close fd;
out
;;
let find_executable ~path ~executable_basename =
let rec loop = function
| [] -> (None [@coverage off])
| path :: rest ->
let fn = Stdlib.Filename.concat path executable_basename in
if Stdlib.Sys.file_exists fn then Some fn else loop rest
in
loop (String.split path ~on:':')
;;
let find_xargs =
lazy
(match Stdlib.Sys.getenv_opt "PATH" with
| None -> None [@coverage off]
| Some path -> find_executable ~path ~executable_basename:"xargs")
;;
module Exit_status = struct
[@@@coverage off]
type t =
[ `Exited of int
| `Signaled of int
| `Stopped of int
]
[@@deriving sexp_of]
end
let null_separator = String.make 1 (Char.of_int_exn 0)
let () =
Sexplib0.Sexp_conv.Exn_converter.add [%extension_constructor Unix.Unix_error] (function
| Unix.Unix_error (error, fn, param) ->
Sexp.List
[ Atom "Unix.Unix_error"; Atom (Unix.error_message error); Atom fn; Atom param ]
| _ -> assert false)
;;
let grep ~vcs ~repo_root ~below =
match Vcs.ls_files vcs ~repo_root ~below with
| [] -> []
| _ :: _ as files_below ->
let files_to_grep =
let stdin_text =
files_below
|> List.map ~f:Vcs.Path_in_repo.to_string
|> String.concat ~sep:null_separator
in
let stdout_ref = ref "<Unknown>" in
let stderr_ref = ref "<Unknown>" in
match
let prog =
match Lazy.force find_xargs with
| Some prog -> prog
| None -> failwith "Cannot find xargs in PATH" [@coverage off]
in
let stdin_reader, stdin_writer = Spawn.safe_pipe () in
let stdout_reader, stdout_writer = Spawn.safe_pipe () in
let stderr_reader, stderr_writer = Spawn.safe_pipe () in
let pid =
Spawn.spawn
~cwd:(Path (Vcs.Repo_root.to_string repo_root))
~prog
~argv:
[ "xargs"
; "-0"
; "grep"
; "--no-messages"
; "-E"
; "-l"
; "--binary-files=without-match"
; cr_pattern_egrep
]
~stdin:stdin_reader
~stdout:stdout_writer
~stderr:stderr_writer
()
in
Unix.close stdin_reader;
Unix.close stdout_writer;
Unix.close stderr_writer;
let () =
let stdin_oc = Unix.out_channel_of_descr stdin_writer in
Out_channel.output_string stdin_oc stdin_text;
Out_channel.flush stdin_oc;
Unix.close stdin_writer
in
let stdout = read_all_from_fd stdout_reader in
stdout_ref := stdout;
let stderr = read_all_from_fd stderr_reader in
stderr_ref := stderr;
let pid', process_status = waitpid_non_intr pid in
assert (pid = pid');
match process_status with
| Unix.WEXITED n ->
if
Int.equal n 0
|| ((Int.equal n 123 || Int.equal n 1 )
&& String.is_empty stderr)
then (
let files = stdout |> String.split_lines |> List.map ~f:Vcs.Path_in_repo.v in
`Files files)
else `Error (`Exited n)
| Unix.WSIGNALED n -> `Error (`Signaled n) [@coverage off]
| Unix.WSTOPPED n -> `Error (`Stopped n) [@coverage off]
with
| `Files files -> files
| `Error exit_status ->
let stdout = !stdout_ref in
let stderr = !stderr_ref in
raise
(Err.E
(Err.create
[ Pp.text "Process xargs exited abnormally."
; Err.sexp
[%sexp
{ exit_status : Exit_status.t; stdout : string; stderr : string }]
]))
| exception exn ->
raise
(Err.E
(Err.create [ Pp.text "Error while running xargs process."; Err.exn exn ]))
[@coverage off]
in
(match files_to_grep with
| [] -> []
| _ :: _ ->
let crs_ignore_rules =
let filename = Fsegment.to_string Crs_ignore.filename in
let crs_ignore_files =
let files_in_repo =
if Vcs.Path_in_repo.equal below Vcs.Path_in_repo.root
then files_below
else Vcs.ls_files vcs ~repo_root ~below:Vcs.Path_in_repo.root
in
List.filter files_in_repo ~f:(fun file ->
String.equal filename (Fpath.basename (Vcs.Path_in_repo.to_fpath file)))
in
List.map crs_ignore_files ~f:(fun path ->
Crs_ignore.File.load_exn
~repo_root
~path
~invalid_patterns_are_errors:false
~emit_github_annotations:false)
|> Crs_ignore.Rules.create
in
List.concat_map files_to_grep ~f:(fun path ->
if Crs_ignore.Rules.is_file_ignored crs_ignore_rules ~path
then []
else (
let file_contents =
In_channel.read_all
(Vcs.Repo_root.append repo_root path |> Absolute_path.to_string)
|> Vcs.File_contents.create
in
parse_file ~path ~file_contents)))
;;
module Private = struct
module Crs_ignore = Crs_ignore
module Github_annotation = Github_annotation
module Invalid_cr_parser = Invalid_cr_parser
module User_message = User_message
end