Skip to content

Commit f0be003

Browse files
committed
Fixed depth=1 and depth=0
1 parent dded9a6 commit f0be003

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed

src/subcommand/clone_subcommand.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ void clone_subcommand::run()
3434
clone_opts.fetch_opts.depth = m_depth;
3535
clone_opts.bare = m_bare ? 1 : 0;
3636

37-
37+
if (m_depth == 0)
38+
{
39+
std::cout << "fatal: depth 0 is not a positive number" << std::endl;
40+
return;
41+
}
3842

3943
std::string short_name = m_directory;
4044
if (m_directory.empty())

src/wrapper/commit_wrapper.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "../wrapper/commit_wrapper.hpp"
2+
#include <iostream>
23

34
commit_wrapper::commit_wrapper(git_commit* commit)
45
: base_type(commit)
@@ -30,6 +31,7 @@ std::string commit_wrapper::commit_oid_tostr() const
3031
commit_list_wrapper commit_wrapper::get_parents_list() const
3132
{
3233
size_t parent_count = git_commit_parentcount(*this);
34+
std::cout << "parent_count: " << parent_count << std::endl;
3335
std::vector<commit_wrapper> parents_list;
3436
parents_list.reserve(parent_count);
3537

src/wrapper/repository_wrapper.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,9 @@ void repository_wrapper::reset(const object_wrapper& target, git_reset_t reset_t
285285

286286
size_t repository_wrapper::shallow_depth_from_head() const
287287
{
288-
size_t depth = 0;
289288
if (!this->is_shallow())
290289
{
291-
return depth;
290+
return 0u;
292291
}
293292

294293
std::string git_path = this->git_path();
@@ -307,44 +306,44 @@ size_t repository_wrapper::shallow_depth_from_head() const
307306
}
308307
}
309308

310-
if (boundaries_list.size()==0)
309+
if (boundaries_list.size() == 0u)
311310
{
312-
return depth;
311+
return 0u;
313312
}
314313

315314
commit_wrapper head_commit = this->find_commit("HEAD");
316315
commit_list_wrapper commits_list = head_commit.get_parents_list();
317-
std::vector<size_t> depth_list(commits_list.size(), 0);
318-
std::vector<size_t> final_depths(boundaries_list.size(), 0);
319-
bool has_parent = commits_list.size() > 0;
316+
std::vector<size_t> depth_list(commits_list.size(), 1u);
317+
std::vector<size_t> final_depths(boundaries_list.size(), 1u);
318+
bool has_parent = commits_list.size() > 0u;
320319
while (has_parent)
321320
{
322321
has_parent = false;
323322
std::vector<commit_wrapper> temp_commits_list;
324323
std::vector<size_t> temp_depth_list;
325324
commit_list_wrapper parent_list({});
326325

327-
for (size_t i = 0; i < commits_list.size(); i++)
326+
for (size_t i = 0u; i < commits_list.size(); i++)
328327
{
329328
const commit_wrapper& commit = commits_list[i];
330329
size_t depth = depth_list[i];
331330
const git_oid& oid = commit.oid();
332331
bool is_boundary = std::find_if(boundaries_list.cbegin(), boundaries_list.cend(), [oid](const git_oid& val) {return git_oid_equal(&oid, &val);}) != boundaries_list.cend();
333332
if (is_boundary)
334333
{
335-
final_depths.push_back(depth + 1);
334+
final_depths.push_back(depth + 1u);
336335
}
337336
else
338337
{
339338
parent_list = commit.get_parents_list();
340-
if (parent_list.size() > 0)
339+
if (parent_list.size() > 0u)
341340
{
342341
has_parent = true;
343-
for (size_t j = 0; parent_list.size(); j++)
342+
for (size_t j = 0u; parent_list.size(); j++)
344343
{
345344
const commit_wrapper& c = parent_list[j];
346345
temp_commits_list.push_back(std::move(const_cast<commit_wrapper&>(c)));
347-
temp_depth_list.push_back(depth + 1);
346+
temp_depth_list.push_back(depth + 1u);
348347
}
349348
}
350349
}
@@ -353,7 +352,7 @@ size_t repository_wrapper::shallow_depth_from_head() const
353352
commits_list = commit_list_wrapper(std::move(temp_commits_list));
354353
}
355354

356-
depth = *std::max_element(final_depths.begin(), final_depths.end());
355+
std::size_t depth = *std::max_element(final_depths.begin(), final_depths.end());
357356
return depth;
358357
}
359358

test/test_remote.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,11 @@ def test_fetch_default_origin(git2cpp_path, repo_with_remote):
319319

320320
def test_fetch_depth(git2cpp_path, tmp_path, run_in_tmp_path):
321321
url = "https://github.com/xtensor-stack/xtl.git"
322+
323+
invalid_clone_cmd = [git2cpp_path, "clone", "--depth", "0", url]
324+
p_invalid_clone = subprocess.run(invalid_clone_cmd, capture_output=True, cwd=tmp_path, text=True))
325+
assert p_invalid_clone.returncode != 0
326+
322327
clone_cmd = [git2cpp_path, "clone", "--depth", "1", url]
323328
p_clone = subprocess.run(clone_cmd, capture_output=True, cwd=tmp_path, text=True)
324329
assert p_clone.returncode == 0

0 commit comments

Comments
 (0)