Module: Git::Repository::Branching
- Included in:
- Git::Repository
- Defined in:
- lib/git/repository/branching.rb
Overview
Facade methods for branching operations: creating, checking out, querying, deleting, and updating branches
Included by Git::Repository.
Defined Under Namespace
Classes: HeadState
Instance Attribute Summary collapse
-
#name ⇒ String
readonly
The branch name, or
'HEAD'when detached. -
#state ⇒ Symbol
readonly
One of
:active,:unborn, or:detached.
Instance Method Summary collapse
-
#branch(branch_name = current_branch) ⇒ Git::Branch
Returns a Branch object for the given branch name.
-
#branch?(branch) ⇒ Boolean
Returns
trueif the named branch exists locally or as a remote-tracking branch. -
#branch_contains(commit, branch_name = '') ⇒ String
Returns the
git branch --list --containsstdout for a given commit. -
#branch_delete(*branches, **options) ⇒ String
Delete one or more local or remote-tracking branches.
-
#branch_new(branch, start_point = nil, options = {})
Create a new branch.
-
#branches ⇒ Git::Branches
Returns a Branches collection of all branches in the repository.
-
#branches_all ⇒ Array<Git::BranchInfo>
Returns all local and remote-tracking branches as structured objects.
-
#change_head_branch(branch_name)
Writes the HEAD symbolic ref to point at the given branch.
-
#checkout(branch = nil, opts = {}) ⇒ String
Switch branches or restore working tree files.
-
#checkout_file(version, file) ⇒ String
Restore working tree files from a tree-ish.
-
#checkout_index(options = {}) ⇒ String
Populate the working tree from the index.
-
#current_branch ⇒ String
Returns the name of the current branch.
-
#current_branch_state ⇒ Git::Repository::Branching::HeadState
Returns the current HEAD state as a structured value object.
-
#is_branch?(branch) ⇒ Boolean
deprecated
Deprecated.
use #branch? instead
-
#is_local_branch?(branch) ⇒ Boolean
deprecated
Deprecated.
use #local_branch? instead
-
#is_remote_branch?(branch) ⇒ Boolean
deprecated
Deprecated.
use #remote_branch? instead
-
#local_branch?(branch) ⇒ Boolean
Returns
trueif the named branch exists as a local branch. -
#remote_branch?(branch) ⇒ Boolean
Returns
trueif the named branch exists as a remote-tracking branch. -
#update_ref(branch, commit) ⇒ Git::CommandLineResult
Update a branch ref to point to a new commit.
Instance Attribute Details
#name ⇒ String (readonly)
Returns the branch name, or 'HEAD' when detached.
38 |
# File 'lib/git/repository/branching.rb', line 38 HeadState = Data.define(:state, :name) |
#state ⇒ Symbol (readonly)
Returns one of :active, :unborn, or :detached.
38 |
# File 'lib/git/repository/branching.rb', line 38 HeadState = Data.define(:state, :name) |
Instance Method Details
#branch(branch_name = current_branch) ⇒ Git::Branch
Returns a Branch object for the given branch name
573 574 575 576 577 578 579 580 581 582 583 |
# File 'lib/git/repository/branching.rb', line 573 def branch(branch_name = current_branch) branch_info = Git::BranchInfo.new( refname: branch_name, target_oid: nil, current: false, worktree: false, symref: nil, upstream: nil ) Git::Branch.new(self, branch_info) end |
#branch?(branch) ⇒ Boolean
Returns true if the named branch exists locally or as a remote-tracking branch
259 260 261 |
# File 'lib/git/repository/branching.rb', line 259 def branch?(branch) local_branch?(branch) || remote_branch?(branch) end |
#branch_contains(commit, branch_name = '') ⇒ String
Returns the git branch --list --contains stdout for a given commit
The output format is the human-readable git branch listing: each
matching branch name appears on its own line, prefixed with two spaces,
or * if it is the currently checked-out branch. This is the same
format returned by Git::Lib#branch_contains in the 4.x gem series.
489 490 491 492 493 494 495 |
# File 'lib/git/repository/branching.rb', line 489 def branch_contains(commit, branch_name = '') branch_name = branch_name.to_s pattern = branch_name.empty? ? nil : branch_name Git::Commands::Branch::List.new(@execution_context) .call(*[pattern].compact, contains: commit, no_color: true) .stdout end |
#branch_delete(*branches, **options) ⇒ String
Delete one or more local or remote-tracking branches
409 410 411 412 413 414 415 416 417 418 |
# File 'lib/git/repository/branching.rb', line 409 def branch_delete(*branches, **) = { force: true }.merge() SharedPrivate.assert_valid_opts!(BRANCH_DELETE_ALLOWED_OPTS, **) result = Git::Commands::Branch::Delete.new(@execution_context).call(*branches, **) raise Git::Error, result.stderr.strip unless result.status.success? result.stdout.strip end |
#branch_new(branch, start_point = nil, options = {})
This method returns an undefined value.
Create a new branch
355 356 357 358 359 360 361 362 363 364 365 |
# File 'lib/git/repository/branching.rb', line 355 def branch_new(branch, start_point = nil, = {}) if start_point.is_a?(Hash) && .empty? = start_point start_point = nil end SharedPrivate.assert_valid_opts!(BRANCH_NEW_ALLOWED_OPTS, **) Git::Commands::Branch::Create.new(@execution_context).call(branch, start_point, **) nil end |
#branches ⇒ Git::Branches
Returns a Branches collection of all branches in the repository
608 609 610 |
# File 'lib/git/repository/branching.rb', line 608 def branches Git::Branches.new(self) end |
#branches_all ⇒ Array<Git::BranchInfo>
Returns all local and remote-tracking branches as structured objects
517 518 519 520 521 522 |
# File 'lib/git/repository/branching.rb', line 517 def branches_all result = Git::Commands::Branch::List.new(@execution_context).call( all: true, format: Git::Parsers::Branch::FORMAT_STRING ) Git::Parsers::Branch.parse_list(result.stdout) end |
#change_head_branch(branch_name)
Pointing HEAD at a branch that does not yet exist places the repository in unborn-branch state. This is intentional for repository initialization workflows — for example, setting a custom default branch name before any commits land — but is unexpected if done by mistake. The repository will appear to have no commits until the first commit is made on the new branch.
This method returns an undefined value.
Writes the HEAD symbolic ref to point at the given branch
Sets HEAD to refs/heads/<branch_name> via git symbolic-ref. This is
equivalent to running git symbolic-ref HEAD refs/heads/<branch_name> on
the command line and is the mechanism git uses internally for branch
renaming and orphan-branch checkout.
448 449 450 451 |
# File 'lib/git/repository/branching.rb', line 448 def change_head_branch(branch_name) Git::Commands::SymbolicRef::Update.new(@execution_context).call('HEAD', "refs/heads/#{branch_name}") nil end |
#checkout(branch = nil, opts = {}) ⇒ String
Switch branches or restore working tree files
161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/git/repository/branching.rb', line 161 def checkout(branch = nil, opts = {}) if branch.is_a?(Hash) && opts.empty? opts = branch branch = nil end SharedPrivate.assert_valid_opts!(CHECKOUT_ALLOWED_OPTS, **opts) target, translated_opts = Private.translate_checkout_opts(branch, opts) Git::Commands::Checkout::Branch.new(@execution_context).call(target, **translated_opts).stdout end |
#checkout_file(version, file) ⇒ String
Restore working tree files from a tree-ish
116 117 118 |
# File 'lib/git/repository/branching.rb', line 116 def checkout_file(version, file) Git::Commands::Checkout::Files.new(@execution_context).call(version, pathspec: [file]).stdout end |
#checkout_index(options = {}) ⇒ String
Populate the working tree from the index
202 203 204 205 206 207 208 |
# File 'lib/git/repository/branching.rb', line 202 def checkout_index( = {}) SharedPrivate.assert_valid_opts!(CHECKOUT_INDEX_ALLOWED_OPTS, **) paths = Private.normalize_pathspecs([:path_limiter], 'path_limiter') keyword_opts = .except(:path_limiter) Git::Commands::CheckoutIndex.new(@execution_context).call(*paths.to_a, **keyword_opts).stdout end |
#current_branch ⇒ String
Returns the name of the current branch
63 64 65 66 67 |
# File 'lib/git/repository/branching.rb', line 63 def current_branch result = Git::Commands::Branch::ShowCurrent.new(@execution_context).call name = result.stdout.strip name.empty? ? 'HEAD' : name end |
#current_branch_state ⇒ Git::Repository::Branching::HeadState
Returns the current HEAD state as a structured value object
HEAD can be in one of three states:
:active— HEAD points to a branch ref that has at least one commit.:unborn— HEAD points to a branch ref that has been created but has no commits yet (e.g. immediately aftergit initbefore any commit).:detached— HEAD points directly to a commit SHA rather than a branch.
94 95 96 97 98 99 100 |
# File 'lib/git/repository/branching.rb', line 94 def current_branch_state branch_name = Git::Commands::Branch::ShowCurrent.new(@execution_context).call.stdout.strip return HeadState.new(state: :detached, name: 'HEAD') if branch_name.empty? state = Private.get_branch_state(@execution_context, branch_name) HeadState.new(state: state, name: branch_name) end |
#is_branch?(branch) ⇒ Boolean
use #branch? instead
Checks whether the named branch exists locally or as a remote-tracking branch
320 321 322 323 324 325 326 |
# File 'lib/git/repository/branching.rb', line 320 def is_branch?(branch) # rubocop:disable Naming/PredicatePrefix Git::Deprecation.warn( 'Git::Repository#is_branch? is deprecated and will be removed in a future version. ' \ 'Use Git::Repository#branch? instead.' ) branch?(branch) end |
#is_local_branch?(branch) ⇒ Boolean
use #local_branch? instead
Checks whether the named branch exists locally
276 277 278 279 280 281 282 |
# File 'lib/git/repository/branching.rb', line 276 def is_local_branch?(branch) # rubocop:disable Naming/PredicatePrefix Git::Deprecation.warn( 'Git::Repository#is_local_branch? is deprecated and will be removed in a future version. ' \ 'Use Git::Repository#local_branch? instead.' ) local_branch?(branch) end |
#is_remote_branch?(branch) ⇒ Boolean
use #remote_branch? instead
Checks whether the named branch exists as a remote-tracking branch
298 299 300 301 302 303 304 |
# File 'lib/git/repository/branching.rb', line 298 def is_remote_branch?(branch) # rubocop:disable Naming/PredicatePrefix Git::Deprecation.warn( 'Git::Repository#is_remote_branch? is deprecated and will be removed in a future version. ' \ 'Use Git::Repository#remote_branch? instead.' ) remote_branch?(branch) end |
#local_branch?(branch) ⇒ Boolean
Returns true if the named branch exists as a local branch
221 222 223 224 |
# File 'lib/git/repository/branching.rb', line 221 def local_branch?(branch) result = Git::Commands::Branch::List.new(@execution_context).call(branch, format: '%(refname:short)') result.stdout.chomp == branch end |
#remote_branch?(branch) ⇒ Boolean
Returns true if the named branch exists as a remote-tracking branch
The branch argument must be the short branch name (e.g. 'master'),
not the combined remote/branch form (e.g. 'origin/master').
241 242 243 244 245 |
# File 'lib/git/repository/branching.rb', line 241 def remote_branch?(branch) result = Git::Commands::Branch::List.new(@execution_context) .call("*/#{branch}", remotes: true, format: '%(refname:lstrip=3)') result.stdout.each_line.any? { |line| line.chomp == branch } end |
#update_ref(branch, commit) ⇒ Git::CommandLineResult
Update a branch ref to point to a new commit
Derives the full ref from the branch argument:
remotes/<remote>/<name>orrefs/remotes/<remote>/<name>→ writes torefs/remotes/<remote>/<name>(remote-tracking branch)- Any other value → writes to
refs/heads/<branch>(local branch)
554 555 556 557 |
# File 'lib/git/repository/branching.rb', line 554 def update_ref(branch, commit) ref = Private.build_update_ref(branch) Git::Commands::UpdateRef::Update.new(@execution_context).call(ref, commit) end |