Skip to content

Commit e0dbd1c

Browse files
jcouballCopilot
andcommitted
feat: add deprecated empty? wrapper on Git::Repository::StatusOperations
- Add empty? deprecated wrapper to Git::Repository::StatusOperations that delegates to no_commits? and emits Git::Deprecation.warn - Add no_commits? delegator to Git::Base forwarding to facade_repository - Add empty? deprecated delegator to Git::Base forwarding to facade_repository.empty? - Add unit tests for empty? covering deprecation warning, delegation, and return value Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent fb964d3 commit e0dbd1c

3 files changed

Lines changed: 62 additions & 0 deletions

File tree

lib/git/base.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,6 +1402,23 @@ def stash_clear
14021402

14031403
# @!group Bucket 6 delegators — Git::Repository::StatusOperations
14041404

1405+
# @return [Boolean] `true` when the repository has no commits, `false` otherwise
1406+
def no_commits?
1407+
facade_repository.no_commits?
1408+
end
1409+
1410+
# @deprecated Use {#no_commits?} instead.
1411+
#
1412+
# @return [Boolean] `true` when the repository has no commits, `false` otherwise
1413+
#
1414+
def empty?
1415+
Git::Deprecation.warn(
1416+
'Git::Base#empty? is deprecated and will be removed in a future version. ' \
1417+
'Use Git::Base#no_commits? instead.'
1418+
)
1419+
no_commits?
1420+
end
1421+
14051422
# @return [Array<String>] list of untracked file paths
14061423
def untracked_files
14071424
facade_repository.untracked_files

lib/git/repository/status_operations.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,27 @@ def no_commits?
4343
true
4444
end
4545

46+
# Returns `true` if the repository has no commits yet
47+
#
48+
# @example Check whether a repository is empty
49+
# repo.empty? #=> true # freshly initialized, no commits yet
50+
# repo.empty? #=> false # at least one commit exists
51+
#
52+
# @return [Boolean] `true` when the repository has no commits, `false` otherwise
53+
#
54+
# @raise [Git::FailedError] if git exits with a non-zero exit status other
55+
# than when the repository has no commits
56+
#
57+
# @deprecated Use {#no_commits?} instead
58+
#
59+
def empty?
60+
Git::Deprecation.warn(
61+
'Git::Repository#empty? is deprecated and will be removed in a future version. ' \
62+
'Use Git::Repository#no_commits? instead.'
63+
)
64+
no_commits?
65+
end
66+
4667
# List all files in the working tree that are not tracked by git
4768
#
4869
# Runs `git ls-files --others --exclude-standard` from the working tree

spec/unit/git/repository/status_operations_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,30 @@
145145
end
146146
end
147147

148+
describe '#empty?' do
149+
subject(:result) { described_instance.empty? }
150+
151+
before do
152+
allow(described_instance).to receive(:no_commits?).and_return(true)
153+
allow(Git::Deprecation).to receive(:warn)
154+
end
155+
156+
it 'emits a deprecation warning matching /empty\? is deprecated/' do
157+
expect(Git::Deprecation).to receive(:warn).with(/empty\? is deprecated/)
158+
result
159+
end
160+
161+
it 'delegates to no_commits?' do
162+
expect(described_instance).to receive(:no_commits?).and_return(true)
163+
result
164+
end
165+
166+
it 'returns the return value of no_commits?' do
167+
allow(described_instance).to receive(:no_commits?).and_return(false)
168+
expect(result).to be(false)
169+
end
170+
end
171+
148172
describe '#untracked_files' do
149173
subject(:result) { described_instance.untracked_files }
150174

0 commit comments

Comments
 (0)