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 Repository#branch or Repository#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 Repository#branch or Repository#branches instead of constructing directly
Initialize a new Branch object
80 81 82 83 84 85 86 |
# File 'lib/git/branch.rb', line 80 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 Repository#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 both local and remote-tracking branches this is the bare branch
name (e.g. 'main' rather than 'remotes/origin/main').
66 67 68 |
# File 'lib/git/branch.rb', line 66 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 = {}) 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.
134 135 136 137 |
# File 'lib/git/branch.rb', line 134 def checkout check_if_create branch_repository.checkout(@full) end |
#contains?(commit) ⇒ Boolean
266 267 268 |
# File 'lib/git/branch.rb', line 266 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).
207 208 209 |
# File 'lib/git/branch.rb', line 207 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.
246 247 248 |
# File 'lib/git/branch.rb', line 246 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.
223 224 225 226 227 228 229 |
# File 'lib/git/branch.rb', line 223 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.
97 98 99 100 |
# File 'lib/git/branch.rb', line 97 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.
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 = 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
301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/git/branch.rb', line 301 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.
111 112 113 |
# File 'lib/git/branch.rb', line 111 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
349 350 351 |
# File 'lib/git/branch.rb', line 349 def to_a [@full] end |
#to_s ⇒ String
Returns the full refname of this branch as a string
360 361 362 |
# File 'lib/git/branch.rb', line 360 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/....
334 335 336 337 338 339 340 |
# File 'lib/git/branch.rb', line 334 def update_ref(commit) if @remote branch_repository.update_ref("remotes/#{@remote.name}/#{@name}", commit) else branch_repository.update_ref(@name, commit) end end |