Skip to content

Instantly share code, notes, and snippets.

@teriiehina
Created July 1, 2016 07:58
Show Gist options
  • Save teriiehina/32e7d585e639c22f2dda7c51b265a602 to your computer and use it in GitHub Desktop.
Save teriiehina/32e7d585e639c22f2dda7c51b265a602 to your computer and use it in GitHub Desktop.
A pre-commit git hook heavily inspired from https://github.com/ashfurrow/danger-swiftlint/
#!/usr/bin/env ruby
require 'json'
def puts_violation(violation)
severity = violation['severity']
filename = violation['file'].split('/').last
line = violation['line']
reason = violation['reason']
puts "#{severity} : #{filename}, line #{line}, #{reason}"
end
config_file = File.file?('./.swiftlint.yml') ? './.swiftlint.yml' : nil
swift_files = `git diff --cached --name-only --diff-filter=AMC | grep "\.swift$"`.lines
swiftlint_command = "swiftlint lint --quiet --reporter json"
swiftlint_command += " --config #{config_file}" if config_file
result_json = swift_files.uniq.collect { |f| JSON.parse(`#{swiftlint_command} --path #{f}`.strip).flatten }.flatten
warnings = result_json.select { |results| results['severity'] == 'Warning' }
errors = result_json.select { |results| results['severity'] == 'Error' }
warnings.each { |violation| puts_violation violation }
errors.each { |violation| puts_violation violation }
exit errors.count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment