Git: Auto stash for branches

Allow user to handle local changes when switching branches.

Task-number: QTCREATORBUG-8417
Change-Id: I62d615aadfbd81c8ae03f3cd292b98773a36e1e7
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
Reviewed-by: Tobias Hunger <tobias.hunger@digia.com>
This commit is contained in:
Petar Perisin
2012-12-16 20:35:58 +01:00
committed by Tobias Hunger
parent 7df112b687
commit 81bf2ec154
6 changed files with 331 additions and 4 deletions

View File

@@ -29,9 +29,11 @@
#include "branchdialog.h"
#include "branchadddialog.h"
#include "branchcheckoutdialog.h"
#include "branchmodel.h"
#include "gitclient.h"
#include "gitplugin.h"
#include "gitutils.h"
#include "ui_branchdialog.h"
#include "stashdialog.h" // Label helpers
@@ -42,6 +44,7 @@
#include <QItemSelectionModel>
#include <QPushButton>
#include <QMessageBox>
#include <QList>
#include <QDebug>
@@ -159,7 +162,59 @@ void BranchDialog::checkout()
QModelIndex idx = selectedIndex();
QTC_CHECK(m_model->isLocal(idx));
m_model->checkoutBranch(idx);
const QString currentBranch = m_model->branchName(m_model->currentBranch());
const QString nextBranch = m_model->branchName(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 && m_model) {
QString stashMessage;
if (branchCheckoutDialog.makeStashOfCurrentBranch()
|| branchCheckoutDialog.moveLocalChangesToNextBranch()) {
stashMessage = gitClient->synchronousStash(m_repository, currentBranch + QLatin1String("-AutoStash"));
} else if (branchCheckoutDialog.discardLocalChanges()) {
gitClient->synchronousReset(m_repository);
}
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 (!stashMessage.isEmpty() && branchCheckoutDialog.moveLocalChangesToNextBranch())
gitClient->stashPop(m_repository);
else if (branchCheckoutDialog.popStashOfNextBranch())
gitClient->synchronousStashRestore(m_repository, stashName);
if (branchCheckoutDialog.hasStashForNextBranch())
gitClient->synchronousStashRemove(m_repository, stashName);
}
enableButtons();
}