GitDownloadStrategy optimization

The current series of fetch invocations in GitDownloadStrategy has the
unfortunate behavior of fetching full history even in shallow clones
that only need the history between the clone point and the remote HEAD.
It should be possible to determine if it is actually necessary to fetch
the full history, including all tags, and if it is not to avoid this
overhead.

Fixes Homebrew/homebrew#11958, and several other recurring problems.

Signed-off-by: Jack Nagel <jacknagel@gmail.com>
This commit is contained in:
Jack Nagel 2012-04-23 20:21:12 -05:00
parent b1d490cbed
commit b8715f6d97

View File

@ -324,14 +324,10 @@ class GitDownloadStrategy < AbstractDownloadStrategy
end end
def support_depth? def support_depth?
!commit_history_required? and depth_supported_host? @spec != :revision and host_supports_depth?
end end
def commit_history_required? def host_supports_depth?
@spec == :sha
end
def depth_supported_host?
@url =~ %r(git://) or @url =~ %r(https://github.com/) @url =~ %r(git://) or @url =~ %r(https://github.com/)
end end
@ -352,16 +348,31 @@ class GitDownloadStrategy < AbstractDownloadStrategy
unless @clone.exist? unless @clone.exist?
# Note: first-time checkouts are always done verbosely # Note: first-time checkouts are always done verbosely
git_args = %w(git clone) clone_args = %w[git clone]
git_args << "--depth" << "1" if support_depth? clone_args << '--single-branch'
git_args << @url << @clone clone_args << '--depth' << '1' if support_depth?
safe_system(*git_args)
case @spec
when :branch, :tag
clone_args << '--branch' << @ref
end
clone_args << @url << @clone
safe_system(*clone_args)
else else
puts "Updating #{@clone}" puts "Updating #{@clone}"
Dir.chdir(@clone) do Dir.chdir(@clone) do
safe_system 'git', 'remote', 'set-url', 'origin', @url safe_system 'git', 'config', 'remote.origin.url', @url
quiet_safe_system 'git', 'fetch', 'origin'
quiet_safe_system 'git', 'fetch', '--tags' if @spec == :tag safe_system 'git', 'config', 'remote.origin.fetch', case @spec
when :branch then "+refs/heads/#{@ref}:refs/remotes/origin/#{@ref}"
when :tag then "+refs/tags/#{@ref}:refs/tags/#{@ref}"
else '+refs/heads/master:refs/remotes/origin/master'
end
git_args = %w[git fetch origin]
git_args << '--depth' << '1' if support_depth?
quiet_safe_system(*git_args)
end end
end end
end end
@ -373,9 +384,9 @@ class GitDownloadStrategy < AbstractDownloadStrategy
ohai "Checking out #{@spec} #{@ref}" ohai "Checking out #{@spec} #{@ref}"
case @spec case @spec
when :branch when :branch
nostdout { quiet_safe_system 'git', 'checkout', "origin/#{@ref}" } nostdout { quiet_safe_system 'git', 'checkout', "origin/#{@ref}", '--' }
when :tag, :sha when :tag, :revision
nostdout { quiet_safe_system 'git', 'checkout', @ref } nostdout { quiet_safe_system 'git', 'checkout', @ref, '--' }
end end
else else
# otherwise the checkout-index won't checkout HEAD # otherwise the checkout-index won't checkout HEAD