forked from QuantStack/git2cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_subcommand.cpp
More file actions
50 lines (41 loc) · 1.61 KB
/
init_subcommand.cpp
File metadata and controls
50 lines (41 loc) · 1.61 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
#include <filesystem>
#include "init_subcommand.hpp"
#include "../wrapper/repository_wrapper.hpp"
init_subcommand::init_subcommand(const libgit2_object&, CLI::App& app)
{
auto *sub = app.add_subcommand("init", "Explanation of init here");
sub->add_flag("--bare", m_bare, "info about bare arg");
// If directory not specified, uses cwd.
sub->add_option("directory", m_directory, "info about directory arg")
->check(CLI::ExistingDirectory | CLI::NonexistentPath)
->default_val(get_current_git_path());
sub->add_option("-b,--initial-branch", m_branch, "Use <branch-name> for the initial branch in the newly created repository. If not specified, fall back to the default name.");
sub->callback([this]() { this->run(); });
}
void init_subcommand::run()
{
std::filesystem::path target_dir = m_directory;
bool reinit = std::filesystem::exists(target_dir / ".git" / "HEAD");
repository_wrapper repo = [this]() {
if (m_branch.empty())
{
return repository_wrapper::init(m_directory, m_bare);
}
else
{
git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
if (m_bare) { opts.flags |= GIT_REPOSITORY_INIT_BARE; }
opts.initial_head = m_branch.c_str();
return repository_wrapper::init_ext(m_directory, &opts);
}
}();
std::string path = repo.path();
if (reinit)
{
std::cout << "Reinitialized existing Git repository in " << path <<std::endl;
}
else
{
std::cout << "Initialized empty Git repository in " << path <<std::endl;
}
}