-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconfig_subcommand.cpp
More file actions
128 lines (107 loc) · 3.85 KB
/
config_subcommand.cpp
File metadata and controls
128 lines (107 loc) · 3.85 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "../subcommand/config_subcommand.hpp"
#include <iostream>
#include <git2/config.h>
#include <git2/remote.h>
#include <git2/types.h>
#include "../utils/git_exception.hpp"
#include "../wrapper/config_wrapper.hpp"
#include "../wrapper/repository_wrapper.hpp"
config_subcommand::config_subcommand(const libgit2_object&, CLI::App& app)
{
auto* config = app.add_subcommand("config", "Get and set repository or global options");
auto* list = config->add_subcommand("list", "List all variables set in config file, along with their values.");
auto* get = config->add_subcommand(
"get",
"Emits the value of the specified key. If key is present multiple times in the configuration, emits the last value. If --all is specified, emits all values associated with key. Returns error code 1 if key is not present."
);
auto* set = config->add_subcommand(
"set",
"Set value for one or more config options. By default, this command refuses to write multi-valued config options. Passing --all will replace all multi-valued config options with the new value, whereas --value= will replace all config options whose values match the given pattern."
);
auto* unset = config->add_subcommand(
"unset",
"Unset value for one or more config options. By default, this command refuses to unset multi-valued keys. Passing --all will unset all multi-valued config options, whereas --value will unset all config options whose values match the given pattern."
);
get->add_option("<name>", m_name, "");
set->add_option("<name>", m_name, "");
set->add_option("<value>", m_value, "");
unset->add_option("<name>", m_name, "");
// TODO:
// sub->add_flag("--local", m_local_flag, "");
// sub->add_flag("--global", m_global_flag, "");
// sub->add_flag("--system", m_system_flag, "");
// sub->add_flag("--worktree", m_worktree_flag, "");
list->callback(
[this]()
{
this->run_list();
}
);
get->callback(
[this]()
{
this->run_get();
}
);
set->callback(
[this]()
{
this->run_set();
}
);
unset->callback(
[this]()
{
this->run_unset();
}
);
}
void config_subcommand::run_list()
{
auto directory = get_current_git_path();
auto repo = repository_wrapper::open(directory);
auto cfg = repo.get_config();
git_config_iterator* iter;
throw_if_error(git_config_iterator_new(&iter, cfg));
git_config_entry* entry;
while (git_config_next(&entry, iter) == GIT_OK)
{
std::cout << entry->name << "=" << entry->value << std::endl;
}
git_config_iterator_free(iter);
}
void config_subcommand::run_get()
{
if (m_name.empty())
{
throw git_exception("error: wrong number of arguments, should be 1", git2cpp_error_code::BAD_ARGUMENT);
}
auto directory = get_current_git_path();
auto repo = repository_wrapper::open(directory);
auto cfg = repo.get_config();
git_config_entry* entry = cfg.get_entry(m_name);
std::cout << entry->value << std::endl;
git_config_entry_free(entry);
}
void config_subcommand::run_set()
{
if (m_name.empty() | m_value.empty())
{
throw git_exception("error: wrong number of arguments, should be 2", git2cpp_error_code::BAD_ARGUMENT);
}
auto directory = get_current_git_path();
auto repo = repository_wrapper::open(directory);
auto cfg = repo.get_config();
cfg.set_entry(m_name, m_value);
}
void config_subcommand::run_unset()
{
if (m_name.empty())
{
throw git_exception("error: wrong number of arguments, should be 1", git2cpp_error_code::BAD_ARGUMENT);
}
auto directory = get_current_git_path();
auto repo = repository_wrapper::open(directory);
auto cfg = repo.get_config();
cfg.delete_entry(m_name);
}