-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpush_subcommand.cpp
More file actions
105 lines (92 loc) · 3.05 KB
/
push_subcommand.cpp
File metadata and controls
105 lines (92 loc) · 3.05 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
#include "../subcommand/push_subcommand.hpp"
#include <iostream>
#include <git2/remote.h>
#include <git2/types.h>
#include "../utils/ansi_code.hpp"
#include "../utils/credentials.hpp"
#include "../utils/progress.hpp"
#include "../wrapper/repository_wrapper.hpp"
push_subcommand::push_subcommand(const libgit2_object&, CLI::App& app)
{
auto* sub = app.add_subcommand("push", "Update remote refs along with associated objects");
sub->add_option("<remote>", m_remote_name, "The remote to push to")->default_val("origin");
sub->add_option("<branch>", m_branch_name, "The branch to push");
sub->add_option("<refspec>", m_refspecs, "The refspec(s) to push");
sub->add_flag(
"--all,--branches",
m_branches_flag,
"Push all branches (i.e. refs under " + ansi_code::bold + "refs/heads/" + ansi_code::reset
+ "); cannot be used with other <refspec>."
);
sub->callback(
[this]()
{
this->run();
}
);
}
void push_subcommand::run()
{
auto directory = get_current_git_path();
auto repo = repository_wrapper::open(directory);
std::string remote_name = m_remote_name.empty() ? "origin" : m_remote_name;
auto remote = repo.find_remote(remote_name);
git_push_options push_opts = GIT_PUSH_OPTIONS_INIT;
push_opts.callbacks.credentials = user_credentials;
push_opts.callbacks.push_transfer_progress = push_transfer_progress;
push_opts.callbacks.push_update_reference = push_update_reference;
if (m_branches_flag)
{
auto iter = repo.iterate_branches(GIT_BRANCH_LOCAL);
auto br = iter.next();
while (br)
{
std::string refspec = "refs/heads/" + std::string(br->name());
m_refspecs.push_back(refspec);
br = iter.next();
}
}
else if (m_refspecs.empty())
{
std::string branch;
if (!m_branch_name.empty())
{
branch = m_branch_name;
}
else
{
try
{
auto head_ref = repo.head();
branch = head_ref.short_name();
}
catch (...)
{
std::cerr << "Could not determine current branch to push." << std::endl;
return;
}
}
std::string refspec = "refs/heads/" + branch;
m_refspecs.push_back(refspec);
}
git_strarray_wrapper refspecs_wrapper(m_refspecs);
git_strarray* refspecs_ptr = nullptr;
refspecs_ptr = refspecs_wrapper;
remote.push(refspecs_ptr, &push_opts);
std::cout << "To " << remote.url() << std::endl;
for (const auto& refspec : m_refspecs)
{
std::string_view ref_view(refspec);
std::string_view prefix = "refs/heads/";
std::string short_name;
if (ref_view.substr(0, prefix.size()) == prefix)
{
short_name = ref_view.substr(prefix.size());
}
else
{
short_name = refspec;
}
std::cout << " * " << short_name << " -> " << short_name << std::endl;
}
}