1
0
mirror of https://github.com/QRouland/UTPass.git synced 2025-02-04 12:06:40 +00:00
UTPass/plugins/Git/libgit.cpp

89 lines
2.4 KiB
C++
Raw Normal View History

2025-01-10 13:48:38 +01:00
#include <QUrl>
#include <QDebug>
#include <type_traits>
2025-01-10 13:48:38 +01:00
extern "C" {
#include <git2.h>
}
#include "libgit.h"
template<class... Ts>
struct overload : Ts... {
using Ts::operator()...;
};
template<class... Ts>
overload(Ts...) -> overload<Ts...>;
2025-01-10 13:48:38 +01:00
LibGit::LibGit()
{
git_libgit2_init();
}
2025-01-10 15:28:42 +01:00
LibGit::~LibGit()
{
2025-01-10 13:48:38 +01:00
git_libgit2_shutdown();
}
void LibGit::set_mode(mode_type type) {
this->mode = type;
}
2025-01-10 13:48:38 +01:00
int LibGit::credentials_cb(git_cred **out, const char *url, const char *username_from_url,
2025-01-10 15:28:42 +01:00
unsigned int allowed_types, void *payload)
2025-01-10 13:48:38 +01:00
{
// TODO : More precise Error Handling for UI
auto instance = LibGit::instance();
auto v = overload {
[](const Unset& x) {
qDebug() << "credentials_cb : Unset ";
qWarning() << "credentials_cb : callback should never be call for Unset ";
return (int) GIT_EUSER;
},
[](const HTTP& x) {
qDebug() << "credentials_cb : HTTP ";
qWarning() << "credentials_cb : callback should never be call for HTTP ";
return (int) GIT_EUSER;
},
[&out, &username_from_url](const HTTPAuth& x) {
qDebug() << "credentials_cb : HTTPAuth ";
if(!username_from_url) {
qWarning() << "credentials_cb : no username provided ";
return (int) GIT_EUSER;
}
return git_cred_userpass_plaintext_new(out, username_from_url, x.pass.toLocal8Bit().constData());
},
[&](const SSHAuth& x) {
qWarning() << "credentials_cb : SSHAuth to be implemented ";
return (int) GIT_EUSER;
}, // TODO
[&](const SSHKey& x) {
qWarning() << "credentials_cb : SSHKey to be implemented ";
return (int) GIT_EUSER;
} // TODO
};
return std::visit(v, instance->mode);
2025-01-10 13:48:38 +01:00
}
2025-01-10 15:28:42 +01:00
bool LibGit::clone(QString url, QString path)
{
2025-01-10 13:48:38 +01:00
git_repository *repo = NULL;
git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
2025-01-10 13:48:38 +01:00
opts.fetch_opts.callbacks.credentials = *credentials_cb;
2025-01-10 13:48:38 +01:00
int ret = git_clone(&repo, url.toLocal8Bit().data(), path.toLocal8Bit().data(), &opts);
2025-01-10 21:33:48 +01:00
if (ret != 0) {
qDebug() << git_error_last()->message;
2025-01-10 21:33:48 +01:00
}
2025-01-10 13:48:38 +01:00
if (repo) {
git_repository_free(repo);
2025-01-10 13:48:38 +01:00
}
return ret == 0; // TODO Clean error handling to return specifics errors for the ui
return ret;
2025-01-10 13:48:38 +01:00
}