// Prompt for restore: Make sure repository is unmodified, // prompt for a branch if desired or just ask to restore. // Note that the stash to be restored changes if the user // chooses to stash away modified repository. bool StashDialog::promptForRestore(QString *stash, QString *branch /* = 0*/, QString *errorMessage) { const QString stashIn = *stash; bool modifiedPromptShown = false; switch (gitClient()->gitStatus(m_repository, StatusMode(NoUntracked | NoSubmodules), 0, errorMessage)) { case GitClient::StatusFailed: return false; case GitClient::StatusChanged: { switch (promptModifiedRepository(*stash)) { case ModifiedRepositoryCancel: return false; case ModifiedRepositoryStash: if (gitClient()->synchronousStash(m_repository, QString(), GitClient::StashPromptDescription).isEmpty()) return false; *stash = nextStash(*stash); // Our stash id to be restored changed QTC_ASSERT(!stash->isEmpty(), return false); break; case ModifiedRepositoryDiscard: if (!gitClient()->synchronousReset(m_repository)) return false; break; } modifiedPromptShown = true; } break; case GitClient::StatusUnchanged: break; } // Prompt for branch or just ask. if (branch) { *branch = stashRestoreDefaultBranch(*stash); if (!inputText(this, tr("Restore Stash to Branch"), tr("Branch:"), branch) || branch->isEmpty()) return false; } else { if (!modifiedPromptShown && !ask(tr("Stash Restore"), tr("Would you like to restore %1?").arg(stashIn))) return false; } return true; }
void BranchDialog::checkout() { if (!Core::DocumentManager::saveAllModifiedDocuments()) return; QModelIndex idx = selectedIndex(); const QString currentBranch = m_model->fullName(m_model->currentBranch()); const QString nextBranch = m_model->fullName(idx); const QString popMessageStart = QCoreApplication::applicationName() + QLatin1String(" ") + nextBranch + QLatin1String("-AutoStash "); BranchCheckoutDialog branchCheckoutDialog(this, currentBranch, nextBranch); GitClient *gitClient = GitPlugin::instance()->gitClient(); if (gitClient->gitStatus(m_repository, StatusMode(NoUntracked | NoSubmodules)) != GitClient::StatusChanged) branchCheckoutDialog.foundNoLocalChanges(); QList<Stash> stashes; gitClient->synchronousStashList(m_repository, &stashes); foreach (const Stash &stash, stashes) { if (stash.message.startsWith(popMessageStart)) { branchCheckoutDialog.foundStashForNextBranch(); break; } } if (!branchCheckoutDialog.hasLocalChanges() && !branchCheckoutDialog.hasStashForNextBranch()) { // No local changes and no Auto Stash - no need to open dialog m_model->checkoutBranch(idx); } else if (branchCheckoutDialog.exec() == QDialog::Accepted) { if (branchCheckoutDialog.makeStashOfCurrentBranch()) { if (gitClient->synchronousStash(m_repository, currentBranch + QLatin1String("-AutoStash")).isEmpty()) { return; } } else if (branchCheckoutDialog.moveLocalChangesToNextBranch()) { if (!gitClient->beginStashScope(m_repository, QLatin1String("Checkout"), NoPrompt)) return; } else if (branchCheckoutDialog.discardLocalChanges()) { if (!gitClient->synchronousReset(m_repository)) return; } m_model->checkoutBranch(idx); QString stashName; gitClient->synchronousStashList(m_repository, &stashes); foreach (const Stash &stash, stashes) { if (stash.message.startsWith(popMessageStart)) { stashName = stash.name; break; } } if (branchCheckoutDialog.moveLocalChangesToNextBranch()) gitClient->endStashScope(m_repository); else if (branchCheckoutDialog.popStashOfNextBranch()) gitClient->synchronousStashRestore(m_repository, stashName, true); } enableButtons(); }
bool BranchUtils::checkout() { if (!Core::DocumentManager::saveAllModifiedDocuments()) return false; const QModelIndex selected = selectedIndex(); const QString currentBranch = m_model->fullName(m_model->currentBranch()); const QString nextBranch = m_model->fullName(selected); const QString popMessageStart = QCoreApplication::applicationName() + ' ' + nextBranch + "-AutoStash "; BranchCheckoutDialog branchCheckoutDialog(m_widget, currentBranch, nextBranch); GitClient *client = GitPlugin::client(); if (client->gitStatus(m_repository, StatusMode(NoUntracked | NoSubmodules)) != GitClient::StatusChanged) branchCheckoutDialog.foundNoLocalChanges(); QList<Stash> stashes; client->synchronousStashList(m_repository, &stashes); for (const Stash &stash : qAsConst(stashes)) { if (stash.message.startsWith(popMessageStart)) { branchCheckoutDialog.foundStashForNextBranch(); break; } } if (!branchCheckoutDialog.hasLocalChanges() && !branchCheckoutDialog.hasStashForNextBranch()) { // No local changes and no Auto Stash - no need to open dialog m_model->checkoutBranch(selected); } else if (branchCheckoutDialog.exec() == QDialog::Accepted) { if (branchCheckoutDialog.makeStashOfCurrentBranch()) { if (client->synchronousStash(m_repository, currentBranch + "-AutoStash").isEmpty()) return false; } else if (branchCheckoutDialog.moveLocalChangesToNextBranch()) { if (!client->beginStashScope(m_repository, "Checkout", NoPrompt)) return false; } else if (branchCheckoutDialog.discardLocalChanges()) { if (!client->synchronousReset(m_repository)) return false; } m_model->checkoutBranch(selected); QString stashName; client->synchronousStashList(m_repository, &stashes); for (const Stash &stash : qAsConst(stashes)) { if (stash.message.startsWith(popMessageStart)) { stashName = stash.name; break; } } if (branchCheckoutDialog.moveLocalChangesToNextBranch()) client->endStashScope(m_repository); else if (branchCheckoutDialog.popStashOfNextBranch()) client->synchronousStashRestore(m_repository, stashName, true); } if (QTC_GUARD(m_branchView)) m_branchView->selectionModel()->clear(); return true; }