Module: Hunkify::Git

Defined in:
lib/hunkify/git.rb

Class Method Summary collapse

Class Method Details

.apply_patch_to_index(patch_content) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/hunkify/git.rb', line 30

def self.apply_patch_to_index(patch_content)
  Tempfile.create(["hunkify_patch", ".patch"]) do |f|
    f.write(patch_content)
    f.flush
    stdout, stderr, status = Open3.capture3("git", "apply", "--cached", "--whitespace=nowarn", f.path)
    raise "Git error: #{stderr.strip}" unless status.success?
    stdout.strip
  end
end

.commit(message) ⇒ Object



58
59
60
61
62
# File 'lib/hunkify/git.rb', line 58

def self.commit(message)
  stdout, stderr, status = Open3.capture3("git", "commit", "-m", message)
  raise "Git error: #{stderr.strip}" unless status.success?
  stdout.strip
end

.has_staged_changes?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/hunkify/git.rb', line 22

def self.has_staged_changes?
  !staged_files.empty?
end

.head_exists?Boolean

Returns:

  • (Boolean)


53
54
55
56
# File 'lib/hunkify/git.rb', line 53

def self.head_exists?
  _o, _e, status = Open3.capture3("git", "rev-parse", "--verify", "HEAD")
  status.success?
end

.restage_files(files) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/hunkify/git.rb', line 64

def self.restage_files(files)
  files.each { |f|
    begin
      run("git add \"#{f}\"")
    rescue
      nil
    end
  }
end

.rootObject



26
27
28
# File 'lib/hunkify/git.rb', line 26

def self.root
  run("git rev-parse --show-toplevel")
end

.run(cmd) ⇒ Object



8
9
10
11
12
# File 'lib/hunkify/git.rb', line 8

def self.run(cmd)
  stdout, stderr, status = Open3.capture3(cmd)
  raise "Git error: #{stderr.strip}" unless status.success?
  stdout.strip
end

.staged_diffObject



14
15
16
# File 'lib/hunkify/git.rb', line 14

def self.staged_diff
  run("git diff --staged")
end

.staged_filesObject



18
19
20
# File 'lib/hunkify/git.rb', line 18

def self.staged_files
  run("git diff --staged --name-only").split("\n")
end

.unstage_allObject



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/hunkify/git.rb', line 40

def self.unstage_all
  if head_exists?
    run("git reset HEAD")
  else
    stdout, _stderr, status = Open3.capture3("git", "ls-files", "-z", "--cached")
    raise "Git error: unable to list index" unless status.success?
    stdout.split("\0").reject(&:empty?).each do |path|
      _o, err, st = Open3.capture3("git", "rm", "--cached", "-f", "--", path)
      raise "Git error: #{err.strip}" unless st.success?
    end
  end
end