Commit 7059b51b authored by Waqar Ahmed's avatar Waqar Ahmed Committed by Christoph Cullmann
Browse files

Implement show stash content as diff



With this change you can see the content of the stash. Command used:

`git stash show -p stash@{0}`
Signed-off-by: Waqar Ahmed's avatarWaqar Ahmed <waqar.17a@gmail.com>
parent 0d8ddcee
......@@ -130,6 +130,16 @@ QProcess *GitWidget::gitprocess()
return &git;
}
KTextEditor::MainWindow *GitWidget::mainWindow()
{
return m_mainWin;
}
std::vector<GitWidget::TempFileViewPair> *GitWidget::tempFilesVector()
{
return &m_filesOpenAtHEAD;
}
void GitWidget::getStatus(bool untracked, bool submodules)
{
disconnect(&git, &QProcess::finished, nullptr, nullptr);
......@@ -467,6 +477,7 @@ QMenu *GitWidget::stashMenu()
auto stashUAct = menu->addAction(i18n("Stash (Include Untracked)"));
auto applyStashAct = menu->addAction(i18n("Apply Stash"));
auto dropAct = menu->addAction(i18n("Drop Stash"));
auto showStashAct = menu->addAction(i18n("Show Stash Content"));
connect(stashAct, &QAction::triggered, this, [this] {
StashDialog stashDialog(this, m_mainWin);
......@@ -500,6 +511,10 @@ QMenu *GitWidget::stashMenu()
StashDialog stashDialog(this, m_mainWin);
stashDialog.openDialog(StashDialog::StashApplyLast);
});
connect(showStashAct, &QAction::triggered, this, [this] {
StashDialog stashDialog(this, m_mainWin);
stashDialog.openDialog(StashDialog::ShowStashContent);
});
return menu;
}
......
......@@ -10,6 +10,8 @@
#include <QProcess>
#include <QWidget>
#include <memory>
#include "git/gitstatus.h"
class QTreeView;
......@@ -38,6 +40,10 @@ public:
void getStatus(bool untracked = true, bool submodules = false);
void sendMessage(const QString &message, bool warn);
QProcess *gitprocess();
KTextEditor::MainWindow *mainWindow();
using TempFileViewPair = std::pair<std::unique_ptr<QTemporaryFile>, KTextEditor::View *>;
std::vector<TempFileViewPair> *tempFilesVector();
private:
QToolButton *m_menuBtn;
......@@ -52,7 +58,6 @@ private:
QString m_commitMessage;
KTextEditor::MainWindow *m_mainWin;
QMenu *m_gitMenu;
using TempFileViewPair = std::pair<std::unique_ptr<QTemporaryFile>, KTextEditor::View *>;
std::vector<TempFileViewPair> m_filesOpenAtHEAD;
void buildMenu();
......@@ -64,7 +69,6 @@ private:
void clean(const QStringList &files);
void openAtHEAD(const QString &file);
void showDiff(const QString &file, bool staged);
void stash();
void launchExternalDiffTool(const QString &file, bool staged);
void commitChanges(const QString &msg, const QString &desc, bool signOff);
QMenu *stashMenu();
......
......@@ -8,6 +8,7 @@
#include "gitwidget.h"
#include <QCoreApplication>
#include <QFileInfo>
#include <QKeyEvent>
#include <QLineEdit>
#include <QPainter>
......@@ -15,6 +16,7 @@
#include <QSortFilterProxyModel>
#include <QStandardItemModel>
#include <QStyledItemDelegate>
#include <QTemporaryFile>
#include <QTextDocument>
#include <QTreeView>
#include <QVBoxLayout>
......@@ -194,6 +196,7 @@ void StashDialog::openDialog(StashDialog::Mode m)
case Mode::StashPop:
case Mode::StashDrop:
case Mode::StashApply:
case Mode::ShowStashContent:
m_lineEdit->setPlaceholderText(i18n("Type to filter, Enter to pop stash, Esc to leave."));
m_currentMode = m;
getStashList();
......@@ -273,6 +276,9 @@ void StashDialog::slotReturnPressed()
case Mode::StashDrop:
dropStash(m_treeView->currentIndex().data(StashIndexRole).toByteArray());
break;
case Mode::ShowStashContent:
showStash(m_treeView->currentIndex().data(StashIndexRole).toByteArray());
break;
default:
break;
}
......@@ -320,10 +326,8 @@ void StashDialog::stash(bool keepIndex, bool includeUntracked)
}
disconnect(git, &QProcess::finished, nullptr, nullptr);
git->setArguments(args);
git->start();
connect(git, &QProcess::finished, m_gitwidget, [gitWidget](int exitCode, QProcess::ExitStatus es) {
disconnect(gitWidget->gitprocess(), &QProcess::finished, nullptr, nullptr);
if (es != QProcess::NormalExit || exitCode != 0) {
gitWidget->sendMessage(i18n("Failed to stash changes"), true);
} else {
......@@ -331,6 +335,8 @@ void StashDialog::stash(bool keepIndex, bool includeUntracked)
gitWidget->sendMessage(i18n("Changes stashed successfully."), true);
}
});
git->setArguments(args);
git->start();
}
void StashDialog::getStashList()
......@@ -384,9 +390,6 @@ void StashDialog::popStash(const QByteArray &index, const QString &command)
if (!index.isEmpty()) {
args.append(QString::fromUtf8(index));
}
git->setArguments(args);
git->start();
connect(git, &QProcess::finished, gitWidget, [gitWidget, command](int exitCode, QProcess::ExitStatus es) {
disconnect(gitWidget->gitprocess(), &QProcess::finished, nullptr, nullptr);
......@@ -410,6 +413,8 @@ void StashDialog::popStash(const QByteArray &index, const QString &command)
}
}
});
git->setArguments(args);
git->start();
}
void StashDialog::applyStash(const QByteArray &index)
......@@ -422,6 +427,61 @@ void StashDialog::dropStash(const QByteArray &index)
popStash(index, QStringLiteral("drop"));
}
void StashDialog::showStash(const QByteArray &index)
{
if (index.isEmpty()) {
return;
}
auto git = m_gitwidget->gitprocess();
auto gitWidget = m_gitwidget;
if (!git) {
return;
}
disconnect(git, &QProcess::finished, nullptr, nullptr);
QStringList args{QStringLiteral("stash"), QStringLiteral("show"), QStringLiteral("-p"), QString::fromUtf8(index)};
connect(git, &QProcess::finished, gitWidget, [gitWidget](int exitCode, QProcess::ExitStatus es) {
// sever connection
disconnect(gitWidget->gitprocess(), &QProcess::finished, nullptr, nullptr);
if (es != QProcess::NormalExit || exitCode != 0) {
gitWidget->sendMessage(i18n("Failed to get Diff of file. Error:\n%1", QString::fromUtf8(gitWidget->gitprocess()->readAllStandardError())), true);
} else {
std::unique_ptr<QTemporaryFile> f(new QTemporaryFile);
f->setFileTemplate(QString(QStringLiteral("XXXXXX.diff")));
if (!f->open()) {
return;
}
f->write(gitWidget->gitprocess()->readAllStandardOutput());
f->flush();
const QUrl tempFileUrl(QUrl::fromLocalFile(f->fileName()));
auto v = gitWidget->mainWindow()->openUrl(tempFileUrl);
if (!v || !v->document()) {
return;
}
std::pair<std::unique_ptr<QTemporaryFile>, KTextEditor::View *> tf = std::make_pair(std::move(f), v);
gitWidget->tempFilesVector()->push_back(std::move(tf));
// close temp on document close
auto clearTemp = [gitWidget](KTextEditor::Document *document) {
auto tempFiles = gitWidget->tempFilesVector();
std::remove_if(tempFiles->begin(), tempFiles->end(), [document](const GitWidget::TempFileViewPair &tf) {
if (tf.second->document() == document) {
return true;
}
return false;
});
};
connect(v->document(), &KTextEditor::Document::aboutToClose, gitWidget, clearTemp);
}
});
git->setArguments(args);
git->start();
}
void StashDialog::updateViewGeometry()
{
m_treeView->resizeColumnToContents(0);
......
......@@ -29,7 +29,18 @@ class StashDialog : public QMenu
{
Q_OBJECT
public:
enum Mode { None, Stash, StashKeepIndex, StashUntrackIncluded, StashPopLast, StashPop, StashDrop, StashApply, StashApplyLast };
enum Mode {
None,
Stash,
StashKeepIndex,
StashUntrackIncluded,
StashPopLast,
StashPop,
StashDrop,
StashApply,
StashApplyLast,
ShowStashContent,
};
StashDialog(QWidget *parent, KTextEditor::MainWindow *mainWindow);
......@@ -53,6 +64,7 @@ private:
void popStash(const QByteArray &index, const QString &command = QStringLiteral("pop"));
void applyStash(const QByteArray &index);
void dropStash(const QByteArray &index);
void showStash(const QByteArray &index);
QTreeView *m_treeView;
QLineEdit *m_lineEdit;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment