Skip to content

Commit

Permalink
Fix Git::Base#status on an empty repo
Browse files Browse the repository at this point in the history
  • Loading branch information
jcouball committed May 16, 2024
1 parent 712fdad commit c8a77db
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
13 changes: 13 additions & 0 deletions lib/git/lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,19 @@ def rm(path = '.', opts = {})
command('rm', *arr_opts)
end

# Returns true if the repository is empty (meaning it has no commits)
#
# @return [Boolean]
#
def empty?
command('rev-parse', '--verify', 'HEAD')
false
rescue Git::FailedError => e
raise unless e.result.status.exitstatus == 128 &&
e.result.stderr == 'fatal: Needed a single revision'
true
end

# Takes the commit message with the options and executes the commit command
#
# accepts options:
Expand Down
8 changes: 5 additions & 3 deletions lib/git/status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,11 @@ def fetch_modified
end

def fetch_added
# find added but not committed - new files
@base.lib.diff_index('HEAD').each do |path, data|
@files[path] ? @files[path].merge!(data) : @files[path] = data
unless @base.lib.empty?
# find added but not committed - new files
@base.lib.diff_index('HEAD').each do |path, data|
@files[path] ? @files[path].merge!(data) : @files[path] = data
end
end
end
end
Expand Down
21 changes: 21 additions & 0 deletions tests/units/test_lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,25 @@ def test_compare_version_to
assert lib.compare_version_to(2, 43, 0) == -1
assert lib.compare_version_to(3, 0, 0) == -1
end

def test_empty_when_not_empty
in_temp_dir do |path|
`git init`
`touch file1`
`git add file1`
`git commit -m "my commit message"`

git = Git.open('.')
assert_false(git.lib.empty?)
end
end

def test_empty_when_empty
in_temp_dir do |path|
`git init`

git = Git.open('.')
assert_true(git.lib.empty?)
end
end
end
10 changes: 10 additions & 0 deletions tests/units/test_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ def test_status_pretty
end
end

def test_on_empty_repo
in_temp_dir do |path|
`git init`
git = Git.open('.')
assert_nothing_raised do
git.status
end
end
end

def test_dot_files_status
in_temp_dir do |path|
git = Git.clone(@wdir, 'test_dot_files_status')
Expand Down

0 comments on commit c8a77db

Please sign in to comment.