Skip to content

Commit 6510681

Browse files
authored
Implemented rev-parse -is-bare-repository (#63)
1 parent 324e7c2 commit 6510681

File tree

7 files changed

+73
-0
lines changed

7 files changed

+73
-0
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ set(GIT2CPP_SRC
6464
${GIT2CPP_SOURCE_DIR}/subcommand/remote_subcommand.hpp
6565
${GIT2CPP_SOURCE_DIR}/subcommand/reset_subcommand.cpp
6666
${GIT2CPP_SOURCE_DIR}/subcommand/reset_subcommand.hpp
67+
${GIT2CPP_SOURCE_DIR}/subcommand/revparse_subcommand.cpp
68+
${GIT2CPP_SOURCE_DIR}/subcommand/revparse_subcommand.hpp
6769
${GIT2CPP_SOURCE_DIR}/subcommand/status_subcommand.cpp
6870
${GIT2CPP_SOURCE_DIR}/subcommand/status_subcommand.hpp
6971
${GIT2CPP_SOURCE_DIR}/utils/ansi_code.cpp

src/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "subcommand/remote_subcommand.hpp"
1919
#include "subcommand/reset_subcommand.hpp"
2020
#include "subcommand/status_subcommand.hpp"
21+
#include "subcommand/revparse_subcommand.hpp"
2122

2223
int main(int argc, char** argv)
2324
{
@@ -44,6 +45,7 @@ int main(int argc, char** argv)
4445
merge_subcommand merge(lg2_obj, app);
4546
push_subcommand push(lg2_obj, app);
4647
remote_subcommand remote(lg2_obj, app);
48+
revparse_subcommand rev(lg2_obj, app);
4749

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "revparse_subcommand.hpp"
2+
#include "../wrapper/repository_wrapper.hpp"
3+
#include <ios>
4+
#include <stdexcept>
5+
6+
revparse_subcommand::revparse_subcommand(const libgit2_object&, CLI::App& app)
7+
{
8+
auto* sub = app.add_subcommand("rev-parse", "Pick out and message parameters");
9+
10+
sub->add_flag("--is-bare-repository", m_is_bare_repository_flag);
11+
12+
sub->callback([this]() { this->run(); });
13+
}
14+
15+
void revparse_subcommand::run()
16+
{
17+
auto directory = get_current_git_path();
18+
auto repo = repository_wrapper::open(directory);
19+
20+
if (m_is_bare_repository_flag)
21+
{
22+
std::cout << std::boolalpha << repo.is_bare() << std::endl;
23+
}
24+
else
25+
{
26+
std::cout << "revparse only supports --is-bare-repository for now" << std::endl;
27+
}
28+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
3+
#include <CLI/CLI.hpp>
4+
5+
#include "../utils/common.hpp"
6+
7+
class revparse_subcommand
8+
{
9+
public:
10+
11+
explicit revparse_subcommand(const libgit2_object&, CLI::App& app);
12+
void run();
13+
14+
private:
15+
16+
bool m_is_bare_repository_flag = false;
17+
};
18+

src/wrapper/repository_wrapper.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ void repository_wrapper::state_cleanup()
4646
throw_if_error(git_repository_state_cleanup(*this));
4747
}
4848

49+
bool repository_wrapper::is_bare() const
50+
{
51+
return git_repository_is_bare(*this);
52+
}
53+
4954
// References
5055

5156
reference_wrapper repository_wrapper::head() const

src/wrapper/repository_wrapper.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class repository_wrapper : public wrapper_base<git_repository>
3333
git_repository_state_t state() const;
3434
void state_cleanup();
3535

36+
bool is_bare() const;
37+
3638
// References
3739
reference_wrapper head() const;
3840
reference_wrapper find_reference(std::string_view ref_name) const;

test/test_revparse.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import subprocess
2+
3+
import pytest
4+
5+
def test_revparse(git2cpp_path, tmp_path, run_in_tmp_path):
6+
# tmp_path exists and is empty.
7+
assert list(tmp_path.iterdir()) == []
8+
9+
cmd = [git2cpp_path, 'init', '--bare']
10+
p = subprocess.run(cmd, cwd = tmp_path)
11+
12+
cmd2 = [git2cpp_path, 'rev-parse', '--is-bare-repository']
13+
p2 = subprocess.run(cmd2, capture_output=True, text=True, cwd = tmp_path)
14+
15+
assert p2.returncode == 0
16+
assert p2.stdout == 'true\n'

0 commit comments

Comments
 (0)