rubocop -A

This commit is contained in:
Sven Weidauer 2022-05-25 19:25:35 +02:00
parent 561a403b4e
commit 83b6b8f61a
7 changed files with 30 additions and 13 deletions

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
source 'http://rubygems.org'
gemspec

View file

@ -1,4 +1,6 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'format-staged'
require 'optparse'
@ -46,7 +48,7 @@ end
parser.parse!
parameters[:patterns] = ARGV
if !parameters[:formatter] or parameters[:patterns].empty?
if !parameters[:formatter] || parameters[:patterns].empty?
puts 'Missing formatter or file patterns!'
puts parser

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
lib = File.expand_path('lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'format-staged/version'
@ -13,6 +15,7 @@ Gem::Specification.new do |s|
s.executables << 'git-format-staged'
s.homepage = 'https://github.com/5sw/format-staged'
s.license = 'MIT'
s.required_ruby_version = '2.7'
s.add_development_dependency 'rubocop', '~> 1.29'
end

View file

@ -1,3 +1,6 @@
# frozen_string_literal: true
require 'English'
require 'format-staged/version'
require 'format-staged/entry'
require 'format-staged/io'
@ -19,7 +22,7 @@ class FormatStaged
files = get_output('git', 'diff-index', '--cached', '--diff-filter=AM', '--no-renames', 'HEAD')
.map { |line| Entry.new(line, root: root) }
.reject { |entry| entry.symlink? }
.reject(&:symlink?)
.filter { |entry| entry.matches?(@patterns) }
files.each do |file|
@ -64,7 +67,7 @@ class FormatStaged
pid2, r = pipe_command format_command, source: r
pid3, r = pipe_command 'git', 'hash-object', '-w', '--stdin', source: r
result = r.readlines.map { |it| it.chomp }
result = r.readlines.map(&:chomp)
if @verbose
result.each do |line|
puts "< #{line}"
@ -72,20 +75,20 @@ class FormatStaged
end
Process.wait pid1
raise "Cannot read #{file.dst_hash} from object database" unless $?.success?
raise "Cannot read #{file.dst_hash} from object database" unless $CHILD_STATUS.success?
Process.wait pid2
raise "Error formatting #{file.src_path}" unless $?.success?
raise "Error formatting #{file.src_path}" unless $CHILD_STATUS.success?
Process.wait pid3
raise 'Error writing formatted file back to object database' unless $?.success? && !result.empty?
raise 'Error writing formatted file back to object database' unless $CHILD_STATUS.success? && !result.empty?
result.first
end
def object_is_empty(hash)
size = get_output('git', 'cat-file', '-s', hash).first.to_i
size == 0
size.zero?
end
def patch_working_file(file, new_hash)
@ -100,7 +103,7 @@ class FormatStaged
patch_out.close
Process.wait pid
raise 'Error applying patch' unless $?.success?
raise 'Error applying patch' unless $CHILD_STATUS.success?
end
def replace_file_in_index(file, new_hash)

View file

@ -1,6 +1,8 @@
# frozen_string_literal: true
class FormatStaged
class Entry
PATTERN = /^:(?<src_mode>\d+) (?<dst_mode>\d+) (?<src_hash>[a-f0-9]+) (?<dst_hash>[a-f0-9]+) (?<status>[A-Z])(?<score>\d+)?\t(?<src_path>[^\t]+)(?:\t(?<dst_path>[^\t]+))?$/
PATTERN = /^:(?<src_mode>\d+) (?<dst_mode>\d+) (?<src_hash>[a-f0-9]+) (?<dst_hash>[a-f0-9]+) (?<status>[A-Z])(?<score>\d+)?\t(?<src_path>[^\t]+)(?:\t(?<dst_path>[^\t]+))?$/.freeze
attr_reader :src_mode, :dst_mode, :src_hash, :dst_hash, :status, :score, :src_path, :dst_path, :path, :root

View file

@ -1,22 +1,25 @@
# frozen_string_literal: true
require 'English'
class FormatStaged
def get_output(*args, lines: true)
puts '> ' + args.join(' ') if @verbose
puts "> #{args.join(' ')}" if @verbose
output = IO.popen(args, err: :err) do |io|
if lines
io.readlines.map { |l| l.chomp }
io.readlines.map(&:chomp)
else
io.read
end
end
if @verbose and lines
if @verbose && lines
output.each do |line|
puts "< #{line}"
end
end
raise 'Failed to run command' unless $?.success?
raise 'Failed to run command' unless $CHILD_STATUS.success?
output
end

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class FormatStaged
VERSION = '0.0.1'
end