Skip to content

When core.ignorecase, check Status for files case-insensitively #724

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
34 changes: 30 additions & 4 deletions lib/git/status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ def changed
# changed?('lib/git.rb')
# @return [Boolean]
def changed?(file)
changed.member?(file)
if ignore_case?
changed.keys.map(&:downcase).include?(file.downcase)
else
changed.member?(file)
end
end

# Returns an Enumerable containing files that have been added.
Expand All @@ -54,7 +58,11 @@ def added
# added?('lib/git.rb')
# @return [Boolean]
def added?(file)
added.member?(file)
if ignore_case?
added.keys.map(&:downcase).include?(file.downcase)
else
added.member?(file)
end
end

#
Expand All @@ -75,7 +83,11 @@ def deleted
# deleted?('lib/git.rb')
# @return [Boolean]
def deleted?(file)
deleted.member?(file)
if ignore_case?
deleted.keys.map(&:downcase).include?(file.downcase)
else
deleted.member?(file)
end
end

#
Expand All @@ -96,7 +108,11 @@ def untracked
# untracked?('lib/git.rb')
# @return [Boolean]
def untracked?(file)
untracked.member?(file)
if ignore_case?
untracked.keys.map(&:downcase).include?(file.downcase)
else
untracked.member?(file)
end
end

def pretty
Expand Down Expand Up @@ -264,5 +280,15 @@ def fetch_added
end
end
end

# It's worth noting that (like git itself) this gem will not behave well if
# ignoreCase is set inconsistently with the file-system itself. For details:
# https://git-scm.com/docs/git-config#Documentation/git-config.txt-coreignoreCase
def ignore_case?
return @_ignore_case if defined?(@_ignore_case)
@_ignore_case = @base.config('core.ignoreCase') == 'true'
rescue Git::FailedError
@_ignore_case = false
end
end
end
23 changes: 23 additions & 0 deletions tests/units/test_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def test_dot_files_status
def test_added_boolean
in_temp_dir do |path|
git = Git.clone(@wdir, 'test_dot_files_status')
git.config('core.ignorecase', 'false')

create_file('test_dot_files_status/test_file_1', 'content tets_file_1')
create_file('test_dot_files_status/test_file_2', 'content tets_file_2')
Expand All @@ -100,12 +101,17 @@ def test_added_boolean

assert(git.status.added?('test_file_1'))
assert(!git.status.added?('test_file_2'))
assert(!git.status.added?('TEST_FILE_1'))

git.config('core.ignorecase', 'true')
assert(git.status.added?('TEST_FILE_1'))
end
end

def test_changed_boolean
in_temp_dir do |path|
git = Git.clone(@wdir, 'test_dot_files_status')
git.config('core.ignorecase', 'false')

create_file('test_dot_files_status/test_file_1', 'content tets_file_1')
create_file('test_dot_files_status/test_file_2', 'content tets_file_2')
Expand All @@ -117,12 +123,20 @@ def test_changed_boolean

assert(git.status.changed?('test_file_1'))
assert(!git.status.changed?('test_file_2'))

update_file('test_dot_files_status/scott/text.txt', 'definitely different')
assert(git.status.changed?('scott/text.txt'))
assert(!git.status.changed?('scott/TEXT.txt'))

git.config('core.ignorecase', 'true')
assert(git.status.changed?('scott/TEXT.txt'))
end
end

def test_deleted_boolean
in_temp_dir do |path|
git = Git.clone(@wdir, 'test_dot_files_status')
git.config('core.ignorecase', 'false')

create_file('test_dot_files_status/test_file_1', 'content tets_file_1')
create_file('test_dot_files_status/test_file_2', 'content tets_file_2')
Expand All @@ -133,6 +147,10 @@ def test_deleted_boolean

assert(git.status.deleted?('test_file_1'))
assert(!git.status.deleted?('test_file_2'))
assert(!git.status.deleted?('TEST_FILE_1'))

git.config('core.ignorecase', 'true')
assert(git.status.deleted?('TEST_FILE_1'))
end
end

Expand Down Expand Up @@ -189,13 +207,18 @@ def test_untracked_from_subdir
def test_untracked_boolean
in_temp_dir do |path|
git = Git.clone(@wdir, 'test_dot_files_status')
git.config('core.ignorecase', 'false')

create_file('test_dot_files_status/test_file_1', 'content tets_file_1')
create_file('test_dot_files_status/test_file_2', 'content tets_file_2')
git.add('test_file_2')

assert(git.status.untracked?('test_file_1'))
assert(!git.status.untracked?('test_file_2'))
assert(!git.status.untracked?('TEST_FILE_1'))

git.config('core.ignorecase', 'true')
assert(git.status.untracked?('TEST_FILE_1'))
end
end

Expand Down