forked from qt-creator/qt-creator
DiffEditor/VCS: Save document before applying/reverting patch chunk
Fixes: QTCREATORBUG-22506 Change-Id: I646f24068c0c81890f36052537320a743fdeb498 Reviewed-by: André Hartmann <aha_1980@gmx.de> Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
committed by
Orgad Shaneh
parent
7edf743583
commit
f696d1e6cf
@@ -38,13 +38,15 @@ void PatchTool::setPatchCommand(const FilePath &newCommand)
|
||||
s->endGroup();
|
||||
}
|
||||
|
||||
bool PatchTool::confirmPatching(QWidget *parent, PatchAction patchAction)
|
||||
bool PatchTool::confirmPatching(QWidget *parent, PatchAction patchAction, bool isModified)
|
||||
{
|
||||
const QString title = patchAction == PatchAction::Apply ? Tr::tr("Apply Chunk")
|
||||
: Tr::tr("Revert Chunk");
|
||||
const QString question = patchAction == PatchAction::Apply
|
||||
QString question = patchAction == PatchAction::Apply
|
||||
? Tr::tr("Would you like to apply the chunk?")
|
||||
: Tr::tr("Would you like to revert the chunk?");
|
||||
if (isModified)
|
||||
question += "\n" + Tr::tr("Note: The file will be saved before this operation.");
|
||||
return QMessageBox::question(parent, title, question, QMessageBox::Yes | QMessageBox::No)
|
||||
== QMessageBox::Yes;
|
||||
}
|
||||
|
@@ -20,7 +20,7 @@ public:
|
||||
static Utils::FilePath patchCommand();
|
||||
static void setPatchCommand(const Utils::FilePath &newCommand);
|
||||
|
||||
static bool confirmPatching(QWidget *parent, PatchAction patchAction);
|
||||
static bool confirmPatching(QWidget *parent, PatchAction patchAction, bool isModified);
|
||||
|
||||
// Utility to run the 'patch' command
|
||||
static bool runPatch(const QByteArray &input, const Utils::FilePath &workingDirectory = {},
|
||||
|
@@ -134,9 +134,6 @@ void DiffEditorWidgetController::patch(PatchAction patchAction, int fileIndex, i
|
||||
if (!chunkExists(fileIndex, chunkIndex))
|
||||
return;
|
||||
|
||||
if (!PatchTool::confirmPatching(m_diffEditorWidget, patchAction))
|
||||
return;
|
||||
|
||||
const FileData fileData = m_contextFileData.at(fileIndex);
|
||||
const QString fileName = patchAction == PatchAction::Apply
|
||||
? fileData.fileInfo[LeftSide].fileName
|
||||
@@ -150,7 +147,17 @@ void DiffEditorWidgetController::patch(PatchAction patchAction, int fileIndex, i
|
||||
: m_document->baseDirectory();
|
||||
const FilePath absFilePath = workingDirectory.resolvePath(fileName).absoluteFilePath();
|
||||
|
||||
auto textDocument = qobject_cast<TextEditor::TextDocument *>(
|
||||
DocumentModel::documentForFilePath(absFilePath));
|
||||
const bool isModified = patchBehaviour == DiffFileInfo::PatchFile &&
|
||||
textDocument && textDocument->isModified();
|
||||
|
||||
if (!PatchTool::confirmPatching(m_diffEditorWidget, patchAction, isModified))
|
||||
return;
|
||||
|
||||
if (patchBehaviour == DiffFileInfo::PatchFile) {
|
||||
if (textDocument && !EditorManager::saveDocument(textDocument))
|
||||
return;
|
||||
const int strip = m_document->baseDirectory().isEmpty() ? -1 : 0;
|
||||
|
||||
const QString patch = m_document->makePatch(fileIndex, chunkIndex, {}, patchAction);
|
||||
@@ -163,8 +170,6 @@ void DiffEditorWidgetController::patch(PatchAction patchAction, int fileIndex, i
|
||||
workingDirectory, strip, patchAction))
|
||||
m_document->reload();
|
||||
} else { // PatchEditor
|
||||
auto textDocument = qobject_cast<TextEditor::TextDocument *>(
|
||||
DocumentModel::documentForFilePath(absFilePath));
|
||||
if (!textDocument)
|
||||
return;
|
||||
|
||||
|
@@ -1578,7 +1578,14 @@ bool VcsBaseEditorWidget::hasDiff() const
|
||||
|
||||
void VcsBaseEditorWidget::slotApplyDiffChunk(const DiffChunk &chunk, PatchAction patchAction)
|
||||
{
|
||||
if (!PatchTool::confirmPatching(this, patchAction))
|
||||
auto textDocument = qobject_cast<TextEditor::TextDocument *>(
|
||||
DocumentModel::documentForFilePath(chunk.fileName));
|
||||
const bool isModified = textDocument && textDocument->isModified();
|
||||
|
||||
if (!PatchTool::confirmPatching(this, patchAction, isModified))
|
||||
return;
|
||||
|
||||
if (textDocument && !EditorManager::saveDocument(textDocument))
|
||||
return;
|
||||
|
||||
if (applyDiffChunk(chunk, patchAction) && patchAction == PatchAction::Revert)
|
||||
|
Reference in New Issue
Block a user