-
Notifications
You must be signed in to change notification settings - Fork 2
/
GitEnvironment.cpp
59 lines (43 loc) · 1.8 KB
/
GitEnvironment.cpp
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
#include "GitEnvironment.h"
#include <git2.h>
using GitEnvironment = Git::GitEnvironment;
void GitEnvironment::init(const std::string& path)
{
git_repository* repository = nullptr;
git_repository_init_options options;
checkLibGitError( git_repository_init_init_options(&options, GIT_REPOSITORY_INIT_OPTIONS_VERSION) );
options.flags |= GIT_REPOSITORY_INIT_MKPATH;
checkLibGitError(git_repository_init_ext(&repository, path.c_str(), &options));
git_repository_free(repository);
}
void GitEnvironment::clone(const std::string& pathToRepo
, const std::string& url
, Authenticable::AuthenticationCallback getUserCredentialsCallback /* = nullptr */
)
{
git_clone_options options;
checkLibGitError( git_clone_init_options(&options, GIT_CLONE_OPTIONS_VERSION) );
options.checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
if(nullptr != getUserCredentialsCallback)
{
auto credentialCallback = getCredentialsCallbackWrapper(getUserCredentialsCallback);
options.fetch_opts.callbacks.credentials = credentialCallback;
options.fetch_opts.callbacks.payload = &getUserCredentialsCallback;
}
git_repository* repository = nullptr;
checkLibGitError( git_clone(&repository, url.c_str(), pathToRepo.c_str(), &options) );
git_repository_free(repository);
}
bool GitEnvironment::isRepository(const std::string& path) const
{
git_buf out = { nullptr, 0, 0};
int error = git_repository_discover(&out, path.c_str(), 1, path.c_str()); // stores the found repository
std::size_t usedMemory = out.size;
git_buf_free(&out);
if(GIT_ENOTFOUND == error)
{
return false;
}
checkLibGitError(error);
return usedMemory > 0;
}