Module: Ocak::GitUtils
- Defined in:
- lib/ocak/git_utils.rb
Class Method Summary collapse
-
.checkout_main(chdir:, logger: nil) ⇒ Object
Checks out the main branch.
-
.commit_changes(chdir:, message:, logger: nil) ⇒ Object
Stages and commits all changes in the given directory.
-
.safe_branch_name?(name) ⇒ Boolean
Validates that a branch name is safe to pass to git commands.
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.
44 45 46 47 48 49 |
# File 'lib/ocak/git_utils.rb', line 44 def self.checkout_main(chdir:, logger: nil) _, stderr, status = Open3.capture3('git', 'checkout', 'main', chdir: chdir) logger&.warn("Cleanup checkout to main failed: #{stderr}") unless status.success? rescue StandardError => e logger&.warn("Cleanup checkout to main error: #{e.}") 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.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/ocak/git_utils.rb', line 19 def self.commit_changes(chdir:, message:, logger: nil) stdout, _, status = Open3.capture3('git', 'status', '--porcelain', chdir: chdir) unless status.success? logger&.warn('git status --porcelain failed') return false end return false if stdout.strip.empty? _, stderr, add_status = Open3.capture3('git', 'add', '-A', chdir: chdir) unless add_status.success? logger&.warn("git add failed: #{stderr[0..200]}") return false end _, stderr, commit_status = Open3.capture3('git', 'commit', '-m', , chdir: chdir) unless commit_status.success? logger&.warn("git commit failed: #{stderr[0..200]}") 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 ..).
10 11 12 13 14 |
# File 'lib/ocak/git_utils.rb', line 10 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 |