1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
/*
SPDX-FileCopyrightText: 2010 Sebastian Doerner <sebastian@sebastian-doerner.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "checkoutdialog.h"
#include "gitwrapper.h"
#include <KConfigGroup>
#include <KLocalizedString>
#include <QCheckBox>
#include <QComboBox>
#include <QDialogButtonBox>
#include <QGridLayout>
#include <QGroupBox>
#include <QLineEdit>
#include <QPushButton>
#include <QRadioButton>
#include <QRegularExpression>
#include <QString>
#include <QVBoxLayout>
CheckoutDialog::CheckoutDialog(QWidget *parent)
: QDialog(parent, Qt::Dialog)
, m_userEditedNewBranchName(false)
, m_shortIdLength(GitWrapper::instance()->shortIdLength())
{
// Typical SHA space.
static const auto minShaWidth = 1.1 * fontMetrics().horizontalAdvance(QStringLiteral("320548ea08f26ecbc28e520f39715f566437a97e"));
// branch/tag selection
setWindowTitle(xi18nc("@title:window", "<application>Git</application> Checkout"));
m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
QWidget *mainWidget = new QWidget(this);
QVBoxLayout *mainLayout = new QVBoxLayout;
setLayout(mainLayout);
mainLayout->addWidget(mainWidget);
QPushButton *okButton = m_buttonBox->button(QDialogButtonBox::Ok);
okButton->setDefault(true);
okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
connect(m_buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
okButton->setText(i18nc("@action:button", "Checkout"));
QWidget *boxWidget = new QWidget(this);
QVBoxLayout *boxLayout = new QVBoxLayout(boxWidget);
mainLayout->addWidget(boxWidget);
m_branchSelectGroupBox = new QGroupBox(boxWidget);
mainLayout->addWidget(m_branchSelectGroupBox);
boxLayout->addWidget(m_branchSelectGroupBox);
QGridLayout *gridLayout = new QGridLayout(m_branchSelectGroupBox);
m_branchSelectGroupBox->setLayout(gridLayout);
m_branchRadioButton = new QRadioButton(i18nc("@option:radio Git Checkout", "Branch:"), m_branchSelectGroupBox);
m_branchRadioButton->setChecked(true);
gridLayout->addWidget(m_branchRadioButton, 0, 0);
m_branchRadioButton->setFocus();
m_branchComboBox = new QComboBox(m_branchSelectGroupBox);
gridLayout->addWidget(m_branchComboBox, 0, 1);
m_tagRadioButton = new QRadioButton(i18nc("@option:radio Git Checkout", "Tag:"), m_branchSelectGroupBox);
gridLayout->addWidget(m_tagRadioButton, 1, 0);
m_tagComboBox = new QComboBox(m_branchSelectGroupBox);
m_tagComboBox->setEnabled(false);
gridLayout->addWidget(m_tagComboBox, 1, 1);
m_commitRadioButton = new QRadioButton(i18nc("@option:radio Git Checkout", "Commit:"), m_branchSelectGroupBox);
gridLayout->addWidget(m_commitRadioButton, 2, 0);
m_commitLineEdit = new QLineEdit(m_branchSelectGroupBox);
m_commitLineEdit->setEnabled(false);
m_commitLineEdit->setMinimumWidth(minShaWidth);
gridLayout->addWidget(m_commitLineEdit, 2, 1);
// options
QGroupBox *optionsGroupBox = new QGroupBox(boxWidget);
mainLayout->addWidget(optionsGroupBox);
boxLayout->addWidget(optionsGroupBox);
optionsGroupBox->setTitle(i18nc("@title:group", "Options"));
QGridLayout *optionsGridLayout = new QGridLayout(optionsGroupBox);
optionsGroupBox->setLayout(optionsGridLayout);
m_newBranchCheckBox = new QCheckBox(i18nc("@option:check", "Create New Branch: "), optionsGroupBox);
m_newBranchCheckBox->setToolTip(i18nc("@info:tooltip", "Create a new branch based on a selected branch or tag."));
optionsGridLayout->addWidget(m_newBranchCheckBox, 0, 0);
mainLayout->addWidget(m_buttonBox);
m_newBranchName = new QLineEdit(optionsGroupBox);
m_newBranchName->setMinimumWidth(150);
m_newBranchName->setClearButtonEnabled(true);
optionsGridLayout->addWidget(m_newBranchName, 0, 1);
// initialize alternate color scheme for errors
m_errorColors = m_newBranchName->palette();
m_errorColors.setColor(QPalette::Normal, QPalette::Base, Qt::red);
m_errorColors.setColor(QPalette::Inactive, QPalette::Base, Qt::red);
m_forceCheckBox = new QCheckBox(i18nc("@option:check", "Force"), optionsGroupBox);
m_forceCheckBox->setToolTip(i18nc("@info:tooltip", "Discard local changes."));
optionsGridLayout->addWidget(m_forceCheckBox, 1, 0);
// get branch names
GitWrapper *gitWrapper = GitWrapper::instance();
int currentBranchIndex;
const QStringList branches = gitWrapper->branches(¤tBranchIndex);
m_branchComboBox->addItems(branches);
if (currentBranchIndex == -1) {
m_branchComboBox->insertItem(0, QStringLiteral("(no branch)"));
m_branchComboBox->setCurrentIndex(0);
} else {
m_branchComboBox->setCurrentIndex(currentBranchIndex);
}
setDefaultNewBranchName(m_branchComboBox->currentText());
// keep local branches to prevent creating an equally named new branch
for (const QString &b : branches) {
if (!b.startsWith(QLatin1String("remotes/"))) {
// you CAN create local branches called "remotes/...", but since no sane person
// would do that, we save the effort of another call to "git branch"
m_branchNames.insert(b);
}
}
// get tag names
const QStringList tags = gitWrapper->tags();
m_tagComboBox->addItems(tags);
m_tagComboBox->setCurrentIndex(m_tagComboBox->count() - 1);
if (m_tagComboBox->count() == 0) {
m_tagRadioButton->setEnabled(false);
const QString tooltip = i18nc("@info:tooltip", "There are no tags in this repository.");
m_tagRadioButton->setToolTip(tooltip);
m_tagComboBox->setToolTip(tooltip);
}
// signals
connect(m_branchRadioButton, &QAbstractButton::toggled, this, [this](bool checked) {
radioButtonToggled(m_branchComboBox, m_branchComboBox->currentText(), checked);
});
connect(m_tagRadioButton, &QAbstractButton::toggled, this, [this](bool checked) {
radioButtonToggled(m_tagComboBox, m_tagComboBox->currentText(), checked);
});
connect(m_commitRadioButton, &QAbstractButton::toggled, this, [this](bool checked) {
radioButtonToggled(m_commitLineEdit, m_commitLineEdit->text(), checked);
});
connect(m_commitLineEdit, &QLineEdit::textChanged, this, &CheckoutDialog::setDefaultNewBranchName);
connect(m_commitLineEdit, &QLineEdit::textChanged, this, &CheckoutDialog::setOkButtonState);
connect(m_branchComboBox, &QComboBox::currentTextChanged, this, &CheckoutDialog::setDefaultNewBranchName);
connect(m_branchComboBox, &QComboBox::currentTextChanged, this, &CheckoutDialog::setOkButtonState);
connect(m_tagComboBox, &QComboBox::currentTextChanged, this, &CheckoutDialog::setDefaultNewBranchName);
connect(m_newBranchCheckBox, &QCheckBox::stateChanged, this, &CheckoutDialog::newBranchCheckBoxStateToggled);
connect(m_newBranchName, &QLineEdit::textChanged, this, &CheckoutDialog::setOkButtonState);
connect(m_newBranchName, &QLineEdit::textEdited, this, &CheckoutDialog::noteUserEditedNewBranchName);
// set initial widget states
newBranchCheckBoxStateToggled(Qt::Unchecked);
}
QString CheckoutDialog::checkoutIdentifier() const
{
QString identifier;
if (m_branchComboBox->isEnabled()) {
identifier = m_branchComboBox->currentText();
} else if (m_tagComboBox->isEnabled()) {
identifier = m_tagComboBox->currentText();
} else {
identifier = m_commitLineEdit->text();
}
if (identifier.length() == 0 || identifier.at(0) == QLatin1Char('(')) {
identifier = QString();
}
return identifier;
}
bool CheckoutDialog::force() const
{
return m_forceCheckBox->isChecked();
}
QString CheckoutDialog::newBranchName() const
{
if (m_newBranchCheckBox->isChecked()) {
return m_newBranchName->text().trimmed();
} else {
return QString();
}
}
void CheckoutDialog::radioButtonToggled(QWidget *buddyWidget, const QString &baseBranchName, bool checked)
{
buddyWidget->setEnabled(checked);
if (checked) {
setDefaultNewBranchName(baseBranchName);
}
setOkButtonState();
}
void CheckoutDialog::newBranchCheckBoxStateToggled(int state)
{
m_newBranchName->setEnabled(state == Qt::Checked);
m_branchSelectGroupBox->setTitle(state == Qt::Checked ? i18nc("@title:group", "Branch Base") : i18nc("@title:group", "Checkout"));
if (state == Qt::Checked) {
m_newBranchName->setFocus(Qt::TabFocusReason);
}
setOkButtonState();
}
void CheckoutDialog::setOkButtonState()
{
static const auto whitespace = QRegularExpression(QStringLiteral("\\s"));
// default to enabled
bool enableButton = true;
bool newNameError = false;
QPushButton *okButton = m_buttonBox->button(QDialogButtonBox::Ok);
//------------disable on these conditions---------------//
if (m_newBranchCheckBox->isChecked()) {
const QString newBranchName = m_newBranchName->text().trimmed();
if (newBranchName.isEmpty()) {
enableButton = false;
newNameError = true;
const QString tt = i18nc("@info:tooltip", "You must enter a valid name for the new branch first.");
m_newBranchName->setToolTip(tt);
okButton->setToolTip(tt);
}
if (m_branchNames.contains(newBranchName)) {
enableButton = false;
newNameError = true;
const QString tt = i18nc("@info:tooltip", "A branch with the name '%1' already exists.", newBranchName);
m_newBranchName->setToolTip(tt);
okButton->setToolTip(tt);
}
if (newBranchName.contains(whitespace)) {
enableButton = false;
newNameError = true;
const QString tt = i18nc("@info:tooltip", "Branch names may not contain any whitespace.");
m_newBranchName->setToolTip(tt);
okButton->setToolTip(tt);
}
} // if we create a new branch and no valid branch is selected we create one based on the currently checked out version
else if (m_branchRadioButton->isChecked() && m_branchComboBox->currentText().at(0) == QLatin1Char('(')) {
enableButton = false;
okButton->setToolTip(i18nc("@info:tooltip", "You must select a valid branch first."));
}
if (m_commitRadioButton->isChecked()) {
if (!GitWrapper::instance()->isCommitIdValid(m_commitLineEdit->text())) {
enableButton = false;
okButton->setToolTip(i18nc("@info:tooltip", "You must enter a valid commit Id (Sha signature)."));
}
}
//------------------------------------------------------//
setLineEditErrorModeActive(newNameError);
okButton->setEnabled(enableButton);
if (!newNameError) {
m_newBranchName->setToolTip(QString());
}
if (enableButton) {
okButton->setToolTip(QString());
}
}
void CheckoutDialog::setDefaultNewBranchName(const QString &baseBranchName)
{
if (!m_userEditedNewBranchName) {
if (baseBranchName.startsWith(QLatin1Char('('))) {
m_newBranchName->setText(QString());
} else {
QString postfix;
if (m_commitRadioButton->isChecked()) {
auto text = m_commitLineEdit->text();
if (m_shortIdLength > 0 && text.size() > m_shortIdLength) {
text.truncate(m_shortIdLength);
}
postfix = text;
} else {
postfix = baseBranchName;
}
m_newBranchName->setText(i18nc("@item:intext Prepended to the current branch name "
"to get the default name for a newly created branch",
"branch")
+ QLatin1Char('_') + postfix);
}
}
}
void CheckoutDialog::noteUserEditedNewBranchName()
{
m_userEditedNewBranchName = true;
}
void CheckoutDialog::setLineEditErrorModeActive(bool active)
{
m_newBranchName->setPalette(active ? m_errorColors : QPalette());
}
#include "moc_checkoutdialog.cpp"
|