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.
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.
-
#checkout(branch = nil, options = {}) ⇒ 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.
-
#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 Method Details
#branch(branch_name = current_branch) ⇒ Git::Branch
Returns a Branch object for the given branch name
430 431 432 433 434 435 436 437 438 439 440 |
# File 'lib/git/repository/branching.rb', line 430 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
214 215 216 |
# File 'lib/git/repository/branching.rb', line 214 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.
346 347 348 349 350 351 352 |
# File 'lib/git/repository/branching.rb', line 346 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
299 300 301 302 303 304 305 306 307 308 |
# File 'lib/git/repository/branching.rb', line 299 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
245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/git/repository/branching.rb', line 245 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
465 466 467 |
# File 'lib/git/repository/branching.rb', line 465 def branches Git::Branches.new(self) end |
#branches_all ⇒ Array<Git::BranchInfo>
Returns all local and remote-tracking branches as structured objects
374 375 376 377 378 379 |
# File 'lib/git/repository/branching.rb', line 374 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 |
#checkout(branch = nil, options = {}) ⇒ String
Switch branches or restore working tree files
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/git/repository/branching.rb', line 116 def checkout(branch = nil, = {}) if branch.is_a?(Hash) && .empty? = branch branch = nil end SharedPrivate.assert_valid_opts!(CHECKOUT_ALLOWED_OPTS, **) target, translated_opts = Private.translate_checkout_opts(branch, ) 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
71 72 73 |
# File 'lib/git/repository/branching.rb', line 71 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
157 158 159 160 161 162 163 |
# File 'lib/git/repository/branching.rb', line 157 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
51 52 53 54 55 |
# File 'lib/git/repository/branching.rb', line 51 def current_branch result = Git::Commands::Branch::ShowCurrent.new(@execution_context).call name = result.stdout.strip name.empty? ? 'HEAD' : name end |
#local_branch?(branch) ⇒ Boolean
Returns true if the named branch exists as a local branch
176 177 178 179 |
# File 'lib/git/repository/branching.rb', line 176 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').
196 197 198 199 200 |
# File 'lib/git/repository/branching.rb', line 196 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)
411 412 413 414 |
# File 'lib/git/repository/branching.rb', line 411 def update_ref(branch, commit) ref = Private.build_update_ref(branch) Git::Commands::UpdateRef::Update.new(@execution_context).call(ref, commit) end |