Class: RubyGit::Worktree

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_git/worktree.rb

Overview

The Worktree is a directory tree consisting of the checked out files that you are currently working on.

Create a new Worktree using Worktree.init, Worktree.clone, or Worktree.open.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#pathPathname (readonly)

The root path of the worktree

Examples:

worktree_path = '/Users/James/myproject'
worktree = Worktree.open(worktree_path)
worktree.path
 => '/Users/James/myproject'

Returns:

  • (Pathname)

    the root path of the worktree



22
23
24
# File 'lib/ruby_git/worktree.rb', line 22

def path
  @path
end

Class Method Details

.clone(repository_url, to_path: '') ⇒ 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 worktree 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: '')

    where to put the checked out worktree once the repository is cloned

Returns:

Raises:

  • (RubyGit::Error)

    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:



101
102
103
104
105
106
107
# File 'lib/ruby_git/worktree.rb', line 101

def self.clone(repository_url, to_path: '')
  command = [RubyGit.git.path.to_s, 'clone', '--', repository_url, to_path]
  _out, err, status = Open3.capture3(*command)
  raise RubyGit::Error, err unless status.success?

  new(to_path)
end

.init(worktree_path) ⇒ RubyGit::Worktree

Create an empty Git repository under the root worktree path

If the repository already exists, it will not be overwritten.

Examples:

worktree = Worktree.init(worktree_path)

Parameters:

  • worktree_path (String)

    the root path of a worktree

Returns:

Raises:

See Also:



39
40
41
42
43
44
45
46
47
# File 'lib/ruby_git/worktree.rb', line 39

def self.init(worktree_path)
  raise RubyGit::Error, "Path '#{worktree_path}' not valid." unless File.directory?(worktree_path)

  command = [RubyGit.git.path.to_s, 'init']
  _out, err, status = Open3.capture3(*command, chdir: worktree_path)
  raise RubyGit::Error, err unless status.success?

  Worktree.new(worktree_path)
end

.open(worktree_path) ⇒ RubyGit::Worktree

Open an existing Git worktree that contains worktree_path

Examples:

worktree = Worktree.open(worktree_path)

Parameters:

  • worktree_path (String)

    the root path of a worktree

Returns:

Raises:

  • (RubyGit::Error)

    if worktree_path does not exist, is not a directory, or is not within a Git worktree.

See Also:



62
63
64
# File 'lib/ruby_git/worktree.rb', line 62

def self.open(worktree_path)
  new(worktree_path)
end