diff --git a/lib/colorls/git.rb b/lib/colorls/git.rb index 2c5b227..ab37d3c 100644 --- a/lib/colorls/git.rb +++ b/lib/colorls/git.rb @@ -6,11 +6,11 @@ require 'set' module ColorLS module Git def self.status(repo_path) - prefix = git_prefix(repo_path) + prefix, success = git_prefix(repo_path) - return unless $CHILD_STATUS.success? + return unless success - prefix = Pathname.new(prefix.chomp) + prefix = Pathname.new(prefix) git_status = Hash.new { |hash, key| hash[key] = Set.new } @@ -52,7 +52,12 @@ module ColorLS private def git_prefix(repo_path) - IO.popen(['git', '-C', repo_path, 'rev-parse', '--show-prefix'], err: :close, &:gets) + [ + IO.popen(['git', '-C', repo_path, 'rev-parse', '--show-prefix'], err: :close, &:gets)&.chomp, + $CHILD_STATUS.success? + ] + rescue Errno::ENOENT + [nil, false] end def git_subdir_status(repo_path) diff --git a/spec/color_ls/git_spec.rb b/spec/color_ls/git_spec.rb index 9cfdaa5..d3c3b8d 100644 --- a/spec/color_ls/git_spec.rb +++ b/spec/color_ls/git_spec.rb @@ -14,14 +14,14 @@ RSpec.describe ColorLS::Git do context 'with file in repository root' do it 'returns `M`' do - allow(subject).to receive(:git_prefix).with('/repo/').and_return('') + allow(subject).to receive(:git_prefix).with('/repo/').and_return(['', true]) allow(subject).to receive(:git_subdir_status).and_yield(git_status(' M foo.txt')) expect(subject.status('/repo/')).to include('foo.txt' => Set['M']) end it 'returns `??`' do - allow(subject).to receive(:git_prefix).with('/repo/').and_return('') + allow(subject).to receive(:git_prefix).with('/repo/').and_return(['', true]) allow(subject).to receive(:git_subdir_status).and_yield(git_status('?? foo.txt')) expect(subject.status('/repo/')).to include('foo.txt' => Set['??']) @@ -30,14 +30,14 @@ RSpec.describe ColorLS::Git do context 'with file in subdir' do it 'returns `M` for subdir' do - allow(subject).to receive(:git_prefix).with('/repo/').and_return('') + allow(subject).to receive(:git_prefix).with('/repo/').and_return(['', true]) allow(subject).to receive(:git_subdir_status).and_yield(git_status(' M subdir/foo.txt')) expect(subject.status('/repo/')).to include('subdir' => Set['M']) end it 'returns `M` and `D` for subdir' do - allow(subject).to receive(:git_prefix).with('/repo/').and_return('') + allow(subject).to receive(:git_prefix).with('/repo/').and_return(['', true]) allow(subject).to receive(:git_subdir_status).and_yield(git_status(' M subdir/foo.txt', 'D subdir/other.c')) expect(subject.status('/repo/')).to include('subdir' => Set['M', 'D'])