Gerrit: Added pushToGerrit dialog

Change-Id: Ic16eae2def11343ef7be5ce8378d24b5fd11a386
Reviewed-by: Petar Perisin <petar.perisin@gmail.com>
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@digia.com>
Reviewed-by: Tobias Hunger <tobias.hunger@digia.com>
This commit is contained in:
Petar Perisin
2013-04-02 15:07:30 +03:00
parent 83d51b2691
commit fe1f7e4bf6
12 changed files with 567 additions and 15 deletions

View File

@@ -4,10 +4,14 @@ SOURCES += $$PWD/gerritdialog.cpp \
$$PWD/gerritmodel.cpp \
$$PWD/gerritparameters.cpp \
$$PWD/gerritplugin.cpp \
$$PWD/gerritoptionspage.cpp
$$PWD/gerritoptionspage.cpp \
$$PWD/gerritpushdialog.cpp
HEADERS += $$PWD/gerritdialog.h \
$$PWD/gerritmodel.h \
$$PWD/gerritparameters.h \
$$PWD/gerritplugin.h \
$$PWD/gerritoptionspage.h
$$PWD/gerritoptionspage.h \
$$PWD/gerritpushdialog.h
FORMS += $$PWD/gerritpushdialog.ui

View File

@@ -32,6 +32,7 @@
#include "gerritdialog.h"
#include "gerritmodel.h"
#include "gerritoptionspage.h"
#include "gerritpushdialog.h"
#include "../gitplugin.h"
#include "../gitclient.h"
@@ -50,6 +51,7 @@
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/command.h>
#include <coreplugin/editormanager/editormanager.h>
#include <locator/commandlocator.h>
#include <vcsbase/vcsbaseoutputwindow.h>
@@ -70,6 +72,7 @@ enum { debug = 0 };
namespace Gerrit {
namespace Constants {
const char GERRIT_OPEN_VIEW[] = "Gerrit.OpenView";
const char GERRIT_PUSH[] = "Gerrit.Push";
}
namespace Internal {
@@ -332,10 +335,59 @@ bool GerritPlugin::initialize(Core::ActionContainer *ac)
connect(openViewAction, SIGNAL(triggered()), this, SLOT(openView()));
ac->addAction(command);
QAction *pushAction = new QAction(tr("Push to Gerrit..."), this);
Core::Command *pushCommand =
Core::ActionManager::registerAction(pushAction, Constants::GERRIT_PUSH,
Core::Context(Core::Constants::C_GLOBAL));
connect(pushAction, SIGNAL(triggered()), this, SLOT(push()));
ac->addAction(pushCommand);
m_pushToGerritPair = ActionCommandPair(pushAction, pushCommand);
Git::Internal::GitPlugin::instance()->addAutoReleasedObject(new GerritOptionsPage(m_parameters));
return true;
}
void GerritPlugin::updateActions(bool hasTopLevel)
{
m_pushToGerritPair.first->setEnabled(hasTopLevel);
}
void GerritPlugin::addToLocator(Locator::CommandLocator *locator)
{
locator->appendCommand(m_pushToGerritPair.second);
}
void GerritPlugin::push()
{
const QString topLevel = Git::Internal::GitPlugin::instance()->currentState().topLevel();
QPointer<GerritPushDialog> dialog = new GerritPushDialog(topLevel, Core::ICore::mainWindow());
if (!dialog->localChangesFound()) {
QMessageBox::critical(Core::ICore::mainWindow(), tr("No Local Changes"),
tr("Change from HEAD appears to be in remote branch already! Aborting."));
return;
}
if (dialog->exec() == QDialog::Rejected)
return;
if (dialog.isNull())
return;
QStringList args;
args << dialog->selectedRemoteName();
args << QLatin1String("HEAD:refs/") + dialog->selectedPushType() +
QLatin1Char('/') + dialog->selectedRemoteBranchName();
Git::Internal::GitPlugin::instance()->gitClient()->synchronousPush(topLevel, args);
delete dialog;
}
// Open or raise the Gerrit dialog window.
void GerritPlugin::openView()
{

View File

@@ -33,9 +33,19 @@
#include <QObject>
#include <QPointer>
#include <QSharedPointer>
#include <QPair>
QT_BEGIN_NAMESPACE
class QAction;
QT_END_NAMESPACE
namespace Core {
class ActionContainer;
class Command;
}
namespace Locator {
class CommandLocator;
}
namespace Gerrit {
@@ -45,6 +55,8 @@ class GerritChange;
class GerritParameters;
class GerritDialog;
typedef QPair<QAction *, Core::Command* > ActionCommandPair;
class GerritPlugin : public QObject
{
Q_OBJECT
@@ -56,11 +68,13 @@ public:
static QString gitBinary();
static QString branch(const QString &repository);
void addToLocator(Locator::CommandLocator *locator);
public slots:
void fetchDisplay(const QSharedPointer<Gerrit::Internal::GerritChange> &change);
void fetchApply(const QSharedPointer<Gerrit::Internal::GerritChange> &change);
void fetchCheckout(const QSharedPointer<Gerrit::Internal::GerritChange> &change);
void updateActions(bool hasTopLevel);
signals:
void fetchStarted(const QSharedPointer<Gerrit::Internal::GerritChange> &change);
@@ -68,6 +82,7 @@ signals:
private slots:
void openView();
void push();
private:
QString findLocalRepository(QString project, const QString &branch) const;
@@ -75,6 +90,7 @@ private:
QSharedPointer<GerritParameters> m_parameters;
QPointer<GerritDialog> m_dialog;
ActionCommandPair m_pushToGerritPair;
};
} // namespace Internal

View File

@@ -0,0 +1,203 @@
/****************************************************************************
**
** Copyright (C) 2013 Petar Perisin.
** Contact: petar.perisin@gmail.com
**
** 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 Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "gerritpushdialog.h"
#include "ui_gerritpushdialog.h"
#include "../gitplugin.h"
#include "../gitclient.h"
#include <coreplugin/icore.h>
#include <QDir>
#include <QMessageBox>
namespace Gerrit {
namespace Internal {
GerritPushDialog::GerritPushDialog(const QString &workingDir, QWidget *parent) :
QDialog(parent),
m_workingDir(workingDir),
m_ui(new Ui::GerritPushDialog),
m_remoteBranches(new QMap<QString,QString>()),
m_localChangesFound(true)
{
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
m_ui->setupUi(this);
m_ui->repositoryLabel->setText(tr("Local Repository: %1").arg(
QDir::toNativeSeparators(workingDir)));
Git::Internal::GitClient *gitClient = Git::Internal::GitPlugin::instance()->gitClient();
QString output;
QString error;
QStringList args;
args << QLatin1String("--no-color") << QLatin1String("--format=%P")
<< QLatin1String("HEAD") << QLatin1String("--not")<< QLatin1String("--remotes");
if (!gitClient->synchronousLog(m_workingDir, args, &output) || output.isEmpty())
reject();
output.chop(1);
if (output.isEmpty()) {
output = QLatin1String("HEAD");
m_localChangesFound = false;
} else {
output = output.mid(output.lastIndexOf(QLatin1Char('\n')) + 1);
}
args.clear();
args << QLatin1String("--remotes") << QLatin1String("--contains") << output;
if (!gitClient->synchronousBranchCmd(m_workingDir, args, &output, &error))
reject();
QString head = QLatin1String("/HEAD");
QStringList refs = output.split(QLatin1Char('\n'));
foreach (const QString &reference, refs) {
if (reference.contains(head) || reference.isEmpty())
continue;
m_suggestedRemoteName = reference.left(reference.indexOf(QLatin1Char('/'))).trimmed();
m_suggestedRemoteBranch = reference.mid(reference.indexOf(QLatin1Char('/')) + 1).trimmed();
break;
}
output.clear();
error.clear();
args.clear();
args << QLatin1String("--remotes");
if (!gitClient->synchronousBranchCmd(m_workingDir, args, &output, &error))
reject();
refs.clear();
refs = output.split(QLatin1String("\n"));
foreach (const QString &reference, refs) {
if (reference.contains(head) || reference.isEmpty())
continue;
int refBranchIndex = reference.indexOf(QLatin1Char('/'));
m_remoteBranches->insertMulti(reference.left(refBranchIndex).trimmed(),
reference.mid(refBranchIndex + 1).trimmed());
}
int currIndex = 0;
QStringList remotes = m_remoteBranches->keys();
remotes.removeDuplicates();
foreach (const QString &remote, remotes) {
m_ui->remoteComboBox->addItem(remote);
if (remote == m_suggestedRemoteName)
m_ui->remoteComboBox->setCurrentIndex(currIndex);
++currIndex;
}
if (m_ui->remoteComboBox->count() < 1)
reject();
m_ui->remoteComboBox->setEnabled(m_ui->remoteComboBox->count() != 1);
connect(m_ui->remoteComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(setRemoteBranches()));
connect(m_ui->branchComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(setChangeRange()));
setRemoteBranches();
}
GerritPushDialog::~GerritPushDialog()
{
delete m_ui;
delete m_remoteBranches;
}
QString GerritPushDialog::calculateChangeRange()
{
QString remote = selectedRemoteName();
remote += QLatin1Char('/');
remote += selectedRemoteBranchName();
QStringList args(remote + QLatin1String("..HEAD"));
args << QLatin1String("--count");
QString number;
if (!Git::Internal::GitPlugin::instance()->gitClient()->
synchronousRevListCmd(m_workingDir, args, &number))
reject();
number.chop(1);
return number;
}
void GerritPushDialog::setChangeRange()
{
QString remote = selectedRemoteName();
remote += QLatin1Char('/');
remote += selectedRemoteBranchName();
m_ui->infoLabel->setText(tr("Number of commits between HEAD and %1: %2").arg(
remote, calculateChangeRange()));
}
bool GerritPushDialog::localChangesFound() const
{
return m_localChangesFound;
}
void GerritPushDialog::setRemoteBranches()
{
m_ui->branchComboBox->clear();
QMap<QString, QString>::const_iterator it;
int i = 0;
for (it = m_remoteBranches->constBegin(); it != m_remoteBranches->constEnd(); ++it) {
if (it.key() == selectedRemoteName()) {
m_ui->branchComboBox->addItem(it.value());
if (it.value() == m_suggestedRemoteBranch)
m_ui->branchComboBox->setCurrentIndex(i);
++i;
}
}
setChangeRange();
}
QString GerritPushDialog::selectedRemoteName() const
{
return m_ui->remoteComboBox->currentText();
}
QString GerritPushDialog::selectedRemoteBranchName() const
{
return m_ui->branchComboBox->currentText();
}
QString GerritPushDialog::selectedPushType() const
{
return m_ui->publicRadioButton->isChecked() ? QLatin1String("for") : QLatin1String("draft");
}
} // namespace Internal
} // namespace Gerrit

View File

@@ -0,0 +1,72 @@
/****************************************************************************
**
** Copyright (C) 2013 Petar Perisin.
** Contact: petar.perisin@gmail.com
**
** 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 Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef GERRITPUSHDIALOG_H
#define GERRITPUSHDIALOG_H
#include <QDialog>
namespace Gerrit {
namespace Internal {
namespace Ui {
class GerritPushDialog;
}
class GerritPushDialog : public QDialog
{
Q_OBJECT
public:
GerritPushDialog(const QString &workingDir, QWidget *parent = 0);
~GerritPushDialog();
QString selectedRemoteName() const;
QString selectedRemoteBranchName() const;
QString selectedPushType() const;
bool localChangesFound() const;
private slots:
void setChangeRange();
void setRemoteBranches();
private:
QString calculateChangeRange();
QString m_workingDir;
QString m_suggestedRemoteName;
QString m_suggestedRemoteBranch;
Ui::GerritPushDialog *m_ui;
QMap<QString,QString> *m_remoteBranches;
bool m_localChangesFound;
};
} // namespace Internal
} // namespace Gerrit
#endif // GERRITPUSHDIALOG_H

View File

@@ -0,0 +1,162 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Gerrit::Internal::GerritPushDialog</class>
<widget class="QDialog" name="Gerrit::Internal::GerritPushDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>306</width>
<height>304</height>
</rect>
</property>
<property name="windowTitle">
<string>Push to Gerrit</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="leftMargin">
<number>10</number>
</property>
<property name="topMargin">
<number>3</number>
</property>
<property name="rightMargin">
<number>3</number>
</property>
<property name="bottomMargin">
<number>3</number>
</property>
<item>
<widget class="QLabel" name="repositoryLabel">
<property name="text">
<string>Local Repository:</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QGroupBox" name="pushTypeGroupBox">
<property name="title">
<string>Push Type:</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QRadioButton" name="publicRadioButton">
<property name="text">
<string>Public</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="draftRadioButton">
<property name="text">
<string>Draft</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="remoteGroupBox">
<property name="title">
<string>Remote Branch:</string>
</property>
<layout class="QFormLayout" name="formLayout">
<item row="1" column="1">
<widget class="QComboBox" name="remoteComboBox"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="remoteLabel">
<property name="text">
<string>Remote Name:</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="branchLabel">
<property name="text">
<string>Branch Name:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QComboBox" name="branchComboBox"/>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QLabel" name="infoLabel">
<property name="text">
<string>Number of commits</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>Gerrit::Internal::GerritPushDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>227</x>
<y>330</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>Gerrit::Internal::GerritPushDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>295</x>
<y>336</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@@ -107,6 +107,9 @@ QtcPlugin {
"gerritparameters.h",
"gerritplugin.cpp",
"gerritplugin.h",
"gerritpushdialog.cpp",
"gerritpushdialog.h",
"gerritpushdialog.ui",
]
}
}

View File

@@ -1121,6 +1121,29 @@ static inline bool splitCommitParents(const QString &line,
return true;
}
bool GitClient::synchronousRevListCmd(const QString &workingDirectory, const QStringList &arguments,
QString *output, QString *errorMessage)
{
QByteArray outputTextData;
QByteArray errorText;
QStringList args(QLatin1String("rev-list"));
args += arguments;
const bool rc = fullySynchronousGit(workingDirectory, args, &outputTextData, &errorText);
if (!rc) {
if (errorMessage)
*errorMessage = commandOutputFromLocal8Bit(errorText);
else
outputWindow()->append(tr("Cannot execute \"git %1\" in \"%2\": %3").arg(
args.join(QLatin1String(" ")), workingDirectory,
commandOutputFromLocal8Bit(errorText)));
return false;
}
*output = commandOutputFromLocal8Bit(outputTextData);
return true;
}
// Find out the immediate parent revisions of a revision of the repository.
// Might be several in case of merges.
bool GitClient::synchronousParentRevisions(const QString &workingDirectory,
@@ -1129,8 +1152,8 @@ bool GitClient::synchronousParentRevisions(const QString &workingDirectory,
QStringList *parents,
QString *errorMessage)
{
QByteArray outputTextData;
QByteArray errorText;
QString outputText;
QString errorText;
QStringList arguments;
if (parents && !isValidRevision(revision)) { // Not Committed Yet
*parents = QStringList(QLatin1String("HEAD"));
@@ -1142,14 +1165,13 @@ bool GitClient::synchronousParentRevisions(const QString &workingDirectory,
arguments.append(QLatin1String("--"));
arguments.append(files);
}
const bool rc = fullySynchronousGit(workingDirectory, arguments, &outputTextData, &errorText);
if (!rc) {
*errorMessage = msgParentRevisionFailed(workingDirectory, revision, commandOutputFromLocal8Bit(errorText));
if (!synchronousRevListCmd(workingDirectory, arguments, &outputText, &errorText)) {
*errorMessage = msgParentRevisionFailed(workingDirectory, revision, errorText);
return false;
}
// Should result in one line of blank-delimited revisions, specifying current first
// unless it is top.
QString outputText = commandOutputFromLocal8Bit(outputTextData);
outputText.remove(QLatin1Char('\n'));
if (!splitCommitParents(outputText, 0, parents)) {
*errorMessage = msgParentRevisionFailed(workingDirectory, revision, msgInvalidRevision());
@@ -2487,14 +2509,14 @@ void GitClient::subversionLog(const QString &workingDirectory)
executeGit(workingDirectory, arguments, editor);
}
bool GitClient::synchronousPush(const QString &workingDirectory, const QString &remote)
bool GitClient::synchronousPush(const QString &workingDirectory, const QStringList &pushArgs)
{
// Disable UNIX terminals to suppress SSH prompting.
const unsigned flags = VcsBase::VcsBasePlugin::SshPasswordPrompt|VcsBase::VcsBasePlugin::ShowStdOutInLogWindow
|VcsBase::VcsBasePlugin::ShowSuccessMessage;
QStringList arguments(QLatin1String("push"));
if (!remote.isEmpty())
arguments << remote;
if (!pushArgs.isEmpty())
arguments += pushArgs;
const Utils::SynchronousProcessResponse resp =
synchronousGit(workingDirectory, arguments, flags);
return resp.result == Utils::SynchronousProcessResponse::Finished;

View File

@@ -189,6 +189,10 @@ public:
QString *errorMessage = 0);
bool synchronousShow(const QString &workingDirectory, const QString &id,
QString *output, QString *errorMessage);
bool synchronousRevListCmd(const QString &workingDirectory, const QStringList &arguments,
QString *output, QString *errorMessage = 0);
bool synchronousParentRevisions(const QString &workingDirectory,
const QStringList &files /* = QStringList() */,
const QString &revision,
@@ -208,7 +212,8 @@ public:
QString vcsGetRepositoryURL(const QString &directory);
bool synchronousFetch(const QString &workingDirectory, const QString &remote);
bool synchronousPull(const QString &workingDirectory, bool rebase);
bool synchronousPush(const QString &workingDirectory, const QString &remote = QString());
bool synchronousPush(const QString &workingDirectory,
const QStringList &pushArgs = QStringList());
bool synchronousMerge(const QString &workingDirectory, const QString &branch);
bool canRebase(const QString &workingDirectory) const;
bool synchronousRebase(const QString &workingDirectory,

View File

@@ -623,8 +623,12 @@ bool GitPlugin::initialize(const QStringList &arguments, QString *errorMessage)
/* "Gerrit" */
Gerrit::Internal::GerritPlugin *gp = new Gerrit::Internal::GerritPlugin(this);
return gp->initialize(remoteRepositoryMenu);
m_gerritPlugin = new Gerrit::Internal::GerritPlugin(this);
const bool ok = m_gerritPlugin->initialize(remoteRepositoryMenu);
m_gerritPlugin->updateActions(currentState().hasTopLevel());
m_gerritPlugin->addToLocator(m_commandLocator);
return ok;
}
GitVersionControl *GitPlugin::gitVersionControl() const
@@ -1245,6 +1249,8 @@ void GitPlugin::updateActions(VcsBase::VcsBasePlugin::ActionState as)
m_submoduleUpdateAction->setVisible(repositoryEnabled
&& QFile::exists(currentState().topLevel() + QLatin1String("/.gitmodules")));
updateRepositoryBrowserAction();
m_gerritPlugin->updateActions(repositoryEnabled);
}
void GitPlugin::updateRepositoryBrowserAction()

View File

@@ -63,6 +63,11 @@ class ParameterAction;
namespace Locator {
class CommandLocator;
}
namespace Gerrit {
namespace Internal {
class GerritPlugin;
}
}
namespace Git {
namespace Internal {
@@ -211,6 +216,7 @@ private:
QVector<Utils::ParameterAction *> m_projectActions;
QVector<QAction *> m_repositoryActions;
Utils::ParameterAction *m_applyCurrentFilePatchAction;
Gerrit::Internal::GerritPlugin *m_gerritPlugin;
GitClient *m_gitClient;
QPointer<StashDialog> m_stashDialog;

View File

@@ -174,7 +174,8 @@ void RemoteDialog::pushToRemote()
const int row = indexList.at(0).row();
const QString remoteName = m_remoteModel->remoteName(row);
m_remoteModel->client()->synchronousPush(m_remoteModel->workingDirectory(), remoteName);
m_remoteModel->client()->synchronousPush(m_remoteModel->workingDirectory(),
QStringList() << remoteName);
}
void RemoteDialog::fetchFromRemote()