From 06190615e0f57132e001f2cf2c6ab8a0ef4bbc52 Mon Sep 17 00:00:00 2001 From: Alex J Date: Wed, 11 Sep 2024 17:52:08 +0530 Subject: [PATCH] OTWO-7300 Get default branch for Git --- lib/ohloh_scm/git/scm.rb | 6 +++++- lib/ohloh_scm/git/status.rb | 7 +++++++ lib/ohloh_scm/status.rb | 2 ++ spec/helpers/repository_helper.rb | 6 +++++- spec/ohloh_scm/cvs/activity_spec.rb | 2 +- spec/ohloh_scm/git/activity_spec.rb | 8 ++++---- spec/ohloh_scm/git/scm_spec.rb | 7 ++++++- spec/ohloh_scm/git/status_spec.rb | 16 +++++++++++++++- 8 files changed, 45 insertions(+), 9 deletions(-) diff --git a/lib/ohloh_scm/git/scm.rb b/lib/ohloh_scm/git/scm.rb index a22bae11..c17d9126 100644 --- a/lib/ohloh_scm/git/scm.rb +++ b/lib/ohloh_scm/git/scm.rb @@ -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: @@ -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) diff --git a/lib/ohloh_scm/git/status.rb b/lib/ohloh_scm/git/status.rb index 5b1c12f8..66fa43fa 100644 --- a/lib/ohloh_scm/git/status.rb +++ b/lib/ohloh_scm/git/status.rb @@ -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 diff --git a/lib/ohloh_scm/status.rb b/lib/ohloh_scm/status.rb index d1782b00..6c55fe32 100644 --- a/lib/ohloh_scm/status.rb +++ b/lib/ohloh_scm/status.rb @@ -21,5 +21,7 @@ def exist? def scm_dir_exist? Dir.exist?(scm.vcs_path) end + + def default_branch; end end end diff --git a/spec/helpers/repository_helper.rb b/spec/helpers/repository_helper.rb index 9e27addd..567b5f49 100644 --- a/spec/helpers/repository_helper.rb +++ b/spec/helpers/repository_helper.rb @@ -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) diff --git a/spec/ohloh_scm/cvs/activity_spec.rb b/spec/ohloh_scm/cvs/activity_spec.rb index 645dbcbe..3dd6dd36 100644 --- a/spec/ohloh_scm/cvs/activity_spec.rb +++ b/spec/ohloh_scm/cvs/activity_spec.rb @@ -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', diff --git a/spec/ohloh_scm/git/activity_spec.rb b/spec/ohloh_scm/git/activity_spec.rb index 90947969..e1d907ce 100644 --- a/spec/ohloh_scm/git/activity_spec.rb +++ b/spec/ohloh_scm/git/activity_spec.rb @@ -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?) @@ -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 @@ -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 @@ -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' diff --git a/spec/ohloh_scm/git/scm_spec.rb b/spec/ohloh_scm/git/scm_spec.rb index 456a2ec1..0e166537 100644 --- a/spec/ohloh_scm/git/scm_spec.rb +++ b/spec/ohloh_scm/git/scm_spec.rb @@ -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? @@ -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 diff --git a/spec/ohloh_scm/git/status_spec.rb b/spec/ohloh_scm/git/status_spec.rb index cb0b49bc..9e51cd03 100644 --- a/spec/ohloh_scm/git/status_spec.rb +++ b/spec/ohloh_scm/git/status_spec.rb @@ -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