Skip to content

Commit 72020b9

Browse files
committed
feat: add support to passing path specs to Worktree#status
1 parent 324a472 commit 72020b9

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

lib/ruby_git/worktree.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ def self.cloned_to(clone_output)
121121
# @example worktree = Worktree.open(worktree_path) worktree.status #=>
122122
# #<RubyGit::Status::Report ...>
123123
#
124+
# @param path_specs [Array<String>] paths to limit the status to
125+
# (default is all paths)
126+
#
127+
# See [git-glossary
128+
# pathspec](https://git-scm.com/docs/gitglossary#Documentation/gitglossary.txt-aiddefpathspecapathspec).
129+
#
124130
# @param untracked_files [:all, :normal, :no] Defines how untracked files will be
125131
# handled
126132
#
@@ -140,11 +146,15 @@ def self.cloned_to(clone_output)
140146
#
141147
# @return [RubyGit::Status::Report] the status of the working tree
142148
#
143-
def status(untracked_files: :all, ignored: :no, ignore_submodules: :all)
149+
def status(*path_specs, untracked_files: :all, ignored: :no, ignore_submodules: :all) # rubocop:disable Metrics/MethodLength
144150
command = %w[status --porcelain=v2 --branch --show-stash --ahead-behind --renames -z]
145151
command << "--untracked-files=#{untracked_files}"
146152
command << "--ignored=#{ignored}"
147153
command << "--ignore-submodules=#{ignore_submodules}"
154+
unless path_specs.empty?
155+
command << '--'
156+
command.concat(path_specs)
157+
end
148158
options = { out: StringIO.new, err: StringIO.new }
149159
status_output = run(*command, **options).stdout
150160
RubyGit::Status.parse(status_output)

spec/lib/ruby_git/worktree_status_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,30 @@
171171
worktree.status(**given_options)
172172
end
173173
end
174+
175+
context 'when a path spec is given' do
176+
it 'should build the correct command' do
177+
expect(worktree).to(
178+
receive(:run) do |*args, **_options|
179+
expect(args).to end_with('--', 'lib')
180+
end.and_return(result)
181+
)
182+
worktree.status('lib')
183+
end
184+
end
185+
186+
context 'when multiple path specs are given' do
187+
let(:given_options) { { path_spec: 'file_1' } }
188+
189+
it 'should build the correct command' do
190+
expect(worktree).to(
191+
receive(:run) do |*args, **_options|
192+
expect(args).to end_with('--', 'lib', 'spec')
193+
end.and_return(result)
194+
)
195+
worktree.status('lib', 'spec')
196+
end
197+
end
174198
end
175199
end
176200
end

0 commit comments

Comments
 (0)