Class: RubyGit::Worktree
- Inherits:
-
Object
- Object
- RubyGit::Worktree
- Extended by:
- OptionValidators
- Includes:
- OptionValidators
- Defined in:
- lib/ruby_git/worktree.rb
Overview
The working tree 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
-
#path ⇒ Pathname
readonly
The root path of the working tree.
Class Method Summary collapse
-
.clone(repository_url, to_path: nil) ⇒ RubyGit::Worktree
Copy the remote repository and checkout the default branch.
-
.init(worktree_path, initial_branch: nil) ⇒ RubyGit::Worktree
Create an empty Git repository under the root working tree
path
. -
.open(worktree_path) ⇒ RubyGit::Worktree
Open an existing Git working tree that contains worktree_path.
Instance Method Summary collapse
-
#add(*pathspecs, all: false, force: false, refresh: false, update: false) ⇒ RubyGit::CommandLineResult
Add changed files to the index to stage for the next commit.
-
#repository ⇒ RubyGit::Repository
Return the repository associated with the worktree.
-
#status(*path_specs, untracked_files: :all, ignored: :no, ignore_submodules: :all) ⇒ RubyGit::Status::Report
Show the working tree and index status.
Methods included from OptionValidators
validate_boolean_option, validate_string_option
Instance Attribute Details
#path ⇒ Pathname (readonly)
The root path of the working tree
23 24 25 |
# File 'lib/ruby_git/worktree.rb', line 23 def path @path end |
Class Method Details
.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.
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 = { out: StringIO.new, err: StringIO.new } clone_output = RubyGit::CommandLine.run(*command, **).stderr new(cloned_to(clone_output)) end |
.init(worktree_path, initial_branch: nil) ⇒ RubyGit::Worktree
Create an empty Git repository under the root working tree path
If the repository already exists, it will not be overwritten.
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/ruby_git/worktree.rb', line 42 def self.init(worktree_path, initial_branch: nil) validate_string_option(name: :initial_branch, value: initial_branch, nullable: true) command = ['init'] command << '--initial-branch' << initial_branch unless initial_branch.nil? = { chdir: worktree_path, out: StringIO.new, err: StringIO.new } RubyGit::CommandLine.run(*command, **) new(worktree_path) end |
.open(worktree_path) ⇒ RubyGit::Worktree
Open an existing Git working tree that contains worktree_path
69 70 71 |
# File 'lib/ruby_git/worktree.rb', line 69 def self.open(worktree_path) new(worktree_path) end |
Instance Method Details
#add(*pathspecs, all: false, force: false, refresh: false, update: false) ⇒ RubyGit::CommandLineResult
Add changed files to the index to stage for the next commit
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/ruby_git/worktree.rb', line 178 def add(*pathspecs, all: false, force: false, refresh: false, update: false) # rubocop:disable Metrics/MethodLength validate_boolean_option(name: :all, value: all) validate_boolean_option(name: :force, value: force) validate_boolean_option(name: :refresh, value: refresh) validate_boolean_option(name: :update, value: update) command = %w[add] command << '--all' if all command << '--force' if force command << '--update' if update command << '--refresh' if refresh command << '--' unless pathspecs.empty? command.concat(pathspecs) = { out: StringIO.new, err: StringIO.new } run_with_context(*command, **) end |
#repository ⇒ RubyGit::Repository
Return the repository associated with the worktree
205 206 207 |
# File 'lib/ruby_git/worktree.rb', line 205 def repository @repository ||= Repository.new(repository_path) end |
#status(*path_specs, untracked_files: :all, ignored: :no, ignore_submodules: :all) ⇒ RubyGit::Status::Report
Show the working tree and index status
handled
See git-staus --untracked-files.
handled, :no to not include ignored files
See git-staus --ignored.
146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/ruby_git/worktree.rb', line 146 def status(*path_specs, untracked_files: :all, ignored: :no, ignore_submodules: :all) command = %w[status --porcelain=v2 --branch --show-stash --ahead-behind --renames -z] command << "--untracked-files=#{untracked_files}" command << "--ignored=#{ignored}" command << "--ignore-submodules=#{ignore_submodules}" command << '--' unless path_specs.empty? command.concat(path_specs) = { out: StringIO.new, err: StringIO.new } status_output = run_with_context(*command, **).stdout RubyGit::Status.parse(status_output) end |