Skip to content

Minimal clone command #25

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 24, 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
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ set(GIT2CPP_SRC
${GIT2CPP_SOURCE_DIR}/subcommand/branch_subcommand.hpp
${GIT2CPP_SOURCE_DIR}/subcommand/checkout_subcommand.cpp
${GIT2CPP_SOURCE_DIR}/subcommand/checkout_subcommand.hpp
${GIT2CPP_SOURCE_DIR}/subcommand/clone_subcommand.cpp
${GIT2CPP_SOURCE_DIR}/subcommand/clone_subcommand.hpp
${GIT2CPP_SOURCE_DIR}/subcommand/init_subcommand.cpp
${GIT2CPP_SOURCE_DIR}/subcommand/init_subcommand.hpp
${GIT2CPP_SOURCE_DIR}/subcommand/status_subcommand.cpp
Expand Down
8 changes: 6 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "subcommand/add_subcommand.hpp"
#include "subcommand/branch_subcommand.hpp"
#include "subcommand/checkout_subcommand.hpp"
#include "subcommand/clone_subcommand.hpp"
#include "subcommand/init_subcommand.hpp"
#include "subcommand/status_subcommand.hpp"

Expand All @@ -25,8 +26,11 @@ int main(int argc, char** argv)
init_subcommand init(lg2_obj, app);
status_subcommand status(lg2_obj, app);
add_subcommand add(lg2_obj, app);
branch_subcommand(lg2_obj, app);
checkout_subcommand(lg2_obj, app);
branch_subcommand branch(lg2_obj, app);
checkout_subcommand checkout(lg2_obj, app);
clone_subcommand clone(lg2_obj, app);

app.require_subcommand(/* min */ 0, /* max */ 1);

CLI11_PARSE(app, argc, argv);

Expand Down
31 changes: 31 additions & 0 deletions src/subcommand/clone_subcommand.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <iostream>

#include "../subcommand/clone_subcommand.hpp"
#include "../wrapper/repository_wrapper.hpp"

clone_subcommand::clone_subcommand(const libgit2_object&, CLI::App& app)
{
auto* sub = app.add_subcommand("clone", "Clone a directory into a new repository");

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->callback([this]() { this->run(); });
}

void clone_subcommand::run()
{
git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
std::string short_name = m_directory;
if (m_directory.empty())
{
auto size = m_repository.size();
auto begin = m_repository.find_last_of('/') + 1;
auto end = m_repository.ends_with(".git") ? size - 4 : size;
auto count = end - begin;
short_name = m_repository.substr(begin, count);
m_directory = get_current_git_path() + '/' + short_name;
}
std::cout << "Cloning into '" + short_name + "'..." << std::endl;
repository_wrapper::clone(m_repository, m_directory, opts);
}
18 changes: 18 additions & 0 deletions src/subcommand/clone_subcommand.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <CLI/CLI.hpp>

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

class clone_subcommand
{
public:

explicit clone_subcommand(const libgit2_object&, CLI::App& app);
void run();

private:

std::string m_repository = {};
std::string m_directory = {};
};
7 changes: 7 additions & 0 deletions src/wrapper/repository_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ repository_wrapper repository_wrapper::init(std::string_view directory, bool bar
return rw;
}

repository_wrapper repository_wrapper::clone(std::string_view url, std::string_view path, const git_clone_options& opts)
{
repository_wrapper rw;
throw_if_error(git_clone(&(rw.p_resource), url.data(), path.data(), &opts));
return rw;
}

git_repository_state_t repository_wrapper::state() const
{
return git_repository_state_t(git_repository_state(*this));
Expand Down
1 change: 1 addition & 0 deletions src/wrapper/repository_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class repository_wrapper : public wrapper_base<git_repository>

static repository_wrapper init(std::string_view directory, bool bare);
static repository_wrapper open(std::string_view directory);
static repository_wrapper clone(std::string_view url, std::string_view path, const git_clone_options& opts);

git_repository_state_t state() const;

Expand Down
18 changes: 18 additions & 0 deletions test/test_clone.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os
import subprocess

import pytest

def test_clone(git2cpp_path):
url = 'https://github.com/xtensor-stack/xtl.git'
working_dir = 'test/data'

clone_cmd = [git2cpp_path, 'clone', url]
subprocess.run(clone_cmd, capture_output=True, cwd = working_dir, text=True)

assert os.path.exists(working_dir + '/xtl')
assert os.path.exists(working_dir + '/xtl/include')

cleanup_cmd = ['rm', '-rf', 'xtl']
subprocess.run(cleanup_cmd, capture_output=True, cwd = working_dir, text=True)