Class: Git::DetachedHeadInfo

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

Overview

Value object representing a detached HEAD state

When HEAD points directly to a commit rather than a branch reference, the repository is in a "detached HEAD" state. This object captures that state along with the commit SHA that HEAD points to.

This class shares a minimal interface with BranchInfo to allow polymorphic usage where appropriate:

  • short_name - returns 'HEAD'
  • target_oid - returns the commit SHA
  • to_s - returns 'HEAD'
  • detached? - returns true

Examples:

Detecting detached HEAD state

head = repo.show_current
if head.detached?
  puts "HEAD detached at #{head.target_oid[0, 7]}"
else
  puts "On branch #{head.short_name}"
end

Polymorphic usage

head = repo.show_current
puts "Checked out: #{head.short_name}"  # Works for both types
system("git log #{head.short_name}")    # Works for both types

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#target_oidObject (readonly)

Returns the value of attribute target_oid

Returns:

  • (Object)

    the current value of target_oid



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/git/detached_head_info.rb', line 41

DetachedHeadInfo = Data.define(:target_oid) do
  # @return [Boolean] always true for DetachedHeadInfo
  def detached? = true

  # @return [Boolean] always false for DetachedHeadInfo (detached HEAD always has a commit)
  def unborn? = false

  # @return [String] always 'HEAD'
  def short_name = 'HEAD'

  # @return [String] always 'HEAD'
  def to_s = 'HEAD'
end

Instance Method Details

#detached?Boolean

Returns always true for DetachedHeadInfo.

Returns:

  • (Boolean)

    always true for DetachedHeadInfo



43
# File 'lib/git/detached_head_info.rb', line 43

def detached? = true

#short_nameString

Returns always 'HEAD'.

Returns:

  • (String)

    always 'HEAD'



49
# File 'lib/git/detached_head_info.rb', line 49

def short_name = 'HEAD'

#to_sString

Returns always 'HEAD'.

Returns:

  • (String)

    always 'HEAD'



52
# File 'lib/git/detached_head_info.rb', line 52

def to_s = 'HEAD'

#unborn?Boolean

Returns always false for DetachedHeadInfo (detached HEAD always has a commit).

Returns:

  • (Boolean)

    always false for DetachedHeadInfo (detached HEAD always has a commit)



46
# File 'lib/git/detached_head_info.rb', line 46

def unborn? = false