Commit 7b886135 authored by Kevin Funk's avatar Kevin Funk
Browse files

Code cleanup: Refactor parent commit

parent 2df0558b
/**************************************************************************
* Copyright 2016 Artur Puzio <cytadela88@gmail.com> *
* Copyright 2016 Kevin Funk <kfunk@kde.org> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Library General Public License as *
......@@ -20,10 +21,13 @@
#include "gitnameemaildialog.h"
#include "ui_gitnameemaildialog.h"
#include <QDialog>
#include <KLocalizedString>
#include "gitplugin.h"
#include <KLocalizedString>
#include <QDialog>
#include <QPushButton>
using namespace KDevelop;
GitNameEmailDialog::GitNameEmailDialog(QWidget *parent)
......@@ -32,10 +36,10 @@ GitNameEmailDialog::GitNameEmailDialog(QWidget *parent)
{
ui->setupUi(this);
ui->submitButton->setDisabled(true);
ui->buttonBox->button(QDialogButtonBox::Save)->setDisabled(true);
connect(ui->submitButton, &QPushButton::clicked, this, &GitNameEmailDialog::accept);
connect(ui->cancelButton, &QPushButton::clicked, this, &GitNameEmailDialog::reject);
connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &GitNameEmailDialog::accept);
connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &GitNameEmailDialog::reject);
QRegularExpression rx(".+");
auto validator = new QRegularExpressionValidator(rx, this);
......@@ -50,8 +54,8 @@ GitNameEmailDialog::~GitNameEmailDialog() = default;
void GitNameEmailDialog::updateUi()
{
ui->submitButton->setDisabled(!ui->nameEdit->hasAcceptableInput() or
!ui->emailEdit->hasAcceptableInput());
ui->buttonBox->button(QDialogButtonBox::Save)->setDisabled(
!ui->nameEdit->hasAcceptableInput() || !ui->emailEdit->hasAcceptableInput());
}
void GitNameEmailDialog::setName(const QString& name)
......@@ -78,5 +82,3 @@ bool GitNameEmailDialog::isGlobal() const
{
return ui->globalCheckBox->isChecked();
}
#include "gitnameemaildialog.moc"
/**************************************************************************
* Copyright 2016 Artur Puzio <cytadela88@gmail.com> *
* Copyright 2016 Kevin Funk <kfunk@kde.org> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Library General Public License as *
......@@ -20,16 +21,8 @@
#ifndef KDEVPLATFORM_PLUGIN_GIT_NAMEEMAILDIALOG_H
#define KDEVPLATFORM_PLUGIN_GIT_NAMEEMAILDIALOG_H
#include <QtWidgets>
#include <QDialog>
class QCheckBox;
class QDialogButtonBox;
class QGroupBox;
class QLabel;
class QLineEdit;
class QPushButton;
namespace Ui { class GitNameEmailDialog; }
class GitNameEmailDialog : public QDialog
......@@ -37,16 +30,19 @@ class GitNameEmailDialog : public QDialog
Q_OBJECT
public:
explicit GitNameEmailDialog(QWidget *parent = 0);
explicit GitNameEmailDialog(QWidget *parent = nullptr);
~GitNameEmailDialog() override;
void setName(const QString& name);
void setEmail(const QString& email);
QString name() const;
void setName(const QString& name);
QString email() const;
void setEmail(const QString& email);
bool isGlobal() const;
private slots:
void updateUi();
private:
QScopedPointer<Ui::GitNameEmailDialog> ui;
};
......
......@@ -9,136 +9,99 @@
<rect>
<x>0</x>
<y>0</y>
<width>308</width>
<height>216</height>
<width>446</width>
<height>282</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>16777215</height>
</size>
</property>
<property name="windowTitle">
<string>Git name and email</string>
</property>
<property name="locale">
<locale language="English" country="UnitedStates"/>
<string>Configure Name and Email for Git</string>
</property>
<widget class="QWidget" name="layoutWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>308</width>
<height>216</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<property name="leftMargin">
<number>3</number>
</property>
<property name="topMargin">
<number>3</number>
</property>
<property name="rightMargin">
<number>3</number>
</property>
<property name="bottomMargin">
<number>3</number>
</property>
<item row="0" column="0">
<widget class="QLabel" name="descriptionLabel">
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;You have not yet configured the name and email to be associated with your Git commits.&lt;br/&gt;The values you enter here will be written to the Git configuration, either locally for the current project only, or globally for all Git projects.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="0">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Name</string>
</property>
<property name="buddy">
<cstring>nameEdit</cstring>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="nameEdit"/>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="emailEdit"/>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Emai&amp;l</string>
</property>
<property name="buddy">
<cstring>emailEdit</cstring>
</property>
</widget>
</item>
</layout>
</item>
<item row="3" column="0">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QCheckBox" name="globalCheckBox">
<property name="text">
<string>Global</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>1</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="submitButton">
<property name="text">
<string>Set</string>
</property>
<property name="default">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="cancelButton">
<property name="text">
<string>Cancel</string>
</property>
<property name="autoDefault">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<layout class="QVBoxLayout" name="_2">
<item>
<widget class="QLabel" name="descriptionLabel">
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;You have not yet configured the name and email to be associated with your Git commits.&lt;br/&gt;The values you enter here will be written to the Git configuration, either locally for the current project only, or globally for all Git projects.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>&amp;Name:</string>
</property>
<property name="buddy">
<cstring>nameEdit</cstring>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="nameEdit"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Emai&amp;l:</string>
</property>
<property name="buddy">
<cstring>emailEdit</cstring>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="emailEdit"/>
</item>
</layout>
</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>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QCheckBox" name="globalCheckBox">
<property name="text">
<string>Write to global config</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>1</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Save</set>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections/>
......
......@@ -421,23 +421,12 @@ VcsJob* GitPlugin::commit(const QString& message,
{
if (localLocations.empty() || message.isEmpty())
return errorsFound(i18n("No files or message specified"));
QDir dir = dotGitDirectory(localLocations.front());
QUrl url = QUrl::fromLocalFile(dir.absolutePath());
{
QString name = readConfigOption(url, QStringLiteral("user.name"));
QString email = readConfigOption(url, QStringLiteral("user.email"));
if (email.isEmpty() || name.isEmpty()) {
GitNameEmailDialog dialog;
dialog.setName(name);
dialog.setEmail(email);
if (dialog.exec()) {
setConfigOption(url, QStringLiteral("user.name"), dialog.name(), dialog.isGlobal())->exec();
setConfigOption(url, QStringLiteral("user.email"), dialog.email(), dialog.isGlobal())->exec();
} else {
return errorsFound(i18n("Email or name for git not specified"));
}
}
const QDir dir = dotGitDirectory(localLocations.front());
if (!ensureValidGitIdentity(dir)) {
return errorsFound(i18n("Email or name for Git not specified"));
}
DVcsJob* job = new DVcsJob(dir, this);
job->setType(VcsJob::Commit);
QList<QUrl> files = (recursion == IBasicVersionControl::Recursive ? localLocations : preventRecursion(localLocations));
......@@ -448,6 +437,28 @@ VcsJob* GitPlugin::commit(const QString& message,
return job;
}
bool GitPlugin::ensureValidGitIdentity(const QDir& dir)
{
const QUrl url = QUrl::fromLocalFile(dir.absolutePath());
const QString name = readConfigOption(url, QStringLiteral("user.name"));
const QString email = readConfigOption(url, QStringLiteral("user.email"));
if (!email.isEmpty() && !name.isEmpty()) {
return true; // already okay
}
GitNameEmailDialog dialog;
dialog.setName(name);
dialog.setEmail(email);
if (!dialog.exec()) {
return false;
}
runSynchronously(setConfigOption(url, QStringLiteral("user.name"), dialog.name(), dialog.isGlobal()));
runSynchronously(setConfigOption(url, QStringLiteral("user.email"), dialog.email(), dialog.isGlobal()));
return true;
}
void GitPlugin::addNotVersionedFiles(const QDir& dir, const QList<QUrl>& files)
{
QStringList otherStr = getLsFiles(dir, QStringList() << QStringLiteral("--others"), KDevelop::OutputJob::Silent);
......
......@@ -197,6 +197,7 @@ signals:
void repositoryBranchChanged(const QUrl& repository);
private:
bool ensureValidGitIdentity(const QDir& dir);
void addNotVersionedFiles(const QDir& dir, const QList<QUrl>& files);
//commit dialog "main" helper
......
......@@ -233,33 +233,43 @@ void GitInitTest::testInit()
repoInit();
}
void GitInitTest::testLocalConfig()
static QString runCommand(const QString& cmd, const QStringList& args)
{
QProcess proc;
proc.setWorkingDirectory(gitTest_BaseDir());
proc.start(cmd, args);
proc.waitForFinished();
return proc.readAllStandardOutput().trimmed();
}
void GitInitTest::testReadAndSetConfigOption()
{
repoInit();
qDebug() << "read notexisting.asdads";
QString pluginRead1 = m_plugin->readConfigOption(QUrl::fromLocalFile(gitTest_BaseDir()), QStringLiteral("notexisting.asdads"));
QCOMPARE(pluginRead1, QStringLiteral(""));
qDebug() << "write user.name = \"John Tester\"";
auto job = m_plugin->setConfigOption(QUrl::fromLocalFile(gitTest_BaseDir()), QStringLiteral("user.name"), QStringLiteral("John Tester"));
VERIFYJOB(job);
QProcess myRead1;
myRead1.setWorkingDirectory(gitTest_BaseDir());
myRead1.start("git", QStringList() << "config" << "--get" << QStringLiteral("user.name"));
myRead1.waitForFinished();
QString myRead1result = myRead1.readAllStandardOutput().trimmed();
QCOMPARE(myRead1result, QStringLiteral("John Tester"));
qDebug() << "read user.name";
QString pluginRead2 = m_plugin->readConfigOption(QUrl::fromLocalFile(gitTest_BaseDir()), QStringLiteral("user.name"));
QProcess myRead2;
myRead2.setWorkingDirectory(gitTest_BaseDir());
myRead2.start("git", QStringList() << "config" << "--get" << QStringLiteral("user.name"));
myRead2.waitForFinished();
QCOMPARE(pluginRead2, QStringLiteral("John Tester"));
QString myRead2result = myRead2.readAllStandardOutput().trimmed();
QCOMPARE(myRead2result, QStringLiteral("John Tester"));
{
qDebug() << "read non-existing config option";
QString nameFromPlugin = m_plugin->readConfigOption(QUrl::fromLocalFile(gitTest_BaseDir()),
QStringLiteral("notexisting.asdads"));
QVERIFY(nameFromPlugin.isEmpty());
}
{
qDebug() << "write user.name = \"John Tester\"";
auto job = m_plugin->setConfigOption(QUrl::fromLocalFile(gitTest_BaseDir()),
QStringLiteral("user.name"), QStringLiteral("John Tester"));
VERIFYJOB(job);
const auto name = runCommand("git", {"config", "--get", QStringLiteral("user.name")});
QCOMPARE(name, QStringLiteral("John Tester"));
}
{
qDebug() << "read user.name";
const QString nameFromPlugin = m_plugin->readConfigOption(QUrl::fromLocalFile(gitTest_BaseDir()),
QStringLiteral("user.name"));
QCOMPARE(nameFromPlugin, QStringLiteral("John Tester"));
const auto name = runCommand("git", {"config", "--get", QStringLiteral("user.name")});
QCOMPARE(name, QStringLiteral("John Tester"));
}
}
void GitInitTest::testAdd()
......
......@@ -51,7 +51,7 @@ private slots:
void cleanup();
void testInit();
void testLocalConfig();
void testReadAndSetConfigOption();
void testAdd();
void testCommit();
void testBranching();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment