#!/usr/bin/env ruby lib = File.expand_path('../lib', __dir__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'git/conform' require 'slop' options = Slop.parse(help: true) do |o| o.banner = <<-"BANNER" Usage: git-conform [options] v#{Git::Conform::VERSION} Options: BANNER o.on '--version', 'Print the version' do dependencies = [ "rugged v#{Rugged::VERSION}", "#{RUBY_ENGINE} #{RUBY_VERSION}p#{RUBY_PATCHLEVEL}", ].join(', ') puts "#{Git::Conform::NAME} v#{Git::Conform::VERSION} (#{dependencies})" exit(0) end o.on '--help', 'Print help message' do puts o exit(0) end o.on '--available', 'List all available conformity checkers' o.on '--files', 'List all files in the git repo that will be processed' o.on '--list', 'List all conformity checkers for this repo' o.on '--verify', 'Verify that all conformity checkers exist' o.on '--check', 'Run all conformity checkers' o.on '--verbose', 'Make the conformity checkers verbose' o.on '--gabby', 'Make the conformity checkers gabby' end if options[:available] STDOUT.puts Git::Conform::FileChecker.available_checkers exit(0) end repo = begin Git::Conform::Repo.new(Git::Conform::Repo.discover(Dir.pwd).path) rescue RuntimeError unless $ERROR_INFO.message =~ /The given path is not a valid Git repository/ unless $ERROR_INFO.message =~ /Not a git repository \(or any of the parent directories\)/ raise # Rugged uses generic RuntimeErrors, so we have to check the exception messages... end end nil end repo || abort("fatal: Not a git repository (or any of the parent directories): #{Dir.pwd}") if options[:files] unless (files = repo.files(type: :text)).empty? STDOUT.puts files.sort.join("\n") end exit(0) end repo.git_conform_enabled? || abort("fatal: unable to read config file '#{repo.git_conform_path}': No such file or directory") if options[:list] unless (checkers = repo.conformity_checkers).empty? STDOUT.puts checkers.sort.join("\n") end exit(0) end if options[:verify] begin repo.verify rescue NameError abort "fatal: #{$ERROR_INFO.message}" end exit(0) end if options[:check] repo.conformity_checkers.each do |checker_class| checker_class = constantize "Git::Conform::#{checker_class}" puts checker_class.name.invert repo.files(type: :text).each do |filename| file_checker = checker_class.new(filename) if file_checker.excluded? puts 'EXCLUDE: ' + filename.blue if options[:verbose] elsif file_checker.conforms? puts 'INCLUDE: ' + filename.green if options[:gabby] else puts 'VIOLATE: ' + filename.red end end end exit(0) end # That's all, Folks!