Class: Git::Branch
- Inherits:
-
Object
- Object
- Git::Branch
- Defined in:
- lib/git/branch.rb
Overview
Represents a Git branch
Branch objects provide access to branch metadata and operations like checkout, delete, and merge. They should be obtained via Git::Base#branch or Git::Base#branches, not constructed directly.
Constant Summary collapse
- BRANCH_NAME_REGEXP =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Regular expression for parsing branch refnames
Matches full and short refnames, capturing an optional remote name and the branch name. Used internally to identify remote-tracking branches.
%r{ ^ # Optional 'remotes/' or 'refs/remotes/' at the beginning to specify a remote tracking branch # with a <remote_name>. <remote_name> is nil if not present. (?: (?:(?:refs/)?remotes/)(?<remote_name>[^/]+)/ )? (?<branch_name>.*) $ }x
Instance Attribute Summary collapse
-
#full ⇒ String
The full refname of this branch.
-
#name ⇒ String
The short branch name without the remote prefix.
-
#remote ⇒ Git::Remote?
The remote for this branch, or
nilfor local or bare-name remote-tracking branches.
Instance Method Summary collapse
-
#archive(file, opts = {}) ⇒ String
Archives this branch and writes the result to a file.
-
#checkout ⇒ String
Checks out this branch, attempting to create it first if it does not already exist.
-
#contains?(commit) ⇒ Boolean
Returns true if this branch contains the given commit.
-
#create ⇒ nil
Creates this branch if it does not already exist.
-
#current ⇒ Boolean
Returns true if this is the currently checked-out branch.
-
#delete ⇒ String
Deletes this branch.
-
#gcommit ⇒ Git::Object
Returns the commit at the tip of this branch.
-
#in_branch(message = 'in branch work') { ... } ⇒ String
Checks out this branch for the duration of a block, then restores the original branch.
-
#initialize(base, branch_info_or_name) ⇒ Branch
constructor
private
Initialize a new Branch object.
-
#merge(branch = nil, message = nil)
Merges a branch into this branch, or merges this branch into the current branch.
-
#stashes ⇒ Git::Stashes
Returns the stash list for this repository.
-
#to_a ⇒ Array<String>
Returns this branch as a single-element array containing its full refname.
-
#to_s ⇒ String
Returns the full refname of this branch as a string.
-
#update_ref(commit) ⇒ Git::CommandLineResult
Updates the git ref for this branch to point to the given commit.
Constructor Details
#initialize(base, branch_info_or_name) ⇒ 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 Git::Base#branch or Git::Base#branches instead of constructing directly
Initialize a new Branch object
85 86 87 88 89 90 91 |
# File 'lib/git/branch.rb', line 85 def initialize(base, branch_info_or_name) @base = base @gcommit = nil @stashes = nil initialize_from_argument(branch_info_or_name) end |
Instance Attribute Details
#full ⇒ String
The full refname of this branch
For local branches this is the short name (e.g. 'main'). For
remote-tracking branches obtained via Git::Base#branches this includes
the remotes/ prefix (e.g. 'remotes/origin/main'). Branches constructed
by Remote#branch use the <remote>/<branch> form (e.g.
'origin/main') which does not populate #remote.
38 39 40 |
# File 'lib/git/branch.rb', line 38 def full @full end |
#name ⇒ String
The short branch name without the remote prefix
For both local and remote-tracking branches this is the bare branch
name (e.g. 'main' rather than 'remotes/origin/main').
67 68 69 |
# File 'lib/git/branch.rb', line 67 def name @name end |
#remote ⇒ Git::Remote?
The remote for this branch, or nil for local or bare-name remote-tracking branches
Set to a Remote object only when this branch was initialized with a
remotes/<remote>/ or refs/remotes/<remote>/ prefix. nil for local
branches and for remote-tracking branches in <remote>/<branch> form
(such as those returned by Remote#branch).
54 55 56 |
# File 'lib/git/branch.rb', line 54 def remote @remote end |
Instance Method Details
#archive(file, opts = {}) ⇒ String
Archives this branch and writes the result to a file
160 161 162 |
# File 'lib/git/branch.rb', line 160 def archive(file, opts = {}) branch_repository.archive(@full, file, opts) end |
#checkout ⇒ String
Checks out this branch, attempting to create it first if it does not already exist
Branch creation is attempted via #check_if_create; any error from that step is silently ignored and the checkout proceeds regardless.
Note: for remote-tracking branches (where #remote is not nil),
check_if_create will attempt to create a local branch named #name
as a side-effect before checking out #full (which typically results in
a detached HEAD). This is a known limitation; see
ruby-git#1280.
139 140 141 142 |
# File 'lib/git/branch.rb', line 139 def checkout check_if_create branch_repository.checkout(@full) end |
#contains?(commit) ⇒ Boolean
271 272 273 |
# File 'lib/git/branch.rb', line 271 def contains?(commit) !branch_repository.branch_contains(commit, name).empty? end |
#create ⇒ nil
Creates this branch if it does not already exist
Silently ignores any error raised during branch creation (including the case where the branch already exists).
212 213 214 |
# File 'lib/git/branch.rb', line 212 def create check_if_create end |
#current ⇒ Boolean
Returns true if this is the currently checked-out branch
Note: this compares the current branch's short name against #name.
For a remote-tracking branch (where #remote is not nil), #name is
still the bare short name (e.g. 'main'), so this will return true
whenever the local branch with that name is checked out — not the
remote-tracking ref itself.
251 252 253 |
# File 'lib/git/branch.rb', line 251 def current # rubocop:disable Naming/PredicateMethod branch_repository.current_branch == @name end |
#delete ⇒ String
Deletes this branch
Remote-tracking branches (one where #remote is not nil) delete the
local remote-tracking ref; they do not push a deletion to the remote.
228 229 230 231 232 233 234 |
# File 'lib/git/branch.rb', line 228 def delete if @remote branch_repository.branch_delete("#{@remote.name}/#{@name}", remotes: true) else branch_repository.branch_delete(@name) end end |
#gcommit ⇒ Git::Object
Returns the commit at the tip of this branch
The result is memoized after the first call.
102 103 104 105 |
# File 'lib/git/branch.rb', line 102 def gcommit @gcommit ||= branch_repository.gcommit(@full) @gcommit end |
#in_branch(message = 'in branch work') { ... } ⇒ String
Checks out this branch for the duration of a block, then restores the original branch
If the block returns a truthy value, all pending changes are committed with the given message before switching back to the original branch. If the block returns a falsy value, a hard reset is performed before switching back.
Note: the restore checkout is not wrapped in ensure. If the block,
the commit, or the reset raises an exception, the repository will be left
checked out on this branch rather than restored to the original.
191 192 193 194 195 196 197 198 199 200 |
# File 'lib/git/branch.rb', line 191 def in_branch( = 'in branch work') old_current = branch_repository.current_branch checkout if yield branch_repository.commit_all() else branch_repository.reset(nil, hard: true) end branch_repository.checkout(old_current) end |
#merge(branch, message = nil) ⇒ String #merge ⇒ String
Merges a branch into this branch, or merges this branch into the current branch
306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/git/branch.rb', line 306 def merge(branch = nil, = nil) if branch in_branch do branch_repository.merge(branch, ) false end # merge a branch into this one else # merge this branch into the current one branch_repository.merge(@name) end end |
#stashes ⇒ Git::Stashes
Returns the stash list for this repository
The result is memoized after the first call.
116 117 118 |
# File 'lib/git/branch.rb', line 116 def stashes @stashes ||= Git::Stashes.new(branch_repository) end |
#to_a ⇒ Array<String>
Returns this branch as a single-element array containing its full refname
354 355 356 |
# File 'lib/git/branch.rb', line 354 def to_a [@full] end |
#to_s ⇒ String
Returns the full refname of this branch as a string
365 366 367 |
# File 'lib/git/branch.rb', line 365 def to_s @full end |
#update_ref(commit) ⇒ Git::CommandLineResult
Updates the git ref for this branch to point to the given commit
The target ref depends on whether #remote is set:
- When #remote is not
nil(i.e. the branch was initialized with aremotes/<remote>/orrefs/remotes/<remote>/prefix), updatesrefs/remotes/<remote>/<name>. - Otherwise updates
refs/heads/<name>. Note that branches in the<remote>/<branch>form (e.g. those returned by Remote#branch) haveremote == niland therefore updaterefs/heads/<remote>/<name>, notrefs/remotes/....
339 340 341 342 343 344 345 |
# File 'lib/git/branch.rb', line 339 def update_ref(commit) if @remote branch_repository.update_ref("remotes/#{@remote.name}/#{@name}", commit) else branch_repository.update_ref(@name, commit) end end |