diff --git a/CMakeLists.txt b/CMakeLists.txt index 3a5a8e5..49f3295 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,6 +41,8 @@ find_package(libgit2) set(GIT2CPP_SRC ${GIT2CPP_SOURCE_DIR}/subcommand/add_subcommand.cpp ${GIT2CPP_SOURCE_DIR}/subcommand/add_subcommand.hpp + ${GIT2CPP_SOURCE_DIR}/subcommand/branch_subcommand.cpp + ${GIT2CPP_SOURCE_DIR}/subcommand/branch_subcommand.hpp ${GIT2CPP_SOURCE_DIR}/subcommand/init_subcommand.cpp ${GIT2CPP_SOURCE_DIR}/subcommand/init_subcommand.hpp ${GIT2CPP_SOURCE_DIR}/subcommand/status_subcommand.cpp @@ -49,6 +51,8 @@ set(GIT2CPP_SRC ${GIT2CPP_SOURCE_DIR}/utils/common.hpp ${GIT2CPP_SOURCE_DIR}/utils/git_exception.cpp ${GIT2CPP_SOURCE_DIR}/utils/git_exception.hpp + ${GIT2CPP_SOURCE_DIR}/wrapper/branch_wrapper.cpp + ${GIT2CPP_SOURCE_DIR}/wrapper/branch_wrapper.hpp ${GIT2CPP_SOURCE_DIR}/wrapper/commit_wrapper.cpp ${GIT2CPP_SOURCE_DIR}/wrapper/commit_wrapper.hpp ${GIT2CPP_SOURCE_DIR}/wrapper/index_wrapper.cpp diff --git a/src/main.cpp b/src/main.cpp index 64d2b72..a04444a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,6 +5,7 @@ #include "utils/git_exception.hpp" #include "version.hpp" #include "subcommand/add_subcommand.hpp" +#include "subcommand/branch_subcommand.hpp" #include "subcommand/init_subcommand.hpp" #include "subcommand/status_subcommand.hpp" @@ -23,6 +24,7 @@ int main(int argc, char** argv) init_subcommand init(lg2_obj, app); status_subcommand status(lg2_obj, app); add_subcommand add(lg2_obj, app); + branch_subcommand(lg2_obj, app); app.parse(argc, argv); diff --git a/src/subcommand/branch_subcommand.cpp b/src/subcommand/branch_subcommand.cpp new file mode 100644 index 0000000..c6e297c --- /dev/null +++ b/src/subcommand/branch_subcommand.cpp @@ -0,0 +1,64 @@ +#include + +#include "../subcommand/branch_subcommand.hpp" +#include "../wrapper/repository_wrapper.hpp" + +branch_subcommand::branch_subcommand(const libgit2_object&, CLI::App& app) +{ + auto* sub = app.add_subcommand("branch", "List, create or delete branches"); + + sub->add_option("", m_branch_name, "The name of the branch to create or delete"); + + sub->add_flag("-d,--delete", m_deletion_flag, "Delete a branch"); + sub->add_flag("-a,--all", m_all_flag, "List both remote-tracking branches and local branches"); + sub->add_flag("-r,--remotes", m_remote_flag, "List or delete (if used with -d) the remote-tracking branches"); + sub->add_flag("-l,--list", m_list_flag, "List branches"); + sub->add_flag("-f,--force", m_force_flag, "Skips confirmation"); + + sub->callback([this]() { this->run(); }); +} + +void branch_subcommand::run() +{ + auto directory = get_current_git_path(); + auto repo = repository_wrapper::open(directory); + + if (m_list_flag || m_branch_name.empty()) + { + auto head_name = repo.head().short_name(); + std::cout << "* " << head_name << std::endl; + git_branch_t type = m_all_flag ? GIT_BRANCH_ALL : (m_remote_flag ? GIT_BRANCH_REMOTE : GIT_BRANCH_LOCAL); + auto iter = repo.iterate_branches(type); + auto br = iter.next(); + while (br) + { + if (br->name() != head_name) + { + std::cout << " " << br->name() << std::endl; + } + br = iter.next(); + } + } + else if (m_deletion_flag) + { + run_deletion(repo); + } + else + { + run_creation(repo); + } +} + +void branch_subcommand::run_deletion(repository_wrapper& repo) +{ + auto branch = repo.find_branch(m_branch_name); + // TODO: handle unmerged stated once we handle upstream repos + delete_branch(std::move(branch)); +} + + +void branch_subcommand::run_creation(repository_wrapper& repo) +{ + // TODO: handle specification of starting commit + repo.create_branch(m_branch_name, m_force_flag); +} diff --git a/src/subcommand/branch_subcommand.hpp b/src/subcommand/branch_subcommand.hpp new file mode 100644 index 0000000..8ec4203 --- /dev/null +++ b/src/subcommand/branch_subcommand.hpp @@ -0,0 +1,28 @@ +#pragma once + +#include + +#include + +#include "../utils/common.hpp" +#include "../wrapper/repository_wrapper.hpp" + +class branch_subcommand +{ +public: + + explicit branch_subcommand(const libgit2_object&, CLI::App& app); + void run(); + +private: + + void run_deletion(repository_wrapper& repo); + void run_creation(repository_wrapper& repo); + + std::string m_branch_name = {}; + bool m_deletion_flag = false; + bool m_all_flag = false; + bool m_remote_flag = false; + bool m_list_flag = false; + bool m_force_flag = false; +}; diff --git a/src/subcommand/status_subcommand.cpp b/src/subcommand/status_subcommand.cpp index 2da0780..83ebd6d 100644 --- a/src/subcommand/status_subcommand.cpp +++ b/src/subcommand/status_subcommand.cpp @@ -186,7 +186,7 @@ void status_subcommand::run() auto bare = false; auto repo = repository_wrapper::init(directory, bare); auto sl = status_list_wrapper::status_list(repo); - auto branch_name = reference_wrapper::get_ref_name(repo); + auto branch_name = repo.head().short_name(); std::set tracked_dir_set{}; std::set untracked_dir_set{}; diff --git a/src/wrapper/branch_wrapper.cpp b/src/wrapper/branch_wrapper.cpp new file mode 100644 index 0000000..afc5d64 --- /dev/null +++ b/src/wrapper/branch_wrapper.cpp @@ -0,0 +1,56 @@ +#include "../utils/git_exception.hpp" +#include "../wrapper/branch_wrapper.hpp" +#include "../wrapper/commit_wrapper.hpp" +#include "../wrapper/repository_wrapper.hpp" + +#include + +branch_wrapper::branch_wrapper(git_reference* ref) + : base_type(ref) +{ +} + +branch_wrapper::~branch_wrapper() +{ + git_reference_free(p_resource); + p_resource = nullptr; +} + +std::string_view branch_wrapper::name() const +{ + const char* out = nullptr; + throwIfError(git_branch_name(&out, p_resource)); + return std::string_view(out); +} + +void delete_branch(branch_wrapper&& branch) +{ + throwIfError(git_branch_delete(branch)); +} + +branch_iterator::branch_iterator(git_branch_iterator* iter) + : base_type(iter) +{ +} + +branch_iterator::~branch_iterator() +{ + git_branch_iterator_free(p_resource); + p_resource = nullptr; +} + + +std::optional branch_iterator::next() +{ + git_reference* ref = nullptr; + git_branch_t type; + int res = git_branch_next(&ref, &type, p_resource); + if (res == 0) + { + return branch_wrapper(ref); + } + else + { + return std::nullopt; + } +} diff --git a/src/wrapper/branch_wrapper.hpp b/src/wrapper/branch_wrapper.hpp new file mode 100644 index 0000000..436c9e4 --- /dev/null +++ b/src/wrapper/branch_wrapper.hpp @@ -0,0 +1,56 @@ +#pragma once + +#include +#include + +#include + +#include "../wrapper/wrapper_base.hpp" + +class commit_wrapper; + +class branch_wrapper : public wrapper_base +{ +public: + + using base_type = wrapper_base; + + ~branch_wrapper(); + + branch_wrapper(branch_wrapper&&) = default; + branch_wrapper& operator=(branch_wrapper&&) = default; + + std::string_view name() const; + +private: + + explicit branch_wrapper(git_reference* ref); + + friend class repository_wrapper; + friend class branch_iterator; +}; + +void delete_branch(branch_wrapper&& br); + +// Rust / Python-like iterator instead of regular C++ iterator, +// because of the libgit2 API. Implementing the postfix increment +// operator of an input iterator would be overcomplicated. +class branch_iterator : public wrapper_base +{ +public: + + using base_type = wrapper_base; + + ~branch_iterator(); + + branch_iterator(branch_iterator&&) = default; + branch_iterator& operator=(branch_iterator&&) = default; + + std::optional next(); + +private: + + explicit branch_iterator(git_branch_iterator* iter); + + friend class repository_wrapper; +}; diff --git a/src/wrapper/commit_wrapper.cpp b/src/wrapper/commit_wrapper.cpp index b562ec5..e8168e0 100644 --- a/src/wrapper/commit_wrapper.cpp +++ b/src/wrapper/commit_wrapper.cpp @@ -1,5 +1,6 @@ -#include "commit_wrapper.hpp" #include "../utils/git_exception.hpp" +#include "../wrapper/commit_wrapper.hpp" +#include "../wrapper/repository_wrapper.hpp" commit_wrapper::~commit_wrapper() { @@ -8,7 +9,7 @@ commit_wrapper::~commit_wrapper() } -commit_wrapper commit_wrapper::last_commit(const repository_wrapper& repo, const std::string& ref_name) +commit_wrapper commit_wrapper::from_reference_name(const repository_wrapper& repo, const std::string& ref_name) { git_oid oid_parent_commit; throwIfError(git_reference_name_to_id(&oid_parent_commit, repo, ref_name.c_str())); diff --git a/src/wrapper/commit_wrapper.hpp b/src/wrapper/commit_wrapper.hpp index 4fccc47..379bd44 100644 --- a/src/wrapper/commit_wrapper.hpp +++ b/src/wrapper/commit_wrapper.hpp @@ -4,9 +4,10 @@ #include -#include "../wrapper/repository_wrapper.hpp" #include "../wrapper/wrapper_base.hpp" +class repository_wrapper; + class commit_wrapper : public wrapper_base { public: @@ -17,7 +18,7 @@ class commit_wrapper : public wrapper_base commit_wrapper& operator=(commit_wrapper&&) noexcept = default; static commit_wrapper - last_commit(const repository_wrapper& repo, const std::string& ref_name = "HEAD"); + from_reference_name(const repository_wrapper& repo, const std::string& ref_name = "HEAD"); private: diff --git a/src/wrapper/refs_wrapper.cpp b/src/wrapper/refs_wrapper.cpp index 7a11c15..21ef84b 100644 --- a/src/wrapper/refs_wrapper.cpp +++ b/src/wrapper/refs_wrapper.cpp @@ -1,6 +1,10 @@ #include "../utils/git_exception.hpp" #include "../wrapper/refs_wrapper.hpp" +reference_wrapper::reference_wrapper(git_reference* ref) + : base_type(ref) +{ +} reference_wrapper::~reference_wrapper() { @@ -8,9 +12,7 @@ reference_wrapper::~reference_wrapper() p_resource=nullptr; } -std::string reference_wrapper::get_ref_name(const repository_wrapper& rw) +std::string reference_wrapper::short_name() const { - reference_wrapper ref; - throwIfError(git_repository_head(&(ref.p_resource), rw)); - return git_reference_shorthand(ref.p_resource); + return git_reference_shorthand(p_resource); } diff --git a/src/wrapper/refs_wrapper.hpp b/src/wrapper/refs_wrapper.hpp index 32f139b..dfe32a7 100644 --- a/src/wrapper/refs_wrapper.hpp +++ b/src/wrapper/refs_wrapper.hpp @@ -4,21 +4,24 @@ #include -#include "../wrapper/repository_wrapper.hpp" #include "../wrapper/wrapper_base.hpp" class reference_wrapper : public wrapper_base { public: + using base_type = wrapper_base; + ~reference_wrapper(); reference_wrapper(reference_wrapper&&) noexcept = default; reference_wrapper& operator=(reference_wrapper&&) noexcept = default; - static std::string get_ref_name(const repository_wrapper& repo); + std::string short_name() const; private: - reference_wrapper() = default; + reference_wrapper(git_reference* ref); + + friend class repository_wrapper; }; diff --git a/src/wrapper/repository_wrapper.cpp b/src/wrapper/repository_wrapper.cpp index 10dc0be..c35eece 100644 --- a/src/wrapper/repository_wrapper.cpp +++ b/src/wrapper/repository_wrapper.cpp @@ -1,5 +1,5 @@ -#include "repository_wrapper.hpp" #include "../utils/git_exception.hpp" +#include "../wrapper/repository_wrapper.hpp" repository_wrapper::~repository_wrapper() { @@ -21,8 +21,41 @@ repository_wrapper repository_wrapper::init(const std::string& directory, bool b return rw; } +reference_wrapper repository_wrapper::head() const +{ + git_reference* ref; + throwIfError(git_repository_head(&ref, *this)); + return reference_wrapper(ref); +} + index_wrapper repository_wrapper::make_index() { index_wrapper index = index_wrapper::init(*this); return index; } + +branch_wrapper repository_wrapper::create_branch(const std::string& name, bool force) +{ + return create_branch(name, commit_wrapper::from_reference_name(*this), force); +} + +branch_wrapper repository_wrapper::create_branch(const std::string& name, const commit_wrapper& commit, bool force) +{ + git_reference* branch = nullptr; + throwIfError(git_branch_create(&branch, *this, name.c_str(), commit, force)); + return branch_wrapper(branch); +} + +branch_wrapper repository_wrapper::find_branch(const std::string& name) +{ + git_reference* branch = nullptr; + throwIfError(git_branch_lookup(&branch, *this, name.c_str(), GIT_BRANCH_LOCAL)); + return branch_wrapper(branch); +} + +branch_iterator repository_wrapper::iterate_branches(git_branch_t type) const +{ + git_branch_iterator* iter = nullptr; + throwIfError(git_branch_iterator_new(&iter, *this, type)); + return branch_iterator(iter); +} diff --git a/src/wrapper/repository_wrapper.hpp b/src/wrapper/repository_wrapper.hpp index 16d9dfa..cf07a20 100644 --- a/src/wrapper/repository_wrapper.hpp +++ b/src/wrapper/repository_wrapper.hpp @@ -4,7 +4,10 @@ #include +#include "../wrapper/branch_wrapper.hpp" +#include "../wrapper/commit_wrapper.hpp" #include "../wrapper/index_wrapper.hpp" +#include "../wrapper/refs_wrapper.hpp" #include "../wrapper/wrapper_base.hpp" class repository_wrapper : public wrapper_base @@ -18,8 +21,18 @@ class repository_wrapper : public wrapper_base static repository_wrapper init(const std::string& directory, bool bare); static repository_wrapper open(const std::string& directory); + + reference_wrapper head() const; + index_wrapper make_index(); + branch_wrapper create_branch(const std::string& name, bool force); + branch_wrapper create_branch(const std::string& name, const commit_wrapper& commit, bool force); + + branch_wrapper find_branch(const std::string& name); + + branch_iterator iterate_branches(git_branch_t type) const; + private: repository_wrapper() = default; diff --git a/src/wrapper/wrapper_base.hpp b/src/wrapper/wrapper_base.hpp index 4750f64..424f274 100644 --- a/src/wrapper/wrapper_base.hpp +++ b/src/wrapper/wrapper_base.hpp @@ -28,7 +28,11 @@ class wrapper_base protected: // Allocation and deletion of p_resource must be handled by inheriting class. - wrapper_base() = default; + explicit wrapper_base(resource_type* resource = nullptr) + : p_resource(resource) + { + } + ~wrapper_base() = default; resource_type* p_resource = nullptr; diff --git a/test/data/status_data/embeded_git/COMMIT_EDITMSG b/test/data/status_data/embedded_git/COMMIT_EDITMSG similarity index 100% rename from test/data/status_data/embeded_git/COMMIT_EDITMSG rename to test/data/status_data/embedded_git/COMMIT_EDITMSG diff --git a/test/data/status_data/embeded_git/HEAD b/test/data/status_data/embedded_git/HEAD similarity index 100% rename from test/data/status_data/embeded_git/HEAD rename to test/data/status_data/embedded_git/HEAD diff --git a/test/data/status_data/embeded_git/config b/test/data/status_data/embedded_git/config similarity index 100% rename from test/data/status_data/embeded_git/config rename to test/data/status_data/embedded_git/config diff --git a/test/data/status_data/embeded_git/description b/test/data/status_data/embedded_git/description similarity index 100% rename from test/data/status_data/embeded_git/description rename to test/data/status_data/embedded_git/description diff --git a/test/data/status_data/embeded_git/hooks/applypatch-msg.sample b/test/data/status_data/embedded_git/hooks/applypatch-msg.sample similarity index 100% rename from test/data/status_data/embeded_git/hooks/applypatch-msg.sample rename to test/data/status_data/embedded_git/hooks/applypatch-msg.sample diff --git a/test/data/status_data/embeded_git/hooks/commit-msg.sample b/test/data/status_data/embedded_git/hooks/commit-msg.sample similarity index 100% rename from test/data/status_data/embeded_git/hooks/commit-msg.sample rename to test/data/status_data/embedded_git/hooks/commit-msg.sample diff --git a/test/data/status_data/embeded_git/hooks/fsmonitor-watchman.sample b/test/data/status_data/embedded_git/hooks/fsmonitor-watchman.sample similarity index 100% rename from test/data/status_data/embeded_git/hooks/fsmonitor-watchman.sample rename to test/data/status_data/embedded_git/hooks/fsmonitor-watchman.sample diff --git a/test/data/status_data/embeded_git/hooks/post-update.sample b/test/data/status_data/embedded_git/hooks/post-update.sample similarity index 100% rename from test/data/status_data/embeded_git/hooks/post-update.sample rename to test/data/status_data/embedded_git/hooks/post-update.sample diff --git a/test/data/status_data/embeded_git/hooks/pre-applypatch.sample b/test/data/status_data/embedded_git/hooks/pre-applypatch.sample similarity index 100% rename from test/data/status_data/embeded_git/hooks/pre-applypatch.sample rename to test/data/status_data/embedded_git/hooks/pre-applypatch.sample diff --git a/test/data/status_data/embeded_git/hooks/pre-commit.sample b/test/data/status_data/embedded_git/hooks/pre-commit.sample similarity index 100% rename from test/data/status_data/embeded_git/hooks/pre-commit.sample rename to test/data/status_data/embedded_git/hooks/pre-commit.sample diff --git a/test/data/status_data/embeded_git/hooks/pre-merge-commit.sample b/test/data/status_data/embedded_git/hooks/pre-merge-commit.sample similarity index 100% rename from test/data/status_data/embeded_git/hooks/pre-merge-commit.sample rename to test/data/status_data/embedded_git/hooks/pre-merge-commit.sample diff --git a/test/data/status_data/embeded_git/hooks/pre-push.sample b/test/data/status_data/embedded_git/hooks/pre-push.sample similarity index 100% rename from test/data/status_data/embeded_git/hooks/pre-push.sample rename to test/data/status_data/embedded_git/hooks/pre-push.sample diff --git a/test/data/status_data/embeded_git/hooks/pre-rebase.sample b/test/data/status_data/embedded_git/hooks/pre-rebase.sample similarity index 100% rename from test/data/status_data/embeded_git/hooks/pre-rebase.sample rename to test/data/status_data/embedded_git/hooks/pre-rebase.sample diff --git a/test/data/status_data/embeded_git/hooks/pre-receive.sample b/test/data/status_data/embedded_git/hooks/pre-receive.sample similarity index 100% rename from test/data/status_data/embeded_git/hooks/pre-receive.sample rename to test/data/status_data/embedded_git/hooks/pre-receive.sample diff --git a/test/data/status_data/embeded_git/hooks/prepare-commit-msg.sample b/test/data/status_data/embedded_git/hooks/prepare-commit-msg.sample similarity index 100% rename from test/data/status_data/embeded_git/hooks/prepare-commit-msg.sample rename to test/data/status_data/embedded_git/hooks/prepare-commit-msg.sample diff --git a/test/data/status_data/embeded_git/hooks/push-to-checkout.sample b/test/data/status_data/embedded_git/hooks/push-to-checkout.sample similarity index 100% rename from test/data/status_data/embeded_git/hooks/push-to-checkout.sample rename to test/data/status_data/embedded_git/hooks/push-to-checkout.sample diff --git a/test/data/status_data/embeded_git/hooks/sendemail-validate.sample b/test/data/status_data/embedded_git/hooks/sendemail-validate.sample similarity index 100% rename from test/data/status_data/embeded_git/hooks/sendemail-validate.sample rename to test/data/status_data/embedded_git/hooks/sendemail-validate.sample diff --git a/test/data/status_data/embeded_git/hooks/update.sample b/test/data/status_data/embedded_git/hooks/update.sample similarity index 100% rename from test/data/status_data/embeded_git/hooks/update.sample rename to test/data/status_data/embedded_git/hooks/update.sample diff --git a/test/data/status_data/embeded_git/index b/test/data/status_data/embedded_git/index similarity index 100% rename from test/data/status_data/embeded_git/index rename to test/data/status_data/embedded_git/index diff --git a/test/data/status_data/embeded_git/info/exclude b/test/data/status_data/embedded_git/info/exclude similarity index 100% rename from test/data/status_data/embeded_git/info/exclude rename to test/data/status_data/embedded_git/info/exclude diff --git a/test/data/status_data/embeded_git/logs/HEAD b/test/data/status_data/embedded_git/logs/HEAD similarity index 100% rename from test/data/status_data/embeded_git/logs/HEAD rename to test/data/status_data/embedded_git/logs/HEAD diff --git a/test/data/status_data/embeded_git/logs/refs/heads/main b/test/data/status_data/embedded_git/logs/refs/heads/main similarity index 100% rename from test/data/status_data/embeded_git/logs/refs/heads/main rename to test/data/status_data/embedded_git/logs/refs/heads/main diff --git a/test/data/status_data/embeded_git/objects/75/743dcbd85064226c77a0b862af817838ae0b2e b/test/data/status_data/embedded_git/objects/75/743dcbd85064226c77a0b862af817838ae0b2e similarity index 100% rename from test/data/status_data/embeded_git/objects/75/743dcbd85064226c77a0b862af817838ae0b2e rename to test/data/status_data/embedded_git/objects/75/743dcbd85064226c77a0b862af817838ae0b2e diff --git a/test/data/status_data/embeded_git/objects/9c/a9a8716bf7f5ab1ff449be0c97a04a7aae4262 b/test/data/status_data/embedded_git/objects/9c/a9a8716bf7f5ab1ff449be0c97a04a7aae4262 similarity index 100% rename from test/data/status_data/embeded_git/objects/9c/a9a8716bf7f5ab1ff449be0c97a04a7aae4262 rename to test/data/status_data/embedded_git/objects/9c/a9a8716bf7f5ab1ff449be0c97a04a7aae4262 diff --git a/test/data/status_data/embeded_git/objects/bd/ef436c07ee899d19659581d2944f573ac620e0 b/test/data/status_data/embedded_git/objects/bd/ef436c07ee899d19659581d2944f573ac620e0 similarity index 100% rename from test/data/status_data/embeded_git/objects/bd/ef436c07ee899d19659581d2944f573ac620e0 rename to test/data/status_data/embedded_git/objects/bd/ef436c07ee899d19659581d2944f573ac620e0 diff --git a/test/data/status_data/embeded_git/objects/dc/430e3c2e6e1193b377040126b95099261e7251 b/test/data/status_data/embedded_git/objects/dc/430e3c2e6e1193b377040126b95099261e7251 similarity index 100% rename from test/data/status_data/embeded_git/objects/dc/430e3c2e6e1193b377040126b95099261e7251 rename to test/data/status_data/embedded_git/objects/dc/430e3c2e6e1193b377040126b95099261e7251 diff --git a/test/data/status_data/embeded_git/objects/e3/323b422ccebe9515b4aa335b40615f398b6553 b/test/data/status_data/embedded_git/objects/e3/323b422ccebe9515b4aa335b40615f398b6553 similarity index 100% rename from test/data/status_data/embeded_git/objects/e3/323b422ccebe9515b4aa335b40615f398b6553 rename to test/data/status_data/embedded_git/objects/e3/323b422ccebe9515b4aa335b40615f398b6553 diff --git a/test/data/status_data/embeded_git/objects/ee/8c4cf874c4f1e3ba755f929fe7811018adee3d b/test/data/status_data/embedded_git/objects/ee/8c4cf874c4f1e3ba755f929fe7811018adee3d similarity index 100% rename from test/data/status_data/embeded_git/objects/ee/8c4cf874c4f1e3ba755f929fe7811018adee3d rename to test/data/status_data/embedded_git/objects/ee/8c4cf874c4f1e3ba755f929fe7811018adee3d diff --git a/test/data/status_data/embeded_git/objects/fd/511fbb5dd2860baabf28e298fa3373634e8660 b/test/data/status_data/embedded_git/objects/fd/511fbb5dd2860baabf28e298fa3373634e8660 similarity index 100% rename from test/data/status_data/embeded_git/objects/fd/511fbb5dd2860baabf28e298fa3373634e8660 rename to test/data/status_data/embedded_git/objects/fd/511fbb5dd2860baabf28e298fa3373634e8660 diff --git a/test/data/status_data/embeded_git/refs/heads/main b/test/data/status_data/embedded_git/refs/heads/main similarity index 100% rename from test/data/status_data/embeded_git/refs/heads/main rename to test/data/status_data/embedded_git/refs/heads/main diff --git a/test/test_branch.py b/test/test_branch.py new file mode 100644 index 0000000..c6d5882 --- /dev/null +++ b/test/test_branch.py @@ -0,0 +1,28 @@ +import os +import subprocess + +import pytest + +@pytest.fixture +def rename_git(): + os.rename("test/data/status_data/embedded_git/", "test/data/status_data/.git/") + yield + os.rename("test/data/status_data/.git/", "test/data/status_data/embedded_git/") + +def test_branch_list(rename_git, git2cpp_path): + cmd = [git2cpp_path, 'branch'] + p = subprocess.run(cmd, capture_output=True, cwd="test/data/status_data", text=True) + assert(p.stdout == '* main\n') + +def test_branch_create_delete(rename_git, git2cpp_path): + create_cmd = [git2cpp_path, 'branch', 'foregone'] + subprocess.run(create_cmd, capture_output=True, cwd="test/data/status_data", text=True) + list_cmd = [git2cpp_path, 'branch'] + p = subprocess.run(list_cmd, capture_output=True, cwd="test/data/status_data", text=True) + assert(p.stdout == '* main\n foregone\n') + del_cmd = [git2cpp_path, 'branch', '-d', 'foregone'] + subprocess.run(del_cmd, capture_output=True, cwd="test/data/status_data", text=True) + p2 = subprocess.run(list_cmd, capture_output=True, cwd="test/data/status_data", text=True) + assert(p2.stdout == '* main\n') + + diff --git a/test/test_status.py b/test/test_status.py index 87abb3d..49f8470 100644 --- a/test/test_status.py +++ b/test/test_status.py @@ -7,9 +7,9 @@ @pytest.fixture def rename_git(): - os.rename("test/data/status_data/embeded_git/", "test/data/status_data/.git/") + os.rename("test/data/status_data/embedded_git/", "test/data/status_data/.git/") yield - os.rename("test/data/status_data/.git/", "test/data/status_data/embeded_git/") + os.rename("test/data/status_data/.git/", "test/data/status_data/embedded_git/") @pytest.mark.parametrize("short_flag", ["", "-s", "--short"])