Class: Ocak::WorktreeManager
- Inherits:
-
Object
- Object
- Ocak::WorktreeManager
- Defined in:
- lib/ocak/worktree_manager.rb
Defined Under Namespace
Classes: Worktree, WorktreeError
Instance Method Summary collapse
- #clean_stale ⇒ Object
- #create(issue_number, setup_command: nil) ⇒ Object
-
#initialize(config:) ⇒ WorktreeManager
constructor
A new instance of WorktreeManager.
- #list ⇒ Object
- #prune ⇒ Object
- #remove(worktree) ⇒ Object
Constructor Details
#initialize(config:) ⇒ WorktreeManager
Returns a new instance of WorktreeManager.
10 11 12 13 14 |
# File 'lib/ocak/worktree_manager.rb', line 10 def initialize(config:) @config = config @worktree_base = File.join(config.project_dir, config.worktree_dir) @mutex = Mutex.new end |
Instance Method Details
#clean_stale ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/ocak/worktree_manager.rb', line 51 def clean_stale removed = [] list.each do |wt| next unless wt[:path]&.include?(@worktree_base) git('worktree', 'remove', '--force', wt[:path]) removed << wt[:path] end prune removed end |
#create(issue_number, setup_command: nil) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/ocak/worktree_manager.rb', line 16 def create(issue_number, setup_command: nil) @mutex.synchronize do FileUtils.mkdir_p(@worktree_base) branch = "auto/issue-#{issue_number}-#{SecureRandom.hex(4)}" path = File.join(@worktree_base, "issue-#{issue_number}") _, stderr, status = git('worktree', 'add', '-b', branch, path, 'main') raise WorktreeError, "Failed to create worktree: #{stderr}" unless status.success? if setup_command _, stderr, status = Open3.capture3(*Shellwords.shellsplit(setup_command), chdir: path) raise WorktreeError, "Setup command failed: #{stderr}" unless status.success? end Worktree.new(path: path, branch: branch, issue_number: issue_number) end end |
#list ⇒ Object
40 41 42 43 44 45 |
# File 'lib/ocak/worktree_manager.rb', line 40 def list stdout, _, status = git('worktree', 'list', '--porcelain') return [] unless status.success? parse_worktree_list(stdout) end |
#prune ⇒ Object
47 48 49 |
# File 'lib/ocak/worktree_manager.rb', line 47 def prune git('worktree', 'prune') end |
#remove(worktree) ⇒ Object
35 36 37 38 |
# File 'lib/ocak/worktree_manager.rb', line 35 def remove(worktree) git('worktree', 'remove', '--force', worktree.path) git('worktree', 'prune') end |