Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/overcommit/git_repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,14 @@ def tracked?(path)
Overcommit::Utils.execute(%W[git ls-files #{path} --error-unmatch]).success?
end

# Returns the names of all files that are tracked by git.
# Returns the names of all files that are tracked by git, excluding files
# that have been deleted from the working tree.
#
# @return [Array<String>] list of absolute file paths
def all_files
`git ls-files`.
split(/\n/).
deleted_files = `git ls-files --deleted`.split(/\n/)

(`git ls-files`.split(/\n/) - deleted_files).
map { |relative_file| File.expand_path(relative_file) }.
reject { |file| File.directory?(file) } # Exclude submodule directories
end
Expand Down
10 changes: 10 additions & 0 deletions spec/overcommit/git_repo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,16 @@

it { should include(*%w[tracked staged].map { |file| File.expand_path(file) }) }
it { should_not include File.expand_path('sub') }

context 'when a tracked file is deleted but the deletion is not staged' do
around do |example|
FileUtils.rm 'tracked'
example.run
end

it { should_not include File.expand_path('tracked') }
it { should include File.expand_path('staged') }
end
end

describe '.initial_commit?' do
Expand Down
Loading