From 561a403b4ef6a04b1c819c21ed11d0024f2a9438 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Wed, 25 May 2022 19:13:01 +0200 Subject: [PATCH] Run rubocop -a --- bin/git-format-staged | 37 ++++---- format-staged.gemspec | 10 +- lib/format-staged.rb | 174 +++++++++++++++++------------------ lib/format-staged/entry.rb | 12 +-- lib/format-staged/io.rb | 30 +++--- lib/format-staged/version.rb | 4 +- 6 files changed, 134 insertions(+), 133 deletions(-) diff --git a/bin/git-format-staged b/bin/git-format-staged index 7a35a41..a6772e5 100755 --- a/bin/git-format-staged +++ b/bin/git-format-staged @@ -3,38 +3,41 @@ require 'format-staged' require 'optparse' parameters = { - :update => true, - :write => true, - :verbose => false, + update: true, + write: true, + verbose: false } parser = OptionParser.new do |opt| opt.banner = "Usage: #{opt.program_name} [options] [patterns]" - opt.separator "" - opt.on('-f', '--formatter COMMAND', 'Shell command to format files, will run once per file. Occurrences of the placeholder `{}` will be replaced with a path to the file being formatted. (Example: "prettier --stdin-filepath \'{}\'")') do |o| + opt.separator '' + opt.on('-f', '--formatter COMMAND', + 'Shell command to format files, will run once per file. Occurrences of the placeholder `{}` will be replaced with a path to the file being formatted. (Example: "prettier --stdin-filepath \'{}\'")') do |o| parameters[:formatter] = o end - - opt.on('--[no-]update-working-tree', 'By default formatting changes made to staged file content will also be applied to working tree files via a patch. This option disables that behavior, leaving working tree files untouched.') do |value| + + opt.on('--[no-]update-working-tree', + 'By default formatting changes made to staged file content will also be applied to working tree files via a patch. This option disables that behavior, leaving working tree files untouched.') do |value| parameters[:update] = value end - - opt.on('--[no-]write', "Prevents #{opt.program_name} from modifying staged or working tree files. You can use this option to check staged changes with a linter instead of formatting. With this option stdout from the formatter command is ignored.") do |value| + + opt.on('--[no-]write', + "Prevents #{opt.program_name} from modifying staged or working tree files. You can use this option to check staged changes with a linter instead of formatting. With this option stdout from the formatter command is ignored.") do |value| parameters[:write] = value end - - opt.on("-v", "--[no-]verbose", 'Shows commands being run') do |value| + + opt.on('-v', '--[no-]verbose', 'Shows commands being run') do |value| parameters[:verbose] = value end - opt.separator "" - + opt.separator '' + opt.on_tail('-h', '--help', 'Prints this help') do puts opt exit end - - opt.on_tail('--version', "Prints the version number and exits") do + + opt.on_tail('--version', 'Prints the version number and exits') do puts FormatStaged::VERSION exit end @@ -44,8 +47,8 @@ parser.parse! parameters[:patterns] = ARGV if !parameters[:formatter] or parameters[:patterns].empty? - puts "Missing formatter or file patterns!" - + puts 'Missing formatter or file patterns!' + puts parser exit end diff --git a/format-staged.gemspec b/format-staged.gemspec index 0ab79ad..e69faed 100644 --- a/format-staged.gemspec +++ b/format-staged.gemspec @@ -1,18 +1,18 @@ -lib = File.expand_path("lib", __dir__) +lib = File.expand_path('lib', __dir__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'format-staged/version' Gem::Specification.new do |s| s.name = 'format-staged' s.version = FormatStaged::VERSION - s.summary = "git format staged!" - s.description = "git format staged" - s.authors = ["Sven Weidauer"] + s.summary = 'git format staged!' + s.description = 'git format staged' + s.authors = ['Sven Weidauer'] s.email = 'sven@5sw.de' s.files = Dir['lib/**/*.rb'] s.executables << 'git-format-staged' s.homepage = 'https://github.com/5sw/format-staged' s.license = 'MIT' - + s.add_development_dependency 'rubocop', '~> 1.29' end diff --git a/lib/format-staged.rb b/lib/format-staged.rb index 69e7c48..5e18a7f 100644 --- a/lib/format-staged.rb +++ b/lib/format-staged.rb @@ -3,107 +3,107 @@ require 'format-staged/entry' require 'format-staged/io' require 'shellwords' -class FormatStaged +class FormatStaged attr_reader :formatter, :patterns, :update, :write, :verbose - - def initialize(formatter:, patterns:, update: true, write: true, verbose: true) + + def initialize(formatter:, patterns:, update: true, write: true, verbose: true) @formatter = formatter @patterns = patterns @update = update @write = write @verbose = verbose end - - def run() + + def run root = get_output('git', 'rev-parse', '--show-toplevel').first - + files = get_output('git', 'diff-index', '--cached', '--diff-filter=AM', '--no-renames', 'HEAD') - .map { |line| Entry.new(line, root: root) } - .reject { |entry| entry.symlink? } - .filter { |entry| entry.matches?(@patterns) } - - files.each do |file| - format_file(file) - end - end - - def format_file(file) - new_hash = format_object file - - return true if not write + .map { |line| Entry.new(line, root: root) } + .reject { |entry| entry.symlink? } + .filter { |entry| entry.matches?(@patterns) } - if new_hash == file.dst_hash - puts "Unchanged #{file.src_path}" - return false - end - - if object_is_empty new_hash - puts "Skipping #{file.src_path}, formatted file is empty" - return false - end + files.each do |file| + format_file(file) + end + end - replace_file_in_index file, new_hash - - if update - begin - patch_working_file file, new_hash - rescue => error - puts "Warning: failed updating #{file.src_path} in working copy: #{error}" - end - end - - true - end - - def format_object(file) - puts "Formatting #{file.src_path}" - - format_command = formatter.sub("{}", file.src_path.shellescape) - - pid1, r = pipe_command "git", "cat-file", "-p", file.dst_hash - 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 } - if @verbose - result.each do |line| - puts "< #{line}" - end - end - - Process.wait pid1 - raise "Cannot read #{file.dst_hash} from object database" unless $?.success? - - Process.wait pid2 - raise "Error formatting #{file.src_path}" unless $?.success? - - Process.wait pid3 - raise "Error writing formatted file back to object database" unless $?.success? && !result.empty? - - result.first - end - - def object_is_empty(hash) - size = get_output("git", "cat-file", "-s", hash).first.to_i - size == 0 + def format_file(file) + new_hash = format_object file + + return true unless write + + if new_hash == file.dst_hash + puts "Unchanged #{file.src_path}" + return false end - def patch_working_file(file, new_hash) - patch = get_output "git", "diff", file.dst_hash, new_hash, lines: false - patch.gsub! "a/#{file.dst_hash}", "a/#{file.src_path}" - patch.gsub! "b/#{new_hash}", "b/#{file.src_path}" - - input, patch_out = IO.pipe - pid, r = pipe_command "git", "apply", "-", source: input - - patch_out.write patch - patch_out.close - - Process.wait pid - raise "Error applying patch" unless $?.success? + if object_is_empty new_hash + puts "Skipping #{file.src_path}, formatted file is empty" + return false end - def replace_file_in_index(file, new_hash) - get_output "git", "update-index", "--cacheinfo", "#{file.dst_mode},#{new_hash},#{file.src_path}" + replace_file_in_index file, new_hash + + if update + begin + patch_working_file file, new_hash + rescue StandardError => e + puts "Warning: failed updating #{file.src_path} in working copy: #{e}" + end end + + true + end + + def format_object(file) + puts "Formatting #{file.src_path}" + + format_command = formatter.sub('{}', file.src_path.shellescape) + + pid1, r = pipe_command 'git', 'cat-file', '-p', file.dst_hash + 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 } + if @verbose + result.each do |line| + puts "< #{line}" + end + end + + Process.wait pid1 + raise "Cannot read #{file.dst_hash} from object database" unless $?.success? + + Process.wait pid2 + raise "Error formatting #{file.src_path}" unless $?.success? + + Process.wait pid3 + raise 'Error writing formatted file back to object database' unless $?.success? && !result.empty? + + result.first + end + + def object_is_empty(hash) + size = get_output('git', 'cat-file', '-s', hash).first.to_i + size == 0 + end + + def patch_working_file(file, new_hash) + patch = get_output 'git', 'diff', file.dst_hash, new_hash, lines: false + patch.gsub! "a/#{file.dst_hash}", "a/#{file.src_path}" + patch.gsub! "b/#{new_hash}", "b/#{file.src_path}" + + input, patch_out = IO.pipe + pid, r = pipe_command 'git', 'apply', '-', source: input + + patch_out.write patch + patch_out.close + + Process.wait pid + raise 'Error applying patch' unless $?.success? + end + + def replace_file_in_index(file, new_hash) + get_output 'git', 'update-index', '--cacheinfo', "#{file.dst_mode},#{new_hash},#{file.src_path}" + end end diff --git a/lib/format-staged/entry.rb b/lib/format-staged/entry.rb index 5f58847..66d4f23 100644 --- a/lib/format-staged/entry.rb +++ b/lib/format-staged/entry.rb @@ -1,9 +1,9 @@ -class FormatStaged +class FormatStaged class Entry PATTERN = /^:(?\d+) (?\d+) (?[a-f0-9]+) (?[a-f0-9]+) (?[A-Z])(?\d+)?\t(?[^\t]+)(?:\t(?[^\t]+))?$/ attr_reader :src_mode, :dst_mode, :src_hash, :dst_hash, :status, :score, :src_path, :dst_path, :path, :root - + def initialize(line, root:) matches = line.match(PATTERN) or raise "Cannot parse output #{line}" @src_mode = matches[:src_mode] @@ -17,17 +17,15 @@ class FormatStaged @path = File.expand_path(@src_path, root) @root = root end - + def symlink? @dst_mode == '120000' end - + def matches?(patterns) result = false patterns.each do |pattern| - if File.fnmatch? pattern, path, File::FNM_EXTGLOB - result = true - end + result = true if File.fnmatch? pattern, path, File::FNM_EXTGLOB end result end diff --git a/lib/format-staged/io.rb b/lib/format-staged/io.rb index 44a447b..9be09d1 100644 --- a/lib/format-staged/io.rb +++ b/lib/format-staged/io.rb @@ -1,7 +1,7 @@ -class FormatStaged +class FormatStaged def get_output(*args, lines: true) puts '> ' + args.join(' ') if @verbose - + output = IO.popen(args, err: :err) do |io| if lines io.readlines.map { |l| l.chomp } @@ -9,32 +9,32 @@ class FormatStaged io.read end end - + if @verbose and lines output.each do |line| puts "< #{line}" end end - - raise "Failed to run command" unless $?.success? - + + raise 'Failed to run command' unless $?.success? + output end - + def pipe_command(*args, source: nil) - puts (source.nil? ? '> ' : '| ') + args.join(' ') if @verbose + puts (source.nil? ? '> ' : '| ') + args.join(' ') if @verbose r, w = IO.pipe - + opts = {} opts[:in] = source unless source.nil? opts[:out] = w opts[:err] = :err - + pid = spawn(*args, **opts) - + w.close - source &.close - - return [pid, r] - end + source&.close + + [pid, r] + end end diff --git a/lib/format-staged/version.rb b/lib/format-staged/version.rb index dc206b1..a695e22 100644 --- a/lib/format-staged/version.rb +++ b/lib/format-staged/version.rb @@ -1,3 +1,3 @@ class FormatStaged - VERSION = "0.0.1" -end \ No newline at end of file + VERSION = '0.0.1' +end