Class: Ace::Git::Worktree::Atoms::GitCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/git/worktree/atoms/git_command.rb

Overview

Git command execution atom

Provides a thin wrapper around ace-git’s CommandExecutor for safe git command execution with proper error handling. This adapter maintains backward compatibility with the original API while delegating to the consolidated ace-git package.

Examples:

Execute a git worktree command

GitCommand.execute("worktree", "add", "/path/to/worktree", "-b", "feature-branch")

Handle command failures

result = GitCommand.execute("worktree", "add", "/invalid/path")
unless result[:success]
  puts "Error: #{result[:error]}"
end

Constant Summary collapse

FALLBACK_TIMEOUT =

Fallback timeout for git commands (30 seconds) Used only when config is unavailable

30

Class Method Summary collapse

Class Method Details

.current_branchString?

Get current git branch name or commit SHA if in detached HEAD state

Returns:

  • (String, nil)

    Current branch name, commit SHA (if detached), or nil on error



93
94
95
96
97
# File 'lib/ace/git/worktree/atoms/git_command.rb', line 93

def current_branch
  # ace-git's current_branch now handles detached HEAD state
  # and returns SHA directly when detached
  Ace::Git::Atoms::CommandExecutor.current_branch
end

.default_timeoutInteger

Get default timeout from config or fallback

Returns:

  • (Integer)

    Timeout in seconds



30
31
32
33
34
# File 'lib/ace/git/worktree/atoms/git_command.rb', line 30

def default_timeout
  Ace::Git::Worktree.default_timeout
rescue
  FALLBACK_TIMEOUT
end

.execute(*args, timeout: nil) ⇒ Hash

Execute a git command safely using ace-git’s CommandExecutor

Examples:

result = GitCommand.execute("worktree", "list")
# => { success: true, output: "/path/to/worktree abc123 [branch-name]\n", error: "", exit_code: 0 }

Parameters:

  • args (Array<String>)

    Command arguments (command and its arguments)

  • timeout (Integer, nil) (defaults to: nil)

    Timeout in seconds (uses config default if nil)

Returns:

  • (Hash)

    Result hash with :success, :output, :error, :exit_code keys



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ace/git/worktree/atoms/git_command.rb', line 45

def execute(*args, timeout: nil)
  timeout ||= default_timeout
  # Ensure all arguments are strings
  string_args = args.map(&:to_s)

  # Delegate to ace-git's CommandExecutor
  # Note: CommandExecutor expects "git" as first argument
  result = Ace::Git::Atoms::CommandExecutor.execute("git", *string_args, timeout:)

  # Normalize ace-git result keys for legacy compatibility
  {
    success: result[:success],
    output: result[:output].to_s,
    error: result[:error].to_s,
    exit_code: result[:exit_code] || 0
  }
rescue => e
  # Handle unexpected exceptions from ace-git to maintain defensive behavior
  {
    success: false,
    output: "",
    error: "Unexpected error: #{e.message}",
    exit_code: 1
  }
end

.git_repository?Boolean

Check if git repository exists

Returns:

  • (Boolean)

    true if current directory is a git repository



86
87
88
# File 'lib/ace/git/worktree/atoms/git_command.rb', line 86

def git_repository?
  Ace::Git::Atoms::CommandExecutor.in_git_repo?
end

.git_rootString?

Get git repository root directory

Delegates to ace-git’s CommandExecutor for repository root detection

Returns:

  • (String, nil)

    Path to git repository root or nil if not in a git repo



113
114
115
# File 'lib/ace/git/worktree/atoms/git_command.rb', line 113

def git_root
  Ace::Git::Atoms::CommandExecutor.repo_root
end

.ref_exists?(ref) ⇒ Boolean

Check if a git ref (branch, tag, commit SHA) exists

Parameters:

  • ref (String)

    Git ref to validate

Returns:

  • (Boolean)

    true if ref exists



103
104
105
106
# File 'lib/ace/git/worktree/atoms/git_command.rb', line 103

def ref_exists?(ref)
  result = execute("rev-parse", "--verify", "--quiet", ref, timeout: 5)
  result[:success]
end

.worktree(*args, timeout: nil) ⇒ Hash

Execute a git worktree command

Examples:

result = GitCommand.worktree("add", "/path/to/worktree", "-b", "feature-branch")

Parameters:

  • args (Array<String>)

    Worktree subcommand arguments

  • timeout (Integer, nil) (defaults to: nil)

    Timeout in seconds (uses config default if nil)

Returns:

  • (Hash)

    Result hash with command execution details



79
80
81
# File 'lib/ace/git/worktree/atoms/git_command.rb', line 79

def worktree(*args, timeout: nil)
  execute("worktree", *args, timeout: timeout)
end