Skip to content

Commit f42361e

Browse files
committed
Add basic git init functionality
1 parent 0bded48 commit f42361e

File tree

6 files changed

+93
-23
lines changed

6 files changed

+93
-23
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/test_git.py

Lines changed: 10 additions & 6 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(arg):
7+
cmd = ['build/git2cpp', 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():
13+
14+
def test_error_on_unknown_option():
1115
cmd = ['build/git2cpp', '--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: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import os
2+
from pathlib import Path
3+
import pytest
4+
import subprocess
5+
6+
7+
# Fixture to run test in current tmp_path
8+
@pytest.fixture
9+
def run_in_tmp_path(tmp_path):
10+
original_cwd = os.getcwd()
11+
os.chdir(tmp_path)
12+
yield
13+
os.chdir(original_cwd)
14+
15+
16+
def test_init_in_directory(tmp_path):
17+
# tmp_path exists and is empty.
18+
assert list(tmp_path.iterdir()) == []
19+
20+
cmd = ['/Users/iant/github/git2cpp/build/git2cpp', 'init', '--bare', str(tmp_path)]
21+
p = subprocess.run(cmd, capture_output=True)
22+
assert p.returncode == 0
23+
assert p.stdout == b''
24+
assert p.stderr == b''
25+
26+
assert sorted(map(lambda path: path.name, tmp_path.iterdir())) == [
27+
'HEAD', 'config', 'description', 'hooks', 'info', 'objects', 'refs'
28+
]
29+
30+
# TODO: check this is a valid git repo
31+
32+
33+
def test_init_in_cwd(tmp_path, run_in_tmp_path):
34+
# tmp_path exists and is empty.
35+
assert list(tmp_path.iterdir()) == []
36+
assert Path.cwd() == tmp_path
37+
38+
cmd = ['/Users/iant/github/git2cpp/build/git2cpp', 'init', '--bare']
39+
p = subprocess.run(cmd, capture_output=True)
40+
assert p.returncode == 0
41+
assert p.stdout == b''
42+
assert p.stderr == b''
43+
44+
assert sorted(map(lambda path: path.name, tmp_path.iterdir())) == [
45+
'HEAD', 'config', 'description', 'hooks', 'info', 'objects', 'refs'
46+
]
47+
48+
# TODO: check this is a valid git repo
49+
50+
51+
# TODO: Test without bare flag.
52+
53+
54+
def test_error_on_unknown_option():
55+
cmd = ['build/git2cpp', 'init', '--unknown']
56+
p = subprocess.run(cmd, capture_output=True)
57+
assert p.returncode == 1
58+
assert p.stdout == b''
59+
assert p.stderr.startswith(b"The following argument was not expected: --unknown")
60+
61+
62+
def test_error_on_repeated_directory():
63+
cmd = ['build/git2cpp', 'init', 'abc', 'def']
64+
p = subprocess.run(cmd, capture_output=True)
65+
assert p.returncode == 1
66+
assert p.stdout == b''
67+
assert p.stderr.startswith(b"The following argument was not expected: def")

0 commit comments

Comments
 (0)