diff --git a/plugins/git/CMakeLists.txt b/plugins/git/CMakeLists.txt --- a/plugins/git/CMakeLists.txt +++ b/plugins/git/CMakeLists.txt @@ -12,6 +12,7 @@ gitpluginmetadata.cpp gitjob.cpp gitplugincheckinrepositoryjob.cpp + gitnameemaildialog.cpp ) ki18n_wrap_ui(kdevgit_PART_SRCS stashmanagerdialog.ui) kdevplatform_add_plugin(kdevgit JSON kdevgit.json SOURCES ${kdevgit_PART_SRCS}) 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,60 @@ + /************************************************************************** + * 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; + +class GitNameEmailDialog : public QDialog +{ + Q_OBJECT + +public: + GitNameEmailDialog(QWidget *parent = 0); + void setNameField(QString & name); + void setEmailField(QString & email); + QString getNameField() const; + QString getEmailField() const; + bool getIsGlobal() const; + +private slots: + void refrashSubmitButton(); + +private: + QLabel *descriptionLabel; + QLabel *nameLabel; + QLineEdit *nameEdit; + QLabel *emailLabel; + QLineEdit *emailEdit; + QCheckBox *globalCheckBox; + QDialogButtonBox *buttonBox; + QPushButton *submitButton; + QPushButton *cancelButton; +}; + +#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,114 @@ + /************************************************************************** + * 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 +#include + +#include "gitnameemaildialog.h" +#include "gitplugin.h" + +using namespace KDevelop; + +GitNameEmailDialog::GitNameEmailDialog(QWidget *parent) + : QDialog(parent) +{ + descriptionLabel = new QLabel(i18n("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 or globally.")); + descriptionLabel->setWordWrap(true); + + nameLabel = new QLabel(i18n("Name:")); + nameEdit = new QLineEdit; + nameLabel->setBuddy(nameEdit); + + emailLabel = new QLabel(i18n("E-mail:")); + emailEdit = new QLineEdit; + emailLabel->setBuddy(nameEdit); + + globalCheckBox = new QCheckBox(i18n("Global")); + + submitButton = new QPushButton(i18n("Set")); + submitButton->setDefault(true); + submitButton->setDisabled(true); + + cancelButton = new QPushButton(i18n("Cancel")); + + buttonBox = new QDialogButtonBox(Qt::Horizontal); + buttonBox->addButton(submitButton, QDialogButtonBox::AcceptRole); + buttonBox->addButton(cancelButton, QDialogButtonBox::RejectRole); + + QHBoxLayout *nameLayout = new QHBoxLayout; + nameLayout->addWidget(nameLabel); + nameLayout->addWidget(nameEdit); + + QHBoxLayout *emailLayout = new QHBoxLayout; + emailLayout->addWidget(emailLabel); + emailLayout->addWidget(emailEdit); + + QGridLayout *mainLayout = new QGridLayout; + mainLayout->setSizeConstraint(QLayout::SetFixedSize); + mainLayout->addWidget(descriptionLabel, 0, 0); + mainLayout->addLayout(nameLayout, 1, 0); + mainLayout->addLayout(emailLayout, 2, 0); + mainLayout->addWidget(globalCheckBox, 3, 0); + mainLayout->addWidget(buttonBox, 4, 0); + + setLayout(mainLayout); + setMaximumWidth(500); + + setWindowTitle(i18n("Git name and email")); + + connect(buttonBox, &QDialogButtonBox::accepted, this, &GitNameEmailDialog::accept); + connect(buttonBox, &QDialogButtonBox::rejected, this, &GitNameEmailDialog::reject); + + QRegExp rx(".+"); + QValidator *validator = new QRegExpValidator(rx, this); + emailEdit->setValidator(validator); + nameEdit->setValidator(validator); + + connect(emailEdit, &QLineEdit::textChanged, this, &GitNameEmailDialog::refrashSubmitButton); + connect(nameEdit, &QLineEdit::textChanged, this, &GitNameEmailDialog::refrashSubmitButton); +} + +void GitNameEmailDialog::refrashSubmitButton() +{ + submitButton->setDisabled( !nameEdit->hasAcceptableInput() or + !emailEdit->hasAcceptableInput() ); +} + +void GitNameEmailDialog::setNameField(QString & name) +{ + nameEdit->setText(name); +} +void GitNameEmailDialog::setEmailField(QString & email) +{ + emailEdit->setText(email); +} +QString GitNameEmailDialog::getNameField() const +{ + return nameEdit->text(); +} +QString GitNameEmailDialog::getEmailField() const +{ + return emailEdit->text(); +} +bool GitNameEmailDialog::getIsGlobal() const +{ + return globalCheckBox->isChecked(); +} +#include "gitnameemaildialog.moc" \ No newline at end of file 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,6 +13,7 @@ ../gitjob.cpp ../gitmessagehighlighter.cpp ../gitplugincheckinrepositoryjob.cpp + ../gitnameemaildialog.cpp ) ki18n_wrap_ui(gittest_SRCS ../stashmanagerdialog.ui) ecm_add_test(${gittest_SRCS}