gerritpushdialog.cpp 7.13 KB
Newer Older
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
/****************************************************************************
**
** Copyright (C) 2013 Petar Perisin.
** Contact: petar.perisin@gmail.com
**
** This file is part of Qt Creator.
**
** 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.
**
** GNU Lesser General Public License Usage
** 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
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#include "gerritpushdialog.h"
#include "ui_gerritpushdialog.h"

#include "../gitplugin.h"
#include "../gitclient.h"

#include <QDir>

namespace Gerrit {
namespace Internal {

41
GerritPushDialog::GerritPushDialog(const QString &workingDir, const QString &reviewerList, QWidget *parent) :
42
43
44
    QDialog(parent),
    m_workingDir(workingDir),
    m_ui(new Ui::GerritPushDialog),
45
46
    m_localChangesFound(false),
    m_valid(false)
47
48
49
{
    setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
    m_ui->setupUi(this);
50
    m_ui->repositoryLabel->setText(tr("<b>Local repository:</b> %1").arg(
51
52
                                       QDir::toNativeSeparators(workingDir)));

53
    if (!m_ui->commitView->init(workingDir, QString(), false))
54
        return;
55

56
57
    QString earliestCommit = m_ui->commitView->earliestCommit();
    if (earliestCommit.isEmpty())
58
        return;
59

60
61
62
63
64
65
66
67
    m_localChangesFound = true;

    Git::Internal::GitClient *gitClient = Git::Internal::GitPlugin::instance()->gitClient();
    QString output;
    QString error;
    QStringList args;
    args << QLatin1String("--remotes") << QLatin1String("--contains")
         << earliestCommit + QLatin1Char('^');
68
69

    if (!gitClient->synchronousBranchCmd(m_workingDir, args, &output, &error))
70
        return;
71
72
73

    QString head = QLatin1String("/HEAD");
    QStringList refs = output.split(QLatin1Char('\n'));
74
75
    QString remoteTrackingBranch = gitClient->synchronousTrackingBranch(m_workingDir);
    QString remoteBranch;
76
    foreach (const QString &reference, refs) {
77
78
        const QString ref = reference.trimmed();
        if (ref.contains(head) || ref.isEmpty())
79
80
            continue;

81
82
83
84
85
86
87
88
89
90
        remoteBranch = ref;

        // Prefer remote tracking branch if it exists and contains the latest remote commit
        if (ref == remoteTrackingBranch)
            break;
    }

    if (!remoteBranch.isEmpty()) {
        m_suggestedRemoteName = remoteBranch.left(remoteBranch.indexOf(QLatin1Char('/')));
        m_suggestedRemoteBranch = remoteBranch.mid(remoteBranch.indexOf(QLatin1Char('/')) + 1);
91
92
93
94
95
96
97
98
99
    }

    output.clear();
    error.clear();
    args.clear();

    args << QLatin1String("--remotes");

    if (!gitClient->synchronousBranchCmd(m_workingDir, args, &output, &error))
100
        return;
101
102
103
104
105
106
107
108

    refs.clear();
    refs = output.split(QLatin1String("\n"));
    foreach (const QString &reference, refs) {
        if (reference.contains(head) || reference.isEmpty())
            continue;

        int refBranchIndex = reference.indexOf(QLatin1Char('/'));
109
        m_remoteBranches.insertMulti(reference.left(refBranchIndex).trimmed(),
110
111
112
113
                                     reference.mid(refBranchIndex + 1).trimmed());
    }

    int currIndex = 0;
114
    QStringList remotes = m_remoteBranches.keys();
115
116
117
118
119
120
121
    remotes.removeDuplicates();
    foreach (const QString &remote, remotes) {
        m_ui->remoteComboBox->addItem(remote);
        if (remote == m_suggestedRemoteName)
            m_ui->remoteComboBox->setCurrentIndex(currIndex);
        ++currIndex;
    }
122
123
124
125
126
127
128
129
130
    const int remoteCount = m_ui->remoteComboBox->count();
    if (remoteCount < 1) {
        return;
    } else if (remoteCount == 1) {
        m_ui->remoteLabel->hide();
        m_ui->remoteComboBox->hide();
    } else {
        connect(m_ui->remoteComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(setRemoteBranches()));
    }
131
132
    connect(m_ui->branchComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(setChangeRange()));
    setRemoteBranches();
133
    m_ui->reviewersLineEdit->setText(reviewerList);
134
    m_valid = true;
135
136
137
138
139
140
141
}

GerritPushDialog::~GerritPushDialog()
{
    delete m_ui;
}

142
143
144
145
QString GerritPushDialog::selectedCommit() const
{
    return m_ui->commitView->commit();
}
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

QString GerritPushDialog::calculateChangeRange()
{
    QString remote = selectedRemoteName();
    remote += QLatin1Char('/');
    remote += selectedRemoteBranchName();

    QStringList args(remote + QLatin1String("..HEAD"));
    args << QLatin1String("--count");

    QString number;

    if (!Git::Internal::GitPlugin::instance()->gitClient()->
            synchronousRevListCmd(m_workingDir, args, &number))
        reject();

    number.chop(1);
    return number;
}

void GerritPushDialog::setChangeRange()
{
    QString remote = selectedRemoteName();
    remote += QLatin1Char('/');
    remote += selectedRemoteBranchName();
    m_ui->infoLabel->setText(tr("Number of commits between HEAD and %1: %2").arg(
                                 remote, calculateChangeRange()));
}

bool GerritPushDialog::localChangesFound() const
{
    return m_localChangesFound;
}

180
181
182
183
184
bool GerritPushDialog::valid() const
{
    return m_valid;
}

185
186
void GerritPushDialog::setRemoteBranches()
{
187
    bool blocked = m_ui->branchComboBox->blockSignals(true);
188
189
190
    m_ui->branchComboBox->clear();

    int i = 0;
191
192
193
    for (RemoteBranchesMap::const_iterator it = m_remoteBranches.constBegin(),
         end = m_remoteBranches.constEnd();
         it != end; ++it) {
194
195
196
197
198
199
200
201
        if (it.key() == selectedRemoteName()) {
            m_ui->branchComboBox->addItem(it.value());
            if (it.value() == m_suggestedRemoteBranch)
                m_ui->branchComboBox->setCurrentIndex(i);
            ++i;
        }
    }
    setChangeRange();
202
    m_ui->branchComboBox->blockSignals(blocked);
203
204
205
206
207
208
209
210
211
212
213
214
215
216
}

QString GerritPushDialog::selectedRemoteName() const
{
    return m_ui->remoteComboBox->currentText();
}

QString GerritPushDialog::selectedRemoteBranchName() const
{
    return m_ui->branchComboBox->currentText();
}

QString GerritPushDialog::selectedPushType() const
{
217
    return m_ui->draftCheckBox->isChecked() ? QLatin1String("draft") : QLatin1String("for");
218
219
}

220
221
222
223
224
QString GerritPushDialog::selectedTopic() const
{
    return m_ui->topicLineEdit->text().trimmed();
}

225
226
227
228
229
QString GerritPushDialog::reviewers() const
{
    return m_ui->reviewersLineEdit->text();
}

230
231
} // namespace Internal
} // namespace Gerrit