From d2a41716b235d36326f334d5e35db6eaaa3471e0 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Wed, 25 May 2022 22:02:48 +0200 Subject: [PATCH 1/7] Add rdoc comments to classes. --- lib/format-staged/entry.rb | 4 ++++ lib/format_staged.rb | 2 ++ 2 files changed, 6 insertions(+) diff --git a/lib/format-staged/entry.rb b/lib/format-staged/entry.rb index 3684b7d..4a5962d 100644 --- a/lib/format-staged/entry.rb +++ b/lib/format-staged/entry.rb @@ -1,6 +1,10 @@ # frozen_string_literal: true class FormatStaged + ## + # Entry in the git index. + # + # Data as produced by `git diff-index` class Entry PATTERN = /^ :(?\d+)\s diff --git a/lib/format_staged.rb b/lib/format_staged.rb index 2201c21..bac0ab4 100644 --- a/lib/format_staged.rb +++ b/lib/format_staged.rb @@ -6,6 +6,8 @@ require 'format-staged/entry' require 'format-staged/io' require 'shellwords' +## +# Runs staged changes through a formatting tool class FormatStaged attr_reader :formatter, :patterns, :update, :write, :verbose -- 2.45.3 From bb0a3a80c446bdadc46778c2db8e5dc38a5d8f1e Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Thu, 26 May 2022 09:16:14 +0200 Subject: [PATCH 2/7] Add colorize gem + cli option --- Gemfile.lock | 2 ++ bin/git-format-staged | 4 ++++ format-staged.gemspec | 2 ++ lib/format-staged/io.rb | 1 + lib/format_staged.rb | 5 ++++- 5 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Gemfile.lock b/Gemfile.lock index 049ae52..23520a5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,11 +2,13 @@ PATH remote: . specs: format-staged (0.0.3) + colorize GEM remote: http://rubygems.org/ specs: ast (2.4.2) + colorize (0.8.1) parallel (1.22.1) parser (3.1.2.0) ast (~> 2.4.1) diff --git a/bin/git-format-staged b/bin/git-format-staged index 6e20263..faa78af 100755 --- a/bin/git-format-staged +++ b/bin/git-format-staged @@ -57,6 +57,10 @@ parser = OptionParser.new do |opt| puts FormatStaged::VERSION exit end + + opt.on('--[no-]color', 'Colorizes output') do |value| + parameters[:color_output] = value + end end parser.parse! diff --git a/format-staged.gemspec b/format-staged.gemspec index bf7935a..ac7abdc 100644 --- a/format-staged.gemspec +++ b/format-staged.gemspec @@ -16,6 +16,8 @@ Gem::Specification.new do |s| s.homepage = 'https://github.com/5sw/format-staged' s.license = 'MIT' s.required_ruby_version = '~> 2.7' + + s.add_dependency 'colorize' s.add_development_dependency 'rake', '~> 13.0' s.add_development_dependency 'rubocop', '~> 1.29' diff --git a/lib/format-staged/io.rb b/lib/format-staged/io.rb index 6e99768..df472e3 100644 --- a/lib/format-staged/io.rb +++ b/lib/format-staged/io.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'English' + class FormatStaged def get_output(*args, lines: true, silent: false) puts "> #{args.join(' ')}" if @verbose diff --git a/lib/format_staged.rb b/lib/format_staged.rb index bac0ab4..d651d1c 100644 --- a/lib/format_staged.rb +++ b/lib/format_staged.rb @@ -5,18 +5,21 @@ require 'format-staged/version' require 'format-staged/entry' require 'format-staged/io' require 'shellwords' +require 'colorize' ## # Runs staged changes through a formatting tool 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, color_output: nil) @formatter = formatter @patterns = patterns @update = update @write = write @verbose = verbose + + String.disable_colorization = !(color_output || STDOUT.isatty) end def run -- 2.45.3 From eb0aa2105ce17c81b470a2c0862106ba0159c002 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Thu, 26 May 2022 09:29:09 +0200 Subject: [PATCH 3/7] Lint --- lib/format_staged.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/format_staged.rb b/lib/format_staged.rb index d651d1c..0eb0d9c 100644 --- a/lib/format_staged.rb +++ b/lib/format_staged.rb @@ -19,7 +19,7 @@ class FormatStaged @write = write @verbose = verbose - String.disable_colorization = !(color_output || STDOUT.isatty) + String.disable_colorization = !(color_output || $stdout.isatty) end def run -- 2.45.3 From b8b7cc8d12a2ba567ca46afe1bd3dd040eeec8a4 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Thu, 26 May 2022 09:32:49 +0200 Subject: [PATCH 4/7] Cleanup --- bin/git-format-staged | 2 +- format-staged.gemspec | 2 +- lib/format-staged/io.rb | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/bin/git-format-staged b/bin/git-format-staged index faa78af..f325ac3 100755 --- a/bin/git-format-staged +++ b/bin/git-format-staged @@ -57,7 +57,7 @@ parser = OptionParser.new do |opt| puts FormatStaged::VERSION exit end - + opt.on('--[no-]color', 'Colorizes output') do |value| parameters[:color_output] = value end diff --git a/format-staged.gemspec b/format-staged.gemspec index ac7abdc..6711751 100644 --- a/format-staged.gemspec +++ b/format-staged.gemspec @@ -16,7 +16,7 @@ Gem::Specification.new do |s| s.homepage = 'https://github.com/5sw/format-staged' s.license = 'MIT' s.required_ruby_version = '~> 2.7' - + s.add_dependency 'colorize' s.add_development_dependency 'rake', '~> 13.0' diff --git a/lib/format-staged/io.rb b/lib/format-staged/io.rb index df472e3..c7cdaf5 100644 --- a/lib/format-staged/io.rb +++ b/lib/format-staged/io.rb @@ -31,15 +31,15 @@ class FormatStaged [pid, r] end - def read_output(r, lines: true, silent: false) - result = r.read + def read_output(output, lines: true, silent: false) + result = output.read splits = result.split("\n") if @verbose && !silent splits.each do |line| puts "< #{line}" end end - r.close + output.close lines ? splits : result end -- 2.45.3 From 4fea7d525e890c78a7bbdac7008fb6c976dfb6da Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Sun, 29 May 2022 09:25:09 +0200 Subject: [PATCH 5/7] Colour output --- lib/format-staged/io.rb | 16 ++++++++++++++++ lib/format_staged.rb | 11 +++++++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/format-staged/io.rb b/lib/format-staged/io.rb index c7cdaf5..04b633b 100644 --- a/lib/format-staged/io.rb +++ b/lib/format-staged/io.rb @@ -43,4 +43,20 @@ class FormatStaged lines ? splits : result end + + def fail!(message) + abort "💣 #{message.red}" + end + + def warning(message) + warn "⚠️ #{message.yellow}" + end + + def info(message) + puts message.blue + end + + def verbose_info(message) + puts "ℹ️ #{message}" if verbose + end end diff --git a/lib/format_staged.rb b/lib/format_staged.rb index 0eb0d9c..804dbf3 100644 --- a/lib/format_staged.rb +++ b/lib/format_staged.rb @@ -23,8 +23,11 @@ class FormatStaged end def run + verbose_info "Finding repository root" root = get_output('git', 'rev-parse', '--show-toplevel').first + verbose_info "Repo at #{root}" + verbose_info "Listing staged files" files = get_output('git', 'diff-index', '--cached', '--diff-filter=AM', '--no-renames', 'HEAD') .map { |line| Entry.new(line, root: root) } .reject(&:symlink?) @@ -41,12 +44,12 @@ class FormatStaged return true unless write if new_hash == file.dst_hash - puts "Unchanged #{file.src_path}" + info "Unchanged #{file.src_path}" return false end if object_is_empty new_hash - puts "Skipping #{file.src_path}, formatted file is empty" + info "Skipping #{file.src_path}, formatted file is empty" return false end @@ -56,7 +59,7 @@ class FormatStaged begin patch_working_file file, new_hash rescue StandardError => e - puts "Warning: failed updating #{file.src_path} in working copy: #{e}" + warning "failed updating #{file.src_path} in working copy: #{e}" end end @@ -64,7 +67,7 @@ class FormatStaged end def format_object(file) - puts "Formatting #{file.src_path}" + info "Formatting #{file.src_path}" format_command = formatter.sub('{}', file.src_path.shellescape) -- 2.45.3 From 77bab64a1249aaa87d724cc5076f3080f7a0be25 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Sun, 29 May 2022 09:28:16 +0200 Subject: [PATCH 6/7] Message for updating the working copy --- lib/format_staged.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/format_staged.rb b/lib/format_staged.rb index 804dbf3..4bbcd86 100644 --- a/lib/format_staged.rb +++ b/lib/format_staged.rb @@ -95,6 +95,8 @@ class FormatStaged end def patch_working_file(file, new_hash) + info "Updating working copy" + patch = get_output 'git', 'diff', file.dst_hash, new_hash, lines: false, silent: true patch.gsub! "a/#{file.dst_hash}", "a/#{file.src_path}" patch.gsub! "b/#{new_hash}", "b/#{file.src_path}" -- 2.45.3 From 2b75e3b6086392be6c2618aed43a160e135907fc Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Sun, 29 May 2022 09:29:08 +0200 Subject: [PATCH 7/7] =?UTF-8?q?Don=E2=80=99t=20split=20lines=20where=20not?= =?UTF-8?q?=20needed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/format_staged.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/format_staged.rb b/lib/format_staged.rb index 4bbcd86..0cae333 100644 --- a/lib/format_staged.rb +++ b/lib/format_staged.rb @@ -24,7 +24,7 @@ class FormatStaged def run verbose_info "Finding repository root" - root = get_output('git', 'rev-parse', '--show-toplevel').first + root = get_output('git', 'rev-parse', '--show-toplevel', lines: false).chomp verbose_info "Repo at #{root}" verbose_info "Listing staged files" -- 2.45.3