Class: GithubIssuesCli::Command::Clone

Inherits:
GithubIssuesCli::Command show all
Defined in:
lib/github_issues_cli/command/clone.rb

Instance Attribute Summary

Attributes inherited from GithubIssuesCli::Command

#git_repo, #username

Instance Method Summary collapse

Methods inherited from GithubIssuesCli::Command

#authenticate, #get_git_push_target, #get_git_repo, #get_issue_number, #get_pullrequest, #get_source_branch, #get_upstream_repo, #initialize, #run

Constructor Details

This class inherits a constructor from GithubIssuesCli::Command

Instance Method Details

#executeObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/github_issues_cli/command/clone.rb', line 7

def execute
  owner, name = repository.split('/')
  upstream_repo = Github::Repos.new.get(:user => owner, :repo => name)
  if upstream_repo.owner. == @username
    origin_repo = upstream_repo
  else
    forks = Github::Repos::Forks.new.list(:user => owner, :repo => name)
    fork = forks.find do |fork|
      fork.owner. == @username
    end

    unless fork
      puts "Forking #{repository} for #{@username}"
      fork = Github::Repos::Forks.new.create(:user => owner, :repo => name)
    end
    origin_repo = fork
  end

  target_directory = target || origin_repo.name
  target_path = Pathname.new(target_directory).expand_path(Dir.getwd)
  puts "Cloning #{repository} into #{target_path.to_s}"
  git_repo = Git.clone(origin_repo.ssh_url, target_path.basename.to_s, :path => target_path.dirname.to_s)
  git_repo.add_remote 'upstream', upstream_repo.ssh_url
end