diff --git a/lib/colorls/core.rb b/lib/colorls/core.rb index 6538c06..67314b5 100644 --- a/lib/colorls/core.rb +++ b/lib/colorls/core.rb @@ -227,32 +227,21 @@ module ColorLS end def process_git_status_details(git_status) - @git_status = nil - - return unless git_status - - @git_root_path = IO.popen(['git', '-C', @input, 'rev-parse', '--show-toplevel'], err: :close, &:gets) - - return false unless $CHILD_STATUS.success? - - @git_status = Git.status(@git_root_path.chomp!) + @git_status = case + when !git_status then nil + when File.directory?(@input) then Git.status(@input) + else Git.status(File.dirname(@input)) + end end - def git_info(path, content) + def git_info(content) return '' unless @git_status - real_path = File.realdirpath(content.name, path) - - return ' ' unless real_path.start_with? path - - relative_path = real_path.remove(Regexp.new('^' + Regexp.escape(@git_root_path) + '/?')) - if content.directory? - git_dir_info(relative_path) + git_dir_info(content.name) else - git_file_info(relative_path) + git_file_info(content.name) end - # puts "\n\n" end def git_file_info(path) @@ -304,7 +293,7 @@ module ColorLS [ long_info(content), - " #{git_info(path,content)} ", + " #{git_info(content)} ", logo.colorize(color), " #{name.colorize(color)}#{slash?(content)}#{symlink_info(content)}" ].join diff --git a/lib/colorls/git.rb b/lib/colorls/git.rb index 0e8f05a..8d5fd97 100644 --- a/lib/colorls/git.rb +++ b/lib/colorls/git.rb @@ -3,13 +3,18 @@ module ColorLS module Git def self.status(repo_path) + prefix = IO.popen(['git', '-C', repo_path, 'rev-parse', '--show-prefix'], err: :close, &:gets) + + return unless $CHILD_STATUS.success? + + prefix.chomp! git_status = {} - IO.popen(['git', '-C', repo_path, 'status', '--porcelain', '-z', '-unormal', '--ignored']) do |output| + IO.popen(['git', '-C', repo_path, 'status', '--porcelain', '-z', '-unormal', '--ignored', '.']) do |output| while (status_line = output.gets "\x0") mode, file = status_line.chomp("\x0").split(' ', 2) - git_status[file] = mode + git_status[file.delete_prefix(prefix)] = mode # skip the next \x0 separated original path for renames, issue #185 output.gets("\x0") if mode.start_with? 'R' diff --git a/lib/colorls/monkeys.rb b/lib/colorls/monkeys.rb index 454478c..bf51783 100644 --- a/lib/colorls/monkeys.rb +++ b/lib/colorls/monkeys.rb @@ -12,6 +12,16 @@ class String def uniq split('').uniq.join end + + unless instance_methods.include? :delete_prefix + define_method(:delete_prefix) do |prefix| + if start_with? prefix + slice(prefix.length..-1) + else + slice(0..length) + end + end + end end class Hash diff --git a/spec/color_ls/monkey_spec.rb b/spec/color_ls/monkey_spec.rb new file mode 100644 index 0000000..f5c6c35 --- /dev/null +++ b/spec/color_ls/monkey_spec.rb @@ -0,0 +1,23 @@ + +RSpec.describe "String#delete_prefix" do + # back compat for Ruby < 2.5 + + it "returns a copy of the string, with the given prefix removed" do + expect('hello'.delete_prefix('hell')).to eq 'o' + expect('hello'.delete_prefix('hello')).to eq '' + end + + it "returns a copy of the string, when the prefix isn't found" do + s = 'hello' + r = s.delete_prefix('hello!') + expect(r).not_to equal s + expect(r).to be == s + r = s.delete_prefix('ell') + expect(r).not_to equal s + expect(r).to be == s + r = s.delete_prefix('') + expect(r).not_to equal s + expect(r).to be == s + end + +end