Skip to content

Commit d290f44

Browse files
committed
initial implementation of Remote::fetch, only few options are supported
1 parent 1fb9e6a commit d290f44

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

include/git2cpp/remote.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
#include <memory>
44

55
struct git_remote;
6+
struct git_oid;
7+
struct git_transfer_progress;
8+
struct git_cred;
69

710
namespace git
811
{
@@ -11,6 +14,17 @@ namespace git
1114
const char * url() const;
1215
const char * pushurl() const;
1316

17+
struct FetchCallbacks
18+
{
19+
virtual void update_tips(char const * refname, git_oid const & a, git_oid const & b) {}
20+
virtual void sideband_progress(char const * str, int len) {}
21+
virtual void transfer_progress(git_transfer_progress const &) {}
22+
23+
virtual git_cred* acquire_cred(const char * url, const char * username_from_url, unsigned int allowed_types) = 0;
24+
};
25+
26+
void fetch(FetchCallbacks &, char const * reflog_message = nullptr);
27+
1428
private:
1529
friend struct Repository;
1630

src/remote.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,41 @@ namespace git
1414
return git_remote_pushurl(remote_.get());
1515
}
1616

17+
void Remote::fetch(FetchCallbacks & callbacks, char const * reflog_message)
18+
{
19+
git_fetch_options opts = GIT_FETCH_OPTIONS_INIT;
20+
opts.callbacks.payload = &callbacks;
21+
22+
opts.callbacks.update_tips = [] (char const * refname, git_oid const * a, git_oid const * b, void * data)
23+
{
24+
auto callbacks = static_cast<FetchCallbacks*>(data);
25+
callbacks->update_tips(refname, *a, *b);
26+
return 0;
27+
};
28+
opts.callbacks.sideband_progress = [] (char const * str, int len, void * data)
29+
{
30+
auto callbacks = static_cast<FetchCallbacks*>(data);
31+
callbacks->sideband_progress(str, len);
32+
return 0;
33+
};
34+
opts.callbacks.transfer_progress = [] (git_transfer_progress const * stats, void * data)
35+
{
36+
auto callbacks = static_cast<FetchCallbacks*>(data);
37+
callbacks->transfer_progress(*stats);
38+
return 0;
39+
};
40+
opts.callbacks.credentials = [] (git_cred ** out, char const *url, char const * user_from_url, unsigned int allowed_types, void * data)
41+
{
42+
auto callbacks = static_cast<FetchCallbacks*>(data);
43+
auto cred = callbacks->acquire_cred(url, user_from_url, allowed_types);
44+
if (!cred)
45+
return -1;
46+
*out = cred;
47+
return 0;
48+
};
49+
git_remote_fetch(remote_.get(), nullptr, &opts, reflog_message);
50+
}
51+
1752
void Remote::Destroy::operator()(git_remote * remote) const
1853
{
1954
git_remote_free(remote);

0 commit comments

Comments
 (0)