8000 fix: raise a Git::FailedError if depth < 0 is passed to Git.clone · ruby-git/ruby-git@803253e · GitHub
[go: up one dir, main page]

Skip to content

Commit 803253e

Browse files
committed
fix: raise a Git::FailedError if depth < 0 is passed to Git.clone
Fixes #805
1 parent e04f08e commit 803253e

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ pkg
99
rdoc
1010
Gemfile.lock
1111
node_modules
12-
package-lock.json
12+
package-lock.json
13+
ai-prompt.erb

lib/git/lib.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def clone(repository_url, directory, opts = {})
126126
arr_opts = []
127127
arr_opts << '--bare' if opts[:bare]
128128
arr_opts << '--branch' << opts[:branch] if opts[:branch]
129-
arr_opts << '--depth' << opts[:depth].to_i if opts[:depth] && opts[:depth].to_i > 0
129+
arr_opts << '--depth' << opts[:depth].to_i if opts[:depth]
130130
arr_opts << '--filter' << opts[:filter] if opts[:filter]
131131
Array(opts[:config]).each { |c| arr_opts << '--config' << c }
132132
arr_opts << '--origin' << opts[:remote] || opts[:origin] if opts[:remote] || opts[:origin]

tests/units/test_git_clone.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,49 @@ def test_git_clone_with_no_name
159159

160160
assert_equal(expected_command_line, actual_command_line)
161161
end
162+
163+
test 'clone with negative depth' do
164+
repository_url = 'https://github.com/ruby-git/ruby-git.git'
165+
destination = 'ruby-git'
166+
167+
actual_command_line = nil
168+
169+
in_temp_dir do |path|
170+
# Give a bare repository with a single commit
171+
repository_path = File.join(path, 'repository.git')
172+
Git.init(repository_path, :bare => true)
173+
worktree_path = File.join(path, 'repository')
174+
worktree = Git.clone(repository_path, worktree_path)
175+
File.write(File.join(worktree_path, 'test.txt'), 'test')
176+
worktree.add('test.txt')
177+
worktree.commit('Initial commit')
178+
worktree.push
179+
FileUtils.rm_rf(worktree_path)
180+
181+
# When I clone it with a negative depth with
182+
error = assert_raises(Git::FailedError) do
183+
Git.clone(repository_path, worktree, depth: -1)
184+
end
185+
186+
assert_match(/depth/, error.result.stderr)
187+
end
188+
189+
# git = Git.init('.')
190+
191+
# # Mock the Git::Lib#command method to capture the actual command line args
192+
# git.lib.define_singleton_method(:command) do |cmd, *opts, &block|
193+
# actual_command_line = [cmd, *opts.flatten]
194+
# end
195+
196+
# git.lib.clone(repository_url, destination, depth: -1)
197+
# end
198+
199+
# expected_command_line = [
200+
# 'clone',
201+
# '--depth', '-1',
202+
# '--', repository_url, destination, {timeout: nil}
203+
# ]
204+
205+
# assert_equal(expected_command_line, actual_command_line)
206+
end
162207
end

0 commit comments

Comments
 (0)
0