Merge pull request #183 from avdv/git-status

Improve git-status processing
This commit is contained in:
Athitya Kumar 2018-04-08 18:46:24 +00:00 committed by GitHub
commit e07f1b9341
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 34 deletions

View file

@ -1,4 +1,4 @@
lib = File.expand_path('../lib', __FILE__)
lib = File.expand_path('lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'colorls/version'

View file

@ -218,45 +218,35 @@ module ColorLS
def git_info(path, content)
return '' unless @git_status
# puts "\n\n"
real_path = File.realdirpath(content.name, path)
relative_path = path.remove(@git_root_path+'/')
relative_path = relative_path==path ? '' : relative_path+'/'
content_path = "#{relative_path}#{content}"
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(content_path)
git_dir_info(relative_path)
else
git_file_info(content_path)
git_file_info(relative_path)
end
# puts "\n\n"
end
def git_file_info(path)
return ' ✓ '.colorize(@colors[:unchanged]) unless @git_status[path]
Git.colored_status_symbols(@git_status[path], @colors)
end
Dir.class_eval do
def self.deep_entries(path)
(Dir.entries(path) - ['.', '..']).map do |entry|
if Dir.exist?("#{path}/#{entry}")
Dir.deep_entries("#{path}/#{entry}")
else
entry
end
end.flatten
end
Git.colored_status_symbols(@git_status[path].uniq, @colors)
end
def git_dir_info(path)
ignored = @git_status.select { |file, mode| file.start_with?(path) && mode=='!!' }.keys
present = Dir.deep_entries(path).map { |p| "#{path}/#{p}" }
return ' ' if (present-ignored).empty?
direct_status = @git_status.fetch("#{path}/", nil)
return Git.colored_status_symbols(direct_status.uniq, @colors) unless direct_status.nil?
modes = @git_status.select { |file, mode| file.start_with?(path) && mode != '!!' }
modes = (present-ignored).map { |file| @git_status[file] }-[nil]
return ' ✓ '.colorize(@colors[:unchanged]) if modes.empty?
Git.colored_status_symbols(modes.join.uniq, @colors)
Git.colored_status_symbols(modes.values.join.uniq, @colors)
end
def long_info(content)

View file

@ -3,9 +3,14 @@ module ColorLS
def self.status(repo_path)
@git_status = {}
IO.popen(['git', '-C', repo_path, 'status', '--porcelain', '-z', '-uall', '--ignored']) do |output|
output.read.split("\x0").map { |x| x.split(' ', 2) }.each do |mode, file|
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
# skip the next \x0 separated original path for renames, issue #185
output.gets("\x0") if mode.start_with? 'R'
end
end
warn "git status failed in #{repo_path}" unless $CHILD_STATUS.success?
@ -14,13 +19,7 @@ module ColorLS
end
def self.colored_status_symbols(modes, colors)
modes =
case modes.length
when 1 then " #{modes} "
when 2 then " #{modes} "
when 3 then "#{modes} "
when 4 then modes
end
modes = modes.rjust(3).ljust(4)
modes
.gsub('?', '?'.colorize(colors[:untracked]))