Repository navigation
Expand file tree
/
Copy pathinit_subcommand.cpp
More file actions
69 lines (58 loc) · 1.88 KB
/
Copy pathinit_subcommand.cpp
File metadata and controls
69 lines (58 loc) · 1.88 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
#include "init_subcommand.hpp"
#include <filesystem>
#include "../wrapper/repository_wrapper.hpp"
init_subcommand::init_subcommand(const libgit2_object&, CLI::App& app)
{
auto* sub = app.add_subcommand("init", "Create an empty Git repository or reinitialize an existing one");
sub->add_flag("--bare", m_bare, "Create a bare repository");
// If directory not specified, uses cwd.
sub->add_option(
"directory",
m_directory,
"Directory to create the repository in, created if it does not exist. Defaults to the current directory."
)
->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;
}
}