Module: Ocak::GitUtils

Extended by:
CommandRunner
Defined in:
lib/ocak/git_utils.rb

Class Method Summary collapse

Class Method Details

.checkout_main(chdir:, logger: nil) ⇒ Object

Checks out the main branch. Intended for cleanup/ensure blocks. Rescues all errors so it never crashes the caller.



47
48
49
50
51
52
53
54
55
56
# File 'lib/ocak/git_utils.rb', line 47

def self.checkout_main(chdir:, logger: nil)
  result = run_git('checkout', 'main', chdir: chdir)
  unless result.success?
    # Use "error" prefix when command not found (status is nil), otherwise "failed"
    prefix = result.status.nil? ? 'Cleanup checkout to main error:' : 'Cleanup checkout to main failed:'
    logger&.warn("#{prefix} #{result.error}")
  end
rescue StandardError => e
  logger&.warn("Cleanup checkout to main error: #{e.message}")
end

.commit_changes(chdir:, message:, logger: nil) ⇒ Object

Stages and commits all changes in the given directory. Returns true if changes were committed, false if no changes or on failure. Logs warnings via logger on failure rather than raising.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ocak/git_utils.rb', line 22

def self.commit_changes(chdir:, message:, logger: nil)
  result = run_git('status', '--porcelain', chdir: chdir)
  unless result.success?
    logger&.warn('git status --porcelain failed')
    return false
  end
  return false if result.output.empty?

  add_result = run_git('add', '-A', chdir: chdir)
  unless add_result.success?
    logger&.warn("git add failed: #{add_result.error}")
    return false
  end

  commit_result = run_git('commit', '-m', message, chdir: chdir)
  unless commit_result.success?
    logger&.warn("git commit failed: #{commit_result.error}")
    return false
  end

  true
end

.safe_branch_name?(name) ⇒ Boolean

Validates that a branch name is safe to pass to git commands. Rejects names that could be interpreted as flags (starting with -) or cause unexpected git behavior (containing ..).

Returns:

  • (Boolean)


13
14
15
16
17
# File 'lib/ocak/git_utils.rb', line 13

def self.safe_branch_name?(name)
  return false if name.nil? || name.empty?

  name.match?(%r{\A[a-zA-Z0-9_./-]+\z}) && !name.start_with?('-') && !name.include?('..')
end