Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactoring v2
  • Loading branch information
SandrineP committed Jun 17, 2025
commit 7eaba8da06c00a98668f05c28bd24d9f542005c5
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "src/utils/git_exception.hpp"
#include "version.hpp"
#include "subcommand/init_subcommand.hpp"
// #include "subcommand/status_subcommand.hpp"
#include "subcommand/status_subcommand.hpp"

int main(int argc, char** argv)
{
Expand All @@ -19,7 +19,7 @@ int main(int argc, char** argv)
auto version = app.add_flag("-v,--version", "Show version");

// Sub commands
InitSubcommand init(lg2_obj, app);
init_subcommand init(lg2_obj, app);

app.parse(argc, argv);

Expand Down
11 changes: 6 additions & 5 deletions src/subcommand/init_subcommand.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include <filesystem>
// #include <filesystem>
#include "init_subcommand.hpp"
#include "src/wrapper/repository_wrapper.hpp"

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

Expand All @@ -10,12 +11,12 @@ InitSubcommand::InitSubcommand(const libgit2_object&, CLI::App& app)
// 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());
->default_val(get_current_git_path());

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

void InitSubcommand::run()
void init_subcommand::run()
{
RepositoryWrapper::init(directory, bare);
repository_wrapper::init(directory, bare);
}
9 changes: 5 additions & 4 deletions src/subcommand/init_subcommand.hpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#pragma once

#include <CLI/CLI.hpp>
#include <string>

#include "src/wrapper/repository_wrapper.hpp"
#include <CLI/CLI.hpp>

#include "../utils/common.hpp"

class InitSubcommand
class init_subcommand
{
public:

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

private:
Expand Down
12 changes: 12 additions & 0 deletions src/utils/common.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <filesystem>

#include <git2.h>

#include "common.hpp"
Expand All @@ -11,3 +13,13 @@ libgit2_object::~libgit2_object()
{
git_libgit2_shutdown();
}

std::string get_current_git_path()
{
return std::filesystem::current_path(); // TODO: make sure that it goes to the root
}

// // 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());
41 changes: 40 additions & 1 deletion src/utils/common.hpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,59 @@
#pragma once

#include <string>
#include <utility>

class noncopiable_nonmovable
{
public:
noncopiable_nonmovable(const noncopiable_nonmovable&) = delete;
Comment thread
ianthomas23 marked this conversation as resolved.
Outdated
noncopiable_nonmovable& operator=(const noncopiable_nonmovable&) = delete;
noncopiable_nonmovable(noncopiable_nonmovable&) = delete;
noncopiable_nonmovable(noncopiable_nonmovable&&) = delete;
noncopiable_nonmovable& operator=(noncopiable_nonmovable&&) = delete;

protected:
noncopiable_nonmovable() = default;
~noncopiable_nonmovable() = default;
};

template <class T>
class wrapper_base
{
public:
using ressource_type = T;
Comment thread
ianthomas23 marked this conversation as resolved.
Outdated

wrapper_base(const wrapper_base&) = delete;
wrapper_base& operator=(const wrapper_base&) = delete;

wrapper_base(wrapper_base&& rhs)
: p_ressource(rhs.p_ressource)
{
rhs.p_ressource = nullptr;
}
wrapper_base& operator=(wrapper_base&& rhs)
{
std::swap(p_ressource, rhs.p_ressource);
return this;
}

operator ressource_type*() const noexcept
{
return p_ressource;
}

protected:
// Allocation and deletion of p_ressource must be handled by inheriting class.
wrapper_base() = default;
~wrapper_base() = default;
ressource_type* p_ressource = nullptr;
};

class libgit2_object : private noncopiable_nonmovable
{
public:

libgit2_object();
~libgit2_object();
};

std::string get_current_git_path();
29 changes: 14 additions & 15 deletions src/wrapper/repository_wrapper.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
#include "src/utils/git_exception.hpp"
#include "../utils/git_exception.hpp"
#include "repository_wrapper.hpp"


RepositoryWrapper::RepositoryWrapper()
: p_repo(nullptr)
{}

RepositoryWrapper::~RepositoryWrapper()
repository_wrapper::~repository_wrapper()
{
git_repository_free(p_repo);
p_repo=nullptr;
git_repository_free(p_ressource);
p_ressource=nullptr;
}

// RepositoryWrapper::RepositoryWrapper open(const std::string path)
// {
//
// };
repository_wrapper repository_wrapper::open(const std::string& directory)
{
repository_wrapper rw;
throwIfError(git_repository_open(&(rw.p_ressource), directory.c_str()));
return rw;
}

void RepositoryWrapper::init(const std::string& directory, bool bare)
repository_wrapper repository_wrapper::init(const std::string& directory, bool bare)
{
RepositoryWrapper rw;
throwIfError(git_repository_init(&(rw.p_repo), directory.c_str(), bare));
repository_wrapper rw;
throwIfError(git_repository_init(&(rw.p_ressource), directory.c_str(), bare));
return rw;
}
17 changes: 10 additions & 7 deletions src/wrapper/repository_wrapper.hpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
#pragma once

#include <git2.h>
#include <string>

#include <git2.h>

#include "../utils/common.hpp"

class RepositoryWrapper : private noncopiable_nonmovable
class repository_wrapper : public wrapper_base<git_repository>
{
public:

~RepositoryWrapper();
~repository_wrapper();

repository_wrapper(repository_wrapper&&) = default;
repository_wrapper& operator=(repository_wrapper&&) = default;

// static RepositoryWrapper open(const std::string path);
static void init(const std::string& directory, bool bare);
static repository_wrapper init(const std::string& directory, bool bare);
static repository_wrapper open(const std::string& directory);

private:

RepositoryWrapper();
git_repository* p_repo;
repository_wrapper() = default;
};
Loading