Class: Hiiro::Git::Worktree

Inherits:
Object
  • Object
show all
Defined in:
lib/hiiro/git/worktree.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, head: nil, branch: nil, detached: false, bare: false) ⇒ Worktree

Returns a new instance of Worktree.



25
26
27
28
29
30
31
# File 'lib/hiiro/git/worktree.rb', line 25

def initialize(path:, head: nil, branch: nil, detached: false, bare: false)
  @path = path
  @head = head
  @branch = branch
  @detached = detached
  @bare = bare
end

Instance Attribute Details

#branchObject (readonly)

Returns the value of attribute branch.



4
5
6
# File 'lib/hiiro/git/worktree.rb', line 4

def branch
  @branch
end

#headObject (readonly)

Returns the value of attribute head.



4
5
6
# File 'lib/hiiro/git/worktree.rb', line 4

def head
  @head
end

#pathObject (readonly)

Returns the value of attribute path.



4
5
6
# File 'lib/hiiro/git/worktree.rb', line 4

def path
  @path
end

Class Method Details

.from_porcelain_block(lines) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/hiiro/git/worktree.rb', line 6

def self.from_porcelain_block(lines)
  attrs = {}
  lines.each do |line|
    case line
    when /^worktree (.+)/
      attrs[:path] = $1
    when /^HEAD (.+)/
      attrs[:head] = $1
    when /^branch refs\/heads\/(.+)/
      attrs[:branch] = $1
    when 'detached'
      attrs[:detached] = true
    when 'bare'
      attrs[:bare] = true
    end
  end
  new(**attrs) if attrs[:path]
end

Instance Method Details

#bare?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/hiiro/git/worktree.rb', line 37

def bare?
  @bare
end

#detached?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/hiiro/git/worktree.rb', line 33

def detached?
  @detached
end

#match?(pwd) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/hiiro/git/worktree.rb', line 45

def match?(pwd)
  pwd == path || pwd.start_with?(path + '/')
end

#nameObject



41
42
43
# File 'lib/hiiro/git/worktree.rb', line 41

def name
  File.basename(path)
end

#to_hObject



49
50
51
52
53
54
55
56
57
# File 'lib/hiiro/git/worktree.rb', line 49

def to_h
  {
    path: path,
    head: head,
    branch: branch,
    detached: detached?,
    bare: bare?,
  }.compact
end