2023-07-10 15:57:17 +02:00
|
|
|
#include "git_client.h"
|
|
|
|
#include "update.h"
|
2023-07-14 13:16:01 +02:00
|
|
|
#include "worker.h"
|
2023-08-02 15:17:10 +02:00
|
|
|
#include "utils.h"
|
2023-07-10 15:57:17 +02:00
|
|
|
|
|
|
|
#include <QRegularExpression>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QDir>
|
|
|
|
|
|
|
|
|
2023-07-17 16:49:44 +02:00
|
|
|
GitClient::GitClient(QString const &customerNrStr,
|
|
|
|
QString const &customerRepository,
|
2023-07-14 13:16:01 +02:00
|
|
|
QString const &workingDirectory,
|
|
|
|
QString const &branchName,
|
|
|
|
QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
, m_worker(qobject_cast<Worker *>(parent))
|
2023-07-17 16:49:44 +02:00
|
|
|
, m_repositoryPath(QString("https://git.mimbach49.de/GerhardHoffmann/%1.git").arg(customerNrStr))
|
|
|
|
, m_customerNr(customerNrStr)
|
2023-07-14 13:16:01 +02:00
|
|
|
, m_workingDirectory(workingDirectory)
|
|
|
|
, m_branchName(branchName)
|
2023-07-17 16:49:44 +02:00
|
|
|
, m_customerRepository(customerRepository) {
|
2023-07-14 13:16:01 +02:00
|
|
|
if (!m_worker) {
|
|
|
|
qCritical() << "ERROR CASTING PARENT TO WORKER FAILED";
|
|
|
|
}
|
2023-07-10 15:57:17 +02:00
|
|
|
}
|
|
|
|
|
2023-07-14 13:16:01 +02:00
|
|
|
bool GitClient::gitCloneCustomerRepository() {
|
2023-07-10 15:57:17 +02:00
|
|
|
QString gitCommand("git clone ");
|
2023-07-14 13:16:01 +02:00
|
|
|
gitCommand += m_repositoryPath;
|
2023-07-10 15:57:17 +02:00
|
|
|
Command c(gitCommand);
|
2023-07-14 13:16:01 +02:00
|
|
|
|
|
|
|
qInfo() << "IN CURRENT WD" << m_workingDirectory
|
|
|
|
<< "CLONE" << m_repositoryPath << "...";
|
|
|
|
|
|
|
|
if (c.execute(m_workingDirectory)) { // execute the command in wd
|
2023-07-19 16:50:54 +02:00
|
|
|
QString const result = c.getCommandResult();
|
2023-07-10 15:57:17 +02:00
|
|
|
if (!result.isEmpty()) {
|
|
|
|
// Cloning into 'customer_281'...\n
|
|
|
|
static QRegularExpression re("(^\\s*Cloning\\s+into\\s+[']\\s*)(.*)(\\s*['].*$)");
|
|
|
|
QRegularExpressionMatch match = re.match(result);
|
|
|
|
if (match.hasMatch()) {
|
|
|
|
if (re.captureCount() == 3) { // start with full match (0), then the other 3 matches
|
2023-07-17 16:49:44 +02:00
|
|
|
if (match.captured(2).trimmed() == m_customerNr) {
|
2023-07-14 13:16:01 +02:00
|
|
|
qInfo() << "CLONING" << m_repositoryPath << "OK";
|
|
|
|
return true;
|
|
|
|
}
|
2023-07-10 15:57:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-19 16:50:54 +02:00
|
|
|
qCritical() << "ERROR CLONE RESULT HAS WRONG FORMAT. CLONE_RESULT="
|
|
|
|
<< result;
|
2023-07-10 15:57:17 +02:00
|
|
|
}
|
2023-07-14 13:16:01 +02:00
|
|
|
return false;
|
2023-07-10 15:57:17 +02:00
|
|
|
}
|
|
|
|
|
2023-07-14 13:16:01 +02:00
|
|
|
bool GitClient::copyGitConfigFromMaster() { // only allowed when called in
|
2023-07-17 16:49:44 +02:00
|
|
|
// master branch (???)
|
2023-07-14 13:16:01 +02:00
|
|
|
if (QDir(m_customerRepository).exists()) {
|
|
|
|
QString const cp = QString("cp .gitconfig .git/config");
|
|
|
|
Command c("bash");
|
|
|
|
if (c.execute(m_customerRepository, QStringList() << "-c" << cp)) {
|
|
|
|
qInfo() << "cp .gitconfig .git/config OK";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
qCritical() << "ERROR cp .gitconfig .git/config";
|
|
|
|
}
|
|
|
|
return false;
|
2023-07-10 15:57:17 +02:00
|
|
|
}
|
|
|
|
|
2023-08-02 15:17:10 +02:00
|
|
|
QStringList GitClient::gitBranchNames() {
|
|
|
|
// git config --global pager.branch false
|
|
|
|
QStringList bNames;
|
|
|
|
if (QDir(m_customerRepository).exists()) {
|
|
|
|
QString gitCommand("git branch -a");
|
|
|
|
Command c(gitCommand);
|
|
|
|
if (c.execute(m_customerRepository)) {
|
|
|
|
QString const result = c.getCommandResult();
|
|
|
|
return result.split('\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bNames;
|
|
|
|
}
|
|
|
|
|
2023-07-14 13:16:01 +02:00
|
|
|
bool GitClient::gitCheckoutBranch() {
|
2023-08-02 15:17:10 +02:00
|
|
|
// TODO: nachsehen, ob der Branch ueberhaupt existiert
|
|
|
|
|
2023-07-14 13:16:01 +02:00
|
|
|
if (QDir(m_customerRepository).exists()) {
|
2023-08-02 15:17:10 +02:00
|
|
|
int zoneNr = Utils::read1stLineOfFile("/etc/zone_nr");
|
|
|
|
m_branchName = (zoneNr != 0)
|
|
|
|
? QString("zg1/zone%1").arg(zoneNr) : "master";
|
|
|
|
|
2023-07-14 13:16:01 +02:00
|
|
|
QString gitCommand("git checkout ");
|
|
|
|
gitCommand += m_branchName;
|
2023-08-02 15:17:10 +02:00
|
|
|
|
2023-07-14 13:16:01 +02:00
|
|
|
Command c(gitCommand);
|
|
|
|
return c.execute(m_customerRepository); // execute command in customerRepo
|
|
|
|
}
|
|
|
|
qCritical() << "ERROR" << m_customerRepository << "DOES NOT EXIST";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GitClient::gitCloneAndCheckoutBranch() {
|
|
|
|
qInfo() << "CLONE" << m_repositoryPath << "AND CHECKOUT" << m_branchName;
|
|
|
|
if (gitCloneCustomerRepository()) {
|
2023-07-17 16:49:44 +02:00
|
|
|
//if (copyGitConfigFromMaster()) {
|
2023-07-19 16:50:54 +02:00
|
|
|
if (gitCheckoutBranch()) {
|
|
|
|
return true;
|
|
|
|
} else {
|
2023-08-02 15:17:10 +02:00
|
|
|
// TODO
|
|
|
|
// m_worker->terminateUpdateProcess();
|
2023-07-19 16:50:54 +02:00
|
|
|
}
|
2023-07-17 16:49:44 +02:00
|
|
|
//}
|
2023-07-19 16:50:54 +02:00
|
|
|
} else {
|
2023-08-02 15:17:10 +02:00
|
|
|
// TODO
|
|
|
|
//m_worker->terminateUpdateProcess();
|
2023-07-10 15:57:17 +02:00
|
|
|
}
|
2023-07-14 13:16:01 +02:00
|
|
|
return false;
|
2023-07-10 15:57:17 +02:00
|
|
|
}
|
|
|
|
|
2023-07-14 13:16:01 +02:00
|
|
|
/*
|
|
|
|
Zu beachten: wird eine datei neu hinzugefuegt (git add/commit) dann aber gleich
|
|
|
|
wieder geloscht, so wird sie im diff nicht angezeigt.
|
|
|
|
*/
|
2023-07-10 15:57:17 +02:00
|
|
|
std::optional<QStringList> GitClient::gitDiff(QString const &commits) {
|
2023-07-14 13:16:01 +02:00
|
|
|
if (QDir(m_customerRepository).exists()) {
|
|
|
|
// 409f198..6c22726
|
|
|
|
QString gitCommand("git diff --compact-summary ");
|
|
|
|
gitCommand += commits;
|
2023-07-10 15:57:17 +02:00
|
|
|
|
2023-07-14 13:16:01 +02:00
|
|
|
Command c(gitCommand);
|
|
|
|
if (c.execute(m_customerRepository)) { // execute command in local customerRepo
|
|
|
|
QString s = c.getCommandResult().trimmed();
|
|
|
|
QStringList lines = Update::split(s, '\n');
|
|
|
|
QStringList fileNames;
|
|
|
|
// each line has the format "etc/psa_config/DC2C_print01.json | 1 +
|
|
|
|
// or the format "etc/psa_config/DC2C_print01.json (new) | 1 +
|
|
|
|
// the filenames are relativ to the repository
|
|
|
|
for (int i = 0; i < lines.size(); ++i) {
|
|
|
|
// TODO: koennte auch (delete) kommen ?
|
|
|
|
int newIndex = lines.at(i).indexOf("(new)"); // for new files
|
|
|
|
// int goneIndex = lines.at(i).indexOf("(gone)"); // for removed files
|
|
|
|
if (newIndex != -1) {
|
|
|
|
QString fileName = lines.at(i).mid(0, newIndex).trimmed();
|
2023-07-10 15:57:17 +02:00
|
|
|
fileNames << fileName;
|
2023-07-14 13:16:01 +02:00
|
|
|
} else {
|
|
|
|
int pipeIndex = lines.at(i).indexOf('|');
|
|
|
|
if (pipeIndex != -1) {
|
|
|
|
QString fileName = lines.at(i).mid(0, pipeIndex).trimmed();
|
|
|
|
fileNames << fileName;
|
|
|
|
}
|
2023-07-10 15:57:17 +02:00
|
|
|
}
|
|
|
|
}
|
2023-07-14 13:16:01 +02:00
|
|
|
if (!fileNames.isEmpty()) {
|
|
|
|
return fileNames;
|
|
|
|
}
|
2023-07-10 15:57:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2023-07-14 13:16:01 +02:00
|
|
|
/*
|
|
|
|
Hat sich nichts geaendert, so werden auch keine Commits <>..<> angezeigt
|
|
|
|
*/
|
2023-07-10 15:57:17 +02:00
|
|
|
std::optional<QString> GitClient::gitFetch() {
|
2023-07-17 16:49:44 +02:00
|
|
|
if (QDir(m_customerRepository).exists()) {
|
2023-08-02 15:17:10 +02:00
|
|
|
|
|
|
|
// TODO
|
|
|
|
UpdateStatus status (UPDATE_STATUS::GIT_FETCH_UPDATES, "GIT FETCH UPDATES");
|
|
|
|
qInfo() << status;
|
|
|
|
|
|
|
|
//emit m_worker->sendCmdEventToIsmas(
|
|
|
|
// m_worker->getIsmasClient().updateOfPSAContinues(
|
|
|
|
// "GIT FETCH UPDATES", status.m_statusDescription));
|
|
|
|
|
2023-07-14 13:16:01 +02:00
|
|
|
Command c("git fetch");
|
2023-07-17 16:49:44 +02:00
|
|
|
if (c.execute(m_customerRepository)) {
|
2023-07-14 13:16:01 +02:00
|
|
|
QString const s = c.getCommandResult().trimmed();
|
|
|
|
if (!s.isEmpty()) {
|
|
|
|
QStringList lines = Update::split(s, '\n');
|
|
|
|
if (!lines.empty()) {
|
|
|
|
// 409f198..6c22726 zg1/zone1 -> origin/zg1/zone1
|
|
|
|
static QRegularExpression re("(^\\s*)([0-9A-Fa-f]+..[0-9A-Fa-f]+)(.*$)");
|
|
|
|
QRegularExpressionMatch match = re.match(lines.last());
|
|
|
|
if (match.hasMatch()) {
|
|
|
|
if (re.captureCount() == 3) { // start with full match (0), then the other 3 matches
|
2023-08-02 15:17:10 +02:00
|
|
|
// TODO
|
|
|
|
status = UpdateStatus(UPDATE_STATUS::GIT_FETCH_UPDATES_REQUEST_SUCCESS,
|
|
|
|
QString("GIT SUCCESSFULLY FETCHED ") + m_customerRepository);
|
|
|
|
qInfo() << status;
|
|
|
|
|
|
|
|
//emit m_worker->sendCmdEventToIsmas(
|
|
|
|
// m_worker->getIsmasClient().updateOfPSAContinues(
|
|
|
|
// "GIT_FETCH_UPDATES SUCCESS", status.m_statusDescription));
|
|
|
|
|
2023-07-14 13:16:01 +02:00
|
|
|
return match.captured(2);
|
|
|
|
} else {
|
|
|
|
qCritical() << "ERROR WRONG CAPTURE COUNT FOR 'GIT FETCH'" << re.captureCount();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
qCritical() << "ERROR NO MATCH OF COMMITS FOR 'GIT FETCH'";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
qCritical() << "ERROR WRONG FORMAT FOR RESULT FOR 'GIT FETCH'" << s;
|
2023-07-10 15:57:17 +02:00
|
|
|
}
|
2023-07-14 13:16:01 +02:00
|
|
|
} else {
|
|
|
|
qCritical() << "ERROR EMPTY RESULT FROM 'GIT FETCH'";
|
2023-07-10 15:57:17 +02:00
|
|
|
}
|
|
|
|
}
|
2023-07-14 13:16:01 +02:00
|
|
|
} else {
|
2023-07-17 16:49:44 +02:00
|
|
|
qCritical() << "ERROR" << m_customerRepository << "DOES NOT EXIST";
|
2023-07-10 15:57:17 +02:00
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GitClient::gitFetchAndDiff() {
|
|
|
|
if (gitFetch()) {
|
|
|
|
QString gitCommand("git diff --compact-summary HEAD..FETCH_HEAD");
|
|
|
|
Command c(gitCommand);
|
|
|
|
return c.execute(m_workingDirectory);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GitClient::gitPull() {
|
2023-07-14 13:16:01 +02:00
|
|
|
if (QDir(m_customerRepository).exists()) {
|
|
|
|
Command c("git pull");
|
|
|
|
if (c.execute(m_customerRepository)) {
|
|
|
|
qInfo() << "PULLED INTO" << m_customerRepository;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
qCritical() << "PULL INTO" << m_customerRepository << "FAILED";
|
|
|
|
}
|
|
|
|
return false;
|
2023-07-10 15:57:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<QStringList> GitClient::gitMerge() {
|
|
|
|
Command c("git merge");
|
|
|
|
if (c.execute(m_workingDirectory)) {
|
|
|
|
QString s = c.getCommandResult();
|
|
|
|
QStringList lst = Update::split(s, '\n');
|
|
|
|
return lst;
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2023-07-14 13:16:01 +02:00
|
|
|
|
|
|
|
QString GitClient::gitLastCommit(QString fileName) {
|
|
|
|
if (QDir(m_customerRepository).exists()) {
|
|
|
|
QString const filePath
|
|
|
|
= QDir::cleanPath(m_customerRepository + QDir::separator() + fileName);
|
|
|
|
QString const gitCommand = QString("git log %1 | head -n 1").arg(fileName);
|
|
|
|
Command c("bash");
|
|
|
|
if (c.execute(m_customerRepository, QStringList() << "-c" << gitCommand)) {
|
|
|
|
QString const r = c.getCommandResult();
|
|
|
|
int const idx = r.indexOf("commit ");
|
|
|
|
if (idx != -1) {
|
|
|
|
return r.mid(idx + 8).trimmed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2023-07-19 16:50:54 +02:00
|
|
|
// fileName has to an absolute path
|
2023-07-14 13:16:01 +02:00
|
|
|
QString GitClient::gitBlob(QString fileName) {
|
2023-07-19 16:50:54 +02:00
|
|
|
QFileInfo fi(fileName);
|
|
|
|
if (fi.exists()) {
|
2023-07-14 13:16:01 +02:00
|
|
|
QString const gitCommand = QString("git hash-object %1").arg(fileName);
|
|
|
|
Command c(gitCommand);
|
2023-07-19 16:50:54 +02:00
|
|
|
if (c.execute(m_workingDirectory)) {
|
|
|
|
return c.getCommandResult().trimmed();
|
2023-07-14 13:16:01 +02:00
|
|
|
}
|
|
|
|
}
|
2023-07-19 16:50:54 +02:00
|
|
|
return "N/A";
|
2023-07-14 13:16:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString GitClient::gitCommitForBlob(QString blob) {
|
|
|
|
if (QDir(m_customerRepository).exists()) {
|
|
|
|
QString const gitCommand
|
|
|
|
= QString("git whatchanged --all --find-object=%1 | head -n 1").arg(blob);
|
|
|
|
Command c(gitCommand);
|
|
|
|
if (c.execute(m_customerRepository)) {
|
|
|
|
return c.getCommandResult();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GitClient::gitIsFileTracked(QString fName) {
|
|
|
|
if (QDir(m_customerRepository).exists()) {
|
|
|
|
QString const gitCommand
|
|
|
|
= QString("git ls-files --error-unmatch %1").arg(fName);
|
|
|
|
Command c(gitCommand);
|
|
|
|
return c.execute(m_customerRepository);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//get_commit_for_blob () {
|
|
|
|
// # search for the blob in all commits for the file(name) $1
|
|
|
|
// echo $(git log --all --pretty=format:%H -- $2 |
|
|
|
|
// xargs -I{} bash -c "git ls-tree {} -- $2 |
|
|
|
|
// grep -q $1 && echo -n {} && head -n 1")
|
|
|
|
//}
|