69 lines
2.0 KiB
C++
69 lines
2.0 KiB
C++
#ifndef GITPROCESS_H
|
|
#define GITPROCESS_H
|
|
|
|
#include <QProcess>
|
|
#include <QString>
|
|
#include <QStringList>
|
|
#include <QStandardPaths>
|
|
|
|
/**
|
|
* Helper to create a QProcess configured for running git in a given working directory.
|
|
* Sets up SSH_ASKPASS for password prompts (ksshaskpass if available).
|
|
*/
|
|
namespace GitProcess
|
|
{
|
|
|
|
/**
|
|
* Create a QProcess ready to run git with the given arguments in workingDir.
|
|
* The caller owns the returned QProcess.
|
|
*/
|
|
inline QProcess *create(const QString &workingDir, const QStringList &args, QObject *parent = nullptr)
|
|
{
|
|
auto *proc = new QProcess(parent);
|
|
proc->setWorkingDirectory(workingDir);
|
|
proc->setProgram(QStringLiteral("git"));
|
|
proc->setArguments(args);
|
|
|
|
// Set up environment for SSH password prompts
|
|
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
|
|
// ksshaskpass for KDE integration
|
|
const QString ksshaskpass = QStandardPaths::findExecutable(QStringLiteral("ksshaskpass"));
|
|
if (!ksshaskpass.isEmpty()) {
|
|
env.insert(QStringLiteral("SSH_ASKPASS"), ksshaskpass);
|
|
env.insert(QStringLiteral("GIT_ASKPASS"), ksshaskpass);
|
|
}
|
|
// Avoid git pager
|
|
env.insert(QStringLiteral("GIT_TERMINAL_PROMPT"), QStringLiteral("0"));
|
|
proc->setProcessEnvironment(env);
|
|
|
|
return proc;
|
|
}
|
|
|
|
/**
|
|
* Run git synchronously and return stdout. Returns empty QByteArray on failure.
|
|
*/
|
|
inline QByteArray run(const QString &workingDir, const QStringList &args)
|
|
{
|
|
QProcess proc;
|
|
proc.setWorkingDirectory(workingDir);
|
|
proc.setProgram(QStringLiteral("git"));
|
|
proc.setArguments(args);
|
|
|
|
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
|
|
env.insert(QStringLiteral("GIT_TERMINAL_PROMPT"), QStringLiteral("0"));
|
|
proc.setProcessEnvironment(env);
|
|
|
|
proc.start();
|
|
if (!proc.waitForFinished(10000)) {
|
|
return {};
|
|
}
|
|
if (proc.exitCode() != 0) {
|
|
return {};
|
|
}
|
|
return proc.readAllStandardOutput();
|
|
}
|
|
|
|
} // namespace GitProcess
|
|
|
|
#endif // GITPROCESS_H
|