Skip to content

Commit

Permalink
Merge branch 'main' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
sds authored Feb 25, 2024
2 parents 7683b10 + c4aa796 commit f46660a
Show file tree
Hide file tree
Showing 15 changed files with 160 additions and 72 deletions.
4 changes: 3 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
inherit_from: .rubocop_todo.yml

AllCops:
TargetRubyVersion: 2.4
TargetRubyVersion: 2.6
NewCops: disable
SuggestExtensions: false

Layout/ClosingParenthesisIndentation:
Enabled: false
Expand Down
66 changes: 38 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,43 @@ are stored in source control. You can also easily
[add your existing hook scripts](#adding-existing-git-hooks) without writing
any Ruby code.

* [Requirements](#requirements)
* [Dependencies](#dependencies)
* [Installation](#installation)
* [Automatically Install Overcommit Hooks](#automatically-install-overcommit-hooks)
* [Usage](#usage)
* [Continuous Integration](#continuous-integration)
* [Configuration](#configuration)
* [Hook Options](#hook-options)
* [Hook Categories](#hook-categories)
* [Gemfile](#gemfile)
* [Plugin Directory](#plugin-directory)
* [Signature Verification](#signature-verification)
* [Built-In Hooks](#built-in-hooks)
* [CommitMsg](#commitmsg)
* [PostCheckout](#postcheckout)
* [PostCommit](#postcommit)
* [PostMerge](#postmerge)
* [PostRewrite](#postrewrite)
* [PreCommit](#precommit)
* [PrePush](#prepush)
* [PreRebase](#prerebase)
* [Repo-Specific Hooks](#repo-specific-hooks)
* [Adding Existing Git Hooks](#adding-existing-git-hooks)
* [Security](#security)
* [Contributing](#contributing)
* [Community](#community)
* [Changelog](#changelog)
* [License](#license)
- [Requirements](#requirements)
- [Windows](#windows)
- [Dependencies](#dependencies)
- [Installation](#installation)
- [Automatically Install Overcommit Hooks](#automatically-install-overcommit-hooks)
- [Usage](#usage)
- [Skipping Hooks](#skipping-hooks)
- [Disabling Overcommit](#disabling-overcommit)
- [Disabling Colorized Output](#disabling-colorized-output)
- [Continuous Integration](#continuous-integration)
- [Configuration](#configuration)
- [Hook Options](#hook-options)
- [Hook Categories](#hook-categories)
- [The `ALL` Hook](#the-all-hook)
- [Gemfile](#gemfile)
- [Plugin Directory](#plugin-directory)
- [Quiet Hook Runs](#quiet-hook-runs)
- [Concurrency](#concurrency)
- [Signature Verification](#signature-verification)
- [Built-In Hooks](#built-in-hooks)
- [CommitMsg](#commitmsg)
- [PostCheckout](#postcheckout)
- [PostCommit](#postcommit)
- [PostMerge](#postmerge)
- [PostRewrite](#postrewrite)
- [PreCommit](#precommit)
- [WARNING: pre-commit hooks cannot have side effects](#warning-pre-commit-hooks-cannot-have-side-effects)
- [PrePush](#prepush)
- [PreRebase](#prerebase)
- [Repo-Specific hooks](#repo-specific-hooks)
- [Adding Existing Git Hooks](#adding-existing-git-hooks)
- [Security](#security)
- [Disabling Signature Checking](#disabling-signature-checking)
- [Contributing](#contributing)
- [Community](#community)
- [Changelog](#changelog)
- [License](#license)

## Requirements

Expand Down Expand Up @@ -561,6 +570,7 @@ issue](https://github.com/sds/overcommit/issues/238) for more details.
* [SemiStandard](lib/overcommit/hook/pre_commit/semi_standard.rb)
* [ShellCheck](lib/overcommit/hook/pre_commit/shell_check.rb)
* [SlimLint](lib/overcommit/hook/pre_commit/slim_lint.rb)
* [Sorbet](lib/overcommit/hook/pre_commit/sorbet.rb)
* [Sqlint](lib/overcommit/hook/pre_commit/sqlint.rb)
* [Standard](lib/overcommit/hook/pre_commit/standard.rb)
* [Stylelint](lib/overcommit/hook/pre_commit/stylelint.rb)
Expand Down
8 changes: 8 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,14 @@ PreCommit:
install_command: 'gem install slim_lint'
include: '**/*.slim'

Sorbet:
enabled: false
description: 'Analyze with Sorbet'
required_executable: 'srb'
install_command: 'gem install sorbet'
command: ['srb', 'tc']
include: '**/*.rb'

Sqlint:
enabled: false
description: 'Analyze with sqlint'
Expand Down
16 changes: 7 additions & 9 deletions lib/overcommit/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,13 @@ def install_or_uninstall
end

@options[:targets].each do |target|
begin
Installer.new(log).run(target, @options)
rescue Overcommit::Exceptions::InvalidGitRepo => e
log.warning "Invalid repo #{target}: #{e}"
halt 69 # EX_UNAVAILABLE
rescue Overcommit::Exceptions::PreExistingHooks => e
log.warning "Unable to install into #{target}: #{e}"
halt 73 # EX_CANTCREAT
end
Installer.new(log).run(target, @options)
rescue Overcommit::Exceptions::InvalidGitRepo => e
log.warning "Invalid repo #{target}: #{e}"
halt 69 # EX_UNAVAILABLE
rescue Overcommit::Exceptions::PreExistingHooks => e
log.warning "Unable to install into #{target}: #{e}"
halt 73 # EX_CANTCREAT
end
end

Expand Down
14 changes: 6 additions & 8 deletions lib/overcommit/hook/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,12 @@ def check_for_libraries
output = []

required_libraries.each do |library|
begin
require library
rescue LoadError
install_command = @config['install_command']
install_command = " -- install via #{install_command}" if install_command

output << "Unable to load '#{library}'#{install_command}"
end
require library
rescue LoadError
install_command = @config['install_command']
install_command = " -- install via #{install_command}" if install_command

output << "Unable to load '#{library}'#{install_command}"
end

return if output.empty?
Expand Down
2 changes: 1 addition & 1 deletion lib/overcommit/hook/commit_msg/text_width.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def find_errors_in_body(lines)

max_body_width = config['max_body_width']

lines[2..-1].each_with_index do |line, index|
lines[2..].each_with_index do |line, index|
if line.chomp.size > max_body_width
@errors << "Line #{index + 3} of commit message has > " \
"#{max_body_width} characters"
Expand Down
2 changes: 1 addition & 1 deletion lib/overcommit/hook/pre_commit/erb_lint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def run
return :pass if result.success?

extract_messages(
result.stdout.split("\n\n")[1..-1],
result.stdout.split("\n\n")[1..],
MESSAGE_REGEX
)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/overcommit/hook/pre_commit/html_hint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def run
lines = group.split("\n").map(&:strip)
file = lines[0][/(.+):/, 1]
extract_messages(
lines[1..-1].map { |msg| "#{file}: #{msg}" },
lines[1..].map { |msg| "#{file}: #{msg}" },
/^(?<file>(?:\w:)?[^:]+): line (?<line>\d+)/
)
end.flatten
Expand Down
10 changes: 4 additions & 6 deletions lib/overcommit/hook/pre_commit/json_syntax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ def run
messages = []

applicable_files.each do |file|
begin
JSON.parse(IO.read(file))
rescue JSON::ParserError => e
error = "#{e.message} parsing #{file}"
messages << Overcommit::Hook::Message.new(:error, file, nil, error)
end
JSON.parse(IO.read(file))
rescue JSON::ParserError => e
error = "#{e.message} parsing #{file}"
messages << Overcommit::Hook::Message.new(:error, file, nil, error)
end

messages
Expand Down
24 changes: 24 additions & 0 deletions lib/overcommit/hook/pre_commit/sorbet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

module Overcommit::Hook::PreCommit
# Runs 'srb tc' against any modified files.
#
# @see https://github.com/sorbet/sorbet
class Sorbet < Base
# example of output:
# sorbet.rb:1: Method `foo` does not exist on `T.class_of(Bar)` https://srb.help/7003
MESSAGE_REGEX = /^(?<file>[^:]+):(?<line>\d+): (?<message>.*)$/.freeze

def run
result = execute(command, args: applicable_files)
return :pass if result.success?

output = result.stderr.split("\n").grep(MESSAGE_REGEX)

extract_messages(
output,
MESSAGE_REGEX
)
end
end
end
10 changes: 4 additions & 6 deletions lib/overcommit/hook/pre_commit/xml_syntax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ def run
messages = []

applicable_files.each do |file|
begin
REXML::Document.new(IO.read(file))
rescue REXML::ParseException => e
error = "Error parsing #{file}: #{e.message}"
messages << Overcommit::Hook::Message.new(:error, file, nil, error)
end
REXML::Document.new(IO.read(file))
rescue REXML::ParseException => e
error = "Error parsing #{file}: #{e.message}"
messages << Overcommit::Hook::Message.new(:error, file, nil, error)
end

messages
Expand Down
16 changes: 7 additions & 9 deletions lib/overcommit/hook/pre_commit/yaml_syntax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@ def run
messages = []

applicable_files.each do |file|
YAML.load_file(file, aliases: true)
rescue ArgumentError
begin
YAML.load_file(file, aliases: true)
rescue ArgumentError
begin
YAML.load_file(file)
rescue ArgumentError, Psych::SyntaxError => e
messages << Overcommit::Hook::Message.new(:error, file, nil, e.message)
end
rescue Psych::DisallowedClass => e
messages << error_message(file, e)
YAML.load_file(file)
rescue ArgumentError, Psych::SyntaxError => e
messages << Overcommit::Hook::Message.new(:error, file, nil, e.message)
end
rescue Psych::DisallowedClass => e
messages << error_message(file, e)
end

messages
Expand Down
2 changes: 1 addition & 1 deletion lib/overcommit/utils/messages_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def extract_messages(output_messages, regex, type_categorizer = nil)
raise Overcommit::Exceptions::MessageProcessingError,
'Unexpected output: unable to determine line number or type ' \
"of error/warning for output:\n" \
"#{output_messages[index..-1].join("\n")}"
"#{output_messages[index..].join("\n")}"
end

file = extract_file(match, message)
Expand Down
2 changes: 1 addition & 1 deletion overcommit.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Gem::Specification.new do |s|
Dir['libexec/**/*'] +
Dir['template-dir/**/*']

s.required_ruby_version = '>= 2.4'
s.required_ruby_version = '>= 2.6'

s.add_dependency 'childprocess', '>= 0.6.3', '< 6'
s.add_dependency 'iniparse', '~> 1.4'
Expand Down
54 changes: 54 additions & 0 deletions spec/overcommit/hook/pre_commit/sorbet_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# frozen_string_literal: true

require 'spec_helper'

describe Overcommit::Hook::PreCommit::Sorbet do
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
let(:context) { double('context') }
subject { described_class.new(config, context) }

before do
subject.stub(:applicable_files).and_return(%w[file1.rb file2.rb])
end

context 'when Sorbet exits successfully' do
let(:result) { double('result') }

before do
result.stub(:success?).and_return(true)
subject.stub(:execute).and_return(result)
end

it { should pass }

context 'and it printed a message to stderr' do
before do
result.stub(:stderr).and_return("No errors! Great job.\n")
end

it { should pass }
end
end

context 'when Sorbet exits unsucessfully' do
let(:result) { double('result') }

before do
result.stub(:success?).and_return(false)
subject.stub(:execute).and_return(result)
end

context 'and it reports an error' do
before do
result.stub(:stderr).and_return(normalize_indent(<<-MSG))
sorbet.rb:1: Method `foo` does not exist on `T.class_of(Bar)` https://srb.help/7003
5 | foo 'bar'
^^^
Errors: 1
MSG
end

it { should fail_hook }
end
end
end

0 comments on commit f46660a

Please sign in to comment.