From 24def32a2ed2f97ac659403e4da67656b1fb1005 Mon Sep 17 00:00:00 2001 From: James Couball Date: Tue, 1 Jul 2025 19:24:20 -0700 Subject: [PATCH] fix: raise a Git::FailedError if depth < 0 is passed to Git.clone Fixes #805 --- .gitignore | 3 ++- lib/git/lib.rb | 2 +- tests/units/test_git_clone.rb | 45 +++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 13dcea11..29f4b966 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,5 @@ pkg rdoc Gemfile.lock node_modules -package-lock.json \ No newline at end of file +package-lock.json +ai-prompt.erb diff --git a/lib/git/lib.rb b/lib/git/lib.rb index 692ceef9..5a3ade32 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -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] diff --git a/tests/units/test_git_clone.rb b/tests/units/test_git_clone.rb index 24221e38..a5c50ddb 100644 --- a/tests/units/test_git_clone.rb +++ b/tests/units/test_git_clone.rb @@ -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