Skip to content

Add basic git init functionality #7

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 2 commits into from
May 29, 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
16 changes: 10 additions & 6 deletions src/subcommand/init_subcommand.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
#include <filesystem>
#include "init_subcommand.hpp"

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

InitSubcommand::InitSubcommand(CLI::App& app)
{
auto *sub = app.add_subcommand("init", "Explanation of init here");

sub->add_flag("--bare", bare, "--- bare ---");
sub->add_flag("--bare", bare, "info about bare arg");

// If directory not specified, uses cwd.
sub->add_option("directory", directory, "info about directory arg")
->check(CLI::ExistingDirectory | CLI::NonexistentPath)
->default_val(std::filesystem::current_path());

sub->callback([this]() { this->run(); });
}

void InitSubcommand::run()
{
std::cout << "RUN " << bare << std::endl;
//RepositoryWrapper repo;
//repo.init(bare);
RepositoryWrapper repo;
repo.init(directory, bare);
}
2 changes: 2 additions & 0 deletions src/subcommand/init_subcommand.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <string>
#include "base_subcommand.hpp"

class InitSubcommand : public BaseSubcommand
Expand All @@ -10,4 +11,5 @@ class InitSubcommand : public BaseSubcommand

private:
bool bare;
std::string directory;
};
13 changes: 3 additions & 10 deletions src/wrapper/repository_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,8 @@ RepositoryWrapper::~RepositoryWrapper()
}
}

void RepositoryWrapper::init(bool bare)
void RepositoryWrapper::init(const std::string& directory, bool bare)
{
std::cout << "repo init - start" << std::endl;

// what if it is already initialised???

// convert error code to exception
std::string path = "repo";
throwIfError(git_repository_init(&_repo, path.c_str(), bare));

std::cout << "repo init - end " << std::endl;
// what if it is already initialised? Throw exception or delete and recreate?
throwIfError(git_repository_init(&_repo, directory.c_str(), bare));
}
2 changes: 1 addition & 1 deletion src/wrapper/repository_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class RepositoryWrapper : public BaseWrapper

virtual ~RepositoryWrapper();

void init(bool bare);
void init(const std::string& directory, bool bare);

private:
git_repository *_repo;
Expand Down
17 changes: 17 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import os
from pathlib import Path
import pytest


# Fixture to run test in current tmp_path
@pytest.fixture
def run_in_tmp_path(tmp_path):
original_cwd = os.getcwd()
os.chdir(tmp_path)
yield
os.chdir(original_cwd)


@pytest.fixture(scope='session')
def git2cpp_path():
return Path(__file__).parent.parent / 'build' / 'git2cpp'
18 changes: 11 additions & 7 deletions test/test_git.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import pytest
import subprocess

def test_version():
cmd = ['build/git2cpp', '-v']

@pytest.mark.parametrize("arg", ['-v', '--version'])
def test_version(git2cpp_path, arg):
cmd = [git2cpp_path, arg]
p = subprocess.run(cmd, capture_output=True)
assert p.returncode == 0
assert len(p.stderr) == 0
assert p.stderr == b''
assert p.stdout.startswith(b'git2cpp ')

def test_unknown_option():
cmd = ['build/git2cpp', '--unknown']

def test_error_on_unknown_option(git2cpp_path):
cmd = [git2cpp_path, '--unknown']
p = subprocess.run(cmd, capture_output=True)
#assert p.returncode == 1
assert len(p.stdout) == 0
assert p.returncode == 1
assert p.stdout == b''
assert p.stderr.startswith(b"The following argument was not expected: --unknown")
56 changes: 56 additions & 0 deletions test/test_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from pathlib import Path
import subprocess


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

cmd = [git2cpp_path, 'init', '--bare', str(tmp_path)]
p = subprocess.run(cmd, capture_output=True)
assert p.returncode == 0
assert p.stdout == b''
assert p.stderr == b''

assert sorted(map(lambda path: path.name, tmp_path.iterdir())) == [
'HEAD', 'config', 'description', 'hooks', 'info', 'objects', 'refs'
]

# TODO: check this is a valid git repo


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

cmd = [git2cpp_path, 'init', '--bare']
p = subprocess.run(cmd, capture_output=True)
assert p.returncode == 0
assert p.stdout == b''
assert p.stderr == b''

assert sorted(map(lambda path: path.name, tmp_path.iterdir())) == [
'HEAD', 'config', 'description', 'hooks', 'info', 'objects', 'refs'
]

# TODO: check this is a valid git repo


# TODO: Test without bare flag.


def test_error_on_unknown_option(git2cpp_path):
cmd = [git2cpp_path, 'init', '--unknown']
p = subprocess.run(cmd, capture_output=True)
assert p.returncode == 1
assert p.stdout == b''
assert p.stderr.startswith(b"The following argument was not expected: --unknown")


def test_error_on_repeated_directory(git2cpp_path):
cmd = [git2cpp_path, 'init', 'abc', 'def']
p = subprocess.run(cmd, capture_output=True)
assert p.returncode == 1
assert p.stdout == b''
assert p.stderr.startswith(b"The following argument was not expected: def")