Skip to content

Commit e735be7

Browse files
authored
Merge pull request #7 from QuantStack/repo-init
Add basic `git init` functionality
2 parents 0bded48 + f0ea04f commit e735be7

File tree

7 files changed

+100
-24
lines changed

7 files changed

+100
-24
lines changed

src/subcommand/init_subcommand.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
1+
#include <filesystem>
12
#include "init_subcommand.hpp"
2-
3-
//#include "../wrapper/repository_wrapper.hpp"
3+
#include "../wrapper/repository_wrapper.hpp"
44

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

9-
sub->add_flag("--bare", bare, "--- bare ---");
9+
sub->add_flag("--bare", bare, "info about bare arg");
10+
11+
// If directory not specified, uses cwd.
12+
sub->add_option("directory", directory, "info about directory arg")
13+
->check(CLI::ExistingDirectory | CLI::NonexistentPath)
14+
->default_val(std::filesystem::current_path());
1015

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

1419
void InitSubcommand::run()
1520
{
16-
std::cout << "RUN " << bare << std::endl;
17-
//RepositoryWrapper repo;
18-
//repo.init(bare);
21+
RepositoryWrapper repo;
22+
repo.init(directory, bare);
1923
}

src/subcommand/init_subcommand.hpp

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

3+
#include <string>
34
#include "base_subcommand.hpp"
45

56
class InitSubcommand : public BaseSubcommand
@@ -10,4 +11,5 @@ class InitSubcommand : public BaseSubcommand
1011

1112
private:
1213
bool bare;
14+
std::string directory;
1315
};

src/wrapper/repository_wrapper.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,8 @@ RepositoryWrapper::~RepositoryWrapper()
1515
}
1616
}
1717

18-
void RepositoryWrapper::init(bool bare)
18+
void RepositoryWrapper::init(const std::string& directory, bool bare)
1919
{
20-
std::cout << "repo init - start" << std::endl;
21-
22-
// what if it is already initialised???
23-
24-
// convert error code to exception
25-
std::string path = "repo";
26-
throwIfError(git_repository_init(&_repo, path.c_str(), bare));
27-
28-
std::cout << "repo init - end " << std::endl;
20+
// what if it is already initialised? Throw exception or delete and recreate?
21+
throwIfError(git_repository_init(&_repo, directory.c_str(), bare));
2922
}

src/wrapper/repository_wrapper.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class RepositoryWrapper : public BaseWrapper
99

1010
virtual ~RepositoryWrapper();
1111

12-
void init(bool bare);
12+
void init(const std::string& directory, bool bare);
1313

1414
private:
1515
git_repository *_repo;

test/conftest.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import os
2+
from pathlib import Path
3+
import pytest
4+
5+
6+
# Fixture to run test in current tmp_path
7+
@pytest.fixture
8+
def run_in_tmp_path(tmp_path):
9+
original_cwd = os.getcwd()
10+
os.chdir(tmp_path)
11+
yield
12+
os.chdir(original_cwd)
13+
14+
15+
@pytest.fixture(scope='session')
16+
def git2cpp_path():
17+
return Path(__file__).parent.parent / 'build' / 'git2cpp'

test/test_git.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1+
import pytest
12
import subprocess
23

3-
def test_version():
4-
cmd = ['build/git2cpp', '-v']
4+
5+
@pytest.mark.parametrize("arg", ['-v', '--version'])
6+
def test_version(git2cpp_path, arg):
7+
cmd = [git2cpp_path, arg]
58
p = subprocess.run(cmd, capture_output=True)
69
assert p.returncode == 0
7-
assert len(p.stderr) == 0
10+
assert p.stderr == b''
811
assert p.stdout.startswith(b'git2cpp ')
912

10-
def test_unknown_option():
11-
cmd = ['build/git2cpp', '--unknown']
13+
14+
def test_error_on_unknown_option(git2cpp_path):
15+
cmd = [git2cpp_path, '--unknown']
1216
p = subprocess.run(cmd, capture_output=True)
13-
#assert p.returncode == 1
14-
assert len(p.stdout) == 0
17+
assert p.returncode == 1
18+
assert p.stdout == b''
1519
assert p.stderr.startswith(b"The following argument was not expected: --unknown")

test/test_init.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from pathlib import Path
2+
import subprocess
3+
4+
5+
def test_init_in_directory(git2cpp_path, tmp_path):
6+
# tmp_path exists and is empty.
7+
assert list(tmp_path.iterdir()) == []
8+
9+
cmd = [git2cpp_path, 'init', '--bare', str(tmp_path)]
10+
p = subprocess.run(cmd, capture_output=True)
11+
assert p.returncode == 0
12+
assert p.stdout == b''
13+
assert p.stderr == b''
14+
15+
assert sorted(map(lambda path: path.name, tmp_path.iterdir())) == [
16+
'HEAD', 'config', 'description', 'hooks', 'info', 'objects', 'refs'
17+
]
18+
19+
# TODO: check this is a valid git repo
20+
21+
22+
def test_init_in_cwd(git2cpp_path, tmp_path, run_in_tmp_path):
23+
# tmp_path exists and is empty.
24+
assert list(tmp_path.iterdir()) == []
25+
assert Path.cwd() == tmp_path
26+
27+
cmd = [git2cpp_path, 'init', '--bare']
28+
p = subprocess.run(cmd, capture_output=True)
29+
assert p.returncode == 0
30+
assert p.stdout == b''
31+
assert p.stderr == b''
32+
33+
assert sorted(map(lambda path: path.name, tmp_path.iterdir())) == [
34+
'HEAD', 'config', 'description', 'hooks', 'info', 'objects', 'refs'
35+
]
36+
37+
# TODO: check this is a valid git repo
38+
39+
40+
# TODO: Test without bare flag.
41+
42+
43+
def test_error_on_unknown_option(git2cpp_path):
44+
cmd = [git2cpp_path, 'init', '--unknown']
45+
p = subprocess.run(cmd, capture_output=True)
46+
assert p.returncode == 1
47+
assert p.stdout == b''
48+
assert p.stderr.startswith(b"The following argument was not expected: --unknown")
49+
50+
51+
def test_error_on_repeated_directory(git2cpp_path):
52+
cmd = [git2cpp_path, 'init', 'abc', 'def']
53+
p = subprocess.run(cmd, capture_output=True)
54+
assert p.returncode == 1
55+
assert p.stdout == b''
56+
assert p.stderr.startswith(b"The following argument was not expected: def")

0 commit comments

Comments
 (0)