Skip to content
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
6 changes: 6 additions & 0 deletions src/subcommand/clone_subcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ clone_subcommand::clone_subcommand(const libgit2_object&, CLI::App& app)

sub->add_option("<repository>", m_repository, "The (possibly remote) repository to clone from.")->required();
sub->add_option("<directory>", m_directory, "The name of a new directory to clone into.");
sub->add_option("--depth", m_depth, "Create a shallow clone of that depth.");
// sub->add_option("--shallow-since", m_shallow_since, "<time>\ndeepen history of shallow repository based on time.");
// sub->add_option("--shallow-exclude", m_shallow_exclude, "<ref>\ndeepen history of shallow clone, excluding ref");
sub->add_flag("--bare", m_bare, "Create a bare Git repository.");

sub->callback([this]() { this->run(); });
Expand All @@ -28,8 +31,11 @@ void clone_subcommand::run()
clone_opts.fetch_opts.callbacks.sideband_progress = sideband_progress;
clone_opts.fetch_opts.callbacks.transfer_progress = fetch_progress;
clone_opts.fetch_opts.callbacks.payload = &pd;
clone_opts.fetch_opts.depth = m_depth;
clone_opts.bare = m_bare ? 1 : 0;



std::string short_name = m_directory;
if (m_directory.empty())
{
Expand Down
3 changes: 3 additions & 0 deletions src/subcommand/clone_subcommand.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ class clone_subcommand
std::string m_repository = {};
std::string m_directory = {};
bool m_bare = false;
size_t m_depth = 0;
// std::string m_shallow_since;
// std::vector<std::string> m_shallow_exclude;
};
23 changes: 23 additions & 0 deletions src/subcommand/fetch_subcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ fetch_subcommand::fetch_subcommand(const libgit2_object&, CLI::App& app)

sub->add_option("<remote>", m_remote_name, "The remote to fetch from")
->default_val("origin");
sub->add_option("--depth", m_depth, "deepen or shorten history of shallow clone");
// sub->add_option("--deepen", m_deepen, "deepen history of shallow clone");
// sub->add_option("--shallow-since", m_shallow_since, "<time>\ndeepen history of shallow repository based on time.");
// sub->add_option("--shallow-exclude", m_shallow_exclude, "<ref>\ndeepen history of shallow clone, excluding ref");
sub->add_flag("--unshallow", m_unshallow, "convert to a complete repository");
// sub->add_flag("--update-shallow", m_update_shallow, "accept refs that update .git/shallow");

sub->callback([this]() { this->run(); });
}
Expand All @@ -33,6 +39,23 @@ void fetch_subcommand::run()
fetch_opts.callbacks.payload = &pd;
fetch_opts.callbacks.update_refs = update_refs;

if (repo.is_shallow())
{
if (m_unshallow)
{
fetch_opts.depth = GIT_FETCH_DEPTH_UNSHALLOW;
}
else
{
fetch_opts.depth = m_depth;
}
}
else
{
fetch_opts.depth = 0;
}


cursor_hider ch;

// Perform the fetch
Expand Down
9 changes: 7 additions & 2 deletions src/subcommand/fetch_subcommand.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <cstddef>
#include <string>

#include <CLI/CLI.hpp>
Expand All @@ -13,7 +14,11 @@ class fetch_subcommand
explicit fetch_subcommand(const libgit2_object&, CLI::App& app);
void run();

private:

std::string m_remote_name;
size_t m_depth = 0;
// size_t m_deepen = 0;
// std::string m_shallow_since;
// std::string m_shallow_exclude;
bool m_unshallow = false;
// bool m_update_shallow = false;
};
7 changes: 6 additions & 1 deletion src/subcommand/revparse_subcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ revparse_subcommand::revparse_subcommand(const libgit2_object&, CLI::App& app)
auto* sub = app.add_subcommand("rev-parse", "Pick out and message parameters");

sub->add_flag("--is-bare-repository", m_is_bare_repository_flag);
sub->add_flag("--is-shallow-repository", m_is_shallow_repository_flag);

sub->callback([this]() { this->run(); });
}
Expand All @@ -21,8 +22,12 @@ void revparse_subcommand::run()
{
std::cout << std::boolalpha << repo.is_bare() << std::endl;
}
else if (m_is_shallow_repository_flag)
{
std::cout << std::boolalpha << repo.is_shallow() << std::endl;
}
else
{
std::cout << "revparse only supports --is-bare-repository for now" << std::endl;
std::cout << "revparse only supports --is-bare-repository and --is-shallow-repository for now" << std::endl;
}
}
2 changes: 1 addition & 1 deletion src/subcommand/revparse_subcommand.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ class revparse_subcommand
private:

bool m_is_bare_repository_flag = false;
bool m_is_shallow_repository_flag = false;
};

9 changes: 7 additions & 2 deletions src/wrapper/repository_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ bool repository_wrapper::is_bare() const
return git_repository_is_bare(*this);
}

bool repository_wrapper::is_shallow() const
{
return git_repository_is_shallow(*this);
}

// Head

bool repository_wrapper::is_head_unborn() const
Expand Down Expand Up @@ -326,13 +331,13 @@ std::vector<std::string> repository_wrapper::list_remotes() const
{
git_strarray remotes = {0};
throw_if_error(git_remote_list(&remotes, *this));

std::vector<std::string> result;
for (size_t i = 0; i < remotes.count; ++i)
{
result.emplace_back(remotes.strings[i]);
}

git_strarray_dispose(&remotes);
return result;
}
1 change: 1 addition & 0 deletions src/wrapper/repository_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class repository_wrapper : public wrapper_base<git_repository>
void state_cleanup();

bool is_bare() const;
bool is_shallow() const;

// Head
bool is_head_unborn() const;
Expand Down
20 changes: 16 additions & 4 deletions test/test_clone.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

import pytest

url = "https://github.com/xtensor-stack/xtl.git"

def test_clone(git2cpp_path, tmp_path, run_in_tmp_path):
url = "https://github.com/xtensor-stack/xtl.git"

def test_clone(git2cpp_path, tmp_path, run_in_tmp_path):
clone_cmd = [git2cpp_path, "clone", url]
p_clone = subprocess.run(clone_cmd, capture_output=True, cwd=tmp_path, text=True)
assert p_clone.returncode == 0
Expand All @@ -16,12 +16,24 @@ def test_clone(git2cpp_path, tmp_path, run_in_tmp_path):


def test_clone_is_bare(git2cpp_path, tmp_path, run_in_tmp_path):
url = "https://github.com/xtensor-stack/xtl.git"

clone_cmd = [git2cpp_path, "clone", "--bare", url]
p_clone = subprocess.run(clone_cmd, capture_output=True, cwd=tmp_path, text=True)
assert p_clone.returncode == 0

status_cmd = [git2cpp_path, "status"]
p_status = subprocess.run(status_cmd, capture_output=True, cwd=tmp_path, text=True)
assert p_status.returncode != 0


def test_clone_shallow(git2cpp_path, tmp_path, run_in_tmp_path):
clone_cmd = [git2cpp_path, "clone", "--depth", "1", url]
p_clone = subprocess.run(clone_cmd, capture_output=True, cwd=tmp_path, text=True)
assert p_clone.returncode == 0
assert (tmp_path / "xtl").exists()

xtl_path = tmp_path / "xtl"

cmd_log = [git2cpp_path, "log"]
p_log = subprocess.run(cmd_log, capture_output=True, cwd=xtl_path, text=True)
assert p_log.returncode == 0
assert p_log.stdout.count("Author") == 1
48 changes: 48 additions & 0 deletions test/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,54 @@ def test_fetch_default_origin(git2cpp_path, repo_with_remote):
assert p.returncode in [0, 1]


def test_fetch_depth(git2cpp_path, tmp_path, run_in_tmp_path):
url = "https://github.com/xtensor-stack/xtl.git"
clone_cmd = [git2cpp_path, "clone", "--depth", "1", url]
p_clone = subprocess.run(clone_cmd, capture_output=True, cwd=tmp_path, text=True)
assert p_clone.returncode == 0
assert (tmp_path / "xtl").exists()

xtl_path = tmp_path / "xtl"

cmd_log = [git2cpp_path, "log"]
p_log = subprocess.run(cmd_log, capture_output=True, cwd=xtl_path, text=True)
assert p_log.returncode == 0
assert p_log.stdout.count("Author") == 1

depth_cmd = [git2cpp_path, "fetch", "--depth", "3"]
p_depth = subprocess.run(depth_cmd, capture_output=True, cwd=xtl_path, text=True)
assert p_depth.returncode == 0

p_log_2 = subprocess.run(cmd_log, capture_output=True, cwd=xtl_path, text=True)
assert p_log_2.returncode == 0
assert p_log_2.stdout.count("Author") > 1


def test_unshallow(git2cpp_path, tmp_path, run_in_tmp_path):
url = "https://github.com/xtensor-stack/xtl.git"
clone_cmd = [git2cpp_path, "clone", "--depth", "1", url]
p_clone = subprocess.run(clone_cmd, capture_output=True, cwd=tmp_path, text=True)
assert p_clone.returncode == 0
assert (tmp_path / "xtl").exists()

xtl_path = tmp_path / "xtl"

cmd_log = [git2cpp_path, "log"]
p_log = subprocess.run(cmd_log, capture_output=True, cwd=xtl_path, text=True)
assert p_log.returncode == 0
assert p_log.stdout.count("Author") == 1

unshallow_cmd = [git2cpp_path, "fetch", "--unshallow"]
p_unshallow = subprocess.run(
unshallow_cmd, capture_output=True, cwd=xtl_path, text=True
)
assert p_unshallow.returncode == 0

p_log_2 = subprocess.run(cmd_log, capture_output=True, cwd=xtl_path, text=True)
assert p_log_2.returncode == 0
assert p_log_2.stdout.count("Author") > 1


def test_remote_in_cloned_repo(xtl_clone, git2cpp_path, tmp_path):
"""Test that cloned repos have remotes configured."""
assert (tmp_path / "xtl").exists()
Expand Down
27 changes: 21 additions & 6 deletions test/test_revparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,30 @@

import pytest

def test_revparse(git2cpp_path, tmp_path, run_in_tmp_path):

def test_revparse_bare(git2cpp_path, tmp_path, run_in_tmp_path):
# tmp_path exists and is empty.
assert list(tmp_path.iterdir()) == []

cmd = [git2cpp_path, 'init', '--bare']
p = subprocess.run(cmd, cwd = tmp_path)
cmd = [git2cpp_path, "init", "--bare"]
p = subprocess.run(cmd, cwd=tmp_path)
assert p.returncode == 0

cmd2 = [git2cpp_path, "rev-parse", "--is-bare-repository"]
p2 = subprocess.run(cmd2, capture_output=True, text=True, cwd=tmp_path)
assert p2.returncode == 0
assert p2.stdout == "true\n"


cmd2 = [git2cpp_path, 'rev-parse', '--is-bare-repository']
p2 = subprocess.run(cmd2, capture_output=True, text=True, cwd = tmp_path)
def test_revparse_shallow(git2cpp_path, tmp_path, run_in_tmp_path):
url = "https://github.com/xtensor-stack/xtl.git"
cmd = [git2cpp_path, "clone", "--depth", "1", url]
p = subprocess.run(cmd, capture_output=True, text=True, cwd=tmp_path)
assert p.returncode == 0
assert (tmp_path / "xtl").exists()

xtl_path = tmp_path / "xtl"
cmd2 = [git2cpp_path, "rev-parse", "--is-shallow-repository"]
p2 = subprocess.run(cmd2, capture_output=True, text=True, cwd=xtl_path)
assert p2.returncode == 0
assert p2.stdout == 'true\n'
assert p2.stdout == "true\n"