File indexing completed on 2024-04-14 05:19:08

0001 /*
0002  *   SPDX-FileCopyrightText: 2019-2020 Aditya Mehra <aix.m@outlook.com>
0003  *
0004  *   SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include "processcommander.h"
0008 #include <QVariant>
0009 #include <QTextCodec>
0010 #include <QEventLoop>
0011 #include <QTimer>
0012 
0013 ProcessCommand::ProcessCommand(QObject *parent)
0014     : QObject(parent),
0015       ExecRemoteCmd(new QProcess(this)),
0016       ExecLocalCmd(new QProcess(this)),
0017       ExecSafeList(new QProcess(this))
0018 {
0019     connect(&ExecRemoteCmd, SIGNAL(readyReadStandardOutput()), this, SLOT(readFromStdOutRemote()));
0020     connect(&ExecLocalCmd, SIGNAL(readyReadStandardOutput()), this, SLOT(readFromStdOutLocal()));
0021 }
0022 
0023 void ProcessCommand::processRemoteGit(const QString url, const QString branch)
0024 {
0025     QString args_url = "grep";
0026     if(ExecRemoteCmd.state() == QProcess::Running){
0027         ExecRemoteCmd.kill();
0028     }
0029     ExecRemoteCmd.start("git", {"ls-remote", url, "|", args_url, branch, "|", "cut", "-f", "1"});
0030 }
0031 
0032 void ProcessCommand::processLocalGit(const QString skillpath)
0033 {
0034     QString args_url = skillpath + "/";
0035     ExecLocalCmd.setWorkingDirectory(args_url);
0036     ExecSafeList.startDetached("git", {"config", "--global", "--add", "safe.directory", args_url});
0037     if(ExecLocalCmd.state() == QProcess::Running){
0038         ExecLocalCmd.kill();
0039     }
0040     ExecLocalCmd.start("git", {"rev-parse", "HEAD"});
0041 }
0042 
0043 void ProcessCommand::readFromStdOutRemote()
0044 {
0045     QByteArray result = ExecRemoteCmd.readAllStandardOutput();
0046     QVariant output = result;
0047     if(output.toString().contains("\t")){
0048         QStringList qoutput = output.toString().split("\t");
0049         m_commitIdRemote = qoutput[0];
0050         if(m_commitIdRemote != ""){
0051             emit processCommandResultCompleted();
0052         }
0053     } else {
0054         emit processCommandResultCompleted();
0055     }
0056 }
0057 
0058 void ProcessCommand::readFromStdOutLocal()
0059 {
0060     QByteArray result = ExecLocalCmd.readAllStandardOutput();
0061     QVariant output = result;
0062     if(output.toString().contains("\n")){
0063         QStringList qoutput = output.toString().split("\n");
0064         m_commitIdLocal = qoutput[0];
0065     } else {
0066         emit processCommandResultCompleted();
0067     }
0068 }
0069 
0070 QString ProcessCommand::getRemoteCommitId()
0071 {
0072     return m_commitIdRemote;
0073 }
0074 
0075 QString ProcessCommand::getLocalCommitId()
0076 {
0077     return m_commitIdLocal;
0078 }
0079 
0080 ProcessCommander::ProcessCommander(QObject *parent) : QObject(parent)
0081 {
0082     m_processCommand.moveToThread(&mThread);
0083     connect(this, &ProcessCommander::processRemoteGit, &m_processCommand, &ProcessCommand::processRemoteGit);
0084     connect(this, &ProcessCommander::processLocalGit, &m_processCommand, &ProcessCommand::processLocalGit);
0085 
0086     mThread.start();
0087 }
0088 
0089 bool ProcessCommander::runUpdateCheck(const QString url, const QString branch, const QString skillpath)
0090 {
0091     QEventLoop loop;
0092     loop.connect(&m_processCommand, SIGNAL(processCommandResultCompleted()), SLOT(quit()));
0093     emit processRemoteGit(url, branch);
0094     emit processLocalGit(skillpath);
0095     QTimer::singleShot(3000, &loop, SLOT(quit()));
0096     loop.exec();
0097     QString remoteId = m_processCommand.getRemoteCommitId();
0098     QString localId = m_processCommand.getLocalCommitId();
0099     bool result = compareCommits(remoteId, localId);
0100     return result;
0101 }
0102 
0103 bool ProcessCommander::compareCommits(const QString remoteId, const QString localId)
0104 {
0105     if(remoteId == localId){
0106         return false;
0107     } else if(remoteId != "" && localId != "") {
0108         return true;
0109     } else {
0110         return false;
0111     }
0112 }
0113 
0114 ProcessCommander::~ProcessCommander()
0115 {
0116     mThread.exit();
0117     mThread.wait();
0118 }