Skip to content

Commit 8f60a12

Browse files
authored
Merge pull request #148 from OpenGeoMetadata/prevent-nested-clones
Avoid nesting cloned OGM repos inside themselves
2 parents e7fd211 + b2f00e6 commit 8f60a12

File tree

3 files changed

+46
-34
lines changed

3 files changed

+46
-34
lines changed

lib/geo_combine/harvester.rb

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,27 +53,35 @@ def docs_to_index
5353
end
5454
end
5555

56-
# Update a repository via git, or all repositories if none specified.
57-
# If the repository doesn't exist, clone it. Return the count of repositories updated.
58-
def pull(repo = nil)
59-
return repositories.map(&method(:pull)).reduce(:+) unless repo
60-
56+
# Update a repository via git
57+
# If the repository doesn't exist, clone it.
58+
def pull(repo)
6159
repo_path = File.join(@ogm_path, repo)
6260
clone(repo) unless File.directory? repo_path
6361

6462
Git.open(repo_path).pull && 1
6563
end
6664

67-
# Clone a repository via git, or all repositories if none specified.
68-
# If the repository already exists, skip it. Return the count of repositories cloned.
69-
def clone(repo = nil)
70-
return repositories.map(&method(:clone)).reduce(:+) unless repo
65+
# Update all repositories
66+
# Return the count of repositories updated
67+
def pull_all
68+
repositories.map(&method(:pull)).reduce(:+)
69+
end
7170

71+
# Clone a repository via git
72+
# If the repository already exists, skip it.
73+
def clone(repo)
7274
repo_path = File.join(@ogm_path, repo)
7375
return 0 if File.directory? repo_path
7476

7577
repo_url = "https://github.com/OpenGeoMetadata/#{repo}.git"
76-
Git.clone(repo_url, repo, path: repo_path, depth: 1) && 1
78+
Git.clone(repo_url, nil, path: ogm_path, depth: 1) && 1
79+
end
80+
81+
# Clone all repositories via git
82+
# Return the count of repositories cloned.
83+
def clone_all
84+
repositories.map(&method(:clone)).reduce(:+)
7785
end
7886

7987
private

lib/tasks/geo_combine.rake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ namespace :geocombine do
1212
desc 'Clone OpenGeoMetadata repositories'
1313
task :clone, [:repo] do |_t, args|
1414
harvester = GeoCombine::Harvester.new
15-
total = harvester.clone(args.repo)
15+
total = args[:repo] ? harvester.clone(args.repo) : harvester.clone_all
1616
puts "Cloned #{total} repositories"
1717
end
1818

1919
desc '"git pull" OpenGeoMetadata repositories'
2020
task :pull, [:repo] do |_t, args|
2121
harvester = GeoCombine::Harvester.new
22-
total = harvester.pull(args.repo)
22+
total = args[:repo] ? harvester.pull(args.repo) : harvester.pull_all
2323
puts "Updated #{total} repositories"
2424
end
2525

spec/lib/geo_combine/harvester_spec.rb

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,26 @@
4949
expect(stub_repo).to have_received(:pull)
5050
end
5151

52+
it 'clones a repo before pulling if it does not exist' do
53+
harvester.pull(repo_name)
54+
expect(Git).to have_received(:clone)
55+
end
56+
end
57+
58+
describe '#pull_all' do
5259
it 'can pull all repositories' do
53-
harvester.pull
60+
harvester.pull_all
5461
expect(Git).to have_received(:open).exactly(2).times
5562
expect(stub_repo).to have_received(:pull).exactly(2).times
5663
end
5764

58-
it 'skips repositories in the denylist' do
59-
harvester.pull
60-
expect(Git).not_to have_received(:open).with('https://github.com/OpenGeoMetadata/aardvark.git')
61-
end
62-
63-
it 'clones a repo before pulling if it does not exist' do
64-
harvester.pull(repo_name)
65-
expect(Git).to have_received(:clone)
65+
it 'returns the count of repositories pulled' do
66+
expect(harvester.pull_all).to eq(2)
6667
end
6768

68-
it 'returns the count of repositories pulled' do
69-
expect(harvester.pull).to eq(2)
69+
it 'skips repositories in the denylist' do
70+
harvester.pull_all
71+
expect(Git).not_to have_received(:open).with('https://github.com/OpenGeoMetadata/aardvark.git')
7072
end
7173
end
7274

@@ -75,31 +77,33 @@
7577
harvester.clone(repo_name)
7678
expect(Git).to have_received(:clone).with(
7779
repo_url,
78-
repo_name, {
80+
nil, {
7981
depth: 1, # shallow clone
80-
path: repo_path
82+
path: harvester.ogm_path
8183
}
8284
)
8385
end
8486

87+
it 'skips repositories that already exist' do
88+
allow(File).to receive(:directory?).with(repo_path).and_return(true)
89+
harvester.clone(repo_name)
90+
expect(Git).not_to have_received(:clone)
91+
end
92+
end
93+
94+
describe '#clone_all' do
8595
it 'can clone all repositories' do
86-
harvester.clone
96+
harvester.clone_all
8797
expect(Git).to have_received(:clone).exactly(2).times
8898
end
8999

90100
it 'skips repositories in the denylist' do
91-
harvester.pull
101+
harvester.clone_all
92102
expect(Git).not_to have_received(:clone).with('https://github.com/OpenGeoMetadata/aardvark.git')
93103
end
94104

95-
it 'skips repositories that already exist' do
96-
allow(File).to receive(:directory?).with(repo_path).and_return(true)
97-
harvester.clone(repo_name)
98-
expect(Git).not_to have_received(:clone)
99-
end
100-
101105
it 'returns the count of repositories cloned' do
102-
expect(harvester.clone).to eq(2)
106+
expect(harvester.clone_all).to eq(2)
103107
end
104108
end
105109
end

0 commit comments

Comments
 (0)