Module: Git::Repository::Branching Private
- Included in:
- Git::Repository
- Defined in:
- lib/git/repository/branching.rb
Overview
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
Facade methods for branching operations: creating, checking out, querying, deleting, and updating branches
Included by Git::Repository.
Defined Under Namespace
Classes: HeadState
Instance Method Summary collapse
-
#branch(branch_name = current_branch) ⇒ Git::Branch
deprecated
private
Deprecated.
Use
branch_list(name).firstand the name-based branch operations instead#branch_list returns immutable BranchInfo value objects rather than Branch. It takes
git branch --listpatterns, so pass the short name of a local branch or"#{remote}/#{name}"for a remote-tracking branch; theremotes/andrefs/prefixes this method accepts match nothing. A"#{remote}/#{name}"pattern also matches a local branch of that name, so takefind(&:remote?)rather thanfirstfor a remote-tracking branch. With no argument this method wraps #current_branch, which is'HEAD'when HEAD is detached; #branch_list has no entry for a detached or unborn HEAD, so use #current_branch_state in those states. Call the corresponding Git::Repository method (e.g. #checkout, #branch_new, #branch_delete) for operations on a branch. -
#branch?(branch) ⇒ Boolean
private
Returns
trueif the named branch exists locally or as a remote-tracking branch. -
#branch_contains(commit, branch_name = '') ⇒ String
private
Returns the
git branch --list --containsstdout for a given commit. -
#branch_delete(*branches, **options) ⇒ String
private
Delete one or more local or remote-tracking branches.
-
#branch_list(*patterns, remote_names: nil) ⇒ Array<Git::BranchInfo>
private
Returns all local and remote-tracking branches as structured objects.
-
#branch_new(branch, start_point = nil, branch_options = {})
private
Create a new branch.
-
#branches ⇒ Git::Branches
deprecated
private
Deprecated.
Use #branch_list instead
#branch_list returns
Array<Git::BranchInfo>(immutable value objects) rather than a Branches collection. Filter it withselect(&:remote?)orreject(&:remote?)in place ofbranches.remoteandbranches.local, and look a branch up by name withbranch_list(name).firstin place ofbranches[name]. -
#branches_all ⇒ Array<Array>
deprecated
private
Deprecated.
Use #branch_list instead, which returns richer BranchInfo objects.
-
#change_head_branch(branch_name)
private
Writes the HEAD symbolic ref to point at the given branch.
-
#checkout(branch = nil, opts = {}) ⇒ String
private
Switch branches or restore working tree files.
-
#checkout_file(version, file) ⇒ String
private
Restore working tree files from a tree-ish.
-
#checkout_index(options = {}) ⇒ String
private
Populate the working tree from the index.
-
#current_branch ⇒ String
private
Returns the name of the current branch.
-
#current_branch_state ⇒ Git::Repository::Branching::HeadState
private
Returns the current HEAD state as a structured value object.
-
#in_branch(branch, message = 'in branch work') { ... } ⇒ String
private
Run a block with the given branch checked out, then restore the original branch.
-
#is_branch?(branch) ⇒ Boolean
deprecated
private
Deprecated.
use #branch? instead
-
#is_local_branch?(branch) ⇒ Boolean
deprecated
private
Deprecated.
use #local_branch? instead
-
#is_remote_branch?(branch) ⇒ Boolean
deprecated
private
Deprecated.
use #remote_branch? instead
-
#local_branch?(branch) ⇒ Boolean
private
Returns
trueif the named branch exists as a local branch. -
#remote_branch?(branch) ⇒ Boolean
private
Returns
trueif the named branch exists as a remote-tracking branch. -
#update_ref(branch, commit) ⇒ Git::CommandLine::Result
private
Update a branch ref to point to a new commit.
Instance Method Details
#branch(branch_name = current_branch) ⇒ Git::Branch
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Use branch_list(name).first and the name-based branch
operations instead
#branch_list returns immutable BranchInfo value objects
rather than Branch. It takes git branch --list patterns, so
pass the short name of a local branch or "#{remote}/#{name}" for a
remote-tracking branch; the remotes/ and refs/ prefixes this
method accepts match nothing. A "#{remote}/#{name}" pattern also
matches a local branch of that name, so take find(&:remote?) rather
than first for a remote-tracking branch. With no argument this
method wraps #current_branch, which is 'HEAD' when HEAD is
detached; #branch_list has no entry for a detached or unborn HEAD,
so use #current_branch_state in those states. Call the
corresponding Git::Repository method (e.g. #checkout,
#branch_new, #branch_delete) for operations on a branch.
Returns a Branch object for the given branch name
718 719 720 721 722 723 724 725 726 |
# File 'lib/git/repository/branching.rb', line 718 def branch(branch_name = current_branch) Git::Deprecation.warn( 'Git::Repository#branch is deprecated and will be removed in v6.0.0. ' \ 'Use Git::Repository#branch_list(name).first for a local branch, ' \ 'Git::Repository#branch_list("remote/name").find(&:remote?) for a remote-tracking branch, ' \ 'and the name-based branch operations instead.' ) Git::Branch.new(self, branch_name) end |
#branch?(branch) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns true if the named branch exists locally or as a remote-tracking branch
343 344 345 |
# File 'lib/git/repository/branching.rb', line 343 def branch?(branch) local_branch?(branch) || remote_branch?(branch) end |
#branch_contains(commit, branch_name = '') ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
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.
573 574 575 576 577 578 579 |
# File 'lib/git/repository/branching.rb', line 573 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
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Delete one or more local or remote-tracking branches
493 494 495 496 497 498 499 500 501 502 |
# File 'lib/git/repository/branching.rb', line 493 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_list(*patterns, remote_names: nil) ⇒ Array<Git::BranchInfo>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns all local and remote-tracking branches as structured objects
619 620 621 622 623 624 625 |
# File 'lib/git/repository/branching.rb', line 619 def branch_list(*patterns, remote_names: nil) remote_names ||= self.remote_names result = Git::Commands::Branch::List.new(@execution_context).call( *patterns, all: true, format: Git::Parsers::Branch::FORMAT_STRING ) Git::Parsers::Branch.parse_list(result.stdout, remote_names:) end |
#branch_new(branch, start_point = nil, branch_options = {})
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Create a new branch
439 440 441 442 443 444 445 446 447 448 449 |
# File 'lib/git/repository/branching.rb', line 439 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
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Use #branch_list instead
#branch_list returns Array<Git::BranchInfo> (immutable value
objects) rather than a Branches collection. Filter it with
select(&:remote?) or reject(&:remote?) in place of
branches.remote and branches.local, and look a branch up by name
with branch_list(name).first in place of branches[name].
Returns a Branches collection of all branches in the repository
761 762 763 764 765 766 767 |
# File 'lib/git/repository/branching.rb', line 761 def branches Git::Deprecation.warn( 'Git::Repository#branches is deprecated and will be removed in v6.0.0. ' \ 'Use Git::Repository#branch_list instead.' ) Git::Branches.new(self) end |
#branches_all ⇒ Array<Array>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Use #branch_list instead, which returns richer BranchInfo objects.
Returns all local and remote-tracking branches in the 4.x-compatible format
Each entry is a 4-element array: [refname, current, worktree, symref].
The refname uses the short form (main, remotes/origin/main) to
match the output of the legacy Git::Lib#branches_all method.
640 641 642 643 644 645 646 647 648 649 |
# File 'lib/git/repository/branching.rb', line 640 def branches_all Git::Deprecation.warn( 'Git::Repository#branches_all is deprecated and will be removed in v6.0.0. ' \ 'Use Git::Repository#branch_list instead.' ) branch_list.map do |info| refname = info.remote? ? "remotes/#{info.remote_name}/#{info.short_name}" : info.short_name [refname, info.current, info.other_worktree?, info.symref] end end |
#change_head_branch(branch_name)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
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.
532 533 534 535 |
# File 'lib/git/repository/branching.rb', line 532 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
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Switch branches or restore working tree files
178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/git/repository/branching.rb', line 178 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
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Restore working tree files from a tree-ish
118 119 120 |
# File 'lib/git/repository/branching.rb', line 118 def checkout_file(version, file) Git::Commands::Checkout::Files.new(@execution_context).call(version, pathspec: [file]).stdout end |
#checkout_index(options = {}) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Populate the working tree from the index
286 287 288 289 290 291 292 |
# File 'lib/git/repository/branching.rb', line 286 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
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the name of the current branch
65 66 67 68 69 |
# File 'lib/git/repository/branching.rb', line 65 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
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
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.
96 97 98 99 100 101 102 |
# File 'lib/git/repository/branching.rb', line 96 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 |
#in_branch(branch, message = 'in branch work') { ... } ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Run a block with the given branch checked out, then restore the original branch
Records the current branch (or the current commit when HEAD is detached),
checks out branch, and yields to the block. If the block returns a truthy
value, all pending changes are committed with message (see
Committing#commit_all); if it returns a falsy value, the index and working tree are
hard-reset instead (see Staging#reset). The original branch or commit is then
checked out again. The hard reset discards changes to tracked files only;
untracked files created by the block are left in place.
Unlike Git::Branch#in_branch, this method does not create branch. The
branch must be an existing local branch. Unlike #checkout, a commit SHA,
tag, or remote-tracking branch is rejected before any checkout happens:
those detach HEAD, and a commit made there would be left dangling once the
original branch is restored. HEAD must
be on a branch with at least one commit, or detached: an unborn branch (no
commits yet) cannot be checked out again by name, so it is rejected before
any checkout happens.
Note: the restore checkout is not wrapped in ensure. If the block,
the commit, or the reset raises an exception, the repository is left
checked out on branch rather than restored to the original branch.
245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/git/repository/branching.rb', line 245 def in_branch(branch, = 'in branch work') SharedPrivate.assert_local_branch!(self, branch) restore_point = SharedPrivate.head_restore_point(self) checkout(branch) if yield commit_all() else reset(nil, hard: true) end checkout(restore_point) end |
#is_branch?(branch) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
use #branch? instead
Checks whether the named branch exists locally or as a remote-tracking branch
404 405 406 407 408 409 410 |
# File 'lib/git/repository/branching.rb', line 404 def is_branch?(branch) # rubocop:disable Naming/PredicatePrefix Git::Deprecation.warn( 'Git::Repository#is_branch? is deprecated and will be removed in v6.0.0. ' \ 'Use Git::Repository#branch? instead.' ) branch?(branch) end |
#is_local_branch?(branch) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
use #local_branch? instead
Checks whether the named branch exists locally
360 361 362 363 364 365 366 |
# File 'lib/git/repository/branching.rb', line 360 def is_local_branch?(branch) # rubocop:disable Naming/PredicatePrefix Git::Deprecation.warn( 'Git::Repository#is_local_branch? is deprecated and will be removed in v6.0.0. ' \ 'Use Git::Repository#local_branch? instead.' ) local_branch?(branch) end |
#is_remote_branch?(branch) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
use #remote_branch? instead
Checks whether the named branch exists as a remote-tracking branch
382 383 384 385 386 387 388 |
# File 'lib/git/repository/branching.rb', line 382 def is_remote_branch?(branch) # rubocop:disable Naming/PredicatePrefix Git::Deprecation.warn( 'Git::Repository#is_remote_branch? is deprecated and will be removed in v6.0.0. ' \ 'Use Git::Repository#remote_branch? instead.' ) remote_branch?(branch) end |
#local_branch?(branch) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns true if the named branch exists as a local branch
305 306 307 308 |
# File 'lib/git/repository/branching.rb', line 305 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
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
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').
325 326 327 328 329 |
# File 'lib/git/repository/branching.rb', line 325 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::CommandLine::Result
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
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)
681 682 683 684 |
# File 'lib/git/repository/branching.rb', line 681 def update_ref(branch, commit) ref = Private.build_update_ref(branch) Git::Commands::UpdateRef::Update.new(@execution_context).call(ref, commit) end |