File tree 4 files changed +49
-3
lines changed
4 files changed +49
-3
lines changed Original file line number Diff line number Diff line change @@ -707,6 +707,19 @@ def rm(path = '.', opts = {})
707
707
command ( 'rm' , *arr_opts )
708
708
end
709
709
710
+ # Returns true if the repository is empty (meaning it has no commits)
711
+ #
712
+ # @return [Boolean]
713
+ #
714
+ def empty?
715
+ command ( 'rev-parse' , '--verify' , 'HEAD' )
716
+ false
717
+ rescue Git ::FailedError => e
718
+ raise unless e . result . status . exitstatus == 128 &&
719
+ e . result . stderr == 'fatal: Needed a single revision'
720
+ true
721
+ end
722
+
710
723
# Takes the commit message with the options and executes the commit command
711
724
#
712
725
# accepts options:
Original file line number Diff line number Diff line change @@ -183,9 +183,11 @@ def fetch_modified
183
183
end
184
184
185
185
def fetch_added
186
- # find added but not committed - new files
187
- @base . lib . diff_index ( 'HEAD' ) . each do |path , data |
188
- @files [ path ] ? @files [ path ] . merge! ( data ) : @files [ path ] = data
186
+ unless @base . lib . empty?
187
+ # find added but not committed - new files
188
+ @base . lib . diff_index ( 'HEAD' ) . each do |path , data |
189
+ @files [ path ] ? @files [ path ] . merge! ( data ) : @files [ path ] = data
190
+ end
189
191
end
190
192
end
191
193
end
Original file line number Diff line number Diff line change @@ -318,4 +318,25 @@ def test_compare_version_to
318
318
assert lib . compare_version_to ( 2 , 43 , 0 ) == -1
319
319
assert lib . compare_version_to ( 3 , 0 , 0 ) == -1
320
320
end
321
+
322
+ def test_empty_when_not_empty
323
+ in_temp_dir do |path |
324
+ `git init`
325
+ `touch file1`
326
+ `git add file1`
327
+ `git commit -m "my commit message"`
328
+
329
+ git = Git . open ( '.' )
330
+ assert_false ( git . lib . empty? )
331
+ end
332
+ end
333
+
334
+ def test_empty_when_empty
335
+ in_temp_dir do |path |
336
+ `git init`
337
+
338
+ git = Git . open ( '.' )
339
+ assert_true ( git . lib . empty? )
340
+ end
341
+ end
321
342
end
Original file line number Diff line number Diff line change @@ -25,6 +25,16 @@ def test_status_pretty
25
25
end
26
26
end
27
27
28
+ def test_on_empty_repo
29
+ in_temp_dir do |path |
30
+ `git init`
31
+ git = Git . open ( '.' )
32
+ assert_nothing_raised do
33
+ git . status
34
+ end
35
+ end
36
+ end
37
+
28
38
def test_dot_files_status
29
39
in_temp_dir do |path |
30
40
git = Git . clone ( @wdir , 'test_dot_files_status' )
You can’t perform that action at this time.
0 commit comments