mirror of
https://github.com/athityakumar/colorls.git
synced 2024-09-19 05:31:23 +02:00
Merge pull request #268 from avdv/git-prefix
Improve git status handling Close #265
This commit is contained in:
commit
3fb904614d
4 changed files with 49 additions and 22 deletions
|
@ -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
|
||||
|
|
|
@ -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'
|
||||
|
|
|
@ -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
|
||||
|
|
23
spec/color_ls/monkey_spec.rb
Normal file
23
spec/color_ls/monkey_spec.rb
Normal file
|
@ -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
|
Loading…
Reference in a new issue