Module: Commiti::GitWriter

Defined in:
lib/services/git/git_writer.rb

Class Method Summary collapse

Class Method Details

.commit_with_message_file(message) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/services/git/git_writer.rb', line 46

def self.commit_with_message_file(message)
  Tempfile.create(['commiti-commit', '.txt']) do |file|
    file.write("#{message.to_s.rstrip}\n")
    file.flush

    out, err, status = Open3.capture3('git', 'commit', '--file', file.path)
    unless status.success?
      detail = err.strip.empty? ? out.strip : err.strip
      raise "git commit failed: #{detail}"
    end

    out
  end
end

.current_branchObject



61
62
63
64
65
66
# File 'lib/services/git/git_writer.rb', line 61

def self.current_branch
  out, status = Open3.capture2('git', 'rev-parse', '--abbrev-ref', 'HEAD')
  raise 'Failed to read current branch.' unless status.success?

  out.strip
end

.origin_urlObject



68
69
70
71
72
73
# File 'lib/services/git/git_writer.rb', line 68

def self.origin_url
  out, status = Open3.capture2('git', 'remote', 'get-url', 'origin')
  raise "Failed to read git remote 'origin'." unless status.success?

  out.strip
end

.stage_all!Object



22
23
24
25
26
27
# File 'lib/services/git/git_writer.rb', line 22

def self.stage_all!
  out, err, status = Open3.capture3('git', 'add', '-A')
  raise "git add failed: #{err.strip.empty? ? out.strip : err.strip}" unless status.success?

  out
end

.stage_files!(paths) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/services/git/git_writer.rb', line 36

def self.stage_files!(paths)
  normalized = Array(paths).map(&:to_s).map(&:strip).reject(&:empty?).uniq
  return '' if normalized.empty?

  out, err, status = Open3.capture3('git', 'add', '--', *normalized)
  raise "git add failed: #{err.strip.empty? ? out.strip : err.strip}" unless status.success?

  out
end

.staged_changes?Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
# File 'lib/services/git/git_writer.rb', line 15

def self.staged_changes?
  out, status = Open3.capture2('git', 'diff', '--cached', '--name-only')
  raise 'Failed to read staged changes.' unless status.success?

  !out.strip.empty?
end

.status_shortObject



8
9
10
11
12
13
# File 'lib/services/git/git_writer.rb', line 8

def self.status_short
  out, status = Open3.capture2('git', 'status', '--short')
  raise 'Failed to read git status.' unless status.success?

  out
end

.unstage_all!Object



29
30
31
32
33
34
# File 'lib/services/git/git_writer.rb', line 29

def self.unstage_all!
  out, err, status = Open3.capture3('git', 'reset')
  raise "git reset failed: #{err.strip.empty? ? out.strip : err.strip}" unless status.success?

  out
end