Git: Enable local branch selection in Push to Gerrit

Show dialog even if there are no local commits in current branch

Change-Id: I11e0c6505981712df51bb33694a6cba9704d7324
Reviewed-by: Tobias Hunger <tobias.hunger@digia.com>
This commit is contained in:
Orgad Shaneh
2014-02-08 21:36:06 +02:00
committed by Orgad Shaneh
parent 292e4599aa
commit 1ed9f6bbca
8 changed files with 189 additions and 29 deletions

View File

@@ -0,0 +1,69 @@
/****************************************************************************
**
** Copyright (C) 2014 Orgad Shaneh <orgads@gmail.com>.
** Contact: http://www.qt-project.org/legal
**
** 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 "branchcombobox.h"
#include "../gitplugin.h"
#include "../gitclient.h"
using namespace Git::Internal;
using namespace Gerrit::Internal;
BranchComboBox::BranchComboBox(QWidget *parent) :
QComboBox(parent),
m_detached(false)
{
m_client = GitPlugin::instance()->gitClient();
}
void BranchComboBox::init(const QString &repository)
{
m_repository = repository;
QString currentBranch = m_client->synchronousCurrentLocalBranch(repository);
if (currentBranch.isEmpty()) {
m_detached = true;
currentBranch = QLatin1String("HEAD");
addItem(currentBranch);
}
QString output;
const QString branchPrefix(QLatin1String("refs/heads/"));
QStringList args;
args << QLatin1String("--format=%(refname)") << branchPrefix;
if (!m_client->synchronousForEachRefCmd(m_repository, args, &output))
return;
QStringList branches = output.trimmed().split(QLatin1Char('\n'));
foreach (const QString &ref, branches) {
const QString branch = ref.mid(branchPrefix.size());
addItem(branch);
}
if (currentBranch.isEmpty())
return;
int index = findText(currentBranch);
if (index != -1)
setCurrentIndex(index);
}

View File

@@ -0,0 +1,59 @@
/****************************************************************************
**
** Copyright (C) 2014 Orgad Shaneh <orgads@gmail.com>.
** Contact: http://www.qt-project.org/legal
**
** 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 BRANCHCOMBOBOX_H
#define BRANCHCOMBOBOX_H
#include <QComboBox>
namespace Git {
namespace Internal {
class GitClient;
}
}
namespace Gerrit {
namespace Internal {
class BranchComboBox : public QComboBox
{
public:
BranchComboBox(QWidget *parent = 0);
void init(const QString &repository);
private:
Git::Internal::GitClient *m_client;
QString m_repository;
bool m_detached;
};
} // namespace Internal
} // namespace Gerrit
#endif // BRANCHCOMBOBOX_H

View File

@@ -3,13 +3,15 @@ SOURCES += $$PWD/gerritdialog.cpp \
$$PWD/gerritparameters.cpp \ $$PWD/gerritparameters.cpp \
$$PWD/gerritplugin.cpp \ $$PWD/gerritplugin.cpp \
$$PWD/gerritoptionspage.cpp \ $$PWD/gerritoptionspage.cpp \
$$PWD/gerritpushdialog.cpp $$PWD/gerritpushdialog.cpp \
$$PWD/branchcombobox.cpp
HEADERS += $$PWD/gerritdialog.h \ HEADERS += $$PWD/gerritdialog.h \
$$PWD/gerritmodel.h \ $$PWD/gerritmodel.h \
$$PWD/gerritparameters.h \ $$PWD/gerritparameters.h \
$$PWD/gerritplugin.h \ $$PWD/gerritplugin.h \
$$PWD/gerritoptionspage.h \ $$PWD/gerritoptionspage.h \
$$PWD/gerritpushdialog.h $$PWD/gerritpushdialog.h \
$$PWD/branchcombobox.h
FORMS += $$PWD/gerritpushdialog.ui FORMS += $$PWD/gerritpushdialog.ui

View File

@@ -313,9 +313,6 @@ void GerritPlugin::push(const QString &topLevel)
// QScopedPointer is required to delete the dialog when leaving the function // QScopedPointer is required to delete the dialog when leaving the function
GerritPushDialog dialog(topLevel, m_reviewers, ICore::mainWindow()); GerritPushDialog dialog(topLevel, m_reviewers, ICore::mainWindow());
if (!dialog.localChangesFound())
return;
if (!dialog.valid()) { if (!dialog.valid()) {
QMessageBox::warning(ICore::mainWindow(), tr("Initialization Failed"), QMessageBox::warning(ICore::mainWindow(), tr("Initialization Failed"),
tr("Failed to initialize dialog. Aborting.")); tr("Failed to initialize dialog. Aborting."));

View File

@@ -29,12 +29,14 @@
#include "gerritpushdialog.h" #include "gerritpushdialog.h"
#include "ui_gerritpushdialog.h" #include "ui_gerritpushdialog.h"
#include "branchcombobox.h"
#include "../gitplugin.h" #include "../gitplugin.h"
#include "../gitclient.h" #include "../gitclient.h"
#include <QDateTime> #include <QDateTime>
#include <QDir> #include <QDir>
#include <QPushButton>
#include <QRegExpValidator> #include <QRegExpValidator>
using namespace Git::Internal; using namespace Git::Internal;
@@ -61,7 +63,6 @@ QString GerritPushDialog::determineRemoteBranch()
{ {
const QString earliestCommit = m_ui->commitView->earliestCommit(); const QString earliestCommit = m_ui->commitView->earliestCommit();
m_localChangesFound = true;
QString output; QString output;
QString error; QString error;
QStringList args; QStringList args;
@@ -73,7 +74,11 @@ QString GerritPushDialog::determineRemoteBranch()
const QString head = QLatin1String("/HEAD"); const QString head = QLatin1String("/HEAD");
QStringList refs = output.split(QLatin1Char('\n')); QStringList refs = output.split(QLatin1Char('\n'));
const QString remoteTrackingBranch = m_client->synchronousTrackingBranch(m_workingDir); QString localBranch = m_ui->localBranchComboBox->currentText();
if (localBranch == QLatin1String("HEAD"))
localBranch.clear();
const QString remoteTrackingBranch = m_client->synchronousTrackingBranch(m_workingDir,
localBranch);
QString remoteBranch; QString remoteBranch;
foreach (const QString &reference, refs) { foreach (const QString &reference, refs) {
const QString ref = reference.trimmed(); const QString ref = reference.trimmed();
@@ -139,9 +144,6 @@ GerritPushDialog::GerritPushDialog(const QString &workingDir, const QString &rev
m_ui->repositoryLabel->setText(tr("<b>Local repository:</b> %1").arg( m_ui->repositoryLabel->setText(tr("<b>Local repository:</b> %1").arg(
QDir::toNativeSeparators(workingDir))); QDir::toNativeSeparators(workingDir)));
if (!m_ui->commitView->init(workingDir))
return;
PushItemDelegate *delegate = new PushItemDelegate(m_ui->commitView); PushItemDelegate *delegate = new PushItemDelegate(m_ui->commitView);
delegate->setParent(this); delegate->setParent(this);
@@ -156,12 +158,18 @@ GerritPushDialog::GerritPushDialog(const QString &workingDir, const QString &rev
} else { } else {
connect(m_ui->remoteComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(setRemoteBranches())); connect(m_ui->remoteComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(setRemoteBranches()));
} }
m_ui->localBranchComboBox->init(workingDir);
connect(m_ui->localBranchComboBox, SIGNAL(currentIndexChanged(int)),
this, SLOT(updateCommits(int)));
connect(m_ui->targetBranchComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(setChangeRange())); connect(m_ui->targetBranchComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(setChangeRange()));
setRemoteBranches(); setRemoteBranches();
m_ui->reviewersLineEdit->setText(reviewerList); m_ui->reviewersLineEdit->setText(reviewerList);
m_ui->topicLineEdit->setValidator(new QRegExpValidator(QRegExp(QLatin1String("^\\S+$")), this)); m_ui->topicLineEdit->setValidator(new QRegExpValidator(QRegExp(QLatin1String("^\\S+$")), this));
updateCommits(m_ui->localBranchComboBox->currentIndex());
m_valid = true; m_valid = true;
} }
@@ -175,13 +183,13 @@ QString GerritPushDialog::selectedCommit() const
return m_ui->commitView->commit(); return m_ui->commitView->commit();
} }
QString GerritPushDialog::calculateChangeRange() QString GerritPushDialog::calculateChangeRange(const QString &branch)
{ {
QString remote = selectedRemoteName(); QString remote = selectedRemoteName();
remote += QLatin1Char('/'); remote += QLatin1Char('/');
remote += selectedRemoteBranchName(); remote += selectedRemoteBranchName();
QStringList args(remote + QLatin1String("..HEAD")); QStringList args(remote + QLatin1String("..") + branch);
args << QLatin1String("--count"); args << QLatin1String("--count");
QString number; QString number;
@@ -202,13 +210,11 @@ void GerritPushDialog::setChangeRange()
QString remote = selectedRemoteName(); QString remote = selectedRemoteName();
remote += QLatin1Char('/'); remote += QLatin1Char('/');
remote += selectedRemoteBranchName(); remote += selectedRemoteBranchName();
m_ui->infoLabel->setText(tr("Number of commits between HEAD and %1: %2").arg( const QString branch = m_ui->localBranchComboBox->currentText();
remote, calculateChangeRange())); m_ui->infoLabel->setText(tr("Number of commits between %1 and %2: %3")
} .arg(branch)
.arg(remote)
bool GerritPushDialog::localChangesFound() const .arg(calculateChangeRange(branch)));
{
return m_localChangesFound;
} }
bool GerritPushDialog::valid() const bool GerritPushDialog::valid() const
@@ -245,6 +251,14 @@ void GerritPushDialog::setRemoteBranches(bool includeOld)
m_ui->targetBranchComboBox->blockSignals(blocked); m_ui->targetBranchComboBox->blockSignals(blocked);
} }
void GerritPushDialog::updateCommits(int index)
{
const QString branch = m_ui->localBranchComboBox->itemText(index);
const bool hasLocalCommits = m_ui->commitView->init(m_workingDir, branch);
m_ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(hasLocalCommits);
setChangeRange();
}
QString GerritPushDialog::selectedRemoteName() const QString GerritPushDialog::selectedRemoteName() const
{ {
return m_ui->remoteComboBox->currentText(); return m_ui->remoteComboBox->currentText();

View File

@@ -61,12 +61,12 @@ public:
QString selectedPushType() const; QString selectedPushType() const;
QString selectedTopic() const; QString selectedTopic() const;
QString reviewers() const; QString reviewers() const;
bool localChangesFound() const;
bool valid() const; bool valid() const;
private slots: private slots:
void setChangeRange(); void setChangeRange();
void setRemoteBranches(bool includeOld = false); void setRemoteBranches(bool includeOld = false);
void updateCommits(int index);
private: private:
typedef QPair<QString, QDate> BranchDate; typedef QPair<QString, QDate> BranchDate;
@@ -74,12 +74,11 @@ private:
QString determineRemoteBranch(); QString determineRemoteBranch();
void initRemoteBranches(); void initRemoteBranches();
QString calculateChangeRange(); QString calculateChangeRange(const QString &branch);
QString m_workingDir; QString m_workingDir;
QString m_suggestedRemoteBranch; QString m_suggestedRemoteBranch;
Ui::GerritPushDialog *m_ui; Ui::GerritPushDialog *m_ui;
RemoteBranchesMap m_remoteBranches; RemoteBranchesMap m_remoteBranches;
bool m_localChangesFound;
bool m_valid; bool m_valid;
Git::Internal::GitClient *m_client; Git::Internal::GitClient *m_client;
}; };

View File

@@ -47,6 +47,19 @@
<enum>QFormLayout::AllNonFixedFieldsGrow</enum> <enum>QFormLayout::AllNonFixedFieldsGrow</enum>
</property> </property>
<item row="1" column="0"> <item row="1" column="0">
<widget class="QLabel" name="localBranchLabel">
<property name="text">
<string>&amp;Local branch:</string>
</property>
<property name="buddy">
<cstring>localBranchComboBox</cstring>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="BranchComboBox" name="localBranchComboBox"/>
</item>
<item row="2" column="0">
<widget class="QLabel" name="remoteLabel"> <widget class="QLabel" name="remoteLabel">
<property name="text"> <property name="text">
<string>R&amp;emote:</string> <string>R&amp;emote:</string>
@@ -56,10 +69,10 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="1" column="1"> <item row="2" column="1">
<widget class="QComboBox" name="remoteComboBox"/> <widget class="QComboBox" name="remoteComboBox"/>
</item> </item>
<item row="2" column="0"> <item row="3" column="0">
<widget class="QLabel" name="targetBranchLabel"> <widget class="QLabel" name="targetBranchLabel">
<property name="text"> <property name="text">
<string>Target &amp;branch:</string> <string>Target &amp;branch:</string>
@@ -69,10 +82,10 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="2" column="1"> <item row="3" column="1">
<widget class="QComboBox" name="targetBranchComboBox"/> <widget class="QComboBox" name="targetBranchComboBox"/>
</item> </item>
<item row="3" column="0"> <item row="4" column="0">
<widget class="QLabel" name="topicLabel"> <widget class="QLabel" name="topicLabel">
<property name="text"> <property name="text">
<string>&amp;Topic:</string> <string>&amp;Topic:</string>
@@ -82,17 +95,17 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="3" column="1"> <item row="4" column="1">
<widget class="QLineEdit" name="topicLineEdit"/> <widget class="QLineEdit" name="topicLineEdit"/>
</item> </item>
<item row="4" column="0"> <item row="5" column="0">
<widget class="QCheckBox" name="draftCheckBox"> <widget class="QCheckBox" name="draftCheckBox">
<property name="text"> <property name="text">
<string>&amp;Draft</string> <string>&amp;Draft</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="5" column="0" colspan="2"> <item row="6" column="0" colspan="2">
<widget class="QLabel" name="infoLabel"> <widget class="QLabel" name="infoLabel">
<property name="text"> <property name="text">
<string>Number of commits</string> <string>Number of commits</string>
@@ -178,6 +191,11 @@ Partial names can be used if they are unambiguous.</string>
<extends>QTreeView</extends> <extends>QTreeView</extends>
<header location="global">git/logchangedialog.h</header> <header location="global">git/logchangedialog.h</header>
</customwidget> </customwidget>
<customwidget>
<class>BranchComboBox</class>
<extends>QComboBox</extends>
<header location="global">git/gerrit/branchcombobox.h</header>
</customwidget>
</customwidgets> </customwidgets>
<resources/> <resources/>
<connections> <connections>

View File

@@ -101,6 +101,8 @@ QtcPlugin {
name: "Gerrit" name: "Gerrit"
prefix: "gerrit/" prefix: "gerrit/"
files: [ files: [
"branchcombobox.cpp",
"branchcombobox.h",
"gerritdialog.cpp", "gerritdialog.cpp",
"gerritdialog.h", "gerritdialog.h",
"gerritmodel.cpp", "gerritmodel.cpp",