Class: Workbush::Worktree

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

Overview

Handles git worktree operations

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, branch: nil, source_path: Dir.pwd) ⇒ Worktree

Initialize a new Worktree instance

Parameters:

  • path (String)

    Path where the worktree will be created

  • branch (String, nil) (defaults to: nil)

    Branch name to checkout

  • source_path (String) (defaults to: Dir.pwd)

    Path to the source/parent worktree



17
18
19
20
21
# File 'lib/workbush/worktree.rb', line 17

def initialize(path:, branch: nil, source_path: Dir.pwd)
  @path = File.expand_path(path)
  @branch = branch
  @source_path = source_path
end

Instance Attribute Details

#branchObject (readonly)

Returns the value of attribute branch.



10
11
12
# File 'lib/workbush/worktree.rb', line 10

def branch
  @branch
end

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/workbush/worktree.rb', line 10

def path
  @path
end

Class Method Details

.listArray<Hash>

List all worktrees in the repository

Returns:

  • (Array<Hash>)

    Array of worktree information

Raises:



47
48
49
50
51
52
53
# File 'lib/workbush/worktree.rb', line 47

def self.list
  stdout, stderr, status = Open3.capture3("git", "worktree", "list", "--porcelain")

  raise GitError, "Failed to list worktrees: #{stderr}" unless status.success?

  parse_worktree_list(stdout)
end

.remove(path, force: false) ⇒ Boolean

Remove a worktree

Parameters:

  • path (String)

    Path to the worktree to remove

  • force (Boolean) (defaults to: false)

    Force removal even if worktree is dirty

Returns:

  • (Boolean)

    True if successful

Raises:



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/workbush/worktree.rb', line 61

def self.remove(path, force: false)
  cmd = ["git", "worktree", "remove", path]
  cmd << "--force" if force

  stdout, stderr, status = Open3.capture3(*cmd)

  unless status.success?
    raise WorktreeError, "Failed to remove worktree: #{stderr}"
  end

  true
end

Instance Method Details

#create(force: false) ⇒ String

Create the worktree

Parameters:

  • force (Boolean) (defaults to: false)

    Force creation even if path exists

Returns:

  • (String)

    Git command output

Raises:



29
30
31
32
33
34
35
# File 'lib/workbush/worktree.rb', line 29

def create(force: false)
  validate_git_repository!
  validate_path!(force)

  cmd = build_git_command(force)
  execute_git_command(cmd)
end

#source_pathString

Get the parent/source worktree path

Returns:

  • (String)


40
41
42
# File 'lib/workbush/worktree.rb', line 40

def source_path
  @source_path
end