Skip to content

Commit 560c6dd

Browse files
committed
Don't git fetch if sha revision is up-to-date
If you're using a sha as your revision parameter and the current HEAD is already pointing at that sha, there's no need to do a git fetch. Making this change should significantly reduce the number of calls made to the remote Git server; since the catalog hasn't changed, you'll be up-to-date and not do the fetch call.
1 parent f2b106f commit 560c6dd

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

lib/puppet/provider/vcsrepo/git.rb

Lines changed: 8 additions & 3 deletions
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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,8 @@ def branch_a_list(include_branch = nil?)
298298
it 'returns the current SHA' do
299299
resource[:revision] = 'currentsha'
300300
provider.stubs(:git).with('branch', '-a').returns(branch_a_list)
301-
provider.expects(:git).with('rev-parse', '--revs-only', resource.value(:revision)).returns('currentsha')
302-
provider.expects(:update_references)
301+
provider.expects(:git).with('rev-parse', '--revs-only', resource.value(:revision)).never
302+
provider.expects(:update_references).never
303303
expect(provider.revision).to eq(resource.value(:revision))
304304
end
305305
end

0 commit comments

Comments
 (0)