69 lines
1.9 KiB
C++
69 lines
1.9 KiB
C++
#include <QDebug>
|
|
#include <QUrl>
|
|
#include <QStandardPaths>
|
|
#include <QDir>
|
|
|
|
extern "C" {
|
|
#include <git2.h>
|
|
}
|
|
#include "git.h"
|
|
#include "passphraseprovider.h"
|
|
|
|
|
|
|
|
Git::Git() {
|
|
git_libgit2_init();
|
|
};
|
|
|
|
Git::~Git() {
|
|
git_libgit2_shutdown();
|
|
};
|
|
|
|
bool Git::clone(QUrl url, QString dir_out_path) {
|
|
auto ret = false;
|
|
auto tmp_dir_path = QStandardPaths::writableLocation(
|
|
QStandardPaths::CacheLocation).append("/clone");
|
|
|
|
auto gitCred = new UTGitCredProvider();
|
|
pt2cred_acquire_cb = gitCred->cred_acquire_cb;
|
|
|
|
QDir tmp_dir(tmp_dir_path);
|
|
tmp_dir.removeRecursively();
|
|
|
|
git_repository *cloned_repo = NULL;
|
|
git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT;
|
|
git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;
|
|
|
|
checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
|
|
clone_opts.checkout_opts = checkout_opts;
|
|
clone_opts.fetch_opts.callbacks.credentials = gitCred->*pt2cred_acquire_cb;
|
|
|
|
qDebug() << "Cloning " << url << " in " << tmp_dir_path;
|
|
auto error = git_clone(&cloned_repo, url.toString().toLocal8Bit().constData(), tmp_dir_path.toLocal8Bit().constData(), &clone_opts);
|
|
if (cloned_repo) {
|
|
git_repository_free(cloned_repo);
|
|
}
|
|
if(error) {
|
|
const git_error *err = giterr_last();
|
|
if (err) {
|
|
qDebug() << "ERROR " << err->klass << ": " << err->message;
|
|
}
|
|
else {
|
|
qDebug() << "ERROR " << error << ": no detailed info";
|
|
}
|
|
}
|
|
else {
|
|
qDebug() << "Removing destination";
|
|
QDir dir_out(dir_out_path);
|
|
dir_out.removeRecursively();
|
|
|
|
qDebug() << "Moving cloned dir to destination";
|
|
QDir dir;
|
|
qDebug() << tmp_dir_path << " to " << dir_out_path;
|
|
ret = dir.rename(tmp_dir_path, dir_out_path);
|
|
}
|
|
tmp_dir.removeRecursively();
|
|
delete gitCred;
|
|
return !error and ret;
|
|
}
|