Class: WorktreeManager::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/worktree_manager/manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository_path = '.') ⇒ Manager

Returns a new instance of Manager.



8
9
10
11
# File 'lib/worktree_manager/manager.rb', line 8

def initialize(repository_path = '.')
  @repository_path = File.expand_path(repository_path)
  validate_git_repository!
end

Instance Attribute Details

#repository_pathObject (readonly)

Returns the value of attribute repository_path.



6
7
8
# File 'lib/worktree_manager/manager.rb', line 6

def repository_path
  @repository_path
end

Instance Method Details

#add(path, branch = nil, force: false) ⇒ Object

Raises:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/worktree_manager/manager.rb', line 20

def add(path, branch = nil, force: false)
  command = %w[worktree add]
  command << '--force' if force
  command << path
  command << branch if branch

  output, status = execute_git_command(command.join(' '))
  raise Error, output unless status.success?

  # Return created worktree information
  worktree_info = { path: path }
  worktree_info[:branch] = branch if branch
  Worktree.new(worktree_info)
end

#add_tracking_branch(path, local_branch, remote_branch, force: false) ⇒ Object

Raises:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/worktree_manager/manager.rb', line 48

def add_tracking_branch(path, local_branch, remote_branch, force: false)
  # Fetch the remote branch first
  remote, branch_name = remote_branch.split('/', 2)
  fetch_output, fetch_status = execute_git_command("fetch #{remote} #{branch_name}")
  raise Error, "Failed to fetch remote branch: #{fetch_output}" unless fetch_status.success?

  # Create worktree with new branch tracking the remote
  command = %w[worktree add]
  command << '--force' if force
  command << '-b' << local_branch
  command << path
  command << "#{remote_branch}"

  output, status = execute_git_command(command.join(' '))
  raise Error, output unless status.success?

  # Return created worktree information
  Worktree.new(path: path, branch: local_branch)
end

#add_with_new_branch(path, branch, force: false) ⇒ Object

Raises:



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/worktree_manager/manager.rb', line 35

def add_with_new_branch(path, branch, force: false)
  command = %w[worktree add]
  command << '--force' if force
  command << '-b' << branch
  command << path

  output, status = execute_git_command(command.join(' '))
  raise Error, output unless status.success?

  # Return created worktree information
  Worktree.new(path: path, branch: branch)
end

#listObject



13
14
15
16
17
18
# File 'lib/worktree_manager/manager.rb', line 13

def list
  output, status = execute_git_command('worktree list --porcelain')
  return [] unless status.success?

  parse_worktree_list(output)
end

#pruneObject

Raises:



79
80
81
82
83
84
# File 'lib/worktree_manager/manager.rb', line 79

def prune
  output, status = execute_git_command('worktree prune')
  raise Error, output unless status.success?

  true
end

#remove(path, force: false) ⇒ Object

Raises:



68
69
70
71
72
73
74
75
76
77
# File 'lib/worktree_manager/manager.rb', line 68

def remove(path, force: false)
  command = %w[worktree remove]
  command << '--force' if force
  command << path

  output, status = execute_git_command(command.join(' '))
  raise Error, output unless status.success?

  true
end