2019-09-21 21:43:01 +00:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QUrl>
|
2019-10-07 15:59:55 +00:00
|
|
|
#include <QStandardPaths>
|
|
|
|
#include <QDir>
|
2019-09-21 21:43:01 +00:00
|
|
|
|
2019-10-07 15:59:55 +00:00
|
|
|
extern "C" {
|
|
|
|
#include <git2.h>
|
|
|
|
}
|
2019-09-21 21:43:01 +00:00
|
|
|
#include "git.h"
|
2019-10-07 15:59:55 +00:00
|
|
|
#include "passphraseprovider.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Git::Git() {
|
|
|
|
git_libgit2_init();
|
|
|
|
};
|
2019-09-21 21:43:01 +00:00
|
|
|
|
2019-10-07 15:59:55 +00:00
|
|
|
Git::~Git() {
|
|
|
|
git_libgit2_shutdown();
|
|
|
|
};
|
2019-09-21 21:43:01 +00:00
|
|
|
|
2019-10-07 15:59:55 +00:00
|
|
|
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;
|
2019-09-21 21:43:01 +00:00
|
|
|
|
2019-10-07 15:59:55 +00:00
|
|
|
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();
|
2019-09-21 21:43:01 +00:00
|
|
|
|
2019-10-07 15:59:55 +00:00
|
|
|
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;
|
|
|
|
}
|