From 83b6b8f61a8bbe68f0de3375eedadb20680764db Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Wed, 25 May 2022 19:25:35 +0200 Subject: [PATCH] rubocop -A --- Gemfile | 2 ++ bin/git-format-staged | 4 +++- format-staged.gemspec | 3 +++ lib/format-staged.rb | 17 ++++++++++------- lib/format-staged/entry.rb | 4 +++- lib/format-staged/io.rb | 11 +++++++---- lib/format-staged/version.rb | 2 ++ 7 files changed, 30 insertions(+), 13 deletions(-) diff --git a/Gemfile b/Gemfile index d65e2a6..f7200f1 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,5 @@ +# frozen_string_literal: true + source 'http://rubygems.org' gemspec diff --git a/bin/git-format-staged b/bin/git-format-staged index a6772e5..d1ac2e7 100755 --- a/bin/git-format-staged +++ b/bin/git-format-staged @@ -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 diff --git a/format-staged.gemspec b/format-staged.gemspec index e69faed..ecdc19d 100644 --- a/format-staged.gemspec +++ b/format-staged.gemspec @@ -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 diff --git a/lib/format-staged.rb b/lib/format-staged.rb index 5e18a7f..d44f5aa 100644 --- a/lib/format-staged.rb +++ b/lib/format-staged.rb @@ -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) diff --git a/lib/format-staged/entry.rb b/lib/format-staged/entry.rb index 66d4f23..82c171f 100644 --- a/lib/format-staged/entry.rb +++ b/lib/format-staged/entry.rb @@ -1,6 +1,8 @@ +# frozen_string_literal: true + class FormatStaged class Entry - PATTERN = /^:(?\d+) (?\d+) (?[a-f0-9]+) (?[a-f0-9]+) (?[A-Z])(?\d+)?\t(?[^\t]+)(?:\t(?[^\t]+))?$/ + PATTERN = /^:(?\d+) (?\d+) (?[a-f0-9]+) (?[a-f0-9]+) (?[A-Z])(?\d+)?\t(?[^\t]+)(?:\t(?[^\t]+))?$/.freeze attr_reader :src_mode, :dst_mode, :src_hash, :dst_hash, :status, :score, :src_path, :dst_path, :path, :root diff --git a/lib/format-staged/io.rb b/lib/format-staged/io.rb index 9be09d1..5a80acd 100644 --- a/lib/format-staged/io.rb +++ b/lib/format-staged/io.rb @@ -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 diff --git a/lib/format-staged/version.rb b/lib/format-staged/version.rb index a695e22..eb9c0cb 100644 --- a/lib/format-staged/version.rb +++ b/lib/format-staged/version.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class FormatStaged VERSION = '0.0.1' end