Skip to content

Commit 87cfbc3

Browse files
committed
#531 add API to get default branch
Signed-off-by: Alex Bobrikovich <abobrikovich@gmail.com>
1 parent 58100b0 commit 87cfbc3

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

lib/git/base.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,10 @@ def current_branch
592592
self.lib.branch_current
593593
end
594594

595+
# @return [String] name of default branch
596+
def branch_default
597+
self.lib.branch_default
598+
end
595599
end
596600

597601
end

lib/git/lib.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,20 @@ def branch_current
398398
branches_all.select { |b| b[1] }.first[0] rescue nil
399399
end
400400

401+
def branch_default
402+
begin
403+
full_name = command("symbolic-ref", "refs/remotes/origin/HEAD")
404+
return full_name.gsub(%r{^refs/remotes/origin/}, "")
405+
rescue Git::GitExecuteError
406+
begin
407+
full_name = command("symbolic-ref", "refs/origin/HEAD")
408+
return full_name.gsub(%r{^refs/origin/}, "")
409+
rescue Git::GitExecuteError
410+
return command("symbolic-ref", "--short", "HEAD")
411+
end
412+
end
413+
end
414+
401415
def branch_contains(commit, branch_name="")
402416
command("branch", [branch_name, "--contains", commit])
403417
end

tests/units/test_lib.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,4 +291,9 @@ def test_show
291291
assert(@lib.show('gitsearch1', 'scott/text.txt') == "hello\nthis is\na file\nthat is\nput here\nto search one\nto search two\nnothing!\n")
292292
end
293293

294+
def test_branch_default
295+
assert_equal(@lib.branch_default, "git_grep")
296+
@lib.change_head_branch("master")
297+
assert_equal(@lib.branch_default, "master")
298+
end
294299
end

0 commit comments

Comments
 (0)