gitsubmiteditorwidget.cpp 7.59 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
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
12
13
14
** 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
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
18
19
20
21
22
23
24
25
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
hjk's avatar
hjk committed
29

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

35
36
#include <QRegExpValidator>
#include <QTextEdit>
37

38
#include <QDir>
39
#include <QGroupBox>
40
#include <QRegExp>
41
#include <QVBoxLayout>
42
#include <QMenu>
43

con's avatar
con committed
44
45
46
namespace Git {
namespace Internal {

47
// ------------------
con's avatar
con committed
48
GitSubmitEditorWidget::GitSubmitEditorWidget(QWidget *parent) :
49
    VcsBase::SubmitEditorWidget(parent),
50
    m_pushAction(CommitOnly),
51
    m_gitSubmitPanel(new QWidget),
52
53
54
    m_logChangeWidget(0),
    m_hasUnmerged(false),
    m_isInitialized(false)
con's avatar
con committed
55
56
{
    m_gitSubmitPanelUi.setupUi(m_gitSubmitPanel);
57
    new GitSubmitHighlighter(descriptionEdit());
58
59
60
61
62
63
64

    m_emailValidator = new QRegExpValidator(QRegExp(QLatin1String("[^@ ]+@[^@ ]+\\.[a-zA-Z]+")), this);

    connect(m_gitSubmitPanelUi.authorLineEdit, SIGNAL(textChanged(QString)),
            this, SLOT(authorInformationChanged()));
    connect(m_gitSubmitPanelUi.emailLineEdit, SIGNAL(textChanged(QString)),
            this, SLOT(authorInformationChanged()));
con's avatar
con committed
65
66
67
68
}

void GitSubmitEditorWidget::setPanelInfo(const GitSubmitEditorPanelInfo &info)
{
69
    m_gitSubmitPanelUi.repositoryLabel->setText(QDir::toNativeSeparators(info.repository));
70
71
72
73
74
    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
75
76
}

77
78
79
80
81
QString GitSubmitEditorWidget::amendSHA1() const
{
    return m_logChangeWidget ? m_logChangeWidget->commit() : QString();
}

82
83
84
85
86
void GitSubmitEditorWidget::setHasUnmerged(bool e)
{
    m_hasUnmerged = e;
}

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

    if (data.hasRemotes && commitType != FixupCommit) {
        QMenu *menu = new QMenu(this);
        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()));
        addSubmitButtonMenu(menu);
    }
118
119
}

120
121
122
123
124
125
void GitSubmitEditorWidget::refreshLog(const QString &repository)
{
    if (m_logChangeWidget)
        m_logChangeWidget->init(repository, QString(), false);
}

con's avatar
con committed
126
127
128
129
130
GitSubmitEditorPanelData GitSubmitEditorWidget::panelData() const
{
    GitSubmitEditorPanelData rc;
    rc.author = m_gitSubmitPanelUi.authorLineEdit->text();
    rc.email = m_gitSubmitPanelUi.emailLineEdit->text();
131
    rc.bypassHooks = m_gitSubmitPanelUi.bypassHooksCheckBox->isChecked();
132
    rc.pushAction = m_pushAction;
con's avatar
con committed
133
    return rc;
134
}
con's avatar
con committed
135

136
void GitSubmitEditorWidget::setPanelData(const GitSubmitEditorPanelData &data)
con's avatar
con committed
137
138
139
{
    m_gitSubmitPanelUi.authorLineEdit->setText(data.author);
    m_gitSubmitPanelUi.emailLineEdit->setText(data.email);
140
    m_gitSubmitPanelUi.bypassHooksCheckBox->setChecked(data.bypassHooks);
141
142
143
144
145
    authorInformationChanged();
}

bool GitSubmitEditorWidget::canSubmit() const
{
146
    if (m_gitSubmitPanelUi.invalidAuthorLabel->isVisible()
147
148
        || m_gitSubmitPanelUi.invalidEmailLabel->isVisible()
        || m_hasUnmerged)
149
150
151
152
        return false;
    return SubmitEditorWidget::canSubmit();
}

153
154
155
156
157
158
159
160
161
162
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;
163
        if (message.at(pos) == hash)
164
            message.remove(pos, startOfNextLine - pos);
165
        else
166
167
168
169
170
171
            pos = startOfNextLine;
    }
    return message;

}

172
173
174
175
176
177
178
179
180
181
QString GitSubmitEditorWidget::commitName() const
{
    if (m_pushAction == CommitAndPush)
        return tr("Commit and Push");
    else if (m_pushAction == CommitAndPushToGerrit)
        return tr("Commit and Push to Gerrit");

    return tr("Commit");
}

182
183
void GitSubmitEditorWidget::authorInformationChanged()
{
184
185
186
    bool bothEmpty = m_gitSubmitPanelUi.authorLineEdit->text().isEmpty() &&
            m_gitSubmitPanelUi.emailLineEdit->text().isEmpty();

187
    m_gitSubmitPanelUi.invalidAuthorLabel->
188
            setVisible(m_gitSubmitPanelUi.authorLineEdit->text().isEmpty() && !bothEmpty);
189
    m_gitSubmitPanelUi.invalidEmailLabel->
190
            setVisible(!emailIsValid() && !bothEmpty);
191

Orgad Shaneh's avatar
Orgad Shaneh committed
192
    updateSubmitAction();
193
194
}

195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
void GitSubmitEditorWidget::commitOnlySlot()
{
    m_pushAction = CommitOnly;
    updateSubmitAction();
}

void GitSubmitEditorWidget::commitAndPushSlot()
{
    m_pushAction = CommitAndPush;
    updateSubmitAction();
}

void GitSubmitEditorWidget::commitAndPushToGerritSlot()
{
    m_pushAction = CommitAndPushToGerrit;
    updateSubmitAction();
}

213
214
215
bool GitSubmitEditorWidget::emailIsValid() const
{
    int pos = m_gitSubmitPanelUi.emailLineEdit->cursorPosition();
Tobias Hunger's avatar
Tobias Hunger committed
216
217
    QString text = m_gitSubmitPanelUi.emailLineEdit->text();
    return m_emailValidator->validate(text, pos) == QValidator::Acceptable;
con's avatar
con committed
218
219
}

hjk's avatar
hjk committed
220
221
} // namespace Internal
} // namespace Git