aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/vcsbase/vcscommand.cpp
blob: eed4c0fc6fbd3e4bbfdf9ccf184a1e04c5104423 (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
// Copyright (C) 2016 Brian McGillion and Hugues Delorme
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "vcscommand.h"

#include "vcsbaseplugin.h"
#include "vcsoutputwindow.h"

#include <coreplugin/icore.h>

#include <QtTaskTree/QSingleTaskTreeRunner>

#include <utils/environment.h>
#include <utils/globalfilechangeblocker.h>
#include <utils/qtcprocess.h>
#include <utils/qtcassert.h>
#include <utils/threadutils.h>

using namespace Core;
using namespace QtTaskTree;
using namespace Utils;

using namespace std::chrono;

namespace VcsBase {

CommandResult::CommandResult(const Process &process)
    : m_result(process.result())
    , m_exitCode(process.exitCode())
    , m_exitMessage(process.exitMessage())
    , m_cleanedStdOut(process.cleanedStdOut())
    , m_cleanedStdErr(process.cleanedStdErr())
    , m_rawStdOut(process.rawStdOut())
    , m_workingDirectory(process.workingDirectory())
{}

CommandResult::CommandResult(const Process &process, ProcessResult result)
    : CommandResult(process)
{
    m_result = result;
}

ExecutableItem errorTask(const FilePath &workingDir, const QString &errorMessage)
{
    return QSyncTask([workingDir, errorMessage] {
        VcsOutputWindow::appendError(workingDir, errorMessage);
        return false;
    });
}

enum class RunMode { Asynchronous, Blocking };

static ProcessTask vcsProcessTaskHelper(
    const VcsProcessData &data,
    const std::optional<Storage<CommandResult>> &resultStorage = {},
    const seconds timeout = seconds(10),
    RunMode runMode = RunMode::Asynchronous)
{
    const auto onDone = [data, resultStorage](const Process &process, DoneWith doneWith) {
        if (data.flags & RunFlags::ExpectRepoChanges)
            GlobalFileChangeBlocker::instance()->forceBlocked(false);
        ProcessResult result;
        if (doneWith == DoneWith::Cancel) {
            result = ProcessResult::Canceled;
        } else if (data.interpreter && process.error() != QProcess::FailedToStart
            && process.exitStatus() == QProcess::NormalExit) {
            result = data.interpreter(process.exitCode());
        } else {
            result = process.result();
        }

        const FilePath workingDirectory = process.workingDirectory();
        const QString message = Process::exitMessage(process.commandLine(), result,
                                                     process.exitCode(), process.processDuration());
        if (result == ProcessResult::FinishedWithSuccess) {
            if (data.flags & RunFlags::ShowSuccessMessage)
                VcsOutputWindow::appendMessage(workingDirectory, message);
        } else if (!(data.flags & RunFlags::SuppressFailMessage)) {
            VcsOutputWindow::appendError(workingDirectory, message);
        }
        if (data.flags & RunFlags::ExpectRepoChanges)
            VcsManager::emitRepositoryChanged(workingDirectory);
        if (resultStorage)
            **resultStorage = CommandResult(process, result);
        return result == ProcessResult::FinishedWithSuccess;
    };

    const auto onSetup = [data, onDone, timeout, runMode](Process &process) {
        Environment environment = data.runData.environment;
        VcsBase::setProcessEnvironment(&environment);
        if (data.flags & RunFlags::ForceCLocale) {
            environment.set("LANG", "C");
            environment.set("LANGUAGE", "C");
        }
        process.setEnvironment(environment);
        process.setWorkingDirectory(data.runData.workingDirectory);
        process.setCommand(data.runData.command);
        process.setDisableUnixTerminal();
        process.setUseCtrlCStub(true);

        if (data.flags & RunFlags::ExpectRepoChanges)
            GlobalFileChangeBlocker::instance()->forceBlocked(true);

        if (!(data.flags & RunFlags::SuppressCommandLogging))
            VcsOutputWindow::appendCommand(data.runData.workingDirectory, data.runData.command);

        if (data.flags & RunFlags::MergeOutputChannels)
            process.setProcessChannelMode(QProcess::MergedChannels);

        if (data.encoding.isValid())
            process.setEncoding(data.encoding);

        const bool installStdError = !(data.flags & RunFlags::MergeOutputChannels)
            && (data.stdErrHandler || data.progressParser || !(data.flags & RunFlags::SuppressStdErr));

        if (installStdError) {
            process.setTextChannelMode(Channel::Error, TextChannelMode::MultiLine);
            QObject::connect(&process, &Process::textOnStandardError, &process,
                             [flags = data.flags, workingDir = process.workingDirectory(),
                              handler = data.stdErrHandler](const QString &text) {
                if (!(flags & RunFlags::SuppressStdErr))
                    VcsOutputWindow::appendError(workingDir, text);
                if (handler)
                    handler(text);
            });
        }
        if (data.progressParser || data.stdOutHandler || data.flags & RunFlags::ShowStdOut) {
            process.setTextChannelMode(Channel::Output, TextChannelMode::MultiLine);
            QObject::connect(&process, &Process::textOnStandardOutput, &process,
                             [flags = data.flags, workingDir = process.workingDirectory(),
                              handler = data.stdOutHandler](const QString &text) {
                if (flags & RunFlags::ShowStdOut)
                    VcsOutputWindow::appendText(workingDir, text);
                if (handler)
                    handler(text);
            });
        }

        if (!(data.flags & RunFlags::SuppressCommandLogging)) {
            ProcessProgress *progress = new ProcessProgress(&process);
            if (data.progressParser)
                progress->setProgressParser(data.progressParser);
        }

        if (runMode == RunMode::Blocking) {
            process.runBlocking(timeout);
            DoneWith doneWith = DoneWith::Error;
            if (process.result() == ProcessResult::FinishedWithSuccess)
                doneWith = DoneWith::Success;
            else if (process.result() == ProcessResult::Canceled)
                doneWith = DoneWith::Cancel;
            const bool success = onDone(process, doneWith);
            return success ? SetupResult::StopWithSuccess : SetupResult::StopWithError;
        }
        return SetupResult::Continue;
    };
    return ProcessTask(onSetup, onDone);
}

ProcessTask vcsProcessTask(const VcsProcessData &data,
                           const std::optional<Storage<CommandResult>> &resultStorage)
{
    return vcsProcessTaskHelper(data, resultStorage);
}

CommandResult vcsRunBlocking(const VcsProcessData &data, const seconds timeout)
{
    CommandResult result;
    const Storage<CommandResult> resultStorage;

    const auto onDone = [resultStorage, &result] { result = *resultStorage; };

    const Group recipe {
        resultStorage,
        vcsProcessTaskHelper(data, resultStorage, timeout, RunMode::Blocking),
        onGroupDone(onDone)
    };

    QSingleTaskTreeRunner taskTreeRunner;
    taskTreeRunner.start(recipe);
    QTC_CHECK(!taskTreeRunner.isRunning());
    return result;
}

} // namespace VcsBase