forked from QuantStack/git2cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbranch_subcommand.cpp
More file actions
72 lines (61 loc) · 2.08 KB
/
branch_subcommand.cpp
File metadata and controls
72 lines (61 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#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())
{
run_list(repo);
}
else if (m_deletion_flag)
{
run_deletion(repo);
}
else
{
run_creation(repo);
}
}
void branch_subcommand::run_list(const repository_wrapper& repo)
{
auto head_name = repo.head_short_name();
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;
}
else
{
std::cout << "* " << br->name() << std::endl;
}
br = iter.next();
}
}
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);
}