diff --git a/plugins/git/CMakeLists.txt b/plugins/git/CMakeLists.txt --- a/plugins/git/CMakeLists.txt +++ b/plugins/git/CMakeLists.txt @@ -12,8 +12,10 @@ gitpluginmetadata.cpp gitjob.cpp gitplugincheckinrepositoryjob.cpp + gitnameemaildialog.cpp ) ki18n_wrap_ui(kdevgit_PART_SRCS stashmanagerdialog.ui) +ki18n_wrap_ui(kdevgit_PART_SRCS gitnameemaildialog.ui) kdevplatform_add_plugin(kdevgit JSON kdevgit.json SOURCES ${kdevgit_PART_SRCS}) target_link_libraries(kdevgit KDev::Util diff --git a/plugins/git/gitnameemaildialog.h b/plugins/git/gitnameemaildialog.h new file mode 100644 --- /dev/null +++ b/plugins/git/gitnameemaildialog.h @@ -0,0 +1,54 @@ + /************************************************************************** + * Copyright 2016 Artur Puzio * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU Library General Public License as * + * published by the Free Software Foundation; either version 2 of the * + * License, or (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#ifndef KDEVPLATFORM_PLUGIN_GIT_NAMEEMAILDIALOG_H +#define KDEVPLATFORM_PLUGIN_GIT_NAMEEMAILDIALOG_H + +#include +#include + + +class QCheckBox; +class QDialogButtonBox; +class QGroupBox; +class QLabel; +class QLineEdit; +class QPushButton; +namespace Ui { class GitNameEmailDialog; } + +class GitNameEmailDialog : public QDialog +{ + Q_OBJECT + +public: + explicit GitNameEmailDialog(QWidget *parent = 0); + ~GitNameEmailDialog() override; + void setName(const QString & name); + void setEmail(const QString & email); + QString name() const; + QString email() const; + bool isGlobal() const; + +private slots: + void refrashSubmitButton(); +private: + QScopedPointer ui; +}; + +#endif //KDEVPLATFORM_PLUGIN_GIT_NAMEEMAILDIALOG_H diff --git a/plugins/git/gitnameemaildialog.cpp b/plugins/git/gitnameemaildialog.cpp new file mode 100644 --- /dev/null +++ b/plugins/git/gitnameemaildialog.cpp @@ -0,0 +1,82 @@ + /************************************************************************** + * Copyright 2016 Artur Puzio * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU Library General Public License as * + * published by the Free Software Foundation; either version 2 of the * + * License, or (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include "gitnameemaildialog.h" +#include "ui_gitnameemaildialog.h" + +#include +#include +#include "gitplugin.h" + +using namespace KDevelop; + +GitNameEmailDialog::GitNameEmailDialog(QWidget *parent) + : QDialog(parent), + ui(new Ui::GitNameEmailDialog) +{ + ui->setupUi(this); + + ui->submitButton->setDisabled(true); + + connect(ui->submitButton, &QPushButton::clicked, this, &GitNameEmailDialog::accept); + connect(ui->cancelButton, &QPushButton::clicked, this, &GitNameEmailDialog::reject); + + QRegularExpression rx(".+"); + auto validator = new QRegularExpressionValidator(rx, this); + ui->emailEdit->setValidator(validator); + ui->nameEdit->setValidator(validator); + + connect(ui->emailEdit, &QLineEdit::textChanged, this, &GitNameEmailDialog::refrashSubmitButton); + connect(ui->nameEdit, &QLineEdit::textChanged, this, &GitNameEmailDialog::refrashSubmitButton); +} + +GitNameEmailDialog::~GitNameEmailDialog() = default; + +void GitNameEmailDialog::refrashSubmitButton() +{ + ui->submitButton->setDisabled(!ui->nameEdit->hasAcceptableInput() or + !ui->emailEdit->hasAcceptableInput()); +} + +void GitNameEmailDialog::setName(const QString & name) +{ + ui->nameEdit->setText(name); +} + +void GitNameEmailDialog::setEmail(const QString & email) +{ + ui->emailEdit->setText(email); +} + +QString GitNameEmailDialog::name() const +{ + return ui->nameEdit->text(); +} + +QString GitNameEmailDialog::email() const +{ + return ui->emailEdit->text(); +} + +bool GitNameEmailDialog::isGlobal() const +{ + return ui->globalCheckBox->isChecked(); +} + +#include "gitnameemaildialog.moc" diff --git a/plugins/git/gitnameemaildialog.ui b/plugins/git/gitnameemaildialog.ui new file mode 100644 --- /dev/null +++ b/plugins/git/gitnameemaildialog.ui @@ -0,0 +1,145 @@ + + + GitNameEmailDialog + + + Qt::WindowModal + + + + 0 + 0 + 308 + 216 + + + + + 0 + 0 + + + + + 16777215 + 16777215 + + + + Git name and email + + + + + + + + 0 + 0 + 308 + 216 + + + + + 3 + + + 3 + + + 3 + + + 3 + + + + + <html><head/><body><p>You have not yet configured the name and email to be associated with your Git commits.<br/>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.</p></body></html> + + + true + + + + + + + + + Name + + + nameEdit + + + + + + + + + + + + + Emai&l + + + emailEdit + + + + + + + + + + + Global + + + + + + + Qt::Horizontal + + + + 40 + 1 + + + + + + + + Set + + + true + + + + + + + Cancel + + + false + + + + + + + + + + + diff --git a/plugins/git/gitplugin.h b/plugins/git/gitplugin.h --- a/plugins/git/gitplugin.h +++ b/plugins/git/gitplugin.h @@ -145,7 +145,8 @@ KDevelop::CheckInRepositoryJob* isInRepository(KTextEditor::Document* document) override; - KDevelop::DVcsJob* setConfigOption(const QUrl& repository, const QString& key, const QString& value); + KDevelop::DVcsJob* setConfigOption(const QUrl& repository, const QString& key, const QString& value, bool global = false); + QString readConfigOption(const QUrl& repository, const QString& key); // this indicates whether the diff() function will generate a diff (patch) which // includes the working copy directory name or not (in which case git diff is called diff --git a/plugins/git/gitplugin.cpp b/plugins/git/gitplugin.cpp --- a/plugins/git/gitplugin.cpp +++ b/plugins/git/gitplugin.cpp @@ -56,6 +56,7 @@ #include "gitjob.h" #include "gitmessagehighlighter.h" #include "gitplugincheckinrepositoryjob.h" +#include "gitnameemaildialog.h" #include "debug.h" Q_LOGGING_CATEGORY(PLUGIN_GIT, "kdevplatform.plugins.git") @@ -420,8 +421,23 @@ { 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")); + } + } + } DVcsJob* job = new DVcsJob(dir, this); job->setType(VcsJob::Commit); QList files = (recursion == IBasicVersionControl::Recursive ? localLocations : preventRecursion(localLocations)); @@ -1455,11 +1471,25 @@ return job; } -DVcsJob* GitPlugin::setConfigOption(const QUrl& repository, const QString& key, const QString& value) +DVcsJob* GitPlugin::setConfigOption(const QUrl& repository, const QString& key, const QString& value, bool global) { auto job = new DVcsJob(urlDir(repository), this); - *job << "git" << "config" << key << value; + QStringList args; + args << "git" << "config"; + if(global) + args << "--global"; + args << key << value; + *job << args; return job; } +QString GitPlugin::readConfigOption(const QUrl& repository, const QString& key) +{ + QProcess exec; + exec.setWorkingDirectory(urlDir(repository).absolutePath()); + exec.start("git", QStringList() << "config" << "--get" << key); + exec.waitForFinished(); + return exec.readAllStandardOutput().trimmed(); +} + #include "gitplugin.moc" diff --git a/plugins/git/tests/CMakeLists.txt b/plugins/git/tests/CMakeLists.txt --- a/plugins/git/tests/CMakeLists.txt +++ b/plugins/git/tests/CMakeLists.txt @@ -13,8 +13,10 @@ ../gitjob.cpp ../gitmessagehighlighter.cpp ../gitplugincheckinrepositoryjob.cpp + ../gitnameemaildialog.cpp ) ki18n_wrap_ui(gittest_SRCS ../stashmanagerdialog.ui) + ki18n_wrap_ui(gittest_SRCS ../gitnameemaildialog.ui) ecm_add_test(${gittest_SRCS} TEST_NAME test_kdevgit LINK_LIBRARIES Qt5::Test KDev::Vcs KDev::Util KDev::Tests diff --git a/plugins/git/tests/test_git.h b/plugins/git/tests/test_git.h --- a/plugins/git/tests/test_git.h +++ b/plugins/git/tests/test_git.h @@ -43,14 +43,16 @@ void repoInit(); void addFiles(); void commitFiles(); + void runTestLocalConfig(bool action, const QString option, const QString data); private slots: void initTestCase(); void cleanupTestCase(); void init(); void cleanup(); void testInit(); + void testLocalConfig(); void testAdd(); void testCommit(); void testBranching(); diff --git a/plugins/git/tests/test_git.cpp b/plugins/git/tests/test_git.cpp --- a/plugins/git/tests/test_git.cpp +++ b/plugins/git/tests/test_git.cpp @@ -233,6 +233,57 @@ repoInit(); } +void GitInitTest::runTestLocalConfig(bool action, const QString option, const QString data) +{ + auto j = m_plugin->status(QList() << QUrl::fromLocalFile(gitTest_BaseDir())); + VERIFYJOB(j); + if(action) { //set + auto job = m_plugin->setConfigOption(QUrl::fromLocalFile(gitTest_BaseDir()), option, data); + VERIFYJOB(job); + + QProcess myRead; + myRead.setWorkingDirectory(gitTest_BaseDir()); + myRead.start("git", QStringList() << "config" << "--get" << option); + myRead.waitForFinished(); + QString result = myRead.readAllStandardOutput(); + if(result.endsWith('\n')) + result.chop(1); + QCOMPARE(result, data); + } else { //read + QString pluginRead = m_plugin->readConfigOption(QUrl::fromLocalFile(gitTest_BaseDir()), option); + + QProcess myRead; + myRead.setWorkingDirectory(gitTest_BaseDir()); + myRead.start("git", QStringList() << "config" << "--get" << option); + myRead.waitForFinished(); + QString result = myRead.readAllStandardOutput(); + if(result.endsWith('\n')) + result.chop(1); + QCOMPARE(result, data); + QCOMPARE(pluginRead, data); + } + +} + +void GitInitTest::testLocalConfig() +{ + repoInit(); + qDebug() << "1getName1"; runTestLocalConfig(false , "user.name" , "My Name"); + qDebug() << "2getEmail1"; runTestLocalConfig(false , "user.email" , "me@example.com"); + qDebug() << "3getEmail1"; runTestLocalConfig(false , "user.email" , "me@example.com"); + qDebug() << "4setName2"; runTestLocalConfig(true , "user.name" , "John Tester"); + qDebug() << "5getName2"; runTestLocalConfig(false , "user.name" , "John Tester"); + qDebug() << "6getEmail1"; runTestLocalConfig(false , "user.email" , "me@example.com"); + qDebug() << "7getName2"; runTestLocalConfig(false , "user.name" , "John Tester"); + qDebug() << "8setEmail2"; runTestLocalConfig(true , "user.email" , "john@tester.com"); + qDebug() << "9getName2"; runTestLocalConfig(false , "user.name" , "John Tester"); + qDebug() << "10getEmail2"; runTestLocalConfig(false , "user.email" , "john@tester.com"); + qDebug() << "11setBackName1"; runTestLocalConfig(true , "user.name" , "My Name"); + qDebug() << "12setBackEmail1"; runTestLocalConfig(true , "user.email" , "me@example.com"); + qDebug() << "13getName1"; runTestLocalConfig(false , "user.name" , "My Name"); + qDebug() << "14getEmail1"; runTestLocalConfig(false , "user.email" , "me@example.com"); +} + void GitInitTest::testAdd() { repoInit();