Method: RubyGit::Worktree.clone

Defined in:
lib/ruby_git/worktree.rb

.clone(repository_url, to_path: nil) ⇒ RubyGit::Worktree

Copy the remote repository and checkout the default branch

Clones the repository referred to by repository_url into a newly created directory, creates remote-tracking branches for each branch in the cloned repository, and checks out the default branch in the Git working tree whose root directory is to_path.

to_path will be created if it does not exist. An error is raised if to_path exists and not an empty directory.

Examples:

Using default for Worktree path

FileUtils.pwd
 => "/Users/jsmith"
worktree = Worktree.clone('https://github.com/main-branch/ruby_git.git')
worktree.path
 => "/Users/jsmith/ruby_git"

Using a specified worktree_path

FileUtils.pwd
 => "/Users/jsmith"
worktree_path = '/tmp/project'
worktree = Worktree.clone('https://github.com/main-branch/ruby_git.git', to_path: worktree_path)
worktree.path
 => "/tmp/project"

Parameters:

  • repository_url (String)

    a reference to a Git repository

  • to_path (String) (defaults to: nil)

    where to put the checked out Git working tree once the repository is cloned

Returns:

  • (RubyGit::Worktree)

    the Git working tree checked out from the cloned repository

Raises:

  • (RubyGit::FailedError)

    if (1) repository_url is not valid or does not point to a valid repository OR (2) to_path is not an empty directory.

See Also:



108
109
110
111
112
113
114
# File 'lib/ruby_git/worktree.rb', line 108

def self.clone(repository_url, to_path: nil)
  command = ['clone', '--', repository_url]
  command << to_path if to_path
  options = { out: StringIO.new, err: StringIO.new }
  clone_output = RubyGit::CommandLine.run(*command, **options).stderr
  new(cloned_to(clone_output))
end