|
| 1 | +#include <git2/config.h> |
| 2 | +#include <git2/types.h> |
| 3 | +#include <iostream> |
| 4 | + |
| 5 | +#include <git2/remote.h> |
| 6 | + |
| 7 | +#include "../utils/git_exception.hpp" |
| 8 | +#include "../subcommand/config_subcommand.hpp" |
| 9 | +#include "../wrapper/config_wrapper.hpp" |
| 10 | +#include "../wrapper/repository_wrapper.hpp" |
| 11 | + |
| 12 | +config_subcommand::config_subcommand(const libgit2_object&, CLI::App& app) |
| 13 | +{ |
| 14 | + auto* config = app.add_subcommand("config", "Get and set repository or global options"); |
| 15 | + auto* list = config->add_subcommand("list", "List all variables set in config file, along with their values."); |
| 16 | + 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."); |
| 17 | + 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."); |
| 18 | + 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."); |
| 19 | + |
| 20 | + get->add_option("<name>", m_name, ""); |
| 21 | + set->add_option("<name>", m_name, ""); |
| 22 | + set->add_option("<value>", m_value, ""); |
| 23 | + unset->add_option("<name>", m_name, ""); |
| 24 | + |
| 25 | + // TODO: |
| 26 | + // sub->add_flag("--local", m_local_flag, ""); |
| 27 | + // sub->add_flag("--global", m_global_flag, ""); |
| 28 | + // sub->add_flag("--system", m_system_flag, ""); |
| 29 | + // sub->add_flag("--worktree", m_worktree_flag, ""); |
| 30 | + |
| 31 | + list->callback([this]() { this->run_list(); }); |
| 32 | + get->callback([this]() { this->run_get(); }); |
| 33 | + set->callback([this]() { this->run_set(); }); |
| 34 | + unset->callback([this]() { this->run_unset(); }); |
| 35 | +} |
| 36 | + |
| 37 | +void config_subcommand::run_list() |
| 38 | +{ |
| 39 | + auto directory = get_current_git_path(); |
| 40 | + auto repo = repository_wrapper::open(directory); |
| 41 | + auto cfg = repo.get_config(); |
| 42 | + |
| 43 | + git_config_iterator* iter; |
| 44 | + throw_if_error(git_config_iterator_new(&iter, cfg)); |
| 45 | + |
| 46 | + git_config_entry* entry; |
| 47 | + while (git_config_next(&entry, iter) == GIT_OK) |
| 48 | + { |
| 49 | + std::cout << entry->name << "=" << entry->value << std::endl; |
| 50 | + } |
| 51 | + |
| 52 | + git_config_iterator_free(iter); |
| 53 | +} |
| 54 | + |
| 55 | +void config_subcommand::run_get() |
| 56 | +{ |
| 57 | + if (m_name.empty()) |
| 58 | + { |
| 59 | + throw git_exception("error: wrong number of arguments, should be 1", 129); |
| 60 | + } |
| 61 | + |
| 62 | + auto directory = get_current_git_path(); |
| 63 | + auto repo = repository_wrapper::open(directory); |
| 64 | + auto cfg = repo.get_config(); |
| 65 | + |
| 66 | + git_config_entry* entry = cfg.get_entry(m_name); |
| 67 | + std::cout << entry->value << std::endl; |
| 68 | + |
| 69 | + git_config_entry_free(entry); |
| 70 | +} |
| 71 | + |
| 72 | +void config_subcommand::run_set() |
| 73 | +{ |
| 74 | + if (m_name.empty() | m_value.empty()) |
| 75 | + { |
| 76 | + throw git_exception("error: wrong number of arguments, should be 2", 129); |
| 77 | + } |
| 78 | + |
| 79 | + auto directory = get_current_git_path(); |
| 80 | + auto repo = repository_wrapper::open(directory); |
| 81 | + auto cfg = repo.get_config(); |
| 82 | + |
| 83 | + cfg.set_entry(m_name, m_value); |
| 84 | +} |
| 85 | + |
| 86 | +void config_subcommand::run_unset() |
| 87 | +{ |
| 88 | + if (m_name.empty()) |
| 89 | + { |
| 90 | + throw git_exception("error: wrong number of arguments, should be 1", 129); |
| 91 | + } |
| 92 | + |
| 93 | + auto directory = get_current_git_path(); |
| 94 | + auto repo = repository_wrapper::open(directory); |
| 95 | + auto cfg = repo.get_config(); |
| 96 | + |
| 97 | + cfg.delete_entry(m_name); |
| 98 | +} |
0 commit comments