mirror of
https://github.com/QRouland/UTPass.git
synced 2025-06-24 22:42:28 +00:00
Refactor cloning feature and ui
This commit is contained in:
@ -4,9 +4,10 @@ set(PLUGIN "Git")
|
||||
set(
|
||||
SRC
|
||||
plugin.cpp
|
||||
libgit.cpp
|
||||
git.cpp
|
||||
utils.h
|
||||
jobs/clonejob.cpp
|
||||
jobs/gitjob.cpp
|
||||
)
|
||||
|
||||
set(CMAKE_AUTOMOC ON)
|
||||
|
@ -2,76 +2,68 @@
|
||||
#include <QtCore/QDir>
|
||||
#include <QDebug>
|
||||
#include <QStandardPaths>
|
||||
|
||||
extern "C" {
|
||||
#include <git2.h>
|
||||
}
|
||||
|
||||
#include "git.h"
|
||||
#include "libgit.h"
|
||||
#include "utils.h"
|
||||
#include "jobs/clonejob.h"
|
||||
#include "jobs/gitjob.h"
|
||||
|
||||
|
||||
QDir Git::cloneSetup()
|
||||
Git::Git():
|
||||
m_sem(std::unique_ptr<QSemaphore>(new QSemaphore(1)))
|
||||
{
|
||||
QDir tmp_dir(QStandardPaths::writableLocation( QStandardPaths::CacheLocation).append("/clone"));
|
||||
git_libgit2_init();
|
||||
}
|
||||
|
||||
tmp_dir.removeRecursively();
|
||||
qDebug() << "Temp dir path is " << tmp_dir.absolutePath();
|
||||
|
||||
return tmp_dir;
|
||||
Git::~Git()
|
||||
{
|
||||
git_libgit2_shutdown();
|
||||
}
|
||||
|
||||
|
||||
bool Git::cloneTearDown(QDir tmp_dir)
|
||||
bool Git::clone(QString url, QString path, cred_type mode)
|
||||
{
|
||||
return tmp_dir.removeRecursively();
|
||||
}
|
||||
|
||||
bool Git::moveToDestination(QString path, QDir tmp_dir)
|
||||
{
|
||||
qDebug() << "Removing password_store " << path;
|
||||
QDir destination_dir(path);
|
||||
destination_dir.removeRecursively();
|
||||
|
||||
qDebug() << "Moving cloned content to destination dir";
|
||||
QDir dir;
|
||||
qDebug() << tmp_dir.absolutePath() << " to " << destination_dir.absolutePath();
|
||||
return dir.rename(tmp_dir.absolutePath(), destination_dir.absolutePath()); // TODO Better error handling
|
||||
}
|
||||
|
||||
bool Git::clone(QString url, QString path, mode_type mode) //, GitPlugin::RepoType type, QString pass)
|
||||
{
|
||||
auto v = overload {
|
||||
[](const Unset & x) { return "Unset"; },
|
||||
[](const HTTP & x) { return "HTTP"; },
|
||||
[](const HTTPAuth & x) { return "HTTPAuth"; },
|
||||
[](const SSHAuth & x) { return "SSHAuth"; },
|
||||
[](const SSHKey & x) { return "SSHKey"; },
|
||||
};
|
||||
qInfo() << "Cloning " << url << " to destination " << path << " using " << std::visit(v, mode);
|
||||
|
||||
LibGit::instance()->setMode(mode);
|
||||
auto tmp_dir = this->cloneSetup();
|
||||
|
||||
qDebug() << "Cloning " << url << " to tmp dir " << tmp_dir.absolutePath();
|
||||
auto ret = LibGit::instance()->clone(url, tmp_dir.absolutePath()); // TODO Better error handling
|
||||
|
||||
if (ret) {
|
||||
this->moveToDestination(path, tmp_dir);
|
||||
if (!this->m_sem->tryAcquire(1, 500)) {
|
||||
qWarning() << "Can acquire git semaphore a command is already running ";
|
||||
return false;
|
||||
}
|
||||
|
||||
this->cloneTearDown(tmp_dir);
|
||||
LibGit::instance()->setMode(Unset());
|
||||
|
||||
return ret ;
|
||||
auto v = overload {
|
||||
[](const HTTP & x) { return "HTTP"; },
|
||||
[](const HTTPUserPass & x) { return "HTTPAuth"; },
|
||||
[](const SSHPass & x) { return "SSHAuth"; },
|
||||
[](const SSHKey & x) { return "SSHKey"; },
|
||||
};
|
||||
qDebug() << "Creating clone Job " << url << " " << path << " " << std::visit(v, mode);
|
||||
CloneJob *clone_job = new CloneJob(url, path, mode);
|
||||
connect(clone_job, &CloneJob::resultReady, this, &Git::cloneResult);
|
||||
connect(clone_job, &CloneJob::finished, clone_job, &QObject::deleteLater);
|
||||
clone_job->start();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Git::cloneHttp(QString url, QString path)
|
||||
{
|
||||
qInfo() << "Call clone command Http " << url << " " << path;
|
||||
HTTP mode = {};
|
||||
return this->clone(url, path, mode);
|
||||
}
|
||||
|
||||
bool Git::cloneHttpPass(QString url, QString path, QString pass)
|
||||
{
|
||||
HTTPAuth mode = { pass };
|
||||
qInfo() << "Call clone command HttpPass " << url << " " << path;
|
||||
HTTPUserPass mode = { pass };
|
||||
return this->clone(url, path, mode);
|
||||
}
|
||||
|
||||
void Git::cloneResult(const bool err)
|
||||
{
|
||||
|
||||
if (err) {
|
||||
emit cloneFailed(); // TODO error message
|
||||
} else {
|
||||
emit cloneSucceed();
|
||||
}
|
||||
this->m_sem->release();
|
||||
}
|
||||
|
@ -1,35 +1,112 @@
|
||||
#ifndef GIT_H
|
||||
#define GIT_H
|
||||
|
||||
#include "jobs/gitjob.h"
|
||||
#include <QUrl>
|
||||
#include <QObject>
|
||||
#include <QSemaphore>
|
||||
#include <QtCore/QDir>
|
||||
|
||||
#include "libgit.h"
|
||||
|
||||
/**
|
||||
* @brief The Git class is class that provide Git functionnly to clone and update a repo.
|
||||
* @class Git
|
||||
* @brief A class that provides Git functionality for cloning and updating repositories.
|
||||
*
|
||||
* The `Git` class provides a set of methods to do Git operation on remote URLs.
|
||||
*/
|
||||
class Git : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
private:
|
||||
QDir cloneSetup();
|
||||
bool moveToDestination(QString path, QDir tmp_dir);
|
||||
bool cloneTearDown(QDir tmp_dir);
|
||||
bool clone(QString url, QString path, mode_type mode);
|
||||
private slots:
|
||||
/**
|
||||
* @brief Slot that handles the result of a cloning operation.
|
||||
*
|
||||
* This slot is connected to the result of the cloning operation and is triggered when the cloning
|
||||
* process finishes. It emits the appropriate signal based on whether the clone operation succeeded
|
||||
* or failed.
|
||||
*
|
||||
* @param err A boolean indicating whether an error occurred during cloning. `true` if the clone failed,
|
||||
* `false` if it succeeded.
|
||||
*/
|
||||
void cloneResult(const bool err);
|
||||
|
||||
signals:
|
||||
/**
|
||||
* @brief Signal emitted when the cloning operation succeeds.
|
||||
*
|
||||
* This signal is emitted when the Git repository is successfully cloned.
|
||||
*/
|
||||
void cloneSucceed();
|
||||
|
||||
/**
|
||||
* @brief Signal emitted when the cloning operation fails.
|
||||
*
|
||||
* This signal is emitted when an error occurs during the cloning operation.
|
||||
*/
|
||||
void cloneFailed();
|
||||
|
||||
private:
|
||||
std::unique_ptr<QSemaphore> m_sem; /**< Semaphore for managing concurrent operations. */
|
||||
|
||||
/**
|
||||
* @brief Clones a repository from a specified URL.
|
||||
*
|
||||
* This private method initiates the cloning job. It sets up the repository cloning process based on
|
||||
* the specified URL, destination path, and cloning mode (HTTP or SSH).
|
||||
*
|
||||
* @param url The URL of the Git repository to clone.
|
||||
* @param path The destination path for the cloned repository.
|
||||
* @param mode The cloning mode, such as HTTP or SSH (represented as `cred_type`).
|
||||
* @return `true` if the cloning process was successful, `false` otherwise.
|
||||
*/
|
||||
bool clone(QString url, QString path, cred_type mode);
|
||||
|
||||
public:
|
||||
Git() = default;
|
||||
~Git() override = default;
|
||||
/**
|
||||
* @brief Constructor for the Git class.
|
||||
*
|
||||
* Initializes the `Git` class, setting up necessary resources such as the semaphore for concurrent operation management.
|
||||
*/
|
||||
Git();
|
||||
|
||||
/**
|
||||
* @brief Destructor for the Git class.
|
||||
*
|
||||
* Cleans up any resources used by the `Git` class, ensuring that no ongoing operations or resources are left hanging.
|
||||
*/
|
||||
~Git() override;
|
||||
|
||||
/**
|
||||
* @brief Clones a repository over HTTP.
|
||||
*
|
||||
* This method clones a Git repository from the specified HTTP URL and saves it to the given destination path.
|
||||
* It is a wrapper around the private `clone()` method, specifying the HTTP cloning mode.
|
||||
*
|
||||
* @param url The HTTP URL of the Git repository to clone.
|
||||
* @param path The destination path for the cloned repository.
|
||||
* @return `true` if the clone operation was successful, `false` otherwise.
|
||||
*/
|
||||
Q_INVOKABLE bool cloneHttp(QString url, QString path);
|
||||
|
||||
/**
|
||||
* @brief Clones a repository over HTTP with a password for authentication.
|
||||
*
|
||||
* This method clones a Git repository from the specified HTTP URL using the provided password for authentication,
|
||||
* and saves it to the given destination path.
|
||||
*
|
||||
* @param url The HTTP URL of the Git repository to clone.
|
||||
* @param path The destination path for the cloned repository.
|
||||
* @param pass The password used for HTTP authentication.
|
||||
* @return `true` if the clone operation was successful, `false` otherwise.
|
||||
*/
|
||||
Q_INVOKABLE bool cloneHttpPass(QString url, QString path, QString pass);
|
||||
|
||||
// Future SSH support methods:
|
||||
// Q_INVOKABLE bool clone_ssh_pass(QString url, QString path, QString pass);
|
||||
// Q_INVOKABLE bool clone_ssh_key(QString url, QString path, QString pub_key, QString priv_key, QString passphrase);
|
||||
|
||||
// Q_INVOKABLE bool update(QUrl url, QString path);
|
||||
// ....
|
||||
};
|
||||
|
||||
#endif
|
||||
|
79
plugins/Git/jobs/clonejob.cpp
Normal file
79
plugins/Git/jobs/clonejob.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
#include <QStandardPaths>
|
||||
#include <QDir>
|
||||
#include <QUrl>
|
||||
#include <QDebug>
|
||||
#include <QObject>
|
||||
#include <type_traits>
|
||||
extern "C" {
|
||||
#include <git2.h>
|
||||
}
|
||||
|
||||
#include "clonejob.h"
|
||||
|
||||
CloneJob::CloneJob(QString url, QString path, cred_type cred):
|
||||
GitJob(cred),
|
||||
m_url(url),
|
||||
m_path(path)
|
||||
{
|
||||
this->setObjectName("CloneJob");
|
||||
}
|
||||
|
||||
|
||||
void CloneJob::run() {
|
||||
auto tmp_dir = this->cloneSetup();
|
||||
auto err = this->clone(this->m_url, tmp_dir.absolutePath(), this->m_cred, this->credentialsCB);
|
||||
if (err) {
|
||||
this->moveToDestination(tmp_dir, this->m_path);
|
||||
}
|
||||
this->cloneTearDown(tmp_dir);
|
||||
|
||||
emit resultReady(err); // TODO Clean error handling to return specifics errors for the ui
|
||||
}
|
||||
|
||||
|
||||
QDir CloneJob::cloneSetup()
|
||||
{
|
||||
QDir tmp_dir(QStandardPaths::writableLocation( QStandardPaths::CacheLocation).append("/clone"));
|
||||
|
||||
tmp_dir.removeRecursively();
|
||||
qDebug() << "Temp dir path is " << tmp_dir.absolutePath();
|
||||
|
||||
return tmp_dir;
|
||||
}
|
||||
|
||||
|
||||
bool CloneJob::cloneTearDown(QDir tmp_dir)
|
||||
{
|
||||
return tmp_dir.removeRecursively();
|
||||
}
|
||||
|
||||
bool CloneJob::moveToDestination(QDir tmp_dir, QString path)
|
||||
{
|
||||
qDebug() << "Removing password_store " << path;
|
||||
QDir destination_dir(path);
|
||||
destination_dir.removeRecursively();
|
||||
|
||||
qDebug() << "Moving cloned content to destination dir";
|
||||
QDir dir;
|
||||
qDebug() << tmp_dir.absolutePath() << " to " << destination_dir.absolutePath();
|
||||
return dir.rename(tmp_dir.absolutePath(), destination_dir.absolutePath()); // TODO Better error handling
|
||||
}
|
||||
|
||||
bool CloneJob::clone(QString url, QString path, cred_type cred, git_cred_acquire_cb cb)
|
||||
{
|
||||
git_repository *repo = NULL;
|
||||
git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
|
||||
|
||||
opts.fetch_opts.callbacks.credentials = cb;
|
||||
opts.fetch_opts.callbacks.payload = &cred;
|
||||
|
||||
int ret = git_clone(&repo, url.toLocal8Bit().data(), path.toLocal8Bit().data(), &opts);
|
||||
if (ret != 0) {
|
||||
qDebug() << git_error_last()->message;
|
||||
}
|
||||
if (repo) {
|
||||
git_repository_free(repo);
|
||||
}// TODO Better error handling
|
||||
return ret != 0;
|
||||
}
|
||||
|
111
plugins/Git/jobs/clonejob.h
Normal file
111
plugins/Git/jobs/clonejob.h
Normal file
@ -0,0 +1,111 @@
|
||||
#ifndef CLONEJOB_H
|
||||
#define CLONEJOB_H
|
||||
|
||||
#include <QDir>
|
||||
extern "C" {
|
||||
#include <git2.h>
|
||||
}
|
||||
#include "gitjob.h"
|
||||
|
||||
/**
|
||||
* @class CloneJob
|
||||
* @brief A class to handle cloning Git repositories in a separate thread.
|
||||
*
|
||||
* The CloneJob runs a cloning process in a separate thread and emits
|
||||
* signals to indicate the success or failure of the operation.
|
||||
*/
|
||||
class CloneJob : public GitJob
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
/**
|
||||
* @brief The main function that performs the cloning operation.
|
||||
*
|
||||
* This function overrides the `run()` method from the `QThread` class. It is
|
||||
* executed when the thread is started, and it handles the process of cloning
|
||||
* a repository from the specified URL to the target path.
|
||||
*/
|
||||
void run() override;
|
||||
|
||||
signals:
|
||||
/**
|
||||
* @brief Signal emitted when the cloning operation is complete.
|
||||
*
|
||||
* This signal is emitted once the cloning operation finishes. It notifies
|
||||
* the caller whether an error occurred during the cloning process.
|
||||
*
|
||||
* @param err A boolean indicating whether an error occurred during cloning.
|
||||
* `true` if an error occurred, `false` if the clone was successful.
|
||||
*/
|
||||
void resultReady(const bool err);
|
||||
|
||||
private:
|
||||
QString m_url; ///< The URL of the Git repository to clone.
|
||||
QString m_path; ///< The destination path for the cloned repository.
|
||||
|
||||
/**
|
||||
* @brief Prepares the temporary directory for cloning.
|
||||
*
|
||||
* This method sets up the required directory structure for cloning a repository.
|
||||
* It is called before the cloning process begins to ensure that a suitable
|
||||
* location exists for the repository to be cloned into.
|
||||
*
|
||||
* @return A `QDir` object representing the prepared temporary directory.
|
||||
*/
|
||||
static QDir cloneSetup();
|
||||
|
||||
/**
|
||||
* @brief Moves the cloned repository to the specified destination.
|
||||
*
|
||||
* After the repository has been cloned into a temporary directory, this method
|
||||
* moves it to the final destination specified by the user.
|
||||
*
|
||||
* @param path The destination path to move the repository to.
|
||||
* @param tmp_dir The temporary directory where the repository was cloned.
|
||||
* @return `true` if the move was successful, `false` otherwise.
|
||||
*/
|
||||
static bool moveToDestination(QDir tmp_dir, QString path);
|
||||
|
||||
/**
|
||||
* @brief Tears down the temporary directory after cloning.
|
||||
*
|
||||
* This method is called to clean up the temporary directory after the repository
|
||||
* has been cloned and moved to the final destination. It removes the temporary
|
||||
* directory and all its contents.
|
||||
*
|
||||
* @param tmp_dir The temporary directory to tear down.
|
||||
* @return `true` if the teardown was successful, `false` otherwise.
|
||||
*/
|
||||
static bool cloneTearDown(QDir tmp_dir);
|
||||
|
||||
/**
|
||||
* @brief Clones a repository from a specified URL.
|
||||
*
|
||||
* This method handles the actual cloning process by setting up a temporary
|
||||
* directory, cloning the repository into it, and then moving it to the final
|
||||
* destination. It uses the provided credentials to authenticate the cloning
|
||||
* operation.
|
||||
*
|
||||
* @param url The URL of the Git repository to clone.
|
||||
* @param path The destination path for the cloned repository.
|
||||
* @param cred The credentials to use for the cloning operation.
|
||||
* @param cb The callback function for acquiring credentials during cloning.
|
||||
* @return `true` if the cloning process was successful, `false` otherwise.
|
||||
*/
|
||||
static bool clone(QString url, QString path, cred_type cred, git_cred_acquire_cb cb);
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief Constructor for the CloneJob class.
|
||||
*
|
||||
* Initializes the CloneJob with the specified repository URL, destination path,
|
||||
* and credentials.
|
||||
*
|
||||
* @param url The URL of the Git repository to clone.
|
||||
* @param path The destination path where the repository will be cloned.
|
||||
* @param cred The credentials to be used for the cloning process.
|
||||
*/
|
||||
CloneJob(QString url, QString path, cred_type cred);
|
||||
};
|
||||
|
||||
#endif // CLONEJOB_H
|
53
plugins/Git/jobs/gitjob.cpp
Normal file
53
plugins/Git/jobs/gitjob.cpp
Normal file
@ -0,0 +1,53 @@
|
||||
#include <QDebug>
|
||||
|
||||
#include "gitjob.h"
|
||||
#include "../utils.h"
|
||||
|
||||
extern "C" {
|
||||
#include <git2.h>
|
||||
}
|
||||
|
||||
GitJob::GitJob(cred_type cred) :
|
||||
m_cred(cred)
|
||||
{
|
||||
git_libgit2_init();
|
||||
}
|
||||
|
||||
GitJob::~GitJob()
|
||||
{
|
||||
git_libgit2_shutdown();
|
||||
}
|
||||
|
||||
int GitJob::credentialsCB(git_cred **out, const char *url, const char *username_from_url,
|
||||
unsigned int allowed_types, void *payload)
|
||||
{
|
||||
cred_type *cred = (cred_type *)payload;
|
||||
auto v = overload {
|
||||
[](const HTTP & x)
|
||||
{
|
||||
qDebug() << "credentialsCB : HTTP ";
|
||||
qWarning() << "credentialsCB : callback should never be call for HTTP ";
|
||||
return (int) GIT_EUSER;
|
||||
},
|
||||
[&out, &username_from_url](const HTTPUserPass & x)
|
||||
{
|
||||
qDebug() << "credentialsCB : HTTPUserPass ";
|
||||
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 SSHPass & 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, *cred);
|
||||
}
|
84
plugins/Git/jobs/gitjob.h
Normal file
84
plugins/Git/jobs/gitjob.h
Normal file
@ -0,0 +1,84 @@
|
||||
#ifndef GITJOB_H
|
||||
#define GITJOB_H
|
||||
|
||||
#include "qthread.h"
|
||||
extern "C" {
|
||||
#include <git2.h>
|
||||
}
|
||||
#include <variant>
|
||||
|
||||
// Forward declarations for the different credential types.
|
||||
struct HTTP { };
|
||||
struct HTTPUserPass {
|
||||
QString pass; ///< Password for HTTP user authentication.
|
||||
};
|
||||
struct SSHPass { };
|
||||
struct SSHKey { };
|
||||
|
||||
/**
|
||||
* @brief Variant type to represent various types of credentials.
|
||||
*
|
||||
* This type is used to store one of the following credential types:
|
||||
* - HTTP
|
||||
* - HTTPUserPass
|
||||
* - SSHPass
|
||||
* - SSHKey
|
||||
*/
|
||||
typedef std::variant<HTTP, HTTPUserPass, SSHPass, SSHKey> cred_type;
|
||||
|
||||
/**
|
||||
* @class GitJob
|
||||
* @brief A class that manages Git-related tasks using libgit2.
|
||||
*
|
||||
* The GitJob class is used to perform Git operations, such as cloning repositories,
|
||||
* in a separate thread. It utilizes libgit2 for interacting with Git repositories.
|
||||
* The class handles credentials for repository access and allows the user to specify
|
||||
* the type of credentials to use.
|
||||
*/
|
||||
class GitJob : public QThread
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
protected:
|
||||
cred_type m_cred; ///< The credentials used for Git operations.
|
||||
|
||||
/**
|
||||
* @brief Callback function for handling Git credentials during cloning.
|
||||
*
|
||||
* This function is called by libgit2 to handle credentials for cloning a repository.
|
||||
* The callback is invoked when Git needs to authenticate with a remote repository.
|
||||
*
|
||||
* @param out Pointer to the credentials object that will be populated by the callback.
|
||||
* @param url The URL of the repository being cloned.
|
||||
* @param username_from_url The username extracted from the URL (if applicable).
|
||||
* @param allowed_types A bitmask of the allowed types of credentials that can be used.
|
||||
* @param payload User-defined data passed to the callback. This is typically the instance
|
||||
* of the class providing the callback, or other user-defined data.
|
||||
*
|
||||
* @return A status code indicating success (0) or failure (non-zero).
|
||||
* @see git_cred
|
||||
* @see git_cred_type
|
||||
*/
|
||||
static int credentialsCB(git_cred **out, const char *url, const char *username_from_url,
|
||||
unsigned int allowed_types, void *payload);
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief Constructor for the GitJob class.
|
||||
*
|
||||
* Initializes the GitJob instance with the given credentials.
|
||||
*
|
||||
* @param cred The credentials to be used for the Git operation. This can be one of
|
||||
* the following types: HTTP, HTTPUserPass, SSHPass, or SSHKey.
|
||||
*/
|
||||
GitJob(cred_type cred);
|
||||
|
||||
/**
|
||||
* @brief Destructor for the GitJob class.
|
||||
*
|
||||
* Cleans up any resources used by the GitJob.
|
||||
*/
|
||||
~GitJob();
|
||||
};
|
||||
|
||||
#endif // GITJOB_H
|
@ -1,89 +0,0 @@
|
||||
#include <QUrl>
|
||||
#include <QDebug>
|
||||
|
||||
#include <type_traits>
|
||||
extern "C" {
|
||||
#include <git2.h>
|
||||
}
|
||||
|
||||
#include "libgit.h"
|
||||
#include "utils.h"
|
||||
|
||||
|
||||
|
||||
LibGit::LibGit()
|
||||
{
|
||||
git_libgit2_init();
|
||||
}
|
||||
|
||||
LibGit::~LibGit()
|
||||
{
|
||||
git_libgit2_shutdown();
|
||||
}
|
||||
|
||||
void LibGit::setMode(mode_type type)
|
||||
{
|
||||
this->mode = type;
|
||||
}
|
||||
|
||||
int LibGit::credentialsCB(git_cred **out, const char *url, const char *username_from_url,
|
||||
unsigned int allowed_types, void *payload)
|
||||
{
|
||||
// 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);
|
||||
}
|
||||
|
||||
|
||||
bool LibGit::clone(QString url, QString path)
|
||||
{
|
||||
git_repository *repo = NULL;
|
||||
git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
|
||||
|
||||
opts.fetch_opts.callbacks.credentials = *credentialsCB;
|
||||
|
||||
int ret = git_clone(&repo, url.toLocal8Bit().data(), path.toLocal8Bit().data(), &opts);
|
||||
if (ret != 0) {
|
||||
qDebug() << git_error_last()->message;
|
||||
}
|
||||
if (repo) {
|
||||
git_repository_free(repo);
|
||||
}
|
||||
return ret == 0; // TODO Clean error handling to return specifics errors for the ui
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
@ -1,46 +0,0 @@
|
||||
#ifndef LIBGIT_H
|
||||
#define LIBGIT_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QUrl>
|
||||
#include <git2/clone.h>
|
||||
extern "C" {
|
||||
#include <git2/transport.h>
|
||||
}
|
||||
#include <memory>
|
||||
#include <variant>
|
||||
|
||||
struct Unset { };
|
||||
struct HTTP { };
|
||||
struct HTTPAuth {
|
||||
QString pass;
|
||||
};
|
||||
struct SSHAuth { };
|
||||
struct SSHKey { };
|
||||
typedef std::variant<Unset, HTTP, HTTPAuth, SSHAuth, SSHKey> mode_type;
|
||||
|
||||
class LibGit
|
||||
{
|
||||
private:
|
||||
LibGit();
|
||||
|
||||
mode_type mode;
|
||||
|
||||
static int credentialsCB(git_cred **out, const char *url, const char *username_from_url,
|
||||
unsigned int allowed_types, void *payload);
|
||||
|
||||
public:
|
||||
~LibGit();
|
||||
static std::shared_ptr<LibGit> instance()
|
||||
{
|
||||
static std::shared_ptr<LibGit> s{new LibGit};
|
||||
return s;
|
||||
}
|
||||
LibGit(LibGit const &) = delete;
|
||||
void operator=(LibGit const &) = delete;
|
||||
|
||||
bool clone(QString url, QString path);
|
||||
void setMode(mode_type type);
|
||||
};
|
||||
|
||||
#endif
|
@ -1,10 +1,18 @@
|
||||
#ifndef UTILS_H
|
||||
#define UTILS_H
|
||||
|
||||
/**
|
||||
* @brief A utility structure for enabling function overloading with template-based classes.
|
||||
* see : https://stackoverflow.com/a/64018031
|
||||
*/
|
||||
template<class... Ts>
|
||||
struct overload : Ts... {
|
||||
using Ts::operator()...;
|
||||
};
|
||||
/**
|
||||
* @brief Deduction guide for the `overload` template.
|
||||
* see : https://stackoverflow.com/a/64018031
|
||||
*/
|
||||
template<class... Ts>
|
||||
overload(Ts...) -> overload<Ts...>;
|
||||
|
||||
|
Reference in New Issue
Block a user