aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/git/changeselectiondialog.cpp
blob: 2f93a2c7a16407f3668855b20dd25a1e75e4f495 (plain)
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
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "changeselectiondialog.h"

#include "logchangedialog.h"
#include "gitclient.h"
#include "gittr.h"

#include <coreplugin/vcsmanager.h>

#include <utils/completinglineedit.h>
#include <utils/layoutbuilder.h>
#include <utils/pathchooser.h>
#include <utils/qtcprocess.h>
#include <utils/theme/theme.h>

#include <QCompleter>
#include <QFileDialog>
#include <QLabel>
#include <QLineEdit>
#include <QPlainTextEdit>
#include <QPushButton>
#include <QStringListModel>
#include <QTimer>

using namespace Utils;
using namespace VcsBase;

namespace Git::Internal {

ChangeSelectionDialog::ChangeSelectionDialog(const FilePath &workingDirectory, Id id,
                                             QWidget *parent) :
    QDialog(parent)
{
    m_gitExecutable = GitClient::instance()->vcsBinary();
    m_gitEnvironment = GitClient::instance()->processEnvironment();

    resize(550, 350);
    setWindowTitle(Tr::tr("Select a Git Commit"));
    setObjectName("Git.ChangeSelectionDialog");

    m_workingDirectoryChooser = new PathChooser(this);

    m_changeNumberEdit = new CompletingLineEdit(this);
    m_changeNumberEdit->setObjectName("changeNumberEdit");
    m_changeNumberEdit->setText(Tr::tr("HEAD"));
    m_changeNumberEdit->setFocus();
    m_changeNumberEdit->selectAll();

    m_detailsText = new QPlainTextEdit(this);
    m_detailsText->setObjectName("detailsText");
    m_detailsText->setUndoRedoEnabled(false);
    m_detailsText->setLineWrapMode(QPlainTextEdit::NoWrap);
    m_detailsText->setReadOnly(true);

    auto selectFromHistoryButton = new QPushButton(Tr::tr("Browse &History..."));
    auto closeButton = new QPushButton(Tr::tr("&Close"));
    auto archiveButton = new QPushButton(Tr::tr("&Archive..."));

    m_checkoutButton = new QPushButton(Tr::tr("Check&out"));
    m_revertButton = new QPushButton(Tr::tr("&Revert"));
    m_cherryPickButton = new QPushButton(Tr::tr("Cherry &Pick"));
    m_showButton = new QPushButton(Tr::tr("&Show"));
    m_showButton->setObjectName("showButton");

    m_workingDirectoryChooser->setExpectedKind(PathChooser::ExistingDirectory);
    m_workingDirectoryChooser->setPromptDialogTitle(Tr::tr("Select Git Directory"));
    m_workingDirectoryChooser->setFilePath(workingDirectory);

    using namespace Layouting;
    Column {
        Grid {
            Tr::tr("Working directory:"), m_workingDirectoryChooser, br,
            Tr::tr("Change:"), m_changeNumberEdit, selectFromHistoryButton,
        },
        m_detailsText,
        Row {
            closeButton, st, archiveButton, m_checkoutButton,
            m_revertButton, m_cherryPickButton, m_showButton
        }
    }.attachTo(this);

    connect(m_changeNumberEdit, &CompletingLineEdit::textChanged,
            this, &ChangeSelectionDialog::changeTextChanged);
    connect(m_workingDirectoryChooser, &PathChooser::textChanged,
            this, &ChangeSelectionDialog::recalculateDetails);
    connect(m_workingDirectoryChooser, &PathChooser::textChanged,
            this, &ChangeSelectionDialog::recalculateCompletion);
    connect(selectFromHistoryButton, &QPushButton::clicked,
            this, &ChangeSelectionDialog::selectCommitFromRecentHistory);
    connect(m_showButton, &QPushButton::clicked,
            this, std::bind(&ChangeSelectionDialog::acceptCommand, this, Show));
    connect(m_cherryPickButton, &QPushButton::clicked,
            this, std::bind(&ChangeSelectionDialog::acceptCommand, this, CherryPick));
    connect(m_revertButton, &QPushButton::clicked,
            this, std::bind(&ChangeSelectionDialog::acceptCommand, this, Revert));
    connect(m_checkoutButton, &QPushButton::clicked,
            this, std::bind(&ChangeSelectionDialog::acceptCommand, this, Checkout));
    connect(archiveButton, &QPushButton::clicked,
            this, std::bind(&ChangeSelectionDialog::acceptCommand, this, Archive));

    if (id == "Git.Revert")
        m_revertButton->setDefault(true);
    else if (id == "Git.CherryPick")
        m_cherryPickButton->setDefault(true);
    else if (id == "Git.Checkout")
        m_checkoutButton->setDefault(true);
    else if (id == "Git.Archive")
        archiveButton->setDefault(true);
    else
        m_showButton->setDefault(true);
    m_changeModel = new QStringListModel(this);
    auto changeCompleter = new QCompleter(m_changeModel, this);
    m_changeNumberEdit->setCompleter(changeCompleter);
    changeCompleter->setCaseSensitivity(Qt::CaseInsensitive);

    recalculateDetails();
    recalculateCompletion();

    connect(closeButton, &QPushButton::clicked, this, &QDialog::reject);
}

ChangeSelectionDialog::~ChangeSelectionDialog() = default;

QString ChangeSelectionDialog::change() const
{
    return m_changeNumberEdit->text().trimmed();
}

void ChangeSelectionDialog::selectCommitFromRecentHistory()
{
    FilePath workingDir = workingDirectory();
    if (workingDir.isEmpty())
        return;

    QString commit = change();
    int tilde = commit.indexOf('~');
    if (tilde != -1)
        commit.truncate(tilde);
    LogChangeDialog dialog(false, this);
    dialog.setWindowTitle(Tr::tr("Select Commit"));

    dialog.runDialog(workingDir, commit, LogChangeWidget::IncludeRemotes);

    if (dialog.result() == QDialog::Rejected || dialog.commitIndex() == -1)
        return;

    m_changeNumberEdit->setText(dialog.commit());
}

FilePath ChangeSelectionDialog::workingDirectory() const
{
    const FilePath workingDir = m_workingDirectoryChooser->filePath();
    if (workingDir.isEmpty() || !workingDir.exists())
        return {};

    return Core::VcsManager::findTopLevelForDirectory(workingDir);
}

ChangeCommand ChangeSelectionDialog::command() const
{
    return m_command;
}

void ChangeSelectionDialog::acceptCommand(ChangeCommand command)
{
    m_command = command;
    QDialog::accept();
}

//! Set commit message in details
void ChangeSelectionDialog::setDetails()
{
    Theme *theme = creatorTheme();

    QPalette palette;
    if (m_process->result() == ProcessResult::FinishedWithSuccess) {
        m_detailsText->setPlainText(m_process->cleanedStdOut());
        palette.setColor(QPalette::Text, theme->color(Theme::TextColorNormal));
        m_changeNumberEdit->setPalette(palette);
    } else if (m_process->result() == ProcessResult::StartFailed) {
        m_detailsText->setPlainText(Tr::tr("Error: Could not start Git."));
    } else {
        m_detailsText->setPlainText(Tr::tr("Error: Unknown reference"));
        palette.setColor(QPalette::Text, theme->color(Theme::TextColorError));
        m_changeNumberEdit->setPalette(palette);
        enableButtons(false);
    }
}

void ChangeSelectionDialog::enableButtons(bool b)
{
    m_showButton->setEnabled(b);
    m_cherryPickButton->setEnabled(b);
    m_revertButton->setEnabled(b);
    m_checkoutButton->setEnabled(b);
}

void ChangeSelectionDialog::recalculateCompletion()
{
    const FilePath workingDir = workingDirectory();
    if (workingDir == m_oldWorkingDir)
        return;
    m_oldWorkingDir = workingDir;
    m_changeModel->setStringList(QStringList());

    if (workingDir.isEmpty())
        return;

    GitClient *client = GitClient::instance();
    Process *process = new Process(this);
    process->setEnvironment(client->processEnvironment());
    process->setCommand({client->vcsBinary(), {"for-each-ref", "--format=%(refname:short)"}});
    process->setWorkingDirectory(workingDir);
    process->setUseCtrlCStub(true);
    connect(process, &Process::done, this, [this, process] {
        if (process->result() == ProcessResult::FinishedWithSuccess)
            m_changeModel->setStringList(process->cleanedStdOut().split('\n'));
        process->deleteLater();
    });
    process->start();
}

void ChangeSelectionDialog::recalculateDetails()
{
    enableButtons(true);

    const FilePath workingDir = workingDirectory();
    if (workingDir.isEmpty()) {
        m_detailsText->setPlainText(Tr::tr("Error: Bad working directory."));
        return;
    }

    const QString ref = change();
    if (ref.isEmpty()) {
        m_detailsText->clear();
        return;
    }

    m_process.reset(new Process);
    connect(m_process.get(), &Process::done, this, &ChangeSelectionDialog::setDetails);
    m_process->setWorkingDirectory(workingDir);
    m_process->setEnvironment(m_gitEnvironment);
    m_process->setCommand({m_gitExecutable, {"show", "--decorate", "--stat=80", ref}});
    m_process->start();
    m_detailsText->setPlainText(Tr::tr("Fetching commit data..."));
}

void ChangeSelectionDialog::changeTextChanged(const QString &text)
{
    if (QCompleter *comp = m_changeNumberEdit->completer()) {
        if (text.isEmpty() && !comp->popup()->isVisible()) {
            comp->setCompletionPrefix(text);
            QTimer::singleShot(0, comp, [comp]{ comp->complete(); });
        }
    }
    recalculateDetails();
}

} // Git::Internal