Skip to content

Get default branch of remote repository (Git.default_branch) #571

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

Merged
merged 1 commit into from
Mar 4, 2023

Conversation

jcouball
Copy link
Member

Signed-off-by: James Couball jcouball@yahoo.com

Your checklist for this pull request

🚨Please review the guidelines for contributing to this repository.

  • Ensure all commits include DCO sign-off.
  • Ensure that your contributions pass unit testing.
  • Ensure that your contributions contain documentation if applicable.

Description

Implements Git.default_branch(repository) which returns the default branch of a repository where repository can be a URI or a Pathname.

For instance:

require 'git'
Git.default_branch('https://github.com/ruby-git/ruby-git') #=> 'master'
Git.default_branch('https://github.com/rspec/rspec-core') #=> 'main'

Can also be used on a local repository by giving a path to the repository. In this case, it returns the branch being tracked by refs/remotes/origin/HEAD if it exists. If not, it returns the current branch.

# Prepare the current directory by:
#   git clone https://github.com/ruby-git/ruby-git test_repository
#   cd test_repository
#   # Even if the branch is changed, will still report the branch tracked by refs/remotes/origin/HEAD
#   git checkout -b new_branch
#   irb
require 'git'
Git.default_branch('.') #=> 'master'

@CAMOBAP
Copy link
Contributor

CAMOBAP commented Feb 11, 2023

It will be really nice to see it in upcoming release

Signed-off-by: James Couball <jcouball@yahoo.com>
@jcouball jcouball merged commit 6db3ea4 into master Mar 4, 2023
@jcouball jcouball deleted the default_branch branch March 4, 2023 15:00
This was referenced Mar 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants