1
0
mirror of https://github.com/QRouland/UTPass.git synced 2025-01-26 00:26:40 +00:00
UTPass/plugins/Git/libgit.cpp

90 lines
2.3 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"
2025-01-13 18:11:16 +01:00
#include "utils.h"
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();
}
2025-01-14 08:15:03 +01:00
void LibGit::setMode(mode_type type)
2025-01-13 18:11:16 +01:00
{
this->mode = type;
}
2025-01-14 08:15:03 +01:00
int LibGit::credentialsCB(git_cred **out, const char *url, const char *username_from_url,
2025-01-14 13:05:09 +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 {
2025-01-13 18:11:16 +01:00
[](const Unset & x)
{
qDebug() << "credentials_cb : Unset ";
qWarning() << "credentials_cb : callback should never be call for Unset ";
return (int) GIT_EUSER;
},
2025-01-13 18:11:16 +01:00
[](const HTTP & x)
{
qDebug() << "credentials_cb : HTTP ";
qWarning() << "credentials_cb : callback should never be call for HTTP ";
return (int) GIT_EUSER;
},
2025-01-13 18:11:16 +01:00
[&out, &username_from_url](const HTTPAuth & x)
{
qDebug() << "credentials_cb : HTTPAuth ";
2025-01-13 18:11:16 +01:00
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());
},
2025-01-13 18:11:16 +01:00
[&](const SSHAuth & x)
{
qWarning() << "credentials_cb : SSHAuth to be implemented ";
return (int) GIT_EUSER;
}, // TODO
2025-01-13 18:11:16 +01:00
[&](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-14 08:15:03 +01:00
opts.fetch_opts.callbacks.credentials = *credentialsCB;
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) {
2025-01-13 18:11:16 +01:00
qDebug() << git_error_last()->message;
2025-01-10 21:33:48 +01:00
}
2025-01-10 13:48:38 +01:00
if (repo) {
2025-01-13 18:11:16 +01:00
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
}