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:
Orgad Shaneh
2022-10-16 04:59:51 +03:00
committed by Orgad Shaneh
parent 7edf743583
commit f696d1e6cf
4 changed files with 23 additions and 9 deletions

View File

@@ -38,13 +38,15 @@ void PatchTool::setPatchCommand(const FilePath &newCommand)
s->endGroup(); 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") const QString title = patchAction == PatchAction::Apply ? Tr::tr("Apply Chunk")
: Tr::tr("Revert 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 apply the chunk?")
: Tr::tr("Would you like to revert 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) return QMessageBox::question(parent, title, question, QMessageBox::Yes | QMessageBox::No)
== QMessageBox::Yes; == QMessageBox::Yes;
} }

View File

@@ -20,7 +20,7 @@ public:
static Utils::FilePath patchCommand(); static Utils::FilePath patchCommand();
static void setPatchCommand(const Utils::FilePath &newCommand); 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 // Utility to run the 'patch' command
static bool runPatch(const QByteArray &input, const Utils::FilePath &workingDirectory = {}, static bool runPatch(const QByteArray &input, const Utils::FilePath &workingDirectory = {},

View File

@@ -134,9 +134,6 @@ void DiffEditorWidgetController::patch(PatchAction patchAction, int fileIndex, i
if (!chunkExists(fileIndex, chunkIndex)) if (!chunkExists(fileIndex, chunkIndex))
return; return;
if (!PatchTool::confirmPatching(m_diffEditorWidget, patchAction))
return;
const FileData fileData = m_contextFileData.at(fileIndex); const FileData fileData = m_contextFileData.at(fileIndex);
const QString fileName = patchAction == PatchAction::Apply const QString fileName = patchAction == PatchAction::Apply
? fileData.fileInfo[LeftSide].fileName ? fileData.fileInfo[LeftSide].fileName
@@ -150,7 +147,17 @@ void DiffEditorWidgetController::patch(PatchAction patchAction, int fileIndex, i
: m_document->baseDirectory(); : m_document->baseDirectory();
const FilePath absFilePath = workingDirectory.resolvePath(fileName).absoluteFilePath(); 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 (patchBehaviour == DiffFileInfo::PatchFile) {
if (textDocument && !EditorManager::saveDocument(textDocument))
return;
const int strip = m_document->baseDirectory().isEmpty() ? -1 : 0; const int strip = m_document->baseDirectory().isEmpty() ? -1 : 0;
const QString patch = m_document->makePatch(fileIndex, chunkIndex, {}, patchAction); const QString patch = m_document->makePatch(fileIndex, chunkIndex, {}, patchAction);
@@ -163,8 +170,6 @@ void DiffEditorWidgetController::patch(PatchAction patchAction, int fileIndex, i
workingDirectory, strip, patchAction)) workingDirectory, strip, patchAction))
m_document->reload(); m_document->reload();
} else { // PatchEditor } else { // PatchEditor
auto textDocument = qobject_cast<TextEditor::TextDocument *>(
DocumentModel::documentForFilePath(absFilePath));
if (!textDocument) if (!textDocument)
return; return;

View File

@@ -1578,7 +1578,14 @@ bool VcsBaseEditorWidget::hasDiff() const
void VcsBaseEditorWidget::slotApplyDiffChunk(const DiffChunk &chunk, PatchAction patchAction) 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; return;
if (applyDiffChunk(chunk, patchAction) && patchAction == PatchAction::Revert) if (applyDiffChunk(chunk, patchAction) && patchAction == PatchAction::Revert)