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

Commit c88db54

Browse files
committed
Merge branch 'nested-submodules' into 'master'
Fix nested submodules Make the `Repository#submodules` method work with submodules that are nested beneath the repo's root directory. This patch uses the existing `#tree_entry` method to recurse through the commit tree as an alternative to the patch in #6. Fixes [issue 8135](https://github.com/gitlabhq/gitlabhq/issues/8135) on github. /cc @jacobvosmaer @dzaporozhets See merge request !9
2 parents e03b796 + 576a2f1 commit c88db54

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

lib/gitlab_git/repository.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -412,10 +412,10 @@ def lookup(oid_or_ref_name)
412412
# }
413413
#
414414
def submodules(ref)
415-
tree = rugged.lookup(rugged.rev_parse_oid(ref)).tree
415+
commit = rugged.rev_parse(ref)
416416

417-
content = blob_content(tree, ".gitmodules")
418-
parse_gitmodules(tree, content)
417+
content = blob_content(commit, ".gitmodules")
418+
parse_gitmodules(commit, content)
419419
end
420420

421421
# Return total commits count accessible from passed ref
@@ -717,21 +717,21 @@ def rev_parse_target(revspec)
717717
end
718718
end
719719

720-
# Get the content of a blob for a given tree. If the blob is a commit
720+
# Get the content of a blob for a given commit. If the blob is a commit
721721
# (for submodules) then return the blob's OID.
722-
def blob_content(tree, blob_name)
723-
blob_hash = tree.detect { |b| b[:name] == blob_name }
722+
def blob_content(commit, blob_name)
723+
blob_entry = tree_entry(commit, blob_name)
724724

725-
if blob_hash[:type] == :commit
726-
blob_hash[:oid]
725+
if blob_entry[:type] == :commit
726+
blob_entry[:oid]
727727
else
728-
rugged.lookup(blob_hash[:oid]).content
728+
rugged.lookup(blob_entry[:oid]).content
729729
end
730730
end
731731

732732
# Parses the contents of a .gitmodules file and returns a hash of
733733
# submodule information.
734-
def parse_gitmodules(tree, content)
734+
def parse_gitmodules(commit, content)
735735
results = {}
736736

737737
current = ""
@@ -744,7 +744,7 @@ def parse_gitmodules(tree, content)
744744
results[current][match_data[1]] = match_data[2]
745745

746746
if match_data[1] == "path"
747-
results[current]["id"] = blob_content(tree, match_data[2])
747+
results[current]["id"] = blob_content(commit, match_data[2])
748748
end
749749
end
750750
end

spec/repository_spec.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@
160160

161161
context :submodules do
162162
let(:repository) { Gitlab::Git::Repository.new(TEST_REPO_PATH) }
163-
let(:submodules) { repository.submodules(SeedRepo::Commit::ID) }
163+
let(:submodules) { repository.submodules('master') }
164164

165165
it { submodules.should be_kind_of Hash }
166166
it { submodules.empty?.should be_false }
@@ -177,6 +177,20 @@
177177
}
178178
]
179179
end
180+
181+
it 'should handle nested submodules correctly' do
182+
nested = submodules['nested/six']
183+
expect(nested['path']).to eq('nested/six')
184+
expect(nested['url']).to eq('git://github.com/randx/six.git')
185+
expect(nested['id']).to eq('24fb71c79fcabc63dfd8832b12ee3bf2bf06b196')
186+
end
187+
188+
it 'should handle deeply nested submodules correctly' do
189+
nested = submodules['deeper/nested/six']
190+
expect(nested['path']).to eq('deeper/nested/six')
191+
expect(nested['url']).to eq('git://github.com/randx/six.git')
192+
expect(nested['id']).to eq('24fb71c79fcabc63dfd8832b12ee3bf2bf06b196')
193+
end
180194
end
181195
end
182196

0 commit comments

Comments
 (0)