8000 [Downloader::Git] Cache repos by fabiopelosin · Pull Request #248 · CocoaPods/CocoaPods · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[Downloader::Git] Cache repos #248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 9, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/cocoapods/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def self.instance=(instance)
attr_accessor :clean, :verbose, :silent
attr_accessor :generate_docs, :doc_install, :force_doc
attr_accessor :integrate_targets
attr_accessor :git_cache_size

alias_method :clean?, :clean
alias_method :verbose?, :verbose
Expand All @@ -27,6 +28,7 @@ def initialize
@repos_dir = Pathname.new(File.expand_path("~/.cocoapods"))
@verbose = @silent = @force_doc = false
@clean = @generate_docs = @doc_install = @integrate_targets = true
@git_cache_size = 500
end

def project_root
Expand Down
4 changes: 2 additions & 2 deletions lib/cocoapods/downloader.rb
10000
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ def initialize(target_path, url, options)
def clean
# implement in sub-classes
end

private

def self.for_target(target_path, options)
options = options.dup
if url = options.delete(:git)
Expand Down
68 changes: 64 additions & 4 deletions lib/cocoapods/downloader/git.rb
Original file line number Diff line number Diff line change
@@ -1,38 +1,98 @@
require 'open-uri'
require 'tempfile'
require 'zlib'
require 'digest/sha1'

module Pod
class Downloader
class Git < Downloader
include Config::Mixin
executable :git

def download
prepare_cache
puts '->'.green << ' Cloning git repo' if config.verbose?
if options[:tag]
download_tag
elsif options[:commit]
download_commit
else
download_head
end
removed_cached_repos_if_needed
end

def prepare_cache
return if config.git_cache_size == 0
if is_cache_valid?
puts '->'.green << " Updating cache git repo (#{cache_path})" if config.verbose?
Dir.chdir(cache_path) do
git "reset --hard HEAD"
git "clean -d -x -f"
git "pull"
end
else
puts '->'.green << " Creating cache git repo (#{cache_path})" if config.verbose?
cache_path.rmtree if cache_path.exist?
cache_path.mkpath
git "clone '#{url}' #{cache_path}"
end
end

def removed_cached_repos_if_needed
return unless caches_dir.exist?
Dir.chdir(caches_dir) do
repos = Pathname.new(caches_dir).children.select { |c| c.directory? }.sort_by(&:ctime)
while caches_size >= config.git_cache_size && !repos.empty?
dir = repos.shift
puts '->'.yellow << " Removing git cache for `#{origin_url(dir)}'" if config.verbose?
dir.rmtree
end
end
end

def cache_path
@cache_path ||= caches_dir + "#{Digest::SHA1.hexdigest(url.to_s)}"
end

def is_cache_valid?
cache_path.exist? && origin_url(cache_path) == url
end

def origin_url(dir)
Dir.chdir(dir) { `git config remote.origin.url`.chomp }
end

def caches_dir
Pathname.new "/var/tmp/CocoaPods/Git"
end

def clone_url
# git_cache_size = 0 disables the cache
config.git_cache_size == 0 ? url : cache_path
end

def caches_size
# expressed in Mb
`du -cm`.split("\n").last.to_i
end

def download_head
git "clone '#{url}' '#{target_path}'"
git "clone '#{clone_url}' '#{target_path}'"
end

def download_tag
Dir.chdir(target_path) do
git "init"
git "remote add origin '#{url}'"
git "remote add origin '#{clone_url}'"
git "fetch origin tags/#{options[:tag]}"
git "reset --hard FETCH_HEAD"
git "checkout -b activated-pod-commit"
end
end

def download_commit
git "clone '#{url}' '#{target_path}'"
git "clone '#{clone_url}' '#{target_path}'"

Dir.chdir(target_path) do
git "checkout -b activated-pod-commit #{options[:commit]}"
Expand Down Expand Up @@ -71,7 +131,7 @@ def tarball_url_for(id)
original_url, username, reponame = *(url.match(/[:\/]([\w\-]+)\/([\w\-]+)\.git/).to_a)
"https://github.com/#{username}/#{reponame}/tarball/#{id}"
end

def tmp_path
target_path + "tarball.tar.gz"
end
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def xit(description, *args)
config = Pod::Config.instance
config.silent = true
config.repos_dir = SpecHelper.tmp_repos_path
config.git_cache_size = 0

require 'tmpdir'

Expand Down
8 changes: 4 additions & 4 deletions spec/unit/downloader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def stub_pod_with_source(source_options)
downloader.url.should == 'http://banana-corp.local/banana-lib.git'
downloader.options.should == { :tag => 'v1.0' }
end

it 'returns a github downloader when the :git URL is on github' do
pod = Pod::LocalPod.new(fixture_spec('banana-lib/BananaLib.podspec'), temporary_sandbox, Pod::Platform.ios)
pod.specification.stubs(:source).returns(:git => "git://github.com/CocoaPods/CocoaPods")
Expand All @@ -36,21 +36,21 @@ def stub_pod_with_source(source_options)
))
downloader.tarball_url_for('master').should == "https://github.com/CocoaPods/CocoaPods/tarball/master"
end

it 'can convert private HTTP repository URLs to the tarball URL' do
downloader = Pod::Downloader.for_pod(stub_pod_with_source(
:git => "https://lukeredpath@github.com/CocoaPods/CocoaPods.git"
))
downloader.tarball_url_for('master').should == "https://github.com/CocoaPods/CocoaPods/tarball/master"
end

it 'can convert private SSH repository URLs to the tarball URL' do
downloader = Pod::Downloader.for_pod(stub_pod_with_source(
:git => "git@github.com:CocoaPods/CocoaPods.git"
))
downloader.tarball_url_for('master').should == "https://github.com/CocoaPods/CocoaPods/tarball/master"
end

it 'can convert public git protocol repository URLs to the tarball URL' do
downloader = Pod::Downloader.for_pod(stub_pod_with_source(
:git => "git://github.com/CocoaPods/CocoaPods.git"
Expand Down
0