1
0
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:
2025-01-17 10:40:54 +01:00
parent beaad58af2
commit c0757da47b
26 changed files with 805 additions and 385 deletions

View 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
View 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

View 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
View 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