Skip to content

A platform for community discussion. Free, open, simple.

PERF: let lefthook run linter commands in parallel (#38649)

Alan Guo Xiang Tan44a37c68ae1c
diff --git a/bin/lint b/bin/lint
index b3d3bc0d77d..1f2d8c5b9e1 100755
--- a/bin/lint
+++ b/bin/lint
@@ -4,7 +4,6 @@
require "optparse"
require "open3"
require "shellwords"
-require "yaml"
require "pathname"
class LefthookLinter
@@ -49,17 +48,17 @@ class LefthookLinter
end
files
- .flat_map { |f|
+ .flat_map do |f|
path = normalize_relative_path(f)
if File.directory?(path)
- expanded = Dir.glob(File.join(path, "**", "*"))
- .select { |g| File.file?(g) && lintable_file?(g) }
+ expanded =
+ Dir.glob(File.join(path, "**", "*")).select { |g| File.file?(g) && lintable_file?(g) }
abort "Error: No lintable files found in directory: #{path}" if expanded.empty?
expanded
else
[path]
end
- }
+ end
.select { |f| File.file?(f) && lintable_file?(f) }
.uniq
end
@@ -163,25 +162,12 @@ class LefthookLinter
end
end
- GLOB_FLAGS = File::FNM_PATHNAME | File::FNM_EXTGLOB | File::FNM_CASEFOLD | File::FNM_DOTMATCH
- LEFTHOOK_CONFIG_PATH = File.expand_path("../lefthook.yml", __dir__)
PROJECT_ROOT = File.expand_path("..", __dir__)
def run_lefthook_command(hook, files)
- commands = lefthook_config.dig(hook, "commands")
-
- if !files.empty? && commands
+ if !files.empty?
normalized = files.map { |f| normalize_relative_path(f) }
- any = false
-
- commands.each do |name, config|
- filtered = filter_files_for_command(normalized, config)
- next if filtered.empty?
-
- any = true
- exec_lefthook(hook, name, filtered)
- end
- puts "No matching linters for provided files." if !any && @verbose
+ exec_lefthook(hook, nil, normalized)
else
exec_lefthook(hook, nil, files)
end
@@ -197,22 +183,6 @@ class LefthookLinter
exit 1 unless system({ "LEFTHOOK" => "1" }, *cmd)
end
- def lefthook_config
- @lefthook_config ||= YAML.load_file(LEFTHOOK_CONFIG_PATH)
- end
-
- def filter_files_for_command(files, config)
- globs = Array(config["glob"]).compact
- excludes = Array(config["exclude"]).compact
-
- files.select do |file|
- matches_includes =
- globs.empty? || globs.any? { |pattern| File.fnmatch?(pattern, file, GLOB_FLAGS) }
- matches_excludes = excludes.any? { |pattern| File.fnmatch?(pattern, file, GLOB_FLAGS) }
- matches_includes && !matches_excludes
- end
- end
-
def normalize_relative_path(file)
cleaned = file.start_with?("./") ? file[2..] : file
path = Pathname.new(cleaned)