Commit e108fe91 authored by Fabio Pelosin's avatar Fabio Pelosin

[Downloader::Git] Cache repos

parent e19bf01c
...@@ -3,7 +3,6 @@ require 'uri' ...@@ -3,7 +3,6 @@ require 'uri'
module Pod module Pod
class Downloader class Downloader
autoload :Git, 'cocoapods/downloader/git' autoload :Git, 'cocoapods/downloader/git'
autoload :GitHub, 'cocoapods/downloader/git'
autoload :Mercurial, 'cocoapods/downloader/mercurial' autoload :Mercurial, 'cocoapods/downloader/mercurial'
autoload :Subversion, 'cocoapods/downloader/subversion' autoload :Subversion, 'cocoapods/downloader/subversion'
autoload :Http, 'cocoapods/downloader/http' autoload :Http, 'cocoapods/downloader/http'
...@@ -32,11 +31,7 @@ module Pod ...@@ -32,11 +31,7 @@ module Pod
def self.for_target(target_path, options) def self.for_target(target_path, options)
options = options.dup options = options.dup
if url = options.delete(:git) if url = options.delete(:git)
if url.to_s =~ /github.com/
GitHub.new(target_path, url, options)
else
Git.new(target_path, url, options) Git.new(target_path, url, options)
end
elsif url = options.delete(:hg) elsif url = options.delete(:hg)
Mercurial.new(target_path, url, options) Mercurial.new(target_path, url, options)
elsif url = options.delete(:svn) elsif url = options.delete(:svn)
......
require 'open-uri' require 'open-uri'
require 'tempfile' require 'tempfile'
require 'zlib' require 'zlib'
require 'digest/sha1'
module Pod module Pod
class Downloader class Downloader
...@@ -8,6 +9,7 @@ module Pod ...@@ -8,6 +9,7 @@ module Pod
executable :git executable :git
def download def download
prepare_cache
if options[:tag] if options[:tag]
download_tag download_tag
elsif options[:commit] elsif options[:commit]
...@@ -17,14 +19,31 @@ module Pod ...@@ -17,14 +19,31 @@ module Pod
end end
end end
def prepare_cache
# TODO:clean oldest repos if the cache becomes too big
if cache_path.exist?
#TODO: check remote and reset hard
Dir.chdir(cache_path) { git "pull" }
else
FileUtils.mkdir_p cache_path
Dir.chdir(cache_path) do
git "clone '#{url}' ."
end
end
end
def cache_path
@cache_path ||= Pathname.new "/var/tmp/CocoaPods/Git/#{Digest::SHA1.hexdigest(url)}/"
end
def download_head def download_head
git "clone '#{url}' '#{target_path}'" git "clone '#{cache_path}' '#{target_path}'"
end end
def download_tag def download_tag
Dir.chdir(target_path) do Dir.chdir(target_path) do
git "init" git "init"
git "remote add origin '#{url}'" git "remote add origin '#{cache_path}'"
git "fetch origin tags/#{options[:tag]}" git "fetch origin tags/#{options[:tag]}"
git "reset --hard FETCH_HEAD" git "reset --hard FETCH_HEAD"
git "checkout -b activated-pod-commit" git "checkout -b activated-pod-commit"
...@@ -32,7 +51,7 @@ module Pod ...@@ -32,7 +51,7 @@ module Pod
end end
def download_commit def download_commit
git "clone '#{url}' '#{target_path}'" git "clone '#{cache_path}' '#{target_path}'"
Dir.chdir(target_path) do Dir.chdir(target_path) do
git "checkout -b activated-pod-commit #{options[:commit]}" git "checkout -b activated-pod-commit #{options[:commit]}"
...@@ -43,54 +62,5 @@ module Pod ...@@ -43,54 +62,5 @@ module Pod
(target_path + '.git').rmtree (target_path + '.git').rmtree
end end
end end
class GitHub < Git
def download_head
download_only? ? download_and_extract_tarball('master') : super
end
def download_tag
super unless download_only?
download_only? ? download_and_extract_tarball(options[:tag]) : super
end
def download_commit
super unless download_only?
download_only? ? download_and_extract_tarball(options[:commit]) : super
end
def clean
if download_only?
FileUtils.rm_f(tmp_path)
else
super
end
end
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
private
def download_only?
@options[:download_only]
end
def download_and_extract_tarball(id)
File.open(tmp_path, "w+") do |tmpfile|
open tarball_url_for(id) do |archive|
tmpfile.write Zlib::GzipReader.new(archive).read
end
system "tar xf #{tmpfile.path} -C #{target_path} --strip-components 1"
end
end
end
end end
end end
...@@ -19,43 +19,4 @@ describe "Pod::Downloader" do ...@@ -19,43 +19,4 @@ describe "Pod::Downloader" do
downloader.url.should == 'http://banana-corp.local/banana-lib.git' downloader.url.should == 'http://banana-corp.local/banana-lib.git'
downloader.options.should == { :tag => 'v1.0' } downloader.options.should == { :tag => 'v1.0' }
end 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")
downloader = Pod::Downloader.for_pod(pod)
downloader.should.be.instance_of Pod::Downloader::GitHub
end
end
describe Pod::Downloader::GitHub do
it 'can convert public HTTP repository URLs to the tarball URL' do
downloader = Pod::Downloader.for_pod(stub_pod_with_source(
:git => "https://github.com/CocoaPods/CocoaPods.git"
))
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"
))
downloader.tarball_url_for('master').should == "https://github.com/CocoaPods/CocoaPods/tarball/master"
end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment