Source file cmd__tools__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
open! Import
let better_chop_prefix ~prefix path =
if Relative_path.equal prefix Relative_path.empty
then Some path
else Relative_path.chop_prefix ~prefix path
;;
let find_crs_ignore_files files_in_repo =
let filename = Fsegment.to_string Crs_ignore.filename in
List.filter files_in_repo ~f:(fun file ->
String.equal filename (Fpath.basename (Vcs.Path_in_repo.to_fpath file)))
;;
let list_included_files_cmd =
Command.make
~summary:"List files that are included when searched for CRs."
~readme:(fun () ->
"This command lists all files that would be included when grepping for CRs, after \
applying $(b,.crs-ignore) rules.\n\n\
This is useful for understanding which files are being searched and for debugging \
$(b,.crs-ignore) configurations.")
(let open Command.Std in
let+ below =
Arg.named_opt
[ "below" ]
(Param.validated_string (module Fpath))
~docv:"PATH"
~doc:"Only list included files located below the supplied path."
in
let cwd = Unix.getcwd () |> Absolute_path.v in
let { Enclosing_repo.repo_root; vcs; vcs_kind = _ } =
Common_helpers.find_enclosing_repo ~from:cwd
in
let files_in_repo = Vcs.ls_files vcs ~repo_root ~below:Vcs.Path_in_repo.root in
let rules =
List.map (find_crs_ignore_files files_in_repo) ~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
let below =
match below with
| None -> Relative_path.empty
| Some path ->
Common_helpers.relativize ~repo_root ~cwd ~path
|> Vcs.Path_in_repo.to_relative_path
in
let included_files =
List.filter files_in_repo ~f:(fun path ->
Option.is_some
(better_chop_prefix ~prefix:below (Vcs.Path_in_repo.to_relative_path path))
&& not (Crs_ignore.Rules.is_file_ignored rules ~path))
|> List.sort ~compare:Vcs.Path_in_repo.compare
in
List.iter included_files ~f:(fun file ->
print_endline (Vcs.Path_in_repo.to_string file)))
;;
let list_ignored_files_cmd =
Command.make
~summary:"List files that are excluded when searched for CRs."
~readme:(fun () ->
"This command lists all files that would be excluded when grepping for CRs, after \
applying $(b,.crs-ignore) rules.\n\n\
This is useful for understanding which files are being searched and for debugging \
$(b,.crs-ignore) configurations.")
(let open Command.Std in
let+ below =
Arg.named_opt
[ "below" ]
(Param.validated_string (module Fpath))
~docv:"PATH"
~doc:"Only list excluded files that are located below the supplied path."
in
let cwd = Unix.getcwd () |> Absolute_path.v in
let { Enclosing_repo.repo_root; vcs; vcs_kind = _ } =
Common_helpers.find_enclosing_repo ~from:cwd
in
let files_in_repo = Vcs.ls_files vcs ~repo_root ~below:Vcs.Path_in_repo.root in
let rules =
List.map (find_crs_ignore_files files_in_repo) ~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
let below =
match below with
| None -> Relative_path.empty
| Some path ->
Common_helpers.relativize ~repo_root ~cwd ~path
|> Vcs.Path_in_repo.to_relative_path
in
let ignored_files =
List.filter files_in_repo ~f:(fun path ->
Option.is_some
(better_chop_prefix ~prefix:below (Vcs.Path_in_repo.to_relative_path path))
&& Crs_ignore.Rules.is_file_ignored rules ~path)
|> List.sort ~compare:Vcs.Path_in_repo.compare
in
List.iter ignored_files ~f:(fun file ->
print_endline (Vcs.Path_in_repo.to_string file)))
;;
let validate_cmd =
Command.make
~summary:"Validate the crs-ignore files found in the repo."
~readme:(fun () ->
"This command validates that the crs-ignore files in the repo are valid.")
(let open Command.Std in
let+ emit_github_annotations =
Common_helpers.emit_github_annotations_arg ~default:false
and+ only_this_file =
Arg.pos_opt
~pos:0
Param.file
~doc:
"Instead of loading all $(b,.crs-ignore) files from the repo, validate this \
specific file only."
in
let cwd = Unix.getcwd () |> Absolute_path.v in
let { Enclosing_repo.repo_root; vcs; vcs_kind = _ } =
Common_helpers.find_enclosing_repo ~from:cwd
in
let files_in_repo = Vcs.ls_files vcs ~repo_root ~below:Vcs.Path_in_repo.root in
let crs_ignore_files =
match only_this_file with
| Some file -> [ Common_helpers.relativize ~repo_root ~cwd ~path:(Fpath.v file) ]
| None -> find_crs_ignore_files files_in_repo
in
let rules =
List.map crs_ignore_files ~f:(fun path ->
Crs_ignore.File.load_exn
~repo_root
~path
~invalid_patterns_are_errors:true
~emit_github_annotations)
|> Crs_ignore.Rules.create
in
let ignored_files =
List.filter files_in_repo ~f:(fun path ->
Crs_ignore.Rules.is_file_ignored rules ~path)
in
ignore (ignored_files : Vcs.Path_in_repo.t list);
List.iter (Crs_ignore.Rules.unused_patterns rules) ~f:(fun pattern ->
User_message.warning
~loc:pattern.loc
~emit_github_annotations
[ Pp.text "This ignore pattern is unused." ]
~hints:
Pp.O.
[ Pp.text "Remove it from this "
++ Pp_tty.kwd (module Fsegment) Crs_ignore.filename
++ Pp.text " file."
]))
;;
let main =
Command.group
~summary:"Utils related to crs-ignore files."
[ "list-ignored-files", list_ignored_files_cmd
; "list-included-files", list_included_files_cmd
; "validate", validate_cmd
]
;;