File indexing completed on 2024-04-28 04:38:52

0001 /*
0002     SPDX-FileCopyrightText: 2013 David E. Narvaez <david@piensalibre.net>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "stashpatchsource.h"
0008 #include "gitplugin.h"
0009 
0010 #include "vcs/dvcs/dvcsjob.h"
0011 #include "interfaces/icore.h"
0012 #include "interfaces/iruncontroller.h"
0013 
0014 #include <QTemporaryFile>
0015 #include <QTextStream>
0016 
0017 using namespace KDevelop;
0018 
0019 StashPatchSource::StashPatchSource(const QString& stashName, GitPlugin* plugin, const QDir & baseDir)
0020  : m_stashName(stashName), m_plugin(plugin), m_baseDir(baseDir)
0021 {
0022     QTemporaryFile tempFile;
0023 
0024     tempFile.setAutoRemove(false);
0025     tempFile.open();
0026     m_patchFile = QUrl::fromLocalFile(tempFile.fileName());
0027 
0028     auto job = qobject_cast<KDevelop::DVcsJob*>(m_plugin->gitStash(m_baseDir, QStringList{QStringLiteral("show"), QStringLiteral("-u"), m_stashName}, KDevelop::OutputJob::Silent));
0029 
0030     connect(job, &DVcsJob::resultsReady, this, &StashPatchSource::updatePatchFile);
0031     KDevelop::ICore::self()->runController()->registerJob(job);
0032 }
0033 
0034 StashPatchSource::~StashPatchSource()
0035 {
0036     QFile::remove(m_patchFile.toLocalFile());
0037 }
0038 
0039 QUrl StashPatchSource::baseDir() const
0040 {
0041     return QUrl::fromLocalFile(m_baseDir.absolutePath());
0042 }
0043 
0044 QUrl StashPatchSource::file() const
0045 {
0046     return m_patchFile;
0047 }
0048 
0049 void StashPatchSource::update()
0050 {
0051 }
0052 
0053 bool StashPatchSource::isAlreadyApplied() const
0054 {
0055     return false;
0056 }
0057 
0058 QString StashPatchSource::name() const
0059 {
0060     return m_stashName;
0061 }
0062 
0063 void StashPatchSource::updatePatchFile(KDevelop::VcsJob* job)
0064 {
0065     auto* dvcsJob = qobject_cast<KDevelop::DVcsJob*>(job);
0066     QFile f(m_patchFile.toLocalFile());
0067     QTextStream txtStream(&f);
0068 
0069     f.open(QIODevice::WriteOnly);
0070     txtStream << dvcsJob->rawOutput();
0071     f.close();
0072 
0073     emit patchChanged();
0074 }
0075 
0076 #include "moc_stashpatchsource.cpp"