Skip to content

Commit 1fd1f51

Browse files
authored
Merge pull request #380 from vpierson/git-no-fetch
Improve Git performance when using SHA revisions
2 parents cb118ed + 560c6dd commit 1fd1f51

File tree

2 files changed

+48
-16
lines changed

2 files changed

+48
-16
lines changed

lib/puppet/provider/vcsrepo/git.rb

+8-3
Original file line numberDiff line numberDiff line change
@@ -503,9 +503,7 @@ def latest_revision
503503
# @return [String] Returns the tag/branch of the current repo if it's up to
504504
# date; otherwise returns the sha of the requested revision.
505505
def get_revision(rev = 'HEAD')
506-
if @resource.value(:source)
507-
update_references
508-
else
506+
unless @resource.value(:source)
509507
status = at_path { git_with_identity('status') }
510508
is_it_new = status =~ %r{Initial commit}
511509
if is_it_new
@@ -515,6 +513,13 @@ def get_revision(rev = 'HEAD')
515513
end
516514
end
517515
current = at_path { git_with_identity('rev-parse', rev).strip }
516+
if @resource.value(:revision) == current
517+
# if already pointed at desired revision, it must be a SHA, so just return it
518+
return current
519+
end
520+
if @resource.value(:source)
521+
update_references
522+
end
518523
if @resource.value(:revision)
519524
canonical = if tag_revision?
520525
# git-rev-parse will give you the hash of the tag object itself rather

spec/unit/puppet/provider/vcsrepo/git_spec.rb

+40-13
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ def branch_a_list(include_branch = nil?)
209209
end
210210
end
211211

212-
context 'when when converting repo type' do
212+
context 'when converting repo type' do
213213
context 'when with working copy to bare' do
214214
it 'converts the repo' do
215215
resource[:ensure] = :bare
@@ -276,54 +276,78 @@ def branch_a_list(include_branch = nil?)
276276
end
277277
end
278278

279-
context 'when when destroying' do
279+
context 'when destroying' do
280280
it 'removes the directory' do
281281
expects_rm_rf
282282
provider.destroy
283283
end
284284
end
285285

286-
context 'when when checking the revision property' do
286+
context 'when checking the revision property' do
287287
before(:each) do
288288
expects_chdir('/tmp/test')
289-
resource[:revision] = 'currentsha'
290289
resource[:source] = 'http://example.com'
291290
provider.stubs(:git).with('config', 'remote.origin.url').returns('')
292291
provider.stubs(:git).with('fetch', 'origin') # FIXME
293292
provider.stubs(:git).with('fetch', '--tags', 'origin')
294293
provider.stubs(:git).with('rev-parse', 'HEAD').returns('currentsha')
295-
provider.stubs(:git).with('branch', '-a').returns(branch_a_list(resource.value(:revision)))
296294
provider.stubs(:git).with('tag', '-l').returns('Hello')
297295
end
298296

299-
context 'when when its SHA is not different than the current SHA' do
297+
context 'when its a SHA and is not different than the current SHA' do
298+
it 'returns the current SHA' do
299+
resource[:revision] = 'currentsha'
300+
provider.stubs(:git).with('branch', '-a').returns(branch_a_list)
301+
provider.expects(:git).with('rev-parse', '--revs-only', resource.value(:revision)).never
302+
provider.expects(:update_references).never
303+
expect(provider.revision).to eq(resource.value(:revision))
304+
end
305+
end
306+
307+
context 'when its a SHA and is different than the current SHA' do
308+
it 'returns the current SHA' do
309+
resource[:revision] = 'othersha'
310+
provider.stubs(:git).with('branch', '-a').returns(branch_a_list)
311+
provider.expects(:git).with('rev-parse', '--revs-only', resource.value(:revision)).returns('othersha')
312+
provider.expects(:update_references)
313+
expect(provider.revision).to eq('currentsha')
314+
end
315+
end
316+
317+
context 'when its a local branch and is not different than the current SHA' do
300318
it 'returns the ref' do
319+
resource[:revision] = 'localbranch'
320+
provider.stubs(:git).with('branch', '-a').returns(branch_a_list('localbranch'))
301321
provider.expects(:git).with('rev-parse', resource.value(:revision)).returns('currentsha')
302322
provider.expects(:update_references)
303323
expect(provider.revision).to eq(resource.value(:revision))
304324
end
305325
end
306326

307-
context 'when when its SHA is different than the current SHA' do
327+
context 'when its a local branch and is different than the current SHA' do
308328
it 'returns the current SHA' do
329+
resource[:revision] = 'localbranch'
330+
provider.stubs(:git).with('branch', '-a').returns(branch_a_list('localbranch'))
309331
provider.expects(:git).with('rev-parse', resource.value(:revision)).returns('othersha')
310332
provider.expects(:update_references)
311-
expect(provider.revision).to eq(resource.value(:revision))
333+
expect(provider.revision).to eq('currentsha')
312334
end
313335
end
314336

315-
context 'when when its a ref to a remote head' do
316-
it 'returns the revision' do
337+
context 'when its a ref to a remote head' do
338+
it 'returns the ref' do
339+
resource[:revision] = 'remotebranch'
317340
provider.stubs(:git).with('branch', '-a').returns(" remotes/origin/#{resource.value(:revision)}")
318-
provider.expects(:git).with('rev-parse', "origin/#{resource.value(:revision)}").returns('newsha')
341+
provider.expects(:git).with('rev-parse', "origin/#{resource.value(:revision)}").returns('currentsha')
319342
provider.expects(:update_references)
320343
expect(provider.revision).to eq(resource.value(:revision))
321344
end
322345
end
323346

324-
context 'when when its a ref to non existant remote head' do
347+
context 'when its a ref to non existant remote head' do
325348
it 'fails' do
326-
provider.expects(:git).with('branch', '-a').returns(branch_a_list)
349+
resource[:revision] = 'remotebranch'
350+
provider.stubs(:git).with('branch', '-a').returns(branch_a_list)
327351
provider.expects(:git).with('rev-parse', '--revs-only', resource.value(:revision)).returns('')
328352
provider.expects(:update_references)
329353
expect { provider.revision }.to raise_error(RuntimeError, %r{not a local or remote ref$})
@@ -332,7 +356,10 @@ def branch_a_list(include_branch = nil?)
332356

333357
context "when there's no source" do
334358
it 'returns the revision' do
359+
resource[:revision] = 'localbranch'
335360
resource.delete(:source)
361+
provider.stubs(:git).with('branch', '-a').returns(branch_a_list('localbranch'))
362+
provider.expects(:update_references).never
336363
provider.expects(:git).with('status')
337364
provider.expects(:git).with('rev-parse', resource.value(:revision)).returns('currentsha')
338365
expect(provider.revision).to eq(resource.value(:revision))

0 commit comments

Comments
 (0)