Skip to content
This repository was archived by the owner on May 12, 2018. It is now read-only.

Commit d2acab8

Browse files
committed
Improve submodule error handling
Add error handling for cases where the specified commit doesn't have a .gitmodules file, or where the .gitmodules file contains a submodule with an invalid path.
1 parent 1f476c9 commit d2acab8

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

lib/gitlab_git/repository.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class Repository
99
include Gitlab::Git::Popen
1010

1111
class NoRepository < StandardError; end
12+
class InvalidBlobName < StandardError; end
1213

1314
# Default branch in the repository
1415
attr_accessor :root_ref
@@ -414,7 +415,12 @@ def lookup(oid_or_ref_name)
414415
def submodules(ref)
415416
commit = rugged.rev_parse(ref)
416417

417-
content = blob_content(commit, ".gitmodules")
418+
begin
419+
content = blob_content(commit, ".gitmodules")
420+
rescue InvalidBlobName
421+
return {}
422+
end
423+
418424
parse_gitmodules(commit, content)
419425
end
420426

@@ -722,6 +728,10 @@ def rev_parse_target(revspec)
722728
def blob_content(commit, blob_name)
723729
blob_entry = tree_entry(commit, blob_name)
724730

731+
unless blob_entry
732+
raise InvalidBlobName.new("Invalid blob name: #{blob_name}")
733+
end
734+
725735
if blob_entry[:type] == :commit
726736
blob_entry[:oid]
727737
else
@@ -740,11 +750,16 @@ def parse_gitmodules(commit, content)
740750
current = txt.match(/(?<=").*(?=")/)[0]
741751
results[current] = {}
742752
else
753+
next unless results[current]
743754
match_data = txt.match(/(\w+)\s*=\s*(.*)/)
744755
results[current][match_data[1]] = match_data[2]
745756

746757
if match_data[1] == "path"
747-
results[current]["id"] = blob_content(commit, match_data[2])
758+
begin
759+
results[current]["id"] = blob_content(commit, match_data[2])
760+
rescue InvalidBlobName
761+
results.delete(current)
762+
end
748763
end
749764
end
750765
end

spec/repository_spec.rb

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,14 @@
160160

161161
context :submodules do
162162
let(:repository) { Gitlab::Git::Repository.new(TEST_REPO_PATH) }
163-
let(:submodules) { repository.submodules('master') }
164163

165-
it { submodules.should be_kind_of Hash }
166-
it { submodules.empty?.should be_false }
167-
168-
describe :submodule do
164+
context 'where repo has submodules' do
165+
let(:submodules) { repository.submodules('master') }
169166
let(:submodule) { submodules.first }
170167

168+
it { submodules.should be_kind_of Hash }
169+
it { submodules.empty?.should be_false }
170+
171171
it 'should have valid data' do
172172
submodule.should == [
173173
"six", {
@@ -191,6 +191,17 @@
191191
expect(nested['url']).to eq('git://github.com/randx/six.git')
192192
expect(nested['id']).to eq('24fb71c79fcabc63dfd8832b12ee3bf2bf06b196')
193193
end
194+
195+
it 'should not have an entry for an invalid submodule' do
196+
expect(submodules).not_to have_key('invalid/path')
197+
end
198+
end
199+
200+
context 'where repo doesn\'t have submodules' do
201+
let(:submodules) { repository.submodules('6d39438') }
202+
it 'should return an empty hash' do
203+
expect(submodules).to be_empty
204+
end
194205
end
195206
end
196207

0 commit comments

Comments
 (0)