Skip to content

Commit 57e3779

Browse files
committed
edit remote
1 parent 72404c2 commit 57e3779

15 files changed

+505
-416
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ set(GIT2CPP_SRC
7474
${GIT2CPP_SOURCE_DIR}/utils/git_exception.hpp
7575
${GIT2CPP_SOURCE_DIR}/utils/output.cpp
7676
${GIT2CPP_SOURCE_DIR}/utils/output.hpp
77+
${GIT2CPP_SOURCE_DIR}/utils/progress.cpp
78+
${GIT2CPP_SOURCE_DIR}/utils/progress.hpp
7779
${GIT2CPP_SOURCE_DIR}/utils/terminal_pager.cpp
7880
${GIT2CPP_SOURCE_DIR}/utils/terminal_pager.hpp
7981
${GIT2CPP_SOURCE_DIR}/wrapper/annotated_commit_wrapper.cpp

src/subcommand/clone_subcommand.cpp

Lines changed: 6 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "../subcommand/clone_subcommand.hpp"
44
#include "../utils/output.hpp"
5+
#include "../utils/progress.hpp"
56
#include "../wrapper/repository_wrapper.hpp"
67

78
clone_subcommand::clone_subcommand(const libgit2_object&, CLI::App& app)
@@ -10,81 +11,11 @@ clone_subcommand::clone_subcommand(const libgit2_object&, CLI::App& app)
1011

1112
sub->add_option("<repository>", m_repository, "The (possibly remote) repository to clone from.")->required();
1213
sub->add_option("<directory>", m_directory, "The name of a new directory to clone into.");
14+
sub->add_flag("--bare", m_bare, "Create a bare Git repository.");
1315

1416
sub->callback([this]() { this->run(); });
1517
}
1618

17-
namespace
18-
{
19-
int sideband_progress(const char* str, int len, void*)
20-
{
21-
printf("remote: %.*s", len, str);
22-
fflush(stdout);
23-
return 0;
24-
}
25-
26-
int fetch_progress(const git_indexer_progress* stats, void* payload)
27-
{
28-
static bool done = false;
29-
30-
// We need to copy stats into payload even if the fetch is done,
31-
// because the checkout_progress callback will be called with the
32-
// same payload and needs the data to be up do date.
33-
auto* pr = reinterpret_cast<git_indexer_progress*>(payload);
34-
*pr = *stats;
35-
36-
if (done)
37-
{
38-
return 0;
39-
}
40-
41-
int network_percent = pr->total_objects > 0 ?
42-
(100 * pr->received_objects / pr->total_objects)
43-
: 0;
44-
size_t mbytes = pr->received_bytes / (1024*1024);
45-
46-
std::cout << "Receiving objects: " << std::setw(4) << network_percent
47-
<< "% (" << pr->received_objects << "/" << pr->total_objects << "), "
48-
<< mbytes << " MiB";
49-
50-
if (pr->received_objects == pr->total_objects)
51-
{
52-
std::cout << ", done." << std::endl;
53-
done = true;
54-
}
55-
else
56-
{
57-
std::cout << '\r';
58-
}
59-
return 0;
60-
}
61-
62-
void checkout_progress(const char* path, size_t cur, size_t tot, void* payload)
63-
{
64-
static bool done = false;
65-
if (done)
66-
{
67-
return;
68-
}
69-
auto* pr = reinterpret_cast<git_indexer_progress*>(payload);
70-
int deltas_percent = pr->total_deltas > 0 ?
71-
(100 * pr->indexed_deltas / pr->total_deltas)
72-
: 0;
73-
74-
std::cout << "Resolving deltas: " << std::setw(4) << deltas_percent
75-
<< "% (" << pr->indexed_deltas << "/" << pr->total_deltas << ")";
76-
if (pr->indexed_deltas == pr->total_deltas)
77-
{
78-
std::cout << ", done." << std::endl;
79-
done = true;
80-
}
81-
else
82-
{
83-
std::cout << '\r';
84-
}
85-
}
86-
}
87-
8819
void clone_subcommand::run()
8920
{
9021
git_indexer_progress pd;
@@ -94,9 +25,10 @@ void clone_subcommand::run()
9425
checkout_opts.progress_cb = checkout_progress;
9526
checkout_opts.progress_payload = &pd;
9627
clone_opts.checkout_opts = checkout_opts;
97-
clone_opts.fetch_opts.callbacks.sideband_progress = sideband_progress;
98-
clone_opts.fetch_opts.callbacks.transfer_progress = fetch_progress;
99-
clone_opts.fetch_opts.callbacks.payload = &pd;
28+
clone_opts.fetch_opts.callbacks.sideband_progress = sideband_progress;
29+
clone_opts.fetch_opts.callbacks.transfer_progress = fetch_progress;
30+
clone_opts.fetch_opts.callbacks.payload = &pd;
31+
clone_opts.bare = m_bare ? 1 : 0;
10032

10133
std::string short_name = m_directory;
10234
if (m_directory.empty())

src/subcommand/clone_subcommand.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ class clone_subcommand
1515

1616
std::string m_repository = {};
1717
std::string m_directory = {};
18+
bool m_bare = false;
1819
};
Lines changed: 5 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,11 @@
11
#include <iostream>
2-
#include <iomanip>
32

43
#include <git2/remote.h>
54

65
#include "../subcommand/fetch_subcommand.hpp"
7-
#include "../wrapper/repository_wrapper.hpp"
8-
#include "../wrapper/remote_wrapper.hpp"
96
#include "../utils/output.hpp"
10-
#include "../utils/git_exception.hpp"
11-
12-
namespace
13-
{
14-
int sideband_progress(const char* str, int len, void*)
15-
{
16-
std::cout << "remote: " << std::string(str, static_cast<std::size_t>(len));
17-
std::cout.flush();
18-
return 0;
19-
}
20-
21-
int fetch_progress(const git_indexer_progress* stats, void* payload)
22-
{
23-
bool done = false;
24-
25-
auto* pr = reinterpret_cast<git_indexer_progress*>(payload);
26-
*pr = *stats;
27-
28-
if (done)
29-
{
30-
return 0;
31-
}
32-
33-
int network_percent = pr->total_objects > 0 ?
34-
(100 * pr->received_objects / pr->total_objects)
35-
: 0;
36-
size_t mbytes = pr->received_bytes / (1024*1024);
37-
38-
std::cout << "Receiving objects: " << std::setw(4) << network_percent
39-
<< "% (" << pr->received_objects << "/" << pr->total_objects << "), "
40-
<< mbytes << " MiB";
41-
42-
if (pr->received_objects == pr->total_objects)
43-
{
44-
std::cout << ", done." << std::endl;
45-
done = true;
46-
}
47-
else
48-
{
49-
std::cout << '\r';
50-
}
51-
return 0;
52-
}
53-
54-
int update_refs(const char* refname, const git_oid* a, const git_oid* b, git_refspec*, void*)
55-
{
56-
char a_str[GIT_OID_SHA1_HEXSIZE+1], b_str[GIT_OID_SHA1_HEXSIZE+1];
57-
58-
git_oid_fmt(b_str, b);
59-
b_str[GIT_OID_SHA1_HEXSIZE] = '\0';
60-
61-
if (git_oid_is_zero(a))
62-
{
63-
std::cout << "[new] "
64-
<< std::string(b_str, 20)
65-
<< " " << refname << std::endl;
66-
}
67-
else
68-
{
69-
git_oid_fmt(a_str, a);
70-
a_str[GIT_OID_SHA1_HEXSIZE] = '\0';
71-
72-
std::cout << "[updated] "
73-
<< std::string(a_str, 10)
74-
<< ".."
75-
<< std::string(b_str, 10)
76-
<< " " << refname << std::endl;
77-
}
78-
79-
return 0;
80-
}
81-
}
7+
#include "../utils/progress.hpp"
8+
#include "../wrapper/repository_wrapper.hpp"
829

8310
fetch_subcommand::fetch_subcommand(const libgit2_object&, CLI::App& app)
8411
{
@@ -107,16 +34,16 @@ void fetch_subcommand::run()
10734
fetch_opts.callbacks.update_refs = update_refs;
10835

10936
cursor_hider ch;
110-
37+
11138
// Perform the fetch
112-
throw_if_error(git_remote_fetch(remote, nullptr, &fetch_opts, "fetch"));
39+
remote.fetch(nullptr, &fetch_opts, "fetch");
11340

11441
// Show statistics
11542
const git_indexer_progress* stats = git_remote_stats(remote);
11643
if (stats->local_objects > 0)
11744
{
11845
std::cout << "\rReceived " << stats->indexed_objects << "/" << stats->total_objects
119-
<< " objects in " << stats->received_bytes << " bytes (used "
46+
<< " objects in " << stats->received_bytes << " bytes (used "
12047
<< stats->local_objects << " local objects)" << std::endl;
12148
}
12249
else
@@ -125,4 +52,3 @@ void fetch_subcommand::run()
12552
<< " objects in " << stats->received_bytes << " bytes" << std::endl;
12653
}
12754
}
128-

src/subcommand/fetch_subcommand.hpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <CLI/CLI.hpp>
66

77
#include "../utils/common.hpp"
8-
#include "../wrapper/repository_wrapper.hpp"
98

109
class fetch_subcommand
1110
{
@@ -18,6 +17,3 @@ class fetch_subcommand
1817

1918
std::string m_remote_name;
2019
};
21-
22-
23-

src/subcommand/push_subcommand.cpp

Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,16 @@
33
#include <git2/remote.h>
44

55
#include "../subcommand/push_subcommand.hpp"
6+
#include "../utils/progress.hpp"
67
#include "../wrapper/repository_wrapper.hpp"
7-
#include "../wrapper/remote_wrapper.hpp"
8-
#include "../utils/git_exception.hpp"
9-
#include "../utils/common.hpp"
10-
11-
namespace
12-
{
13-
int push_transfer_progress(unsigned int current, unsigned int total, size_t bytes, void*)
14-
{
15-
if (total > 0)
16-
{
17-
int percent = (100 * current) / total;
18-
std::cout << "Writing objects: " << percent << "% (" << current
19-
<< "/" << total << "), " << bytes << " bytes\r";
20-
}
21-
return 0;
22-
}
23-
24-
int push_update_reference(const char* refname, const char* status, void*)
25-
{
26-
if (status)
27-
{
28-
std::cout << " " << refname << " " << status << std::endl;
29-
}
30-
else
31-
{
32-
std::cout << " " << refname << std::endl;
33-
}
34-
return 0;
35-
}
36-
}
378

389
push_subcommand::push_subcommand(const libgit2_object&, CLI::App& app)
3910
{
4011
auto* sub = app.add_subcommand("push", "Update remote refs along with associated objects");
4112

4213
sub->add_option("<remote>", m_remote_name, "The remote to push to")
4314
->default_val("origin");
44-
15+
4516
sub->add_option("<refspec>", m_refspecs, "The refspec(s) to push");
4617

4718
sub->callback([this]() { this->run(); });
@@ -59,14 +30,25 @@ void push_subcommand::run()
5930
push_opts.callbacks.push_transfer_progress = push_transfer_progress;
6031
push_opts.callbacks.push_update_reference = push_update_reference;
6132

62-
git_strarray_wrapper refspecs_wrapper(m_refspecs);
63-
git_strarray* refspecs_ptr = nullptr;
64-
if (!m_refspecs.empty())
33+
if (m_refspecs.empty())
6534
{
66-
refspecs_ptr = refspecs_wrapper;
35+
try
36+
{
37+
auto head_ref = repo.head();
38+
std::string short_name = head_ref.short_name();
39+
std::string refspec = "refs/heads/" + short_name;
40+
m_refspecs.push_back(refspec);
41+
}
42+
catch (...)
43+
{
44+
std::cerr << "Could not determine current branch to push." << std::endl;
45+
return;
46+
}
6747
}
48+
git_strarray_wrapper refspecs_wrapper(m_refspecs);
49+
git_strarray* refspecs_ptr = nullptr;
50+
refspecs_ptr = refspecs_wrapper;
6851

69-
throw_if_error(git_remote_push(remote, refspecs_ptr, &push_opts));
52+
remote.push(refspecs_ptr, &push_opts);
7053
std::cout << "Pushed to " << remote_name << std::endl;
7154
}
72-

src/subcommand/push_subcommand.hpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include <CLI/CLI.hpp>
77

88
#include "../utils/common.hpp"
9-
#include "../wrapper/repository_wrapper.hpp"
109

1110
class push_subcommand
1211
{
@@ -20,6 +19,3 @@ class push_subcommand
2019
std::string m_remote_name;
2120
std::vector<std::string> m_refspecs;
2221
};
23-
24-
25-

0 commit comments

Comments
 (0)