Class: Git::Worktree

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

Overview

A worktree in a Git repository

Represents a single linked or main worktree. Constructed by Repository::WorktreeOperations#worktree or populated by Worktrees.

Examples:

Add and remove a linked worktree

worktree = repo.worktree('/path/to/new-worktree')
worktree.add
worktree.remove

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, dir, gcommit = nil)

Creates a new Worktree object

Parameters:

  • base (Git::Repository)

    the repository that owns this worktree

  • dir (String)

    filesystem path of the worktree

  • gcommit (String, nil) (defaults to: nil)

    commitish associated with the worktree; when non-nil it is appended to #full



43
44
45
46
47
48
49
# File 'lib/git/worktree.rb', line 43

def initialize(base, dir, gcommit = nil)
  @full = dir
  @full += " #{gcommit}" unless gcommit.nil?
  @base = base
  @dir = dir
  @gcommit = gcommit
end

Instance Attribute Details

#dirString

Filesystem path of this worktree

Returns:

  • (String)

    the filesystem path of the worktree directory



29
30
31
# File 'lib/git/worktree.rb', line 29

def dir
  @dir
end

#fullString

Full worktree descriptor including the optional commitish

Returns:

  • (String)

    the filesystem path, space-separated with the commitish when one was given at construction time



23
24
25
# File 'lib/git/worktree.rb', line 23

def full
  @full
end

Instance Method Details

#addString

Creates this worktree on disk

Runs git worktree add for #dir, optionally at the commitish passed at construction time.

Examples:

Add a worktree

worktree = repo.worktree('/path/to/new-worktree')
worktree.add

Returns:

  • (String)

    stdout from the git command

Raises:



90
91
92
# File 'lib/git/worktree.rb', line 90

def add
  worktree_repository.worktree_add(@dir, @gcommit)
end

#gcommitGit::Object::Commit, String

Returns the commit (or commitish string) associated with this worktree

When a commitish string was supplied at construction time (e.g. by Git::Worktrees which passes the raw SHA from git worktree list), that string is returned as-is. Otherwise the value is lazily resolved on first call via worktree_repository.gcommit(@full) and the result is memoized.

Examples:

When resolved lazily (no commitish at construction)

worktree = repo.worktree('/path/to/wt')
worktree.gcommit  # => #<Git::Object::Commit ...>

When the commitish was given at construction

worktree = repo.worktrees['/path/to/wt']
worktree.gcommit  # => "4bef5ab8c9..."   (raw SHA string)

Returns:

  • (Git::Object::Commit, String)

    a commit object when lazily resolved, or the raw commitish string when pre-set at construction

Raises:

  • (Git::FailedError)

    if git must resolve the commit and exits with a non-zero exit status



72
73
74
75
# File 'lib/git/worktree.rb', line 72

def gcommit
  @gcommit ||= worktree_repository.gcommit(@full)
  @gcommit
end

#removeString

Removes this worktree from disk

Runs git worktree remove for #dir.

Examples:

Remove a worktree

worktree.remove

Returns:

  • (String)

    stdout from the git command (typically empty)

Raises:



105
106
107
# File 'lib/git/worktree.rb', line 105

def remove
  worktree_repository.worktree_remove(@dir)
end

#to_aArray<String>

Returns an array containing the full worktree descriptor

Examples:

Get the descriptor array

worktree.to_a  # => ["/path/to/worktree"]

Returns:

  • (Array<String>)

    array containing the full worktree descriptor



116
117
118
# File 'lib/git/worktree.rb', line 116

def to_a
  [@full]
end

#to_sString

Returns the full worktree descriptor as a string

Examples:

Get the descriptor string

worktree.to_s  # => "/path/to/worktree"

Returns:

  • (String)

    the full worktree descriptor (path and optional commitish)



127
128
129
# File 'lib/git/worktree.rb', line 127

def to_s
  @full
end