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 ⇒ String?
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, 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) ⇒ String
Updates the git ref for this branch to point to the given commit.
Constructor Details
#initialize(base, 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
82 83 84 85 86 87 88 |
# File 'lib/git/branch.rb', line 82 def initialize(base, name) @full = name @base = base @gcommit = nil @stashes = nil @remote, @name = parse_name(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.
37 38 39 |
# File 'lib/git/branch.rb', line 37 def full @full end |
#name ⇒ String
The short branch name without the remote prefix
For branches initialized with a remotes/ or refs/remotes/ prefix, the
prefix is stripped and this returns the bare branch name (e.g. 'main'
rather than 'remotes/origin/main'). For branches in the
<remote>/<branch> form (such as those created by Remote#branch),
no stripping occurs and name returns the full form (e.g. 'origin/main').
70 71 72 |
# File 'lib/git/branch.rb', line 70 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).
53 54 55 |
# File 'lib/git/branch.rb', line 53 def remote @remote end |
Instance Method Details
#archive(file, opts = {}) ⇒ String
Archives this branch and writes the result to a file
155 156 157 |
# File 'lib/git/branch.rb', line 155 def archive(file, opts = {}) @base.lib.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),
#full is a ref such as 'remotes/origin/main'. Checking out a
remote-tracking ref places the repository in a detached HEAD state.
134 135 136 137 |
# File 'lib/git/branch.rb', line 134 def checkout check_if_create @base.checkout(@full) end |
#contains?(commit) ⇒ Boolean
267 268 269 |
# File 'lib/git/branch.rb', line 267 def contains?(commit) !@base.lib.branch_contains(commit, name).empty? end |
#create ⇒ String?
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).
208 209 210 |
# File 'lib/git/branch.rb', line 208 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.
247 248 249 |
# File 'lib/git/branch.rb', line 247 def current # rubocop:disable Naming/PredicateMethod @base.lib.branch_current == @name end |
#delete ⇒ String
Deletes this branch
Note: this method only works correctly for local branches. Calling it on
a remote-tracking branch (one where #remote is not nil) will attempt to
delete a local branch with the same short name rather than the
remote-tracking ref, which is almost certainly not what you want.
See ruby-git#1280 for
the planned fix.
228 229 230 |
# File 'lib/git/branch.rb', line 228 def delete @base.lib.branch_delete(@name) end |
#gcommit ⇒ Git::Object
Returns the commit at the tip of this branch
The result is memoized after the first call.
99 100 101 102 |
# File 'lib/git/branch.rb', line 99 def gcommit @gcommit ||= @base.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.
186 187 188 189 190 191 192 193 194 195 |
# File 'lib/git/branch.rb', line 186 def in_branch( = 'in branch work') old_current = @base.lib.branch_current checkout if yield @base.commit_all() else @base.reset_hard end @base.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
303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/git/branch.rb', line 303 def merge(branch = nil, = nil) if branch in_branch do @base.merge(branch, ) false end # merge a branch into this one else # merge this branch into the current one @base.merge(@name) end end |
#stashes ⇒ Git::Stashes
Returns the stash list for this repository
The result is memoized after the first call.
113 114 115 |
# File 'lib/git/branch.rb', line 113 def stashes @stashes ||= Git::Stashes.new(@base) end |
#to_a ⇒ Array<String>
Returns this branch as a single-element array containing its full refname
351 352 353 |
# File 'lib/git/branch.rb', line 351 def to_a [@full] end |
#to_s ⇒ String
Returns the full refname of this branch as a string
362 363 364 |
# File 'lib/git/branch.rb', line 362 def to_s @full end |
#update_ref(commit) ⇒ String
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 initialised 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/....
336 337 338 339 340 341 342 |
# File 'lib/git/branch.rb', line 336 def update_ref(commit) if @remote @base.lib.update_ref("refs/remotes/#{@remote.name}/#{@name}", commit) else @base.lib.update_ref("refs/heads/#{@name}", commit) end end |