File indexing completed on 2025-01-05 05:14:45

0001 /*
0002 SPDX-FileCopyrightText: 2021 Hamed Masafi <hamed.masfi@gmail.com>
0003 
0004 SPDX-License-Identifier: GPL-3.0-or-later
0005 */
0006 
0007 #include "gitglobal.h"
0008 
0009 #include <QDebug>
0010 #include <QIcon>
0011 #include <QProcess>
0012 
0013 namespace Git
0014 {
0015 
0016 void run(const QString &workingDir, const QStringList &args)
0017 {
0018     QProcess p;
0019     p.setProgram(QStringLiteral("git"));
0020     p.setArguments(args);
0021     p.setWorkingDirectory(workingDir);
0022     p.start();
0023     p.waitForFinished();
0024 }
0025 void run(const QString &workingDir, const AbstractCommand &cmd)
0026 {
0027     QProcess p;
0028     p.setProgram(QStringLiteral("git"));
0029     p.setArguments(cmd.generateArgs());
0030     p.setWorkingDirectory(workingDir);
0031     p.start();
0032     p.waitForFinished();
0033 }
0034 
0035 QByteArray runGit(const QString &workingDir, const QStringList &args)
0036 {
0037     //    qCDebug(KOMMITLIB_LOG).noquote() << "Running: git " << args.join(" ");
0038 
0039     QProcess p;
0040     p.setProgram(QStringLiteral("git"));
0041     p.setArguments(args);
0042     p.setWorkingDirectory(workingDir);
0043     p.start();
0044     p.waitForFinished();
0045     auto out = p.readAllStandardOutput();
0046     //    auto err = p.readAllStandardError();
0047 
0048     return out; // + err;
0049 }
0050 
0051 QStringList readAllNonEmptyOutput(const QString &workingDir, const QStringList &cmd, bool trim)
0052 {
0053     QStringList list;
0054     const auto out = QString(runGit(workingDir, cmd)).split(QLatin1Char('\n'));
0055 
0056     for (const auto &line : out) {
0057         QString b;
0058 
0059         if (trim)
0060             b = line.trimmed();
0061         else
0062             b = line;
0063 
0064         if (b.isEmpty())
0065             continue;
0066 
0067         list.append(b);
0068     }
0069     return list;
0070 }
0071 
0072 QIcon statusIcon(ChangeStatus status)
0073 {
0074     switch (status) {
0075     case ChangeStatus::Added:
0076         return QIcon::fromTheme(QStringLiteral("git-status-added"));
0077     case ChangeStatus::Ignored:
0078         return QIcon::fromTheme(QStringLiteral("git-status-ignored"));
0079     case ChangeStatus::Modified:
0080         return QIcon::fromTheme(QStringLiteral("git-status-modified"));
0081     case ChangeStatus::Removed:
0082         return QIcon::fromTheme(QStringLiteral("git-status-removed"));
0083     case ChangeStatus::Renamed:
0084         return QIcon::fromTheme(QStringLiteral("git-status-renamed"));
0085         //    case ChangeStatus::Unknown:
0086     case ChangeStatus::Untracked:
0087         return QIcon::fromTheme(QStringLiteral("git-status-unknown"));
0088     case ChangeStatus::Copied:
0089     case ChangeStatus::UpdatedButInmerged:
0090     case ChangeStatus::Unmodified:
0091         return QIcon::fromTheme(QStringLiteral("git-status-update"));
0092     case ChangeStatus::Unknown:
0093         return {};
0094     default:
0095         qWarning() << "Unknown icon" << (int)status;
0096     }
0097     return QIcon::fromTheme(QStringLiteral("git-status-update"));
0098 }
0099 
0100 } // namespace Git