Module: Ace::Git::Atoms::LockErrorDetector

Defined in:
lib/ace/git/atoms/lock_error_detector.rb

Overview

Pure functions for detecting git index lock errors from command output

Git operations can fail with “Unable to create .git/index.lock” errors when:

  • Previous git operations were interrupted (Ctrl+C, crashes, timeouts)

  • Multiple concurrent operations contend for the same lock

  • Agents are blocked mid-operation leaving orphan lock files

This detector identifies these errors so retry logic can handle them.

Constant Summary collapse

LOCK_ERROR_PATTERNS =

Git error patterns that indicate index lock issues These patterns appear in stderr when git cannot acquire the lock Covers various git versions and platforms

[
  /Unable to create.*index\.lock.*File exists/i,
  /fatal:\s*cannot create.*index\.lock/i,
  /Another git process seems to be running/i,
  /git.*index\.lock.*exists/i,
  /could not open.*index\.lock/i,
  /unable to create.*index\.lock/i,
  /lock file.*index\.lock.*already exists/i
].freeze
LOCK_EXIT_CODE =

Exit code 128 often indicates lock issues across different git versions

128

Class Method Summary collapse

Class Method Details

.lock_error?(stderr) ⇒ Boolean

Detect if a git error is related to index lock issues

Examples:

Detect lock error

lock_error?("fatal: Unable to create '.git/index.lock': File exists.")
# => true

Non-lock error

lock_error?("error: pathspec 'unknown' did not match")
# => false

Parameters:

  • stderr (String)

    The error output from a git command

Returns:

  • (Boolean)

    true if the error indicates a lock issue



45
46
47
48
49
# File 'lib/ace/git/atoms/lock_error_detector.rb', line 45

def lock_error?(stderr)
  return false if stderr.nil? || stderr.empty?

  LOCK_ERROR_PATTERNS.any? { |pattern| pattern.match?(stderr) }
end

.lock_error_result?(result) ⇒ Boolean

Check if git command result indicates a lock error

Examples:

Check result hash

result = { success: false, error: "fatal: Unable to create...", exit_code: 128 }
lock_error_result?(result)
# => true

Parameters:

  • result (Hash)

    Result hash from CommandExecutor.execute

Returns:

  • (Boolean)

    true if the result indicates a lock error



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ace/git/atoms/lock_error_detector.rb', line 60

def lock_error_result?(result)
  return false if result.nil? || result[:success]
  return false if result[:error].nil? || result[:error].empty?

  # Primary check: known lock error patterns
  return true if lock_error?(result[:error])

  # Fallback: exit code 128 with "lock" keyword in error
  # Handles edge cases from different git versions/locales
  if result[:exit_code] == LOCK_EXIT_CODE && result[:error] =~ /lock/i
    return true
  end

  false
end