Class: RubyGit::Worktree
- Inherits:
-
Object
- Object
- RubyGit::Worktree
- 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) ⇒ 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.
Instance Attribute Details
#path ⇒ Pathname (readonly)
The root path of the working tree
20 21 22 |
# File 'lib/ruby_git/worktree.rb', line 20 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.
100 101 102 103 104 105 106 |
# File 'lib/ruby_git/worktree.rb', line 100 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) ⇒ RubyGit::Worktree
Create an empty Git repository under the root working tree path
If the repository already exists, it will not be overwritten.
37 38 39 40 41 42 43 44 45 |
# File 'lib/ruby_git/worktree.rb', line 37 def self.init(worktree_path) raise RubyGit::Error, "Path '#{worktree_path}' not valid." unless File.directory?(worktree_path) command = ['init'] = { 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
61 62 63 |
# File 'lib/ruby_git/worktree.rb', line 61 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
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/ruby_git/worktree.rb', line 170 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
197 198 199 |
# File 'lib/ruby_git/worktree.rb', line 197 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.
138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/ruby_git/worktree.rb', line 138 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 |