| 1 |
#pragma once |
| 2 |
|
| 3 |
#include <git2.h> |
| 4 |
#include "Repository.h" |
| 5 |
#include "CredentialManager.h" |
| 6 |
#include "GitException.h" |
| 7 |
#include <wx/uri.h> |
| 8 |
#include <fmt/format.h> |
| 9 |
|
| 10 |
namespace vcs |
| 11 |
{ |
| 12 |
|
| 13 |
namespace git |
| 14 |
{ |
| 15 |
|
| 16 |
class Remote final |
| 17 |
{ |
| 18 |
private: |
| 19 |
git_remote* _remote; |
| 20 |
|
| 21 |
public: |
| 22 |
using Ptr = std::shared_ptr<Remote>; |
| 23 |
|
| 24 |
Remote(git_remote* remote) : |
| 25 |
_remote(remote) |
| 26 |
{} |
| 27 |
|
| 28 |
~Remote() |
| 29 |
{ |
| 30 |
git_remote_free(_remote); |
| 31 |
} |
| 32 |
|
| 33 |
void fetch() |
| 34 |
{ |
| 35 |
if (!_remote) |
| 36 |
{ |
| 37 |
throw GitException("Not a valid remote"); |
| 38 |
} |
| 39 |
|
| 40 |
auto url = wxURI(git_remote_url(_remote)); |
| 41 |
|
| 42 |
git_fetch_options options = GIT_FETCH_OPTIONS_INIT; |
| 43 |
|
| 44 |
// We will be asked for credentials when the server asks libgit |
| 45 |
options.callbacks.credentials = AcquireCredentials; |
| 46 |
options.callbacks.payload = this; |
| 47 |
|
| 48 |
auto remoteName = git_remote_name(_remote); |
| 49 |
|
| 50 |
rMessage() << "Fetching from remote " << remoteName << std::endl; |
| 51 |
|
| 52 |
auto error = git_remote_fetch(_remote, nullptr, &options, "fetch"); |
| 53 |
GitException::ThrowOnError(error); |
| 54 |
|
| 55 |
rMessage() << "Fetch complete" << std::endl; |
| 56 |
} |
| 57 |
|
| 58 |
void push(const Reference& ref) |
| 59 |
{ |
| 60 |
git_push_options options = GIT_PUSH_OPTIONS_INIT; |
| 61 |
auto refName = ref.getName(); |
| 62 |
char* refNamePtr = refName.data(); |
| 63 |
|
| 64 |
const git_strarray refspecs = { |
| 65 |
&refNamePtr, |
| 66 |
1 |
| 67 |
}; |
| 68 |
|
| 69 |
auto url = wxURI(git_remote_url(_remote)); |
| 70 |
|
| 71 |
// We will be asked for credentials when the server asks libgit |
| 72 |
options.callbacks.credentials = AcquireCredentials; |
| 73 |
options.callbacks.payload = this; |
| 74 |
|
| 75 |
auto remoteName = git_remote_name(_remote); |
| 76 |
rMessage() << "Pushing to remote " << remoteName << std::endl; |
| 77 |
|
| 78 |
auto error = git_remote_push(_remote, &refspecs, &options); |
| 79 |
GitException::ThrowOnError(error); |
| 80 |
|
| 81 |
rMessage() << "Push complete" << std::endl; |
| 82 |
} |
| 83 |
|
| 84 |
static Ptr CreateFromName(Repository& repository, const std::string& name) |
| 85 |
{ |
| 86 |
git_remote* remote; |
| 87 |
auto error = git_remote_lookup(&remote, repository._get(), name.c_str()); |
| 88 |
|
| 89 |
GitException::ThrowOnError(error); |
| 90 |
|
| 91 |
return std::make_shared<Remote>(remote); |
| 92 |
} |
| 93 |
|
| 94 |
private: |
| 95 |
|
| 96 |
// Compatibility hack: In Ubuntu 20 we only have older libgit2 versions, |
| 97 |
// where git_credential was still called git_cred, map them |
| 98 |
#if LIBGIT2_VER_MAJOR < 1 |
| 99 |
#define git_credential git_cred |
| 100 |
#define git_credential_userpass_plaintext_new git_cred_userpass_plaintext_new |
| 101 |
#endif |
| 102 |
|
| 103 |
static int AcquireCredentials(git_cred** out, const char* url, const char* username_from_url, unsigned int allowed_types, void* payload) |
| 104 |
{ |
| 105 |
*out = GetCredentialsForRemote(url); |
| 106 |
return *out == nullptr ? GIT_PASSTHROUGH : 0; |
| 107 |
} |
| 108 |
|
| 109 |
static git_credential* GetCredentialsForRemote(const std::string& remoteUrl) |
| 110 |
{ |
| 111 |
wxURI uri(remoteUrl); |
| 112 |
|
| 113 |
// Create the git:scheme://server string to query the credential manager |
| 114 |
auto credentialResource = fmt::format("git:{0}://{1}", uri.GetScheme().ToStdString(), uri.GetServer().ToStdString()); |
| 115 |
|
| 116 |
auto userAndPass = CredentialManager::RetrievePassword(credentialResource); |
| 117 |
|
| 118 |
if (!userAndPass.first.empty() && !userAndPass.second.empty()) |
| 119 |
{ |
| 120 |
rMessage() << "Found credentials for resource " << credentialResource << " in the credential store" << std::endl; |
| 121 |
|
| 122 |
git_credential* credentials = nullptr; |
| 123 |
|
| 124 |
auto error = git_credential_userpass_plaintext_new(&credentials, userAndPass.first.c_str(), userAndPass.second.c_str()); |
| 125 |
GitException::ThrowOnError(error); |
| 126 |
|
| 127 |
// Clear out the password |
| 128 |
memset(userAndPass.second.data(), '\0', userAndPass.second.length()); |
| 129 |
|
| 130 |
return credentials; |
| 131 |
} |
| 132 |
|
| 133 |
return nullptr; |
| 134 |
} |
| 135 |
}; |
| 136 |
|
| 137 |
} |
| 138 |
|
| 139 |
} |