Class: Git::BranchInfo

Inherits:
Data
  • Object
show all
Defined in:
lib/git/branch_info.rb

Overview

Value object representing branch metadata from git branch output

This is a lightweight, immutable data structure returned by branch listing commands. It contains only the data parsed from git output without any repository context or operations.

Examples:

Creating from git branch output

info = Git::BranchInfo.new(
  refname: 'main',
  target_oid: 'abc123def456789012345678901234567890abcd',
  current: true,
  worktree: false,
  symref: nil,
  upstream: nil
)
info.current?     #=> true
info.remote?      #=> false
info.short_name   #=> 'main'

Remote branch

info = Git::BranchInfo.new(
  refname: 'remotes/origin/main',
  target_oid: 'abc123def456789012345678901234567890abcd',
  current: false,
  worktree: false,
  symref: nil,
  upstream: nil
)
info.remote?      #=> true
info.remote_name  #=> 'origin'
info.short_name   #=> 'main'

Local branch with upstream tracking

upstream_info = Git::BranchInfo.new(
  refname: 'remotes/origin/main',
  target_oid: 'abc123def456789012345678901234567890abcd',
  current: false,
  worktree: false,
  symref: nil,
  upstream: nil
)
info = Git::BranchInfo.new(
  refname: 'main',
  target_oid: 'abc123def456789012345678901234567890abcd',
  current: true,
  worktree: false,
  symref: nil,
  upstream: upstream_info
)
info.upstream.remote_name  #=> 'origin'

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current

Returns:

  • (Object)

    the current value of current



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/git/branch_info.rb', line 133

BranchInfo = Data.define(:refname, :target_oid, :current, :worktree, :symref, :upstream) do
  # @return [Boolean] always false for BranchInfo (see DetachedHeadInfo for detached state)
  def detached? = false

  # @return [Boolean] true if this is an unborn branch (no commits yet)
  def unborn? = target_oid.nil?

  # @return [Boolean] true if this is the currently checked out branch
  def current? = current

  # @return [Boolean] true if this branch is checked out in another worktree
  def worktree? = worktree

  # @return [Boolean] true if this is a symbolic reference
  def symref? = !symref.nil?

  # @return [Boolean] true if this is a remote-tracking branch
  def remote? = !remote_name.nil?

  # @return [String, nil] the name of the remote (e.g., 'origin'), or nil for local branches
  def remote_name
    parse_refname[:remote_name]
  end

  # @return [String] the branch name without remote prefix (e.g., 'main' or 'feature/foo')
  def short_name
    parse_refname[:branch_name]
  end

  # @return [String] string representation (the full refname)
  def to_s = refname

  private

  # Parse the refname and return match data
  #
  # The regex is guaranteed to match any non-empty string due to the `.+` pattern,
  # so we don't need nil checking. If refname is empty/nil, this would fail at
  # object creation time since refname is a required attribute.
  #
  # @return [MatchData] the match result
  def parse_refname
    refname.match(Git::BRANCH_REFNAME_REGEXP)
  end
end

#refnameObject (readonly)

Returns the value of attribute refname

Returns:

  • (Object)

    the current value of refname



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/git/branch_info.rb', line 133

BranchInfo = Data.define(:refname, :target_oid, :current, :worktree, :symref, :upstream) do
  # @return [Boolean] always false for BranchInfo (see DetachedHeadInfo for detached state)
  def detached? = false

  # @return [Boolean] true if this is an unborn branch (no commits yet)
  def unborn? = target_oid.nil?

  # @return [Boolean] true if this is the currently checked out branch
  def current? = current

  # @return [Boolean] true if this branch is checked out in another worktree
  def worktree? = worktree

  # @return [Boolean] true if this is a symbolic reference
  def symref? = !symref.nil?

  # @return [Boolean] true if this is a remote-tracking branch
  def remote? = !remote_name.nil?

  # @return [String, nil] the name of the remote (e.g., 'origin'), or nil for local branches
  def remote_name
    parse_refname[:remote_name]
  end

  # @return [String] the branch name without remote prefix (e.g., 'main' or 'feature/foo')
  def short_name
    parse_refname[:branch_name]
  end

  # @return [String] string representation (the full refname)
  def to_s = refname

  private

  # Parse the refname and return match data
  #
  # The regex is guaranteed to match any non-empty string due to the `.+` pattern,
  # so we don't need nil checking. If refname is empty/nil, this would fail at
  # object creation time since refname is a required attribute.
  #
  # @return [MatchData] the match result
  def parse_refname
    refname.match(Git::BRANCH_REFNAME_REGEXP)
  end
end

#symrefObject (readonly)

Returns the value of attribute symref

Returns:

  • (Object)

    the current value of symref



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/git/branch_info.rb', line 133

BranchInfo = Data.define(:refname, :target_oid, :current, :worktree, :symref, :upstream) do
  # @return [Boolean] always false for BranchInfo (see DetachedHeadInfo for detached state)
  def detached? = false

  # @return [Boolean] true if this is an unborn branch (no commits yet)
  def unborn? = target_oid.nil?

  # @return [Boolean] true if this is the currently checked out branch
  def current? = current

  # @return [Boolean] true if this branch is checked out in another worktree
  def worktree? = worktree

  # @return [Boolean] true if this is a symbolic reference
  def symref? = !symref.nil?

  # @return [Boolean] true if this is a remote-tracking branch
  def remote? = !remote_name.nil?

  # @return [String, nil] the name of the remote (e.g., 'origin'), or nil for local branches
  def remote_name
    parse_refname[:remote_name]
  end

  # @return [String] the branch name without remote prefix (e.g., 'main' or 'feature/foo')
  def short_name
    parse_refname[:branch_name]
  end

  # @return [String] string representation (the full refname)
  def to_s = refname

  private

  # Parse the refname and return match data
  #
  # The regex is guaranteed to match any non-empty string due to the `.+` pattern,
  # so we don't need nil checking. If refname is empty/nil, this would fail at
  # object creation time since refname is a required attribute.
  #
  # @return [MatchData] the match result
  def parse_refname
    refname.match(Git::BRANCH_REFNAME_REGEXP)
  end
end

#target_oidObject (readonly)

Returns the value of attribute target_oid

Returns:

  • (Object)

    the current value of target_oid



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/git/branch_info.rb', line 133

BranchInfo = Data.define(:refname, :target_oid, :current, :worktree, :symref, :upstream) do
  # @return [Boolean] always false for BranchInfo (see DetachedHeadInfo for detached state)
  def detached? = false

  # @return [Boolean] true if this is an unborn branch (no commits yet)
  def unborn? = target_oid.nil?

  # @return [Boolean] true if this is the currently checked out branch
  def current? = current

  # @return [Boolean] true if this branch is checked out in another worktree
  def worktree? = worktree

  # @return [Boolean] true if this is a symbolic reference
  def symref? = !symref.nil?

  # @return [Boolean] true if this is a remote-tracking branch
  def remote? = !remote_name.nil?

  # @return [String, nil] the name of the remote (e.g., 'origin'), or nil for local branches
  def remote_name
    parse_refname[:remote_name]
  end

  # @return [String] the branch name without remote prefix (e.g., 'main' or 'feature/foo')
  def short_name
    parse_refname[:branch_name]
  end

  # @return [String] string representation (the full refname)
  def to_s = refname

  private

  # Parse the refname and return match data
  #
  # The regex is guaranteed to match any non-empty string due to the `.+` pattern,
  # so we don't need nil checking. If refname is empty/nil, this would fail at
  # object creation time since refname is a required attribute.
  #
  # @return [MatchData] the match result
  def parse_refname
    refname.match(Git::BRANCH_REFNAME_REGEXP)
  end
end

#upstreamObject (readonly)

Returns the value of attribute upstream

Returns:

  • (Object)

    the current value of upstream



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/git/branch_info.rb', line 133

BranchInfo = Data.define(:refname, :target_oid, :current, :worktree, :symref, :upstream) do
  # @return [Boolean] always false for BranchInfo (see DetachedHeadInfo for detached state)
  def detached? = false

  # @return [Boolean] true if this is an unborn branch (no commits yet)
  def unborn? = target_oid.nil?

  # @return [Boolean] true if this is the currently checked out branch
  def current? = current

  # @return [Boolean] true if this branch is checked out in another worktree
  def worktree? = worktree

  # @return [Boolean] true if this is a symbolic reference
  def symref? = !symref.nil?

  # @return [Boolean] true if this is a remote-tracking branch
  def remote? = !remote_name.nil?

  # @return [String, nil] the name of the remote (e.g., 'origin'), or nil for local branches
  def remote_name
    parse_refname[:remote_name]
  end

  # @return [String] the branch name without remote prefix (e.g., 'main' or 'feature/foo')
  def short_name
    parse_refname[:branch_name]
  end

  # @return [String] string representation (the full refname)
  def to_s = refname

  private

  # Parse the refname and return match data
  #
  # The regex is guaranteed to match any non-empty string due to the `.+` pattern,
  # so we don't need nil checking. If refname is empty/nil, this would fail at
  # object creation time since refname is a required attribute.
  #
  # @return [MatchData] the match result
  def parse_refname
    refname.match(Git::BRANCH_REFNAME_REGEXP)
  end
end

#worktreeObject (readonly)

Returns the value of attribute worktree

Returns:

  • (Object)

    the current value of worktree



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/git/branch_info.rb', line 133

BranchInfo = Data.define(:refname, :target_oid, :current, :worktree, :symref, :upstream) do
  # @return [Boolean] always false for BranchInfo (see DetachedHeadInfo for detached state)
  def detached? = false

  # @return [Boolean] true if this is an unborn branch (no commits yet)
  def unborn? = target_oid.nil?

  # @return [Boolean] true if this is the currently checked out branch
  def current? = current

  # @return [Boolean] true if this branch is checked out in another worktree
  def worktree? = worktree

  # @return [Boolean] true if this is a symbolic reference
  def symref? = !symref.nil?

  # @return [Boolean] true if this is a remote-tracking branch
  def remote? = !remote_name.nil?

  # @return [String, nil] the name of the remote (e.g., 'origin'), or nil for local branches
  def remote_name
    parse_refname[:remote_name]
  end

  # @return [String] the branch name without remote prefix (e.g., 'main' or 'feature/foo')
  def short_name
    parse_refname[:branch_name]
  end

  # @return [String] string representation (the full refname)
  def to_s = refname

  private

  # Parse the refname and return match data
  #
  # The regex is guaranteed to match any non-empty string due to the `.+` pattern,
  # so we don't need nil checking. If refname is empty/nil, this would fail at
  # object creation time since refname is a required attribute.
  #
  # @return [MatchData] the match result
  def parse_refname
    refname.match(Git::BRANCH_REFNAME_REGEXP)
  end
end

Instance Method Details

#current?Boolean

Returns true if this is the currently checked out branch.

Returns:

  • (Boolean)

    true if this is the currently checked out branch



141
# File 'lib/git/branch_info.rb', line 141

def current? = current

#detached?Boolean

Returns always false for BranchInfo (see DetachedHeadInfo for detached state).

Returns:

  • (Boolean)

    always false for BranchInfo (see DetachedHeadInfo for detached state)



135
# File 'lib/git/branch_info.rb', line 135

def detached? = false

#remote?Boolean

Returns true if this is a remote-tracking branch.

Returns:

  • (Boolean)

    true if this is a remote-tracking branch



150
# File 'lib/git/branch_info.rb', line 150

def remote? = !remote_name.nil?

#remote_nameString?

Returns the name of the remote (e.g., 'origin'), or nil for local branches.

Returns:

  • (String, nil)

    the name of the remote (e.g., 'origin'), or nil for local branches



153
154
155
# File 'lib/git/branch_info.rb', line 153

def remote_name
  parse_refname[:remote_name]
end

#short_nameString

Returns the branch name without remote prefix (e.g., 'main' or 'feature/foo').

Returns:

  • (String)

    the branch name without remote prefix (e.g., 'main' or 'feature/foo')



158
159
160
# File 'lib/git/branch_info.rb', line 158

def short_name
  parse_refname[:branch_name]
end

#symref?Boolean

Returns true if this is a symbolic reference.

Returns:

  • (Boolean)

    true if this is a symbolic reference



147
# File 'lib/git/branch_info.rb', line 147

def symref? = !symref.nil?

#to_sString

Returns string representation (the full refname).

Returns:

  • (String)

    string representation (the full refname)



163
# File 'lib/git/branch_info.rb', line 163

def to_s = refname

#unborn?Boolean

Returns true if this is an unborn branch (no commits yet).

Returns:

  • (Boolean)

    true if this is an unborn branch (no commits yet)



138
# File 'lib/git/branch_info.rb', line 138

def unborn? = target_oid.nil?

#worktree?Boolean

Returns true if this branch is checked out in another worktree.

Returns:

  • (Boolean)

    true if this branch is checked out in another worktree



144
# File 'lib/git/branch_info.rb', line 144

def worktree? = worktree