Class: GemContribute::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/gem_contribute/git.rb

Overview

Thin wrapper around the ‘git` CLI so callers can substitute a fake in tests without shelling out. Uses Open3 with arg-list invocation (no shell) so there’s no injection surface.

Instance Method Summary collapse

Instance Method Details

#add_remote(path, name, url) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/gem_contribute/git.rb', line 18

def add_remote(path, name, url)
  # Idempotent: if the remote already exists (e.g. reusing a clone)
  # we silently succeed rather than fail the whole flow.
  return if remote_exists?(path, name)

  run!(["git", "-C", path, "remote", "add", name, url])
end

#branch_exists?(path, branch) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
# File 'lib/gem_contribute/git.rb', line 35

def branch_exists?(path, branch)
  _out, _err, status = Open3.capture3("git", "-C", path,
                                      "rev-parse", "--verify", "--quiet",
                                      "refs/heads/#{branch}")
  status.success?
end

#checkout_branch(path, branch) ⇒ Object



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

def checkout_branch(path, branch)
  run!(["git", "-C", path, "checkout", "-b", branch])
end

#clone(url, target) ⇒ Object



10
11
12
# File 'lib/gem_contribute/git.rb', line 10

def clone(url, target)
  run!(["git", "clone", url, target])
end

#push(path, remote, branch) ⇒ Object



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

def push(path, remote, branch)
  run!(["git", "-C", path, "push", "-u", remote, branch])
end

#remote_exists?(path, name) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
# File 'lib/gem_contribute/git.rb', line 30

def remote_exists?(path, name)
  out, _err, status = Open3.capture3("git", "-C", path, "remote")
  status.success? && out.split("\n").include?(name)
end

#run!(argv) ⇒ Object



42
43
44
45
46
47
# File 'lib/gem_contribute/git.rb', line 42

def run!(argv)
  _stdout, stderr_str, status = Open3.capture3(*argv)
  return if status.success?

  raise GemContribute::AdapterError, "git #{argv[1..].join(" ")} failed: #{stderr_str.strip}"
end