Git: Improve branch name validation

* Do not allow leading dash
* Do not allow existing local branches

Change-Id: I3efbb90b7099719a7e90cada85a58a0f91bd9730
Reviewed-by: Tobias Hunger <tobias.hunger@digia.com>
This commit is contained in:
Orgad Shaneh
2013-10-07 10:26:13 +03:00
committed by Orgad Shaneh
parent 5e585b9eee
commit 2559f98693
3 changed files with 17 additions and 14 deletions

View File

@@ -30,6 +30,8 @@
#include "branchadddialog.h" #include "branchadddialog.h"
#include "ui_branchadddialog.h" #include "ui_branchadddialog.h"
#include <utils/hostosinfo.h>
#include <QPushButton> #include <QPushButton>
#include <QValidator> #include <QValidator>
@@ -46,7 +48,7 @@ namespace Internal {
class BranchNameValidator : public QValidator class BranchNameValidator : public QValidator
{ {
public: public:
BranchNameValidator(QObject *parent = 0) : BranchNameValidator(const QStringList &localBranches, QObject *parent = 0) :
QValidator(parent), QValidator(parent),
m_invalidChars(QLatin1String( m_invalidChars(QLatin1String(
"\\s" // no whitespace "\\s" // no whitespace
@@ -59,8 +61,9 @@ public:
"|@\\{" // no "@{" sequence "|@\\{" // no "@{" sequence
"|\\\\" // no backslash "|\\\\" // no backslash
"|//" // no double slash "|//" // no double slash
"|^/" // no leading slash "|^[/-]" // no leading slash or dash
)) )),
m_localBranches(localBranches)
{ {
} }
@@ -87,22 +90,28 @@ public:
if (input.endsWith(QLatin1Char('/'))) // no slash at the end (but allowed in the middle) if (input.endsWith(QLatin1Char('/'))) // no slash at the end (but allowed in the middle)
return Intermediate; return Intermediate;
if (m_localBranches.contains(input, Utils::HostOsInfo::isWindowsHost()
? Qt::CaseInsensitive : Qt::CaseSensitive)) {
return Intermediate;
}
// is a valid branch name // is a valid branch name
return Acceptable; return Acceptable;
} }
private: private:
const QRegExp m_invalidChars; const QRegExp m_invalidChars;
QStringList m_localBranches;
}; };
BranchAddDialog::BranchAddDialog(bool addBranch, QWidget *parent) : BranchAddDialog::BranchAddDialog(const QStringList &localBranches, bool addBranch, QWidget *parent) :
QDialog(parent), QDialog(parent),
m_ui(new Ui::BranchAddDialog) m_ui(new Ui::BranchAddDialog)
{ {
m_ui->setupUi(this); m_ui->setupUi(this);
setWindowTitle(addBranch ? tr("Add Branch") : tr("Rename Branch")); setWindowTitle(addBranch ? tr("Add Branch") : tr("Rename Branch"));
m_ui->branchNameEdit->setValidator(new BranchNameValidator(this)); m_ui->branchNameEdit->setValidator(new BranchNameValidator(localBranches, this));
connect(m_ui->branchNameEdit, SIGNAL(textChanged(QString)), this, SLOT(updateButtonStatus())); connect(m_ui->branchNameEdit, SIGNAL(textChanged(QString)), this, SLOT(updateButtonStatus()));
} }

View File

@@ -45,7 +45,7 @@ class BranchAddDialog : public QDialog
Q_OBJECT Q_OBJECT
public: public:
BranchAddDialog(bool addBranch, QWidget *parent); BranchAddDialog(const QStringList &localBranches, bool addBranch, QWidget *parent);
~BranchAddDialog(); ~BranchAddDialog();
void setBranchName(const QString &); void setBranchName(const QString &);

View File

@@ -154,7 +154,7 @@ void BranchDialog::add()
++i; ++i;
} }
BranchAddDialog branchAddDialog(true, this); BranchAddDialog branchAddDialog(localNames, true, this);
branchAddDialog.setBranchName(suggestedName); branchAddDialog.setBranchName(suggestedName);
branchAddDialog.setTrackedBranchName(isTag ? QString() : trackedBranch, !isLocal); branchAddDialog.setTrackedBranchName(isTag ? QString() : trackedBranch, !isLocal);
@@ -276,7 +276,7 @@ void BranchDialog::rename()
if (!isTag) if (!isTag)
localNames = m_model->localBranchNames(); localNames = m_model->localBranchNames();
BranchAddDialog branchAddDialog(false, this); BranchAddDialog branchAddDialog(localNames, false, this);
if (isTag) if (isTag)
branchAddDialog.setWindowTitle(tr("Rename Tag")); branchAddDialog.setWindowTitle(tr("Rename Tag"));
branchAddDialog.setBranchName(oldName); branchAddDialog.setBranchName(oldName);
@@ -287,12 +287,6 @@ void BranchDialog::rename()
if (branchAddDialog.result() == QDialog::Accepted && m_model) { if (branchAddDialog.result() == QDialog::Accepted && m_model) {
if (branchAddDialog.branchName() == oldName) if (branchAddDialog.branchName() == oldName)
return; return;
if (localNames.contains(branchAddDialog.branchName())) {
QMessageBox::critical(this, tr("Branch Exists"),
tr("Local branch \'%1\' already exists.")
.arg(branchAddDialog.branchName()));
return;
}
if (isTag) if (isTag)
m_model->renameTag(oldName, branchAddDialog.branchName()); m_model->renameTag(oldName, branchAddDialog.branchName());
else else