-
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
aac0ece
Minimal command for branches
JohanMabille 3ee5cfc
copilot revision
JohanMabille e1c5ce2
Displays current branch when listing
JohanMabille aab94f6
Better arguments names
JohanMabille 0b6cab5
Fixed typo in embeded_git
JohanMabille File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#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 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); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,18 @@ | ||
#include "../utils/git_exception.hpp" | ||
#include "../wrapper/refs_wrapper.hpp" | ||
|
||
reference_wrapper::reference_wrapper(git_reference* jaila_ref) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better argument name again. |
||
: base_type(jaila_ref) | ||
{ | ||
} | ||
|
||
reference_wrapper::~reference_wrapper() | ||
{ | ||
git_reference_free(p_resource); | ||
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); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
.