-
Notifications
You must be signed in to change notification settings - Fork 3
Minimal command for branches #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
aac0ece
3ee5cfc
e1c5ce2
aab94f6
0b6cab5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| #include <iostream> | ||
|
|
||
| #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("<branchname>", 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 bare = false; | ||
| auto repo = repository_wrapper::open(directory); | ||
|
|
||
| if (m_list_flag || m_branch_name.empty()) | ||
| { | ||
| 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) | ||
| { | ||
| 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); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| #pragma once | ||
|
|
||
| #include <string> | ||
|
|
||
| #include <CLI/CLI.hpp> | ||
|
|
||
| #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; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 <iostream> | ||
|
|
||
| branch_wrapper::branch_wrapper(git_reference* polna_ref) | ||
| : base_type(polna_ref) | ||
| { | ||
| } | ||
|
|
||
| branch_wrapper::~branch_wrapper() | ||
| { | ||
| git_reference_free(p_resource); | ||
| p_resource=nullptr; | ||
JohanMabille marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| 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_wrapper> 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; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| #pragma once | ||
|
|
||
| #include <optional> | ||
| #include <string_view> | ||
|
|
||
| #include <git2.h> | ||
|
|
||
| #include "../wrapper/wrapper_base.hpp" | ||
|
|
||
| class commit_wrapper; | ||
|
|
||
| class branch_wrapper : public wrapper_base<git_reference> | ||
| { | ||
| public: | ||
|
|
||
| using base_type = wrapper_base<git_reference>; | ||
|
|
||
| ~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<git_branch_iterator> | ||
| { | ||
| public: | ||
|
|
||
| using base_type = wrapper_base<git_branch_iterator>; | ||
|
|
||
| ~branch_iterator(); | ||
|
|
||
| branch_iterator(branch_iterator&&) = default; | ||
| branch_iterator& operator=(branch_iterator&&) = default; | ||
|
|
||
| std::optional<branch_wrapper> next(); | ||
|
|
||
| private: | ||
|
|
||
| explicit branch_iterator(git_branch_iterator* iter); | ||
|
|
||
| friend class repository_wrapper; | ||
| }; |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,28 @@ | ||||||||||||||||||||||||||
| import os | ||||||||||||||||||||||||||
| import subprocess | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| import pytest | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| @pytest.fixture | ||||||||||||||||||||||||||
| def rename_git(): | ||||||||||||||||||||||||||
| os.rename("test/data/status_data/embeded_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/embeded_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/embedded_git/", "test/data/status_data/.git/") | |
| yield | |
| os.rename("test/data/status_data/.git/", "test/data/status_data/embedded_git/") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This suggesting seems sensible to me.
Outdated
Copilot
AI
Jul 19, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The directory name 'embeded_git' contains a spelling error. It should be 'embedded_git'.
| os.rename("test/data/status_data/embeded_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/embedded_git/", "test/data/status_data/.git/") | |
| yield | |
| os.rename("test/data/status_data/.git/", "test/data/status_data/embedded_git/") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we have a better name for the argument here? I'd be happy with just
ref.