gitsubmiteditorwidget.cpp 7.93 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
Eike Ziller's avatar
Eike Ziller committed
3
4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8
9
10
11
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
Eike Ziller's avatar
Eike Ziller committed
12
13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18
19
20
21
22
23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hjk's avatar
hjk committed
24
**
Eike Ziller's avatar
Eike Ziller committed
25
26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
con's avatar
con committed
27
28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
hjk's avatar
hjk committed
30

con's avatar
con committed
31
#include "commitdata.h"
32
33
#include "gitsubmiteditorwidget.h"
#include "githighlighters.h"
34
#include "logchangedialog.h"
con's avatar
con committed
35

36
37
#include <utils/completingtextedit.h>

38
39
#include <QRegExpValidator>
#include <QTextEdit>
40

41
#include <QDir>
42
#include <QGroupBox>
43
#include <QRegExp>
44
#include <QVBoxLayout>
45
#include <QMenu>
46

con's avatar
con committed
47
48
49
namespace Git {
namespace Internal {

50
// ------------------
51
GitSubmitEditorWidget::GitSubmitEditorWidget() :
Petar Perisin's avatar
Petar Perisin committed
52
    m_pushAction(NoPush),
53
    m_gitSubmitPanel(new QWidget),
54
55
56
    m_logChangeWidget(0),
    m_hasUnmerged(false),
    m_isInitialized(false)
con's avatar
con committed
57
58
{
    m_gitSubmitPanelUi.setupUi(m_gitSubmitPanel);
59
    new GitSubmitHighlighter(descriptionEdit());
60
61

    m_emailValidator = new QRegExpValidator(QRegExp(QLatin1String("[^@ ]+@[^@ ]+\\.[a-zA-Z]+")), this);
62
    m_gitSubmitPanelUi.invalidEmailLabel->setToolTip(tr("Provide a valid email to commit."));
63

Montel Laurent's avatar
Montel Laurent committed
64
65
66
67
    connect(m_gitSubmitPanelUi.authorLineEdit, &QLineEdit::textChanged,
            this, &GitSubmitEditorWidget::authorInformationChanged);
    connect(m_gitSubmitPanelUi.emailLineEdit, &QLineEdit::textChanged,
            this, &GitSubmitEditorWidget::authorInformationChanged);
con's avatar
con committed
68
69
70
71
}

void GitSubmitEditorWidget::setPanelInfo(const GitSubmitEditorPanelInfo &info)
{
72
    m_gitSubmitPanelUi.repositoryLabel->setText(QDir::toNativeSeparators(info.repository));
73
74
75
76
77
    if (info.branch.contains(QLatin1String("(no branch)")))
        m_gitSubmitPanelUi.branchLabel->setText(QString::fromLatin1("<span style=\"color:red\">%1</span>")
                                                .arg(tr("Detached HEAD")));
    else
        m_gitSubmitPanelUi.branchLabel->setText(info.branch);
con's avatar
con committed
78
79
}

80
81
82
83
84
QString GitSubmitEditorWidget::amendSHA1() const
{
    return m_logChangeWidget ? m_logChangeWidget->commit() : QString();
}

85
86
87
88
89
void GitSubmitEditorWidget::setHasUnmerged(bool e)
{
    m_hasUnmerged = e;
}

90
91
92
void GitSubmitEditorWidget::initialize(CommitType commitType,
                                       const QString &repository,
                                       const GitSubmitEditorPanelData &data,
93
94
                                       const GitSubmitEditorPanelInfo &info,
                                       bool enablePush)
95
96
97
98
99
{
    if (m_isInitialized)
        return;
    m_isInitialized = true;
    if (commitType == FixupCommit) {
Tobias Hunger's avatar
Tobias Hunger committed
100
101
        auto logChangeGroupBox = new QGroupBox(tr("Select Change"));
        auto logChangeLayout = new QVBoxLayout;
102
103
        logChangeGroupBox->setLayout(logChangeLayout);
        m_logChangeWidget = new LogChangeWidget;
104
        m_logChangeWidget->init(repository);
Montel Laurent's avatar
Montel Laurent committed
105
106
        connect(m_logChangeWidget, &LogChangeWidget::activated,
                this, &GitSubmitEditorWidget::show);
107
        logChangeLayout->addWidget(m_logChangeWidget);
108
        insertLeftWidget(logChangeGroupBox);
109
110
111
112
        m_gitSubmitPanelUi.editGroup->hide();
        hideDescription();
    }
    insertTopWidget(m_gitSubmitPanel);
113
114
    setPanelData(data);
    setPanelInfo(info);
115

Petar Perisin's avatar
Petar Perisin committed
116
    if (enablePush) {
Tobias Hunger's avatar
Tobias Hunger committed
117
        auto menu = new QMenu(this);
Orgad Shaneh's avatar
Orgad Shaneh committed
118
119
120
        menu->addAction(tr("&Commit only"), this, SLOT(commitOnlySlot()));
        menu->addAction(tr("Commit and &Push"), this, SLOT(commitAndPushSlot()));
        menu->addAction(tr("Commit and Push to &Gerrit"), this, SLOT(commitAndPushToGerritSlot()));
121
122
        addSubmitButtonMenu(menu);
    }
123
124
}

125
126
127
void GitSubmitEditorWidget::refreshLog(const QString &repository)
{
    if (m_logChangeWidget)
128
        m_logChangeWidget->init(repository);
129
130
}

con's avatar
con committed
131
132
133
GitSubmitEditorPanelData GitSubmitEditorWidget::panelData() const
{
    GitSubmitEditorPanelData rc;
134
135
136
137
138
139
    const QString author = m_gitSubmitPanelUi.authorLineEdit->text();
    const QString email = m_gitSubmitPanelUi.emailLineEdit->text();
    if (author != m_originalAuthor || email != m_originalEmail) {
        rc.author = author;
        rc.email = email;
    }
140
    rc.bypassHooks = m_gitSubmitPanelUi.bypassHooksCheckBox->isChecked();
141
    rc.pushAction = m_pushAction;
con's avatar
con committed
142
    return rc;
143
}
con's avatar
con committed
144

145
void GitSubmitEditorWidget::setPanelData(const GitSubmitEditorPanelData &data)
con's avatar
con committed
146
{
147
148
    m_originalAuthor = data.author;
    m_originalEmail = data.email;
con's avatar
con committed
149
150
    m_gitSubmitPanelUi.authorLineEdit->setText(data.author);
    m_gitSubmitPanelUi.emailLineEdit->setText(data.email);
151
    m_gitSubmitPanelUi.bypassHooksCheckBox->setChecked(data.bypassHooks);
152
153
154
155
156
    authorInformationChanged();
}

bool GitSubmitEditorWidget::canSubmit() const
{
157
    if (m_gitSubmitPanelUi.invalidAuthorLabel->isVisible()
158
159
        || m_gitSubmitPanelUi.invalidEmailLabel->isVisible()
        || m_hasUnmerged)
160
161
162
163
        return false;
    return SubmitEditorWidget::canSubmit();
}

164
165
166
167
168
169
170
171
172
173
QString GitSubmitEditorWidget::cleanupDescription(const QString &input) const
{
    // We need to manually purge out comment lines starting with
    // hash '#' since git does not do that when using -F.
    const QChar newLine = QLatin1Char('\n');
    const QChar hash = QLatin1Char('#');
    QString message = input;
    for (int pos = 0; pos < message.size(); ) {
        const int newLinePos = message.indexOf(newLine, pos);
        const int startOfNextLine = newLinePos == -1 ? message.size() : newLinePos + 1;
174
        if (message.at(pos) == hash)
175
            message.remove(pos, startOfNextLine - pos);
176
        else
177
178
179
180
181
182
            pos = startOfNextLine;
    }
    return message;

}

183
184
QString GitSubmitEditorWidget::commitName() const
{
Petar Perisin's avatar
Petar Perisin committed
185
    if (m_pushAction == NormalPush)
Orgad Shaneh's avatar
Orgad Shaneh committed
186
        return tr("&Commit and Push");
Petar Perisin's avatar
Petar Perisin committed
187
    else if (m_pushAction == PushToGerrit)
Orgad Shaneh's avatar
Orgad Shaneh committed
188
        return tr("&Commit and Push to Gerrit");
189

Orgad Shaneh's avatar
Orgad Shaneh committed
190
    return tr("&Commit");
191
192
}

193
194
void GitSubmitEditorWidget::authorInformationChanged()
{
195
196
197
    bool bothEmpty = m_gitSubmitPanelUi.authorLineEdit->text().isEmpty() &&
            m_gitSubmitPanelUi.emailLineEdit->text().isEmpty();

198
    m_gitSubmitPanelUi.invalidAuthorLabel->
199
            setVisible(m_gitSubmitPanelUi.authorLineEdit->text().isEmpty() && !bothEmpty);
200
    m_gitSubmitPanelUi.invalidEmailLabel->
201
            setVisible(!emailIsValid() && !bothEmpty);
202

Orgad Shaneh's avatar
Orgad Shaneh committed
203
    updateSubmitAction();
204
205
}

206
207
void GitSubmitEditorWidget::commitOnlySlot()
{
Petar Perisin's avatar
Petar Perisin committed
208
    m_pushAction = NoPush;
209
210
211
212
213
    updateSubmitAction();
}

void GitSubmitEditorWidget::commitAndPushSlot()
{
Petar Perisin's avatar
Petar Perisin committed
214
    m_pushAction = NormalPush;
215
216
217
218
219
    updateSubmitAction();
}

void GitSubmitEditorWidget::commitAndPushToGerritSlot()
{
Petar Perisin's avatar
Petar Perisin committed
220
    m_pushAction = PushToGerrit;
221
222
223
    updateSubmitAction();
}

224
225
226
bool GitSubmitEditorWidget::emailIsValid() const
{
    int pos = m_gitSubmitPanelUi.emailLineEdit->cursorPosition();
Tobias Hunger's avatar
Tobias Hunger committed
227
228
    QString text = m_gitSubmitPanelUi.emailLineEdit->text();
    return m_emailValidator->validate(text, pos) == QValidator::Acceptable;
con's avatar
con committed
229
230
}

hjk's avatar
hjk committed
231
232
} // namespace Internal
} // namespace Git