Skip to content
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 @@ -64,6 +64,8 @@ set(GIT2CPP_SRC
${GIT2CPP_SOURCE_DIR}/subcommand/remote_subcommand.hpp
${GIT2CPP_SOURCE_DIR}/subcommand/reset_subcommand.cpp
${GIT2CPP_SOURCE_DIR}/subcommand/reset_subcommand.hpp
${GIT2CPP_SOURCE_DIR}/subcommand/revparse_subcommand.cpp
${GIT2CPP_SOURCE_DIR}/subcommand/revparse_subcommand.hpp
${GIT2CPP_SOURCE_DIR}/subcommand/status_subcommand.cpp
${GIT2CPP_SOURCE_DIR}/subcommand/status_subcommand.hpp
${GIT2CPP_SOURCE_DIR}/utils/ansi_code.cpp
Expand Down
2 changes: 2 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "subcommand/remote_subcommand.hpp"
#include "subcommand/reset_subcommand.hpp"
#include "subcommand/status_subcommand.hpp"
#include "subcommand/revparse_subcommand.hpp"

int main(int argc, char** argv)
{
Expand All @@ -44,6 +45,7 @@ int main(int argc, char** argv)
merge_subcommand merge(lg2_obj, app);
push_subcommand push(lg2_obj, app);
remote_subcommand remote(lg2_obj, app);
revparse_subcommand rev(lg2_obj, app);

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

Expand Down
28 changes: 28 additions & 0 deletions src/subcommand/revparse_subcommand.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "revparse_subcommand.hpp"
#include "../wrapper/repository_wrapper.hpp"
#include <ios>
#include <stdexcept>

revparse_subcommand::revparse_subcommand(const libgit2_object&, CLI::App& app)
{
auto* sub = app.add_subcommand("rev-parse", "Pick out and message parameters");

sub->add_flag("--is-bare-repository", m_is_bare_repository_flag);

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

void revparse_subcommand::run()
{
auto directory = get_current_git_path();
auto repo = repository_wrapper::open(directory);

if (m_is_bare_repository_flag)
{
std::cout << std::boolalpha << repo.is_bare() << std::endl;
}
else
{
std::cout << "revparse only supports --is-bare-repository for now" << std::endl;
}
}
18 changes: 18 additions & 0 deletions src/subcommand/revparse_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 revparse_subcommand
{
public:

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

private:

bool m_is_bare_repository_flag = false;
};

5 changes: 5 additions & 0 deletions src/wrapper/repository_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ void repository_wrapper::state_cleanup()
throw_if_error(git_repository_state_cleanup(*this));
}

bool repository_wrapper::is_bare() const
{
return git_repository_is_bare(*this);
}

// References

reference_wrapper repository_wrapper::head() const
Expand Down
2 changes: 2 additions & 0 deletions src/wrapper/repository_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class repository_wrapper : public wrapper_base<git_repository>
git_repository_state_t state() const;
void state_cleanup();

bool is_bare() const;

// References
reference_wrapper head() const;
reference_wrapper find_reference(std::string_view ref_name) const;
Expand Down
16 changes: 16 additions & 0 deletions test/test_revparse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import subprocess

import pytest

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

cmd = [git2cpp_path, 'init', '--bare']
p = subprocess.run(cmd, cwd = tmp_path)

cmd2 = [git2cpp_path, 'rev-parse', '--is-bare-repository']
p2 = subprocess.run(cmd2, capture_output=True, text=True, cwd = tmp_path)

assert p2.returncode == 0
assert p2.stdout == 'true\n'