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

Commit d6c3928

Browse files
committed
Merge branch 'handle-invalid-submodule' into 'master'
Handle invalid submodule Add better error handling to the submodule processing in `Gitlab::Git::Repository`: * Return an empty hash from `#submodules` when the specified commit doesn't have a `.gitmodules` file. * Omit entries from the return value of `#submodules` when the entry in `.gitmodules` has an invalid path. Fixes #7, gitlab-org/gitlab-ce#714, and gitlab-org/gitlab-ce#763. Also fixes one of the recent [comments](https://gitlab.com/gitlab-org/gitlab-ce/issues/333#note_367106) from gitlab-org/gitlab-ce#333. /cc @jacobvosmaer @dzaporozhets See merge request !14
2 parents 38867ca + d2acab8 commit d6c3928

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
@@ -11,6 +11,7 @@ class Repository
1111
SEARCH_CONTEXT_LINES = 3
1212

1313
class NoRepository < StandardError; end
14+
class InvalidBlobName < StandardError; end
1415

1516
# Default branch in the repository
1617
attr_accessor :root_ref
@@ -416,7 +417,12 @@ def lookup(oid_or_ref_name)
416417
def submodules(ref)
417418
commit = rugged.rev_parse(ref)
418419

419-
content = blob_content(commit, ".gitmodules")
420+
begin
421+
content = blob_content(commit, ".gitmodules")
422+
rescue InvalidBlobName
423+
return {}
424+
end
425+
420426
parse_gitmodules(commit, content)
421427
end
422428

@@ -724,6 +730,10 @@ def rev_parse_target(revspec)
724730
def blob_content(commit, blob_name)
725731
blob_entry = tree_entry(commit, blob_name)
726732

733+
unless blob_entry
734+
raise InvalidBlobName.new("Invalid blob name: #{blob_name}")
735+
end
736+
727737
if blob_entry[:type] == :commit
728738
blob_entry[:oid]
729739
else
@@ -742,11 +752,16 @@ def parse_gitmodules(commit, content)
742752
current = txt.match(/(?<=").*(?=")/)[0]
743753
results[current] = {}
744754
else
755+
next unless results[current]
745756
match_data = txt.match(/(\w+)\s*=\s*(.*)/)
746757
results[current][match_data[1]] = match_data[2]
747758

748759
if match_data[1] == "path"
749-
results[current]["id"] = blob_content(commit, match_data[2])
760+
begin
761+
results[current]["id"] = blob_content(commit, match_data[2])
762+
rescue InvalidBlobName
763+
results.delete(current)
764+
end
750765
end
751766
end
752767
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)