Module: Mighost::WorktreeRecovery

Defined in:
lib/mighost/worktree_recovery.rb

Overview

Searches other git worktrees for migration files that exist on disk but haven't been committed yet (invisible to git log --all)

Class Method Summary collapse

Class Method Details

.find_migration(version) ⇒ Object

Find migration file in other worktrees Returns { content:, filename:, branch:, worktree_path: } or nil



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mighost/worktree_recovery.rb', line 15

def find_migration(version)
  return nil unless GitMetadata.git_available?

  worktrees = list_worktrees
  return nil if worktrees.empty?

  current_root = git_root
  return nil unless current_root

  worktrees.each do |wt|
    next if wt[:path] == current_root

    result = find_in_worktree(wt, version)
    return result if result
  end

  nil
end

.list_worktreesObject

List all git worktrees with their paths and branches



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mighost/worktree_recovery.rb', line 35

def list_worktrees
  output = `git worktree list --porcelain 2>/dev/null`.strip
  return [] if output.empty?

  worktrees = []
  current = {}

  output.each_line do |line|
    line = line.chomp
    if line.start_with?("worktree ")
      current = {path: line.sub("worktree ", "")}
    elsif line.start_with?("branch ")
      ref = line.sub("branch ", "")
      # refs/heads/feature-branch -> feature-branch
      current[:branch] = ref.sub(%r{^refs/heads/}, "")
    elsif line == "detached"
      current[:branch] = nil
    elsif line.empty? && current[:path]
      worktrees << current
      current = {}
    end
  end

  # Last entry (no trailing blank line)
  worktrees << current if current[:path]

  worktrees
end