Skip to content

Commit

Permalink
Apply GitHub checking logic to all GitHub hosted sources
Browse files Browse the repository at this point in the history
  • Loading branch information
greysteil committed May 16, 2017
1 parent 5574572 commit cead504
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 34 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -12,3 +12,4 @@ coverage/
.coveralls.yml
.idea/
documentation/
.bundle/
5 changes: 5 additions & 0 deletions .travis.yml
Expand Up @@ -33,3 +33,8 @@ before_install:
git remote add origin https://github.com/CocoaPods/Specs.git
)
git submodule update --init
(
cd spec/fixtures/spec-repos/test_repo
git init
git remote add origin https://bitbucket.com/test/test_repo.git
)
2 changes: 1 addition & 1 deletion lib/cocoapods-core/github.rb
Expand Up @@ -131,7 +131,7 @@ def self.normalized_repo_id(url_or_id)
# @return [Nil] if the given url is not a valid github repo url.
#
def self.repo_id_from_url(url)
url[%r{github.com/([^/]*/[^/]*)\.*}, 1]
url[%r{github.com[/:]([^/]*/(?:(?!\.git)[^/])*)\.*}, 1]
end

# Performs a get request with the given URL.
Expand Down
34 changes: 3 additions & 31 deletions lib/cocoapods-core/master_source.rb
@@ -1,35 +1,7 @@
module Pod
class MasterSource < Source
# @!group Updating the source
#-------------------------------------------------------------------------#

# Updates the local clone of the source repo.
#
# @param [Bool] show_output
#
# @return [Array<String>] changed_spec_paths
# Returns the list of changed spec paths.
#
def update(show_output)
if requires_update?
super
else
[]
end
end

private

# Returns whether a source requires updating.
#
# @param [Source] source
# The source to check.
#
# @return [Bool] Whether the given source should be updated.
#
def requires_update?
commit_hash = git_commit_hash
GitHub.modified_since_commit('CocoaPods/Specs', commit_hash)
end
# For now, the MasterSource behaves exactly the same as any other Source.
# In the future we may apply separate logic to the MasterSource that doesn't
# depend on the file system.
end
end
7 changes: 7 additions & 0 deletions lib/cocoapods-core/source.rb
Expand Up @@ -330,6 +330,7 @@ def fuzzy_search(query)
# Returns the list of changed spec paths.
#
def update(show_output)
return [] if unchanged_github_repo?
prev_commit_hash = git_commit_hash
update_git_repo(show_output)
refresh_metadata
Expand Down Expand Up @@ -437,6 +438,12 @@ def repo_git(args, include_error: false)
(`#{command}` || '').strip
end

def unchanged_github_repo?
url = repo_git(%w(config --get remote.origin.url))
return unless url =~ /github.com/
!GitHub.modified_since_commit(url, git_commit_hash)
end

#-------------------------------------------------------------------------#
end
end
14 changes: 14 additions & 0 deletions spec/github_spec.rb
Expand Up @@ -17,6 +17,20 @@ module Pod
end
end

it 'handles SSH as well as HTTP' do
VCR.use_cassette('GitHub', :record => :new_episodes) do
repo = GitHub.repo('git@github.com:CocoaPods/CocoaPods')
repo['name'].should == 'CocoaPods'
end
end

it 'strips any trailing .git suffix' do
VCR.use_cassette('GitHub', :record => :new_episodes) do
repo = GitHub.repo('git@github.com:CocoaPods/CocoaPods.git')
repo['name'].should == 'CocoaPods'
end
end

it 'returns the information of a repo with dots in the name' do
VCR.use_cassette('GitHub', :record => :new_episodes) do
repo = GitHub.repo('https://github.com/contentful/contentful.objc')
Expand Down
2 changes: 1 addition & 1 deletion spec/master_source_spec.rb
Expand Up @@ -17,7 +17,7 @@ module Pod
end
end

it 'fetches if the GitHub API returns not-modified' do
it 'fetches if the GitHub API returns modified' do
VCR.use_cassette('MasterSource_fetch', :record => :new_episodes) do
@source.expects(:update_git_repo)
@source.send :update, true
Expand Down
2 changes: 1 addition & 1 deletion spec/source/manager_spec.rb
Expand Up @@ -221,7 +221,7 @@ module Pod

describe 'Updating Sources' do
before do
MasterSource.any_instance.stubs(:requires_update?).returns(true)
Source.any_instance.stubs(:unchanged_github_repo?).returns(false)
end

it 'updates search index for changed paths if source is updated' do
Expand Down
30 changes: 30 additions & 0 deletions spec/source_spec.rb
Expand Up @@ -195,6 +195,36 @@ module Pod
#-------------------------------------------------------------------------#

describe '#update' do
describe 'for a GitHub source' do
before do
# The master source repo uses GitHub, so we recycle that here. Note
# that we initialize it as just a `Source`, though.
@path = fixture('spec-repos/master')
@source = Source.new(@path)
end

it 'does not git fetch if the GitHub API returns not-modified' do
VCR.use_cassette('MasterSource_nofetch', :record => :new_episodes) do
@source.expects(:update_git_repo).never
@source.send :update, true
end
end

it 'fetches if the GitHub API returns modified' do
VCR.use_cassette('MasterSource_fetch', :record => :new_episodes) do
@source.expects(:update_git_repo)
@source.send :update, true
end
end
end

describe 'for a non-GitHub source' do
it 'always calls update_git_repo' do
@source.expects(:update_git_repo)
@source.send :update, true
end
end

it 'uses the only fast forward git option' do
@source.expects(:`).with("git -C \"#{@path}\" checkout master")
@source.expects(:`).with("git -C \"#{@path}\" pull --ff-only 2>&1")
Expand Down

0 comments on commit cead504

Please sign in to comment.