Skip to content

Commit

Permalink
#531 add API to get default branch
Browse files Browse the repository at this point in the history
Signed-off-by: Alexande B <abobrikovich@gmail.com>
  • Loading branch information
CAMOBAP committed Jul 15, 2021
1 parent 58100b0 commit f88a0c5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/git/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,10 @@ def current_branch
self.lib.branch_current
end

# @return [String] name of default branch
def branch_default
self.lib.branch_default
end
end

end
14 changes: 14 additions & 0 deletions lib/git/lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,20 @@ def branch_current
branches_all.select { |b| b[1] }.first[0] rescue nil
end

def branch_default
begin
full_name = command("symbolic-ref", "refs/remotes/origin/HEAD")
return full_name.gsub(%r{^refs/remotes/origin/}, "")
rescue Git::GitExecuteError
begin
full_name = command("symbolic-ref", "refs/origin/HEAD")
return full_name.gsub(%r{^refs/origin/}, "")
rescue Git::GitExecuteError
return command("symbolic-ref", "--short", "HEAD")
end
end
end

def branch_contains(commit, branch_name="")
command("branch", [branch_name, "--contains", commit])
end
Expand Down
5 changes: 5 additions & 0 deletions tests/units/test_lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -291,4 +291,9 @@ def test_show
assert(@lib.show('gitsearch1', 'scott/text.txt') == "hello\nthis is\na file\nthat is\nput here\nto search one\nto search two\nnothing!\n")
end

def test_branch_default
assert_equal(@lib.branch_default, "git_grep")
@lib.change_head_branch("master")
assert_equal(@lib.branch_default, "master")
end
end

0 comments on commit f88a0c5

Please sign in to comment.