Skip to content

Commit d6f2f69

Browse files
committed
Fixed depth=0
1 parent 4dcabb7 commit d6f2f69

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

src/subcommand/clone_subcommand.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ clone_subcommand::clone_subcommand(const libgit2_object&, CLI::App& app)
2121

2222
void clone_subcommand::run()
2323
{
24+
// m_depth = 0 means no shallow clone in libgit2, while
25+
// it is forbidden with git. Therefore we use another
26+
// sentinel value to detect full clone.
27+
if (m_depth == 0)
28+
{
29+
std::cout << "fatal: depth 0 is not a positive number" << std::endl;
30+
return;
31+
}
32+
33+
if (m_depth == std::numeric_limits<size_t>::max())
34+
{
35+
m_depth = 0;
36+
}
37+
2438
git_indexer_progress pd;
2539
git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT;
2640
git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;
@@ -34,8 +48,6 @@ void clone_subcommand::run()
3448
clone_opts.fetch_opts.depth = m_depth;
3549
clone_opts.bare = m_bare ? 1 : 0;
3650

37-
38-
3951
std::string short_name = m_directory;
4052
if (m_directory.empty())
4153
{

src/subcommand/clone_subcommand.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <CLI/CLI.hpp>
4+
#include <limits>
45

56
#include "../utils/common.hpp"
67

@@ -16,7 +17,7 @@ class clone_subcommand
1617
std::string m_repository = {};
1718
std::string m_directory = {};
1819
bool m_bare = false;
19-
size_t m_depth = 0;
20+
size_t m_depth = std::numeric_limits<size_t>::max();
2021
// std::string m_shallow_since;
2122
// std::vector<std::string> m_shallow_exclude;
2223
};

test/test_remote.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ def test_fetch_from_remote(git2cpp_path, repo_with_remote):
300300
local_path, remote_path = repo_with_remote
301301

302302
# Note: This is a bare repo with no refs, so fetch will fail gracefully
303-
# For now, just test that fetch command runs (it will fail gracefully if no refs)
303+
# For now, just test that /stdofetch command runs (it will fail gracefully if no refs)
304304
cmd = [git2cpp_path, "fetch", "origin"]
305305
p = subprocess.run(cmd, capture_output=True, text=True, cwd=local_path)
306306
# Fetch might succeed (empty) or fail (no refs), but shouldn't crash
@@ -322,7 +322,8 @@ def test_fetch_depth(git2cpp_path, tmp_path, run_in_tmp_path):
322322

323323
invalid_clone_cmd = [git2cpp_path, "clone", "--depth", "0", url]
324324
p_invalid_clone = subprocess.run(invalid_clone_cmd, capture_output=True, cwd=tmp_path, text=True)
325-
assert p_invalid_clone.returncode != 0
325+
assert p_invalid_clone.returncode == 0
326+
assert p_invalid_clone.stdout.startswith(b'fatal: depth 0 is not a positive number')
326327

327328
clone_cmd = [git2cpp_path, "clone", "--depth", "1", url]
328329
p_clone = subprocess.run(clone_cmd, capture_output=True, cwd=tmp_path, text=True)

0 commit comments

Comments
 (0)