Class: GemContribute::Operations::Clone

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

Overview

Bootstrap step 2: clone the fork into ‘<root>/<owner>/<repo>` (reusing an existing clone if one is there), and ensure an `upstream` remote points at the canonical project. Returns a `Result` carrying the local path and a `reused` flag, or a tagged `Failure`.

The “reuse if ‘.git` exists” rule and the upstream-remote convention are gem-contribute policy on top of git, not git itself — that’s why they live here rather than in ‘Git`.

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Constructor Details

#initialize(git: Git.new) ⇒ Clone

Returns a new instance of Clone.



21
22
23
# File 'lib/gem_contribute/operations/clone.rb', line 21

def initialize(git: Git.new)
  @git = git
end

Instance Method Details

#call(adapter:, project:, fork_clone_url:, root:) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/gem_contribute/operations/clone.rb', line 25

def call(adapter:, project:, fork_clone_url:, root:)
  target = File.join(root, project.owner, project.repo)
  reused = File.directory?(File.join(target, ".git"))

  unless reused
    FileUtils.mkdir_p(File.dirname(target))
    @git.clone(fork_clone_url, target)
  end

  @git.add_remote(target, "upstream", adapter.clone_url(project.owner, project.repo))
  Success(Result.new(path: target, reused: reused))
rescue GemContribute::AdapterError => e
  Failure([:adapter_error, e.message])
end