1
0
mirror of https://github.com/QRouland/UTPass.git synced 2025-06-24 22:42:28 +00:00

Initial git clone feature

This commit is contained in:
2025-01-10 13:48:38 +01:00
parent d33932be6d
commit 5e5a092da1
22 changed files with 334 additions and 91 deletions

View File

@ -0,0 +1,37 @@
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(PLUGIN "Git")
set(
SRC
plugin.cpp
libgit.cpp
git.cpp
)
set(CMAKE_AUTOMOC ON)
execute_process(
COMMAND dpkg-architecture -qDEB_HOST_MULTIARCH
OUTPUT_VARIABLE ARCH_TRIPLET
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(ARCH_TRIPLET STREQUAL "")
set(ARCH_TRIPLET x86_64-linux-gnu)
endif()
add_library(${PLUGIN} MODULE ${SRC})
set_target_properties(${PLUGIN} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${PLUGIN})
qt5_use_modules(${PLUGIN} Qml Quick DBus)
add_library(libgit2 SHARED IMPORTED)
set_property(TARGET libgit2 PROPERTY IMPORTED_LOCATION "/usr/lib/${ARCH_TRIPLET}/libgit2.so")
target_link_libraries(${PLUGIN} libgit2)
set(QT_IMPORTS_DIR "/lib/${ARCH_TRIPLET}")
install(TARGETS ${PLUGIN} DESTINATION ${QT_IMPORTS_DIR}/${PLUGIN}/)
install(FILES qmldir DESTINATION ${QT_IMPORTS_DIR}/${PLUGIN}/)

18
plugins/Git/git.cpp Normal file
View File

@ -0,0 +1,18 @@
#include <QUrl>
#include <QtCore/QDir>
#include <QDebug>
#include "git.h"
#include "libgit.h"
Git::Git()
{}
bool Git::clone(QString url, QString path)
{
qInfo() << "Cloning " << url << "password_store to " << path;
QDir dir(path);
dir.removeRecursively(); // TODO see if we delete only after sucessfull clone / Will be change anyway when will add update etc..
return LibGit::instance()->clone(url, path);
}

20
plugins/Git/git.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef GIT_H
#define GIT_H
#include <QUrl>
#include <QObject>
class Git : public QObject
{
Q_OBJECT
public:
Git();
~Git() override = default;
Q_INVOKABLE bool clone(QString url, QString path);
// Q_INVOKABLE bool update(QUrl url, QString path);
};
#endif

51
plugins/Git/libgit.cpp Normal file
View File

@ -0,0 +1,51 @@
#include <QUrl>
#include <QDebug>
extern "C" {
#include <git2.h>
}
#include "libgit.h"
LibGit::LibGit()
{
git_libgit2_init();
}
LibGit::~LibGit() {
git_libgit2_shutdown();
}
int LibGit::credentials_cb(git_cred **out, const char *url, const char *username_from_url,
unsigned int allowed_types, void *payload)
{
int error;
const char *user, *pass;
/*
* Ask the user via the UI. On error, store the information and return GIT_EUSER which will be
* bubbled up to the code performing the fetch or push. Using GIT_EUSER allows the application
* to know it was an error from the application instead of libgit2.
*/
// if ((error = ask_user(&user, &pass, url, username_from_url, allowed_types)) < 0) {
// store_error(error);
// return GIT_EUSER;
// }
// user = "user";
// pass = "pass";
// return git_cred_userpass_plaintext_new(out, user, pass);
return GIT_EUSER;
}
bool LibGit::clone(QString url, QString path) {
qDebug("yo");
git_repository *repo = NULL;
git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
opts.fetch_opts.callbacks.credentials = *credentials_cb;
int ret = git_clone(&repo, url.toLocal8Bit().data(), path.toLocal8Bit().data(), &opts);
if (repo) {
git_repository_free(repo);
}
return ret == 0; // TODO Clean error handling to return specifics errors for the ui
}

32
plugins/Git/libgit.h Normal file
View File

@ -0,0 +1,32 @@
#ifndef LIBGIT_H
#define LIBGIT_H
#include <QObject>
#include <QUrl>
extern "C" {
#include <git2/transport.h>
}
#include <memory>
class LibGit
{
private:
LibGit();
static int credentials_cb(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);
};
#endif

10
plugins/Git/plugin.cpp Normal file
View File

@ -0,0 +1,10 @@
#include <QtQml>
#include "plugin.h"
#include "git.h"
void GitPlugin::registerTypes(const char *uri)
{
//@uri Git
qmlRegisterSingletonType<Git>(uri, 1, 0, "Git", [](QQmlEngine *, QJSEngine *) -> QObject * { return new Git; });
}

16
plugins/Git/plugin.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef GITPLUGIN_H
#define GITPLUGIN_H
#include <QQmlExtensionPlugin>
class GitPlugin : public QQmlExtensionPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID
"org.qt-project.Qt.QQmlExtensionInterface")
public:
void registerTypes(const char *uri) override;
};
#endif

2
plugins/Git/qmldir Normal file
View File

@ -0,0 +1,2 @@
module Git
plugin Git