Skip to content

Commit 9fd11f1

Browse files
committed
OTWO-7300 Get default branch for Git
1 parent ab5a5dd commit 9fd11f1

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

lib/ohloh_scm/git/scm.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Git
55
class Scm < OhlohScm::Scm
66
def initialize(core:, url:, branch_name:, username:, password:)
77
super
8-
@branch_name = branch_name || 'master'
8+
@branch_name = branch_name
99
end
1010

1111
# == Example:

lib/ohloh_scm/git/status.rb

+10
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ def branch?(name = scm.branch_name)
88

99
activity.branches.include?(name)
1010
end
11+
12+
def default_branch
13+
return unless remote_url?
14+
15+
run("git remote show '#{scm.url}' | grep 'HEAD branch' | awk '{print $3}'").strip
16+
end
17+
18+
def remote_url?
19+
scm.url =~ /^(git|http)/
20+
end
1121
end
1222
end
1323
end

lib/ohloh_scm/status.rb

+2
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@ def exist?
2121
def scm_dir_exist?
2222
Dir.exist?(scm.vcs_path)
2323
end
24+
25+
def default_branch; end
2426
end
2527
end

spec/ohloh_scm/git/status_spec.rb

+24-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,31 @@
44
it 'branch?' do
55
with_git_repository('git') do |git|
66
git.activity.send(:branches).must_equal %w[develop master]
7-
assert git.status.branch? # checks master.
7+
assert git.status.branch?('master')
88
assert git.status.branch?('develop')
99
end
1010
end
11+
12+
it 'remote_url is true for https' do
13+
git_scm = OhlohScm::Factory.get_core(scm_type: :git, url: 'https://example.com', branch_name: 'test')
14+
assert git_scm.status.remote_url?
15+
end
16+
17+
it 'remote_url is true for git' do
18+
git_scm = OhlohScm::Factory.get_core(scm_type: :git, url: '[email protected]', branch_name: 'test')
19+
assert git_scm.status.remote_url?
20+
end
21+
22+
it 'remote_url is false for local urls' do
23+
with_git_repository('git') do |git|
24+
refute git.status.remote_url?
25+
end
26+
end
27+
28+
it 'default_branch' do
29+
with_git_repository('git') do |git|
30+
git.status.stubs(:remote_url?).returns(true)
31+
git.status.default_branch.must_equal 'master'
32+
end
33+
end
1134
end

0 commit comments

Comments
 (0)