|
| 1 | +#include <iostream> |
| 2 | + |
| 3 | +#include "../subcommand/branch_subcommand.hpp" |
| 4 | +#include "../wrapper/repository_wrapper.hpp" |
| 5 | + |
| 6 | +branch_subcommand::branch_subcommand(const libgit2_object&, CLI::App& app) |
| 7 | +{ |
| 8 | + auto* sub = app.add_subcommand("branch", "List, create or delete branches"); |
| 9 | + |
| 10 | + sub->add_option("<branchname>", m_branch_name, "The name of the branch to create or delete"); |
| 11 | + |
| 12 | + sub->add_flag("-d,--delete", m_deletion_flag, "Delete a branch"); |
| 13 | + sub->add_flag("-a,--all", m_all_flag, "List both remote-tracking branches and local branches"); |
| 14 | + sub->add_flag("-r,--remotes", m_remote_flag, "List or delete (if used with -d) the remote-tracking branches"); |
| 15 | + sub->add_flag("-l,--list", m_list_flag, "List branches"); |
| 16 | + sub->add_flag("-f,--force", m_force_flag, "Skips confirmation"); |
| 17 | + |
| 18 | + sub->callback([this]() { this->run(); }); |
| 19 | +} |
| 20 | + |
| 21 | +void branch_subcommand::run() |
| 22 | +{ |
| 23 | + auto directory = get_current_git_path(); |
| 24 | + auto repo = repository_wrapper::open(directory); |
| 25 | + |
| 26 | + if (m_list_flag || m_branch_name.empty()) |
| 27 | + { |
| 28 | + auto head_name = repo.head().short_name(); |
| 29 | + std::cout << "* " << head_name << std::endl; |
| 30 | + git_branch_t type = m_all_flag ? GIT_BRANCH_ALL : (m_remote_flag ? GIT_BRANCH_REMOTE : GIT_BRANCH_LOCAL); |
| 31 | + auto iter = repo.iterate_branches(type); |
| 32 | + auto br = iter.next(); |
| 33 | + while (br) |
| 34 | + { |
| 35 | + if (br->name() != head_name) |
| 36 | + { |
| 37 | + std::cout << " " << br->name() << std::endl; |
| 38 | + } |
| 39 | + br = iter.next(); |
| 40 | + } |
| 41 | + } |
| 42 | + else if (m_deletion_flag) |
| 43 | + { |
| 44 | + run_deletion(repo); |
| 45 | + } |
| 46 | + else |
| 47 | + { |
| 48 | + run_creation(repo); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +void branch_subcommand::run_deletion(repository_wrapper& repo) |
| 53 | +{ |
| 54 | + auto branch = repo.find_branch(m_branch_name); |
| 55 | + // TODO: handle unmerged stated once we handle upstream repos |
| 56 | + delete_branch(std::move(branch)); |
| 57 | +} |
| 58 | + |
| 59 | + |
| 60 | +void branch_subcommand::run_creation(repository_wrapper& repo) |
| 61 | +{ |
| 62 | + // TODO: handle specification of starting commit |
| 63 | + repo.create_branch(m_branch_name, m_force_flag); |
| 64 | +} |
0 commit comments