8000 fix: raise a Git::FailedError if depth < 0 is passed to Git.clone by jcouball · Pull Request #806 · ruby-git/ruby-git · GitHub
[go: up one dir, main page]

Skip to content

fix: raise a Git::FailedError if depth < 0 is passed to Git.clone #806

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ pkg
rdoc
Gemfile.lock
node_modules
package-lock.json
package-lock.json
ai-prompt.erb
2 changes: 1 addition & 1 deletion lib/git/lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def clone(repository_url, directory, opts = {})
arr_opts = []
arr_opts << '--bare' if opts[:bare]
arr_opts << '--branch' << opts[:branch] if opts[:branch]
arr_opts << '--depth' << opts[:depth].to_i if opts[:depth] && opts[:depth].to_i > 0
arr_opts << '--depth' << opts[:depth].to_i if opts[:depth]
arr_opts << '--filter' << opts[:filter] if opts[:filter]
Array(opts[:config]).each { |c| arr_opts << '--config' << c }
arr_opts << '--origin' << opts[:remote] || opts[:origin] if opts[:remote] || opts[:origin]
Expand Down
45 changes: 45 additions & 0 deletions tests/units/test_git_clone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,49 @@ def test_git_clone_with_no_name

assert_equal(expected_command_line, actual_command_line)
end

test 'clone with negative depth' do
repository_url = 'https://github.com/ruby-git/ruby-git.git'
destination = 'ruby-git'

actual_command_line = nil

in_temp_dir do |path|
# Give a bare repository with a single commit
repository_path = File.join(path, 'repository.git')
Git.init(repository_path, :bare => true)
worktree_path = File.join(path, 'repository')
worktree = Git.clone(repository_path, worktree_path)
File.write(File.join(worktree_path, 'test.txt'), 'test')
worktree.add('test.txt')
worktree.commit('Initial commit')
worktree.push
FileUtils.rm_rf(worktree_path)

# When I clone it with a negative depth with
error = assert_raises(Git::FailedError) do
Git.clone(repository_path, worktree, depth: -1)
end

assert_match(/depth/, error.result.stderr)
end

# git = Git.init('.')

# # Mock the Git::Lib#command method to capture the actual command line args
# git.lib.define_singleton_method(:command) do |cmd, *opts, &block|
# actual_command_line = [cmd, *opts.flatten]
# end

# git.lib.clone(repository_url, destination, depth: -1)
# end

# expected_command_line = [
# 'clone',
# '--depth', '-1',
# '--', repository_url, destination, {timeout: nil}
# ]

# assert_equal(expected_command_line, actual_command_line)
end
end
0