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.
-
.cloned_to(clone_output) ⇒ String
private
Get path of the cloned worktree from
git clonestderr output. -
.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 |
.cloned_to(clone_output) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get path of the cloned worktree from git clone stderr output
115 116 117 |
# File 'lib/ruby_git/worktree.rb', line 115 def self.cloned_to(clone_output) clone_output.match(/Cloning into ['"](.+)['"]\.\.\./)[1] 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
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/ruby_git/worktree.rb', line 181 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(*command, **) end |
#repository ⇒ RubyGit::Repository
Return the repository associated with the worktree
208 209 210 211 212 213 214 215 216 |
# File 'lib/ruby_git/worktree.rb', line 208 def repository @repository ||= begin command = %w[rev-parse --git-dir] = { chdir: path, chomp: true, out: StringIO.new, err: StringIO.new } # rev-parse path might be relative to the worktree, thus the need to expand it git_dir = File.realpath(RubyGit::CommandLine.run(*command, **).stdout, path) Repository.new(git_dir) end 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.
149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/ruby_git/worktree.rb', line 149 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(*command, **).stdout RubyGit::Status.parse(status_output) end |