Skip to content
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

OTWO-7300 Get default branch for Git #151

Merged
merged 1 commit into from
Oct 17, 2024
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
6 changes: 5 additions & 1 deletion lib/ohloh_scm/git/scm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Git
class Scm < OhlohScm::Scm
def initialize(core:, url:, branch_name:, username:, password:)
super
@branch_name = branch_name || 'master'
@branch_name = branch_name
end

# == Example:
Expand All @@ -28,6 +28,10 @@ def checkout_files(names)
run "cd #{url} && git checkout $(git ls-files #{filenames})"
end

def branch_name_or_default
branch_name || 'master'
end

private

def clone_or_fetch(remote_scm, callback)
Expand Down
7 changes: 7 additions & 0 deletions lib/ohloh_scm/git/status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ def branch?(name = scm.branch_name)

activity.branches.include?(name)
end

def default_branch
return scm.branch_name_or_default unless exist?

name = run("git remote show '#{scm.url}' | grep 'HEAD branch' | awk '{print $3}'").strip
name.to_s.empty? ? scm.branch_name_or_default : name
end
end
end
end
2 changes: 2 additions & 0 deletions lib/ohloh_scm/status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ def exist?
def scm_dir_exist?
Dir.exist?(scm.vcs_path)
end

def default_branch; end
end
end
6 changes: 5 additions & 1 deletion spec/helpers/repository_helper.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# frozen_string_literal: true

module RepositoryHelper
%w[git svn git_svn cvs hg bzr].each do |scm_type|
%w[svn git_svn cvs hg bzr].each do |scm_type|
define_method("with_#{scm_type}_repository") do |name, branch_name = nil, &block|
with_repository(scm_type, name, branch_name) { |core| block.call(core) }
end
end

def with_git_repository(name, branch_name = 'master', &block)
with_repository('git', name, branch_name) { |core| block.call(core) }
end

private

def with_repository(scm_type, name, branch_name = nil)
Expand Down
2 changes: 1 addition & 1 deletion spec/ohloh_scm/cvs/activity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
it 'must correctly convert commits to git' do
with_cvs_repository('cvs', 'simple') do |cvs|
tmpdir do |tmp_dir|
git_core = OhlohScm::Factory.get_core(url: tmp_dir)
git_core = OhlohScm::Factory.get_core(url: tmp_dir, branch_name: 'master')
git_core.scm.pull(cvs.scm, TestCallback.new)
utc_dates = ['2006-06-29 16:21:07 UTC', '2006-06-29 18:14:47 UTC',
'2006-06-29 18:45:29 UTC', '2006-06-29 18:48:54 UTC',
Expand Down
8 changes: 4 additions & 4 deletions spec/ohloh_scm/git/activity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@

it 'must commit all changes in the working directory' do
tmpdir do |dir|
core = OhlohScm::Factory.get_core(scm_type: :git, url: dir)
core = OhlohScm::Factory.get_core(scm_type: :git, branch_name: 'master', url: dir)

core.activity.send(:init_db)
refute core.activity.send(:anything_to_commit?)
Expand All @@ -356,7 +356,7 @@

it 'must test that no token returns nil' do
tmpdir do |dir|
core = OhlohScm::Factory.get_core(scm_type: :git, url: dir)
core = OhlohScm::Factory.get_core(scm_type: :git, branch_name: 'master', url: dir)
refute core.activity.read_token
core.activity.send(:init_db)
refute core.activity.read_token
Expand All @@ -365,7 +365,7 @@

it 'must test write and read token' do
tmpdir do |dir|
core = OhlohScm::Factory.get_core(scm_type: :git, url: dir)
core = OhlohScm::Factory.get_core(scm_type: :git, branch_name: 'master', url: dir)
core.activity.send(:init_db)
core.activity.send(:write_token, 'FOO')
refute core.activity.read_token # Token not valid until committed
Expand All @@ -376,7 +376,7 @@

it 'must test that commit_all includes write token' do
tmpdir do |dir|
core = OhlohScm::Factory.get_core(scm_type: :git, url: dir)
core = OhlohScm::Factory.get_core(scm_type: :git, branch_name: 'master', url: dir)
core.activity.send(:init_db)
c = OhlohScm::Commit.new
c.token = 'BAR'
Expand Down
7 changes: 6 additions & 1 deletion spec/ohloh_scm/git/scm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
it 'must test the basic conversion to git' do
with_cvs_repository('cvs', 'simple') do |src_core|
tmpdir do |dest_dir|
core = OhlohScm::Factory.get_core(scm_type: :git, url: dest_dir)
core = OhlohScm::Factory.get_core(scm_type: :git, branch_name: 'master', url: dest_dir)
refute core.status.scm_dir_exist?
core.scm.pull(src_core.scm, TestCallback.new)
assert core.status.scm_dir_exist?
Expand Down Expand Up @@ -126,4 +126,9 @@
assert system("ls #{dir}/Godeps/Godeps.json > /dev/null")
end
end

it 'must return master when branch_name is null' do
core = OhlohScm::Factory.get_core(scm_type: :git, url: 'foobar')
_(core.scm.branch_name_or_default).must_equal 'master'
end
end
16 changes: 15 additions & 1 deletion spec/ohloh_scm/git/status_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,22 @@
it 'branch?' do
with_git_repository('git') do |git|
git.activity.send(:branches).must_equal %w[develop master]
assert git.status.branch? # checks master.
assert git.status.branch?('master')
assert git.status.branch?('develop')
end
end

describe 'default_branch' do
it 'must return default branch when repository doesnt exist' do
git = OhlohScm::Factory.get_core(scm_type: :git, url: 'foobar')
git.status.stubs(:exist?)
_(git.status.default_branch).must_equal 'master'
end

it 'must return default branch when no HEAD branch is found in remote' do
git = OhlohScm::Factory.get_core(scm_type: :git, url: 'foobar')
git.status.stubs(:exist?).returns(true)
git.status.default_branch.must_equal 'master'
end
end
end
Loading