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,55 @@ + /************************************************************************** + * 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 setNameField(QString & name); + void setEmailField(QString & email); + QString getNameField() const; + QString getEmailField() const; + bool getIsGlobal() const; + +private slots: + void refrashSubmitButton(); +private: + Ui::GitNameEmailDialog* ui; + QValidator* validator; +}; + +#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); + + QRegExp rx(".+"); + validator = new QRegExpValidator(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() +{ + delete ui; + delete validator; +} + +void GitNameEmailDialog::refrashSubmitButton() +{ + ui->submitButton->setDisabled( !ui->nameEdit->hasAcceptableInput() or + !ui->emailEdit->hasAcceptableInput() ); +} + +void GitNameEmailDialog::setNameField(QString & name) +{ + ui->nameEdit->setText(name); +} +void GitNameEmailDialog::setEmailField(QString & email) +{ + ui->emailEdit->setText(email); +} +QString GitNameEmailDialog::getNameField() const +{ + return ui->nameEdit->text(); +} +QString GitNameEmailDialog::getEmailField() const +{ + return ui->emailEdit->text(); +} +bool GitNameEmailDialog::getIsGlobal() 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,139 @@ + + + GitNameEmailDialog + + + Qt::WindowModal + + + + 0 + 0 + 320 + 183 + + + + + 16777215 + 16777215 + + + + Git name and email + + + + + + + + 0 + 0 + 320 + 181 + + + + + 3 + + + 3 + + + 3 + + + 3 + + + + + <html><head/><body><p>You haven't set up user.name and user.email configs in Git. Plase specify name and email to attach to commits for this project only or globally.</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 getConfigOption(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,31 @@ { 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 = getConfigOption(url, "user.name"); + QString email = getConfigOption(url, "user.email"); + if(name.endsWith('\n')) + name.chop(1); + if(email.endsWith('\n')) + email.chop(1); + if (email.length()==0 or name.length()==0) + { + GitNameEmailDialog dialog; + dialog.setNameField(name); + dialog.setEmailField(email); + if (dialog.exec()) + { + setConfigOption(url, "user.name", dialog.getNameField(), dialog.getIsGlobal())->exec(); + setConfigOption(url, "user.email", dialog.getEmailField(), dialog.getIsGlobal())->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 +1479,29 @@ 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; - return job; + if(global) + { + auto job = new DVcsJob(urlDir(repository), this); + *job << "git" << "config" << "--global" << key << value; + return job; + } + else + { + auto job = new DVcsJob(urlDir(repository), this); + *job << "git" << "config" << key << value; + return job; + } +} + +QString GitPlugin::getConfigOption(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(); } #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