forked from qt-creator/qt-creator
Git: Extract BranchUtils for re-use in Branches Side Panel
Done-with: Orgad Shaneh <orgad.shaneh@audiocodes.com> Change-Id: I1f48ffb3d9bb9b8d743ab6891c472fcfab778cf6 Reviewed-by: Orgad Shaneh <orgads@gmail.com> Reviewed-by: André Hartmann <aha_1980@gmx.de>
This commit is contained in:
committed by
André Hartmann
parent
205c930592
commit
a2edfda1ca
@@ -27,6 +27,7 @@
|
|||||||
#include "branchadddialog.h"
|
#include "branchadddialog.h"
|
||||||
#include "branchcheckoutdialog.h"
|
#include "branchcheckoutdialog.h"
|
||||||
#include "branchmodel.h"
|
#include "branchmodel.h"
|
||||||
|
#include "branchutils.h"
|
||||||
#include "gitclient.h"
|
#include "gitclient.h"
|
||||||
#include "gitplugin.h"
|
#include "gitplugin.h"
|
||||||
#include "gitutils.h"
|
#include "gitutils.h"
|
||||||
@@ -55,14 +56,15 @@ namespace Internal {
|
|||||||
|
|
||||||
BranchDialog::BranchDialog(QWidget *parent) :
|
BranchDialog::BranchDialog(QWidget *parent) :
|
||||||
QDialog(parent),
|
QDialog(parent),
|
||||||
m_ui(new Ui::BranchDialog),
|
BranchUtils(this),
|
||||||
m_model(new BranchModel(GitPlugin::client(), this))
|
m_ui(new Ui::BranchDialog)
|
||||||
{
|
{
|
||||||
setModal(false);
|
setModal(false);
|
||||||
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
|
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
|
||||||
setAttribute(Qt::WA_DeleteOnClose, true); // Do not update unnecessarily
|
setAttribute(Qt::WA_DeleteOnClose, true); // Do not update unnecessarily
|
||||||
|
|
||||||
m_ui->setupUi(this);
|
m_ui->setupUi(this);
|
||||||
|
setBranchView(m_ui->branchView);
|
||||||
m_ui->includeOldCheckBox->setToolTip(
|
m_ui->includeOldCheckBox->setToolTip(
|
||||||
tr("Include branches and tags that have not been active for %n days.", 0,
|
tr("Include branches and tags that have not been active for %n days.", 0,
|
||||||
Constants::OBSOLETE_COMMIT_AGE_IN_DAYS));
|
Constants::OBSOLETE_COMMIT_AGE_IN_DAYS));
|
||||||
@@ -70,16 +72,16 @@ BranchDialog::BranchDialog(QWidget *parent) :
|
|||||||
GitSettings::showTagsKey));
|
GitSettings::showTagsKey));
|
||||||
|
|
||||||
connect(m_ui->refreshButton, &QAbstractButton::clicked, this, &BranchDialog::refreshCurrentRepository);
|
connect(m_ui->refreshButton, &QAbstractButton::clicked, this, &BranchDialog::refreshCurrentRepository);
|
||||||
connect(m_ui->addButton, &QAbstractButton::clicked, this, &BranchDialog::add);
|
connect(m_ui->addButton, &QAbstractButton::clicked, this, [this] { add(); });
|
||||||
connect(m_ui->checkoutButton, &QAbstractButton::clicked, this, &BranchDialog::checkout);
|
connect(m_ui->checkoutButton, &QAbstractButton::clicked, this, [this] { checkout(); });
|
||||||
connect(m_ui->removeButton, &QAbstractButton::clicked, this, &BranchDialog::remove);
|
connect(m_ui->removeButton, &QAbstractButton::clicked, this, [this] { remove(); });
|
||||||
connect(m_ui->renameButton, &QAbstractButton::clicked, this, &BranchDialog::rename);
|
connect(m_ui->renameButton, &QAbstractButton::clicked, this, &BranchDialog::rename);
|
||||||
connect(m_ui->diffButton, &QAbstractButton::clicked, this, &BranchDialog::diff);
|
connect(m_ui->diffButton, &QAbstractButton::clicked, this, &BranchDialog::diff);
|
||||||
connect(m_ui->logButton, &QAbstractButton::clicked, this, &BranchDialog::log);
|
connect(m_ui->logButton, &QAbstractButton::clicked, this, &BranchDialog::log);
|
||||||
connect(m_ui->resetButton, &QAbstractButton::clicked, this, &BranchDialog::reset);
|
connect(m_ui->resetButton, &QAbstractButton::clicked, this, [this] { reset(); });
|
||||||
connect(m_ui->mergeButton, &QAbstractButton::clicked, this, &BranchDialog::merge);
|
connect(m_ui->mergeButton, &QAbstractButton::clicked, this, &BranchDialog::merge);
|
||||||
connect(m_ui->rebaseButton, &QAbstractButton::clicked, this, &BranchDialog::rebase);
|
connect(m_ui->rebaseButton, &QAbstractButton::clicked, this, [this] { rebase(); });
|
||||||
connect(m_ui->cherryPickButton, &QAbstractButton::clicked, this, &BranchDialog::cherryPick);
|
connect(m_ui->cherryPickButton, &QAbstractButton::clicked, this, [this] { cherryPick(); });
|
||||||
connect(m_ui->trackButton, &QAbstractButton::clicked, this, &BranchDialog::setRemoteTracking);
|
connect(m_ui->trackButton, &QAbstractButton::clicked, this, &BranchDialog::setRemoteTracking);
|
||||||
connect(m_ui->includeOldCheckBox, &QCheckBox::toggled, this, [this](bool value) {
|
connect(m_ui->includeOldCheckBox, &QCheckBox::toggled, this, [this](bool value) {
|
||||||
m_model->setOldBranchesIncluded(value);
|
m_model->setOldBranchesIncluded(value);
|
||||||
@@ -168,171 +170,11 @@ void BranchDialog::refreshCurrentRepository()
|
|||||||
refresh(m_repository, true);
|
refresh(m_repository, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BranchDialog::add()
|
|
||||||
{
|
|
||||||
QModelIndex trackedIndex = selectedIndex();
|
|
||||||
QString trackedBranch = m_model->fullName(trackedIndex);
|
|
||||||
if (trackedBranch.isEmpty()) {
|
|
||||||
trackedIndex = m_model->currentBranch();
|
|
||||||
trackedBranch = m_model->fullName(trackedIndex);
|
|
||||||
}
|
|
||||||
const bool isLocal = m_model->isLocal(trackedIndex);
|
|
||||||
const bool isTag = m_model->isTag(trackedIndex);
|
|
||||||
|
|
||||||
QStringList localNames = m_model->localBranchNames();
|
|
||||||
|
|
||||||
QString suggestedName;
|
|
||||||
if (!isTag) {
|
|
||||||
const QString suggestedNameBase = trackedBranch.mid(trackedBranch.lastIndexOf('/') + 1);
|
|
||||||
suggestedName = suggestedNameBase;
|
|
||||||
int i = 2;
|
|
||||||
while (localNames.contains(suggestedName)) {
|
|
||||||
suggestedName = suggestedNameBase + QString::number(i);
|
|
||||||
++i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
BranchAddDialog branchAddDialog(localNames, true, this);
|
|
||||||
branchAddDialog.setBranchName(suggestedName);
|
|
||||||
branchAddDialog.setTrackedBranchName(isTag ? QString() : trackedBranch, !isLocal);
|
|
||||||
|
|
||||||
if (branchAddDialog.exec() == QDialog::Accepted) {
|
|
||||||
QModelIndex idx = m_model->addBranch(branchAddDialog.branchName(), branchAddDialog.track(), trackedIndex);
|
|
||||||
if (!idx.isValid())
|
|
||||||
return;
|
|
||||||
m_ui->branchView->selectionModel()->select(idx, QItemSelectionModel::Clear
|
|
||||||
| QItemSelectionModel::Select
|
|
||||||
| QItemSelectionModel::Current);
|
|
||||||
m_ui->branchView->scrollTo(idx);
|
|
||||||
if (QMessageBox::question(this, tr("Checkout"), tr("Checkout branch?"),
|
|
||||||
QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes)
|
|
||||||
checkout();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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() +
|
|
||||||
' ' + nextBranch + "-AutoStash ";
|
|
||||||
|
|
||||||
BranchCheckoutDialog branchCheckoutDialog(this, 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 : Utils::asConst(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 (client->synchronousStash(m_repository, currentBranch + "-AutoStash").isEmpty())
|
|
||||||
return;
|
|
||||||
} else if (branchCheckoutDialog.moveLocalChangesToNextBranch()) {
|
|
||||||
if (!client->beginStashScope(m_repository, "Checkout", NoPrompt))
|
|
||||||
return;
|
|
||||||
} else if (branchCheckoutDialog.discardLocalChanges()) {
|
|
||||||
if (!client->synchronousReset(m_repository))
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
m_model->checkoutBranch(idx);
|
|
||||||
|
|
||||||
QString stashName;
|
|
||||||
client->synchronousStashList(m_repository, &stashes);
|
|
||||||
for (const Stash &stash : Utils::asConst(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);
|
|
||||||
}
|
|
||||||
m_ui->branchView->selectionModel()->clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Prompt to delete a local branch and do so. */
|
|
||||||
void BranchDialog::remove()
|
|
||||||
{
|
|
||||||
QModelIndex selected = selectedIndex();
|
|
||||||
QTC_CHECK(selected != m_model->currentBranch()); // otherwise the button would not be enabled!
|
|
||||||
|
|
||||||
QString branchName = m_model->fullName(selected);
|
|
||||||
if (branchName.isEmpty())
|
|
||||||
return;
|
|
||||||
|
|
||||||
const bool isTag = m_model->isTag(selected);
|
|
||||||
const bool wasMerged = isTag ? true : m_model->branchIsMerged(selected);
|
|
||||||
QString message;
|
|
||||||
if (isTag)
|
|
||||||
message = tr("Would you like to delete the tag \"%1\"?").arg(branchName);
|
|
||||||
else if (wasMerged)
|
|
||||||
message = tr("Would you like to delete the branch \"%1\"?").arg(branchName);
|
|
||||||
else
|
|
||||||
message = tr("Would you like to delete the <b>unmerged</b> branch \"%1\"?").arg(branchName);
|
|
||||||
|
|
||||||
if (QMessageBox::question(this, isTag ? tr("Delete Tag") : tr("Delete Branch"),
|
|
||||||
message, QMessageBox::Yes | QMessageBox::No,
|
|
||||||
wasMerged ? QMessageBox::Yes : QMessageBox::No) == QMessageBox::Yes) {
|
|
||||||
if (isTag)
|
|
||||||
m_model->removeTag(selected);
|
|
||||||
else
|
|
||||||
m_model->removeBranch(selected);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void BranchDialog::rename()
|
void BranchDialog::rename()
|
||||||
{
|
{
|
||||||
QModelIndex selected = selectedIndex();
|
if (BranchUtils::rename())
|
||||||
QTC_CHECK(selected != m_model->currentBranch()); // otherwise the button would not be enabled!
|
|
||||||
const bool isTag = m_model->isTag(selected);
|
|
||||||
QTC_CHECK(m_model->isLocal(selected) || isTag);
|
|
||||||
|
|
||||||
QString oldName = m_model->fullName(selected);
|
|
||||||
QStringList localNames;
|
|
||||||
if (!isTag)
|
|
||||||
localNames = m_model->localBranchNames();
|
|
||||||
|
|
||||||
BranchAddDialog branchAddDialog(localNames, false, this);
|
|
||||||
if (isTag)
|
|
||||||
branchAddDialog.setWindowTitle(tr("Rename Tag"));
|
|
||||||
branchAddDialog.setBranchName(oldName);
|
|
||||||
branchAddDialog.setTrackedBranchName(QString(), false);
|
|
||||||
|
|
||||||
branchAddDialog.exec();
|
|
||||||
|
|
||||||
if (branchAddDialog.result() == QDialog::Accepted) {
|
|
||||||
if (branchAddDialog.branchName() == oldName)
|
|
||||||
return;
|
|
||||||
if (isTag)
|
|
||||||
m_model->renameTag(oldName, branchAddDialog.branchName());
|
|
||||||
else
|
|
||||||
m_model->renameBranch(oldName, branchAddDialog.branchName());
|
|
||||||
refreshCurrentRepository();
|
refreshCurrentRepository();
|
||||||
}
|
}
|
||||||
m_ui->branchView->selectionModel()->clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
void BranchDialog::diff()
|
void BranchDialog::diff()
|
||||||
{
|
{
|
||||||
@@ -350,31 +192,10 @@ void BranchDialog::log()
|
|||||||
GitPlugin::client()->log(m_repository, QString(), false, {branchName});
|
GitPlugin::client()->log(m_repository, QString(), false, {branchName});
|
||||||
}
|
}
|
||||||
|
|
||||||
void BranchDialog::reset()
|
|
||||||
{
|
|
||||||
QString currentName = m_model->fullName(m_model->currentBranch());
|
|
||||||
QString branchName = m_model->fullName(selectedIndex());
|
|
||||||
if (currentName.isEmpty() || branchName.isEmpty())
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (QMessageBox::question(this, tr("Git Reset"), tr("Hard reset branch \"%1\" to \"%2\"?")
|
|
||||||
.arg(currentName).arg(branchName),
|
|
||||||
QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
|
|
||||||
GitPlugin::client()->reset(m_repository, "--hard", branchName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void BranchDialog::merge()
|
void BranchDialog::merge()
|
||||||
{
|
{
|
||||||
if (!Core::DocumentManager::saveAllModifiedDocuments())
|
|
||||||
return;
|
|
||||||
QModelIndex idx = selectedIndex();
|
|
||||||
QTC_CHECK(idx != m_model->currentBranch()); // otherwise the button would not be enabled!
|
|
||||||
|
|
||||||
const QString branch = m_model->fullName(idx, true);
|
|
||||||
GitClient *client = GitPlugin::client();
|
|
||||||
bool allowFastForward = true;
|
bool allowFastForward = true;
|
||||||
if (client->isFastForwardMerge(m_repository, branch)) {
|
if (isFastForwardMerge()) {
|
||||||
QMenu popup;
|
QMenu popup;
|
||||||
QAction *fastForward = popup.addAction(tr("Fast-Forward"));
|
QAction *fastForward = popup.addAction(tr("Fast-Forward"));
|
||||||
popup.addAction(tr("No Fast-Forward"));
|
popup.addAction(tr("No Fast-Forward"));
|
||||||
@@ -383,32 +204,8 @@ void BranchDialog::merge()
|
|||||||
return;
|
return;
|
||||||
allowFastForward = (chosen == fastForward);
|
allowFastForward = (chosen == fastForward);
|
||||||
}
|
}
|
||||||
if (client->beginStashScope(m_repository, "merge", AllowUnstashed))
|
|
||||||
client->synchronousMerge(m_repository, branch, allowFastForward);
|
|
||||||
}
|
|
||||||
|
|
||||||
void BranchDialog::rebase()
|
BranchUtils::merge(allowFastForward);
|
||||||
{
|
|
||||||
if (!Core::DocumentManager::saveAllModifiedDocuments())
|
|
||||||
return;
|
|
||||||
QModelIndex idx = selectedIndex();
|
|
||||||
QTC_CHECK(idx != m_model->currentBranch()); // otherwise the button would not be enabled!
|
|
||||||
|
|
||||||
const QString baseBranch = m_model->fullName(idx, true);
|
|
||||||
GitClient *client = GitPlugin::client();
|
|
||||||
if (client->beginStashScope(m_repository, "rebase"))
|
|
||||||
client->rebase(m_repository, baseBranch);
|
|
||||||
}
|
|
||||||
|
|
||||||
void BranchDialog::cherryPick()
|
|
||||||
{
|
|
||||||
if (!Core::DocumentManager::saveAllModifiedDocuments())
|
|
||||||
return;
|
|
||||||
QModelIndex idx = selectedIndex();
|
|
||||||
QTC_CHECK(idx != m_model->currentBranch()); // otherwise the button would not be enabled!
|
|
||||||
|
|
||||||
const QString branch = m_model->fullName(idx, true);
|
|
||||||
GitPlugin::client()->synchronousCherryPick(m_repository, branch);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void BranchDialog::setRemoteTracking()
|
void BranchDialog::setRemoteTracking()
|
||||||
@@ -416,13 +213,5 @@ void BranchDialog::setRemoteTracking()
|
|||||||
m_model->setRemoteTracking(selectedIndex());
|
m_model->setRemoteTracking(selectedIndex());
|
||||||
}
|
}
|
||||||
|
|
||||||
QModelIndex BranchDialog::selectedIndex()
|
|
||||||
{
|
|
||||||
QModelIndexList selected = m_ui->branchView->selectionModel()->selectedIndexes();
|
|
||||||
if (selected.isEmpty())
|
|
||||||
return QModelIndex();
|
|
||||||
return selected.at(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace Internal
|
} // namespace Internal
|
||||||
} // namespace Git
|
} // namespace Git
|
||||||
|
|||||||
@@ -25,6 +25,8 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "branchutils.h"
|
||||||
|
|
||||||
#include <QDialog>
|
#include <QDialog>
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
@@ -43,7 +45,7 @@ class BranchModel;
|
|||||||
* branches below. Offers to checkout/delete local branches.
|
* branches below. Offers to checkout/delete local branches.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
class BranchDialog : public QDialog
|
class BranchDialog : public QDialog, public BranchUtils
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
@@ -59,23 +61,13 @@ private:
|
|||||||
void resizeColumns();
|
void resizeColumns();
|
||||||
void enableButtons();
|
void enableButtons();
|
||||||
void refreshCurrentRepository();
|
void refreshCurrentRepository();
|
||||||
void add();
|
|
||||||
void checkout();
|
|
||||||
void remove();
|
|
||||||
void rename();
|
void rename();
|
||||||
void diff();
|
void diff();
|
||||||
void log();
|
void log();
|
||||||
void reset();
|
|
||||||
void merge();
|
void merge();
|
||||||
void rebase();
|
|
||||||
void cherryPick();
|
|
||||||
void setRemoteTracking();
|
void setRemoteTracking();
|
||||||
|
|
||||||
QModelIndex selectedIndex();
|
|
||||||
|
|
||||||
Ui::BranchDialog *m_ui;
|
Ui::BranchDialog *m_ui;
|
||||||
BranchModel *m_model;
|
|
||||||
QString m_repository;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Internal
|
} // namespace Internal
|
||||||
|
|||||||
308
src/plugins/git/branchutils.cpp
Normal file
308
src/plugins/git/branchutils.cpp
Normal file
@@ -0,0 +1,308 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
**
|
||||||
|
** Copyright (C) 2018 Andre Hartmann <aha_1980@gmx.de>
|
||||||
|
** Contact: https://www.qt.io/licensing/
|
||||||
|
**
|
||||||
|
** This file is part of Qt Creator.
|
||||||
|
**
|
||||||
|
** Commercial License Usage
|
||||||
|
** Licensees holding valid commercial Qt licenses may use this file in
|
||||||
|
** accordance with the commercial license agreement provided with the
|
||||||
|
** Software or, alternatively, in accordance with the terms contained in
|
||||||
|
** a written agreement between you and The Qt Company. For licensing terms
|
||||||
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||||
|
** information use the contact form at https://www.qt.io/contact-us.
|
||||||
|
**
|
||||||
|
** GNU General Public License Usage
|
||||||
|
** Alternatively, this file may be used under the terms of the GNU
|
||||||
|
** General Public License version 3 as published by the Free Software
|
||||||
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
||||||
|
** included in the packaging of this file. Please review the following
|
||||||
|
** information to ensure the GNU General Public License requirements will
|
||||||
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
||||||
|
**
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include "branchutils.h"
|
||||||
|
|
||||||
|
#include "branchadddialog.h"
|
||||||
|
#include "branchcheckoutdialog.h"
|
||||||
|
#include "branchmodel.h"
|
||||||
|
#include "gitclient.h"
|
||||||
|
#include "gitplugin.h"
|
||||||
|
#include "gitutils.h"
|
||||||
|
|
||||||
|
#include <coreplugin/documentmanager.h>
|
||||||
|
#include <utils/asconst.h>
|
||||||
|
#include <utils/qtcassert.h>
|
||||||
|
|
||||||
|
#include <QMessageBox>
|
||||||
|
#include <QTreeView>
|
||||||
|
|
||||||
|
namespace Git {
|
||||||
|
namespace Internal {
|
||||||
|
|
||||||
|
BranchUtils::BranchUtils(QWidget *parent)
|
||||||
|
: m_model(new BranchModel(GitPlugin::client(), parent))
|
||||||
|
, m_widget(parent)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void BranchUtils::setBranchView(QTreeView *branchView)
|
||||||
|
{
|
||||||
|
m_branchView = branchView;
|
||||||
|
}
|
||||||
|
|
||||||
|
QModelIndex BranchUtils::selectedIndex()
|
||||||
|
{
|
||||||
|
QTC_ASSERT(m_branchView, return QModelIndex());
|
||||||
|
QModelIndexList selected = m_branchView->selectionModel()->selectedIndexes();
|
||||||
|
if (selected.isEmpty())
|
||||||
|
return QModelIndex();
|
||||||
|
return selected.at(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool BranchUtils::add()
|
||||||
|
{
|
||||||
|
QModelIndex trackedIndex = selectedIndex();
|
||||||
|
QString trackedBranch = m_model->fullName(trackedIndex);
|
||||||
|
if (trackedBranch.isEmpty()) {
|
||||||
|
trackedIndex = m_model->currentBranch();
|
||||||
|
trackedBranch = m_model->fullName(trackedIndex);
|
||||||
|
}
|
||||||
|
const bool isLocal = m_model->isLocal(trackedIndex);
|
||||||
|
const bool isTag = m_model->isTag(trackedIndex);
|
||||||
|
|
||||||
|
const QStringList localNames = m_model->localBranchNames();
|
||||||
|
|
||||||
|
QString suggestedName;
|
||||||
|
if (!isTag) {
|
||||||
|
const QString suggestedNameBase = trackedBranch.mid(trackedBranch.lastIndexOf('/') + 1);
|
||||||
|
suggestedName = suggestedNameBase;
|
||||||
|
int i = 2;
|
||||||
|
while (localNames.contains(suggestedName)) {
|
||||||
|
suggestedName = suggestedNameBase + QString::number(i);
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
BranchAddDialog branchAddDialog(localNames, true, m_widget);
|
||||||
|
branchAddDialog.setBranchName(suggestedName);
|
||||||
|
branchAddDialog.setTrackedBranchName(isTag ? QString() : trackedBranch, !isLocal);
|
||||||
|
|
||||||
|
if (branchAddDialog.exec() == QDialog::Accepted) {
|
||||||
|
QModelIndex idx = m_model->addBranch(branchAddDialog.branchName(), branchAddDialog.track(), trackedIndex);
|
||||||
|
if (!idx.isValid())
|
||||||
|
return false;
|
||||||
|
QTC_ASSERT(m_branchView, return false);
|
||||||
|
m_branchView->selectionModel()->select(idx, QItemSelectionModel::Clear
|
||||||
|
| QItemSelectionModel::Select
|
||||||
|
| QItemSelectionModel::Current);
|
||||||
|
m_branchView->scrollTo(idx);
|
||||||
|
if (QMessageBox::question(m_widget, tr("Checkout"), tr("Checkout branch?"),
|
||||||
|
QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
|
||||||
|
return checkout();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 : Utils::asConst(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 : Utils::asConst(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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prompt to delete a local branch and do so.
|
||||||
|
bool BranchUtils::remove()
|
||||||
|
{
|
||||||
|
const QModelIndex selected = selectedIndex();
|
||||||
|
QTC_CHECK(selected != m_model->currentBranch());
|
||||||
|
|
||||||
|
QString branchName = m_model->fullName(selected);
|
||||||
|
if (branchName.isEmpty())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
const bool isTag = m_model->isTag(selected);
|
||||||
|
const bool wasMerged = isTag ? true : m_model->branchIsMerged(selected);
|
||||||
|
QString message;
|
||||||
|
if (isTag)
|
||||||
|
message = tr("Would you like to delete the tag \"%1\"?").arg(branchName);
|
||||||
|
else if (wasMerged)
|
||||||
|
message = tr("Would you like to delete the branch \"%1\"?").arg(branchName);
|
||||||
|
else
|
||||||
|
message = tr("Would you like to delete the <b>unmerged</b> branch \"%1\"?").arg(branchName);
|
||||||
|
|
||||||
|
if (QMessageBox::question(m_widget, isTag ? tr("Delete Tag") : tr("Delete Branch"),
|
||||||
|
message, QMessageBox::Yes | QMessageBox::No,
|
||||||
|
wasMerged ? QMessageBox::Yes : QMessageBox::No) == QMessageBox::Yes) {
|
||||||
|
if (isTag)
|
||||||
|
m_model->removeTag(selected);
|
||||||
|
else
|
||||||
|
m_model->removeBranch(selected);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool BranchUtils::rename()
|
||||||
|
{
|
||||||
|
const QModelIndex selected = selectedIndex();
|
||||||
|
QTC_CHECK(selected != m_model->currentBranch());
|
||||||
|
const bool isTag = m_model->isTag(selected);
|
||||||
|
QTC_CHECK(m_model->isLocal(selected) || isTag);
|
||||||
|
|
||||||
|
QString oldName = m_model->fullName(selected);
|
||||||
|
QStringList localNames;
|
||||||
|
if (!isTag)
|
||||||
|
localNames = m_model->localBranchNames();
|
||||||
|
|
||||||
|
BranchAddDialog branchAddDialog(localNames, false, m_widget);
|
||||||
|
if (isTag)
|
||||||
|
branchAddDialog.setWindowTitle(tr("Rename Tag"));
|
||||||
|
branchAddDialog.setBranchName(oldName);
|
||||||
|
branchAddDialog.setTrackedBranchName(QString(), false);
|
||||||
|
|
||||||
|
branchAddDialog.exec();
|
||||||
|
|
||||||
|
if (branchAddDialog.result() == QDialog::Accepted) {
|
||||||
|
if (branchAddDialog.branchName() == oldName)
|
||||||
|
return false;
|
||||||
|
if (isTag)
|
||||||
|
m_model->renameTag(oldName, branchAddDialog.branchName());
|
||||||
|
else
|
||||||
|
m_model->renameBranch(oldName, branchAddDialog.branchName());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (QTC_GUARD(m_branchView))
|
||||||
|
m_branchView->selectionModel()->clear();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool BranchUtils::reset()
|
||||||
|
{
|
||||||
|
const QString currentName = m_model->fullName(m_model->currentBranch());
|
||||||
|
const QString branchName = m_model->fullName(selectedIndex());
|
||||||
|
if (currentName.isEmpty() || branchName.isEmpty())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (QMessageBox::question(m_widget, tr("Git Reset"), tr("Hard reset branch \"%1\" to \"%2\"?")
|
||||||
|
.arg(currentName).arg(branchName),
|
||||||
|
QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
|
||||||
|
GitPlugin::client()->reset(m_repository, "--hard", branchName);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool BranchUtils::isFastForwardMerge()
|
||||||
|
{
|
||||||
|
const QModelIndex selected = selectedIndex();
|
||||||
|
QTC_CHECK(selected != m_model->currentBranch());
|
||||||
|
|
||||||
|
const QString branch = m_model->fullName(selected, true);
|
||||||
|
return GitPlugin::client()->isFastForwardMerge(m_repository, branch);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool BranchUtils::merge(bool allowFastForward)
|
||||||
|
{
|
||||||
|
if (!Core::DocumentManager::saveAllModifiedDocuments())
|
||||||
|
return false;
|
||||||
|
const QModelIndex selected = selectedIndex();
|
||||||
|
QTC_CHECK(selected != m_model->currentBranch());
|
||||||
|
|
||||||
|
const QString branch = m_model->fullName(selected, true);
|
||||||
|
GitClient *client = GitPlugin::client();
|
||||||
|
if (client->beginStashScope(m_repository, "merge", AllowUnstashed))
|
||||||
|
return client->synchronousMerge(m_repository, branch, allowFastForward);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void BranchUtils::rebase()
|
||||||
|
{
|
||||||
|
if (!Core::DocumentManager::saveAllModifiedDocuments())
|
||||||
|
return;
|
||||||
|
const QModelIndex selected = selectedIndex();
|
||||||
|
QTC_CHECK(selected != m_model->currentBranch());
|
||||||
|
|
||||||
|
const QString baseBranch = m_model->fullName(selected, true);
|
||||||
|
GitClient *client = GitPlugin::client();
|
||||||
|
if (client->beginStashScope(m_repository, "rebase"))
|
||||||
|
client->rebase(m_repository, baseBranch);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool BranchUtils::cherryPick()
|
||||||
|
{
|
||||||
|
if (!Core::DocumentManager::saveAllModifiedDocuments())
|
||||||
|
return false;
|
||||||
|
const QModelIndex selected = selectedIndex();
|
||||||
|
QTC_CHECK(selected != m_model->currentBranch());
|
||||||
|
|
||||||
|
const QString branch = m_model->fullName(selected, true);
|
||||||
|
return GitPlugin::client()->synchronousCherryPick(m_repository, branch);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Internal
|
||||||
|
} // namespace Git
|
||||||
69
src/plugins/git/branchutils.h
Normal file
69
src/plugins/git/branchutils.h
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
**
|
||||||
|
** Copyright (C) 2018 Andre Hartmann <aha_1980@gmx.de>
|
||||||
|
** Contact: https://www.qt.io/licensing/
|
||||||
|
**
|
||||||
|
** This file is part of Qt Creator.
|
||||||
|
**
|
||||||
|
** Commercial License Usage
|
||||||
|
** Licensees holding valid commercial Qt licenses may use this file in
|
||||||
|
** accordance with the commercial license agreement provided with the
|
||||||
|
** Software or, alternatively, in accordance with the terms contained in
|
||||||
|
** a written agreement between you and The Qt Company. For licensing terms
|
||||||
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||||
|
** information use the contact form at https://www.qt.io/contact-us.
|
||||||
|
**
|
||||||
|
** GNU General Public License Usage
|
||||||
|
** Alternatively, this file may be used under the terms of the GNU
|
||||||
|
** General Public License version 3 as published by the Free Software
|
||||||
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
||||||
|
** included in the packaging of this file. Please review the following
|
||||||
|
** information to ensure the GNU General Public License requirements will
|
||||||
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
||||||
|
**
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <QCoreApplication>
|
||||||
|
#include <QModelIndex>
|
||||||
|
|
||||||
|
QT_BEGIN_NAMESPACE
|
||||||
|
class QTreeView;
|
||||||
|
class QWidget;
|
||||||
|
QT_END_NAMESPACE
|
||||||
|
|
||||||
|
namespace Git {
|
||||||
|
namespace Internal {
|
||||||
|
|
||||||
|
class BranchModel;
|
||||||
|
|
||||||
|
class BranchUtils
|
||||||
|
{
|
||||||
|
Q_DECLARE_TR_FUNCTIONS(Git::Internal::BranchUtils)
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit BranchUtils(QWidget *parent);
|
||||||
|
QModelIndex selectedIndex();
|
||||||
|
bool add();
|
||||||
|
bool checkout();
|
||||||
|
bool remove();
|
||||||
|
bool rename();
|
||||||
|
bool reset();
|
||||||
|
bool isFastForwardMerge();
|
||||||
|
bool merge(bool allowFastForward = false);
|
||||||
|
void rebase();
|
||||||
|
bool cherryPick();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void setBranchView(QTreeView *branchView);
|
||||||
|
BranchModel *m_model = nullptr;
|
||||||
|
QString m_repository;
|
||||||
|
|
||||||
|
private:
|
||||||
|
QWidget *m_widget = nullptr;
|
||||||
|
QTreeView *m_branchView = nullptr;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Internal
|
||||||
|
} // namespace Git
|
||||||
@@ -25,7 +25,8 @@ HEADERS += gitplugin.h \
|
|||||||
mergetool.h \
|
mergetool.h \
|
||||||
branchcheckoutdialog.h \
|
branchcheckoutdialog.h \
|
||||||
githighlighters.h \
|
githighlighters.h \
|
||||||
gitgrep.h
|
gitgrep.h \
|
||||||
|
branchutils.h
|
||||||
|
|
||||||
SOURCES += gitplugin.cpp \
|
SOURCES += gitplugin.cpp \
|
||||||
gitclient.cpp \
|
gitclient.cpp \
|
||||||
@@ -49,7 +50,8 @@ SOURCES += gitplugin.cpp \
|
|||||||
mergetool.cpp \
|
mergetool.cpp \
|
||||||
branchcheckoutdialog.cpp \
|
branchcheckoutdialog.cpp \
|
||||||
githighlighters.cpp \
|
githighlighters.cpp \
|
||||||
gitgrep.cpp
|
gitgrep.cpp \
|
||||||
|
branchutils.cpp
|
||||||
|
|
||||||
FORMS += changeselectiondialog.ui \
|
FORMS += changeselectiondialog.ui \
|
||||||
settingspage.ui \
|
settingspage.ui \
|
||||||
|
|||||||
@@ -27,6 +27,8 @@ QtcPlugin {
|
|||||||
"branchdialog.ui",
|
"branchdialog.ui",
|
||||||
"branchmodel.cpp",
|
"branchmodel.cpp",
|
||||||
"branchmodel.h",
|
"branchmodel.h",
|
||||||
|
"branchutils.cpp",
|
||||||
|
"branchutils.h",
|
||||||
"changeselectiondialog.cpp",
|
"changeselectiondialog.cpp",
|
||||||
"changeselectiondialog.h",
|
"changeselectiondialog.h",
|
||||||
"changeselectiondialog.ui",
|
"changeselectiondialog.ui",
|
||||||
|
|||||||
Reference in New Issue
Block a user