Class: Hiiro::Tree

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, head: nil, branch: nil) ⇒ Tree

Returns a new instance of Tree.



1495
1496
1497
1498
1499
# File 'lib/hiiro/tasks.rb', line 1495

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

Instance Attribute Details

#branchObject (readonly)

Returns the value of attribute branch.



1478
1479
1480
# File 'lib/hiiro/tasks.rb', line 1478

def branch
  @branch
end

#headObject (readonly)

Returns the value of attribute head.



1478
1479
1480
# File 'lib/hiiro/tasks.rb', line 1478

def head
  @head
end

#pathObject (readonly)

Returns the value of attribute path.



1478
1479
1480
# File 'lib/hiiro/tasks.rb', line 1478

def path
  @path
end

Class Method Details

.all(repo_path: Hiiro::REPO_PATH) ⇒ Object



1487
1488
1489
1490
1491
1492
1493
# File 'lib/hiiro/tasks.rb', line 1487

def self.all(repo_path: Hiiro::REPO_PATH)
  git = Hiiro::Git.new(nil, repo_path)
  git.worktrees(repo_path: repo_path).filter_map do |wt|
    next if wt.bare?
    new(path: wt.path, head: wt.head, branch: wt.branch)
  end
end

.from_path(path) ⇒ Object



1480
1481
1482
1483
1484
1485
# File 'lib/hiiro/tasks.rb', line 1480

def self.from_path(path)
  expanded = File.expand_path(path.to_s)
  branch = Hiiro::Git.new(nil, expanded).branch if Dir.exist?(expanded)
  branch = nil if branch.nil? || branch.empty? || branch == 'HEAD'
  new(path: expanded, branch: branch)
end

Instance Method Details

#==(other) ⇒ Object



1517
1518
1519
# File 'lib/hiiro/tasks.rb', line 1517

def ==(other)
  other.is_a?(Tree) && path == other.path
end

#detached?Boolean

Returns:

  • (Boolean)


1513
1514
1515
# File 'lib/hiiro/tasks.rb', line 1513

def detached?
  branch.nil?
end

#match?(pwd = Dir.pwd) ⇒ Boolean

Returns:

  • (Boolean)


1509
1510
1511
# File 'lib/hiiro/tasks.rb', line 1509

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

#nameObject



1501
1502
1503
1504
1505
1506
1507
# File 'lib/hiiro/tasks.rb', line 1501

def name
  @name ||= if path.start_with?(Hiiro::WORK_DIR + '/')
    path.sub(Hiiro::WORK_DIR + '/', '')
  else
    File.basename(path)
  end
end

#to_sObject



1521
1522
1523
# File 'lib/hiiro/tasks.rb', line 1521

def to_s
  name
end