Class: GemContribute::CLI::Git

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

Overview

Thin wrapper around git so the spec can swap in a fake without shelling out. The real implementation uses Open3 with arg-list invocation — no shell, so no injection surface.

Instance Method Summary collapse

Instance Method Details

#add_remote(path, name, url) ⇒ Object



172
173
174
175
176
177
178
# File 'lib/gem_contribute/cli/fork_clone_branch.rb', line 172

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

#checkout_branch(path, branch) ⇒ Object



168
169
170
# File 'lib/gem_contribute/cli/fork_clone_branch.rb', line 168

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

#clone(url, target) ⇒ Object



164
165
166
# File 'lib/gem_contribute/cli/fork_clone_branch.rb', line 164

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

#push(path, remote, branch) ⇒ Object



180
181
182
# File 'lib/gem_contribute/cli/fork_clone_branch.rb', line 180

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

#remote_exists?(path, name) ⇒ Boolean

Returns:

  • (Boolean)


184
185
186
187
# File 'lib/gem_contribute/cli/fork_clone_branch.rb', line 184

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



189
190
191
192
193
194
# File 'lib/gem_contribute/cli/fork_clone_branch.rb', line 189

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