Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/tools/RepositoryReport.cpp
blob: f985335ee1ef7f882c2ac06fc769913953a209a2 (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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Copyright (c) 2026 Progmasoft.
// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0

#include <algorithm>
#include <array>
#include <cstdio>
#include <cstdlib>
#include <memory>
#include <optional>
#include <string_view>
#include <system_error>

#if !defined(_WIN32)
#    include <unistd.h>
#endif

#include "tools/Amalgamation.hpp"
#include "tools/Repository.hpp"
#include "tools/RepositoryReport.hpp"
#include "tools/Versions.hpp"

// yyjson, compiled here and nowhere else in the tools, like in the runner.
#define yyjson_api static
#include <yyjson.c>

namespace Progmasoft::Catch3::Tools
{
    namespace
    {
        namespace fs = std::filesystem;

#if defined(_WIN32)
        constexpr char kPathSeparator = ';';
        constexpr std::array<std::string_view, 4> kExtensions{ ".exe",
                                                               ".cmd",
                                                               ".bat",
                                                               ".com" };
        constexpr std::string_view kNoErrors = " 2>nul";
#else
        constexpr char kPathSeparator = ':';
        constexpr std::array<std::string_view, 1> kExtensions{ "" };
        constexpr std::string_view kNoErrors = " 2>/dev/null";
#endif

        constexpr std::array<std::string_view, 11> kTools{
            "git",      "bazelisk",     "cmake",    "ninja",
            "meson",    "xmake",        "premake5", "clang",
            "clang-cl", "clang-format", "gcc",
        };

        constexpr std::string_view
        OperatingSystem()
        {
#if defined(_WIN32)
            return "windows";
#elif defined(__APPLE__)
            return "darwin";
#elif defined(__linux__)
            return "linux";
#else
            return "unknown";
#endif
        }

        constexpr std::string_view
        Architecture()
        {
#if defined(_M_X64) || defined(__x86_64__)
            return "amd64";
#elif defined(_M_ARM64) || defined(__aarch64__)
            return "arm64";
#else
            return "unknown";
#endif
        }

        /// Whether `file` can be started, which only POSIX says of a file.
        [[nodiscard]] bool
        IsProgram(const fs::path &file)
        {
            std::error_code error;
            if (!fs::is_regular_file(file, error))
            {
                return false;
            }
#if defined(_WIN32)
            return true;
#else
            return access(file.c_str(), X_OK) == 0;
#endif
        }

        [[nodiscard]] ToolInfo
        Find(std::string_view name, std::string_view searchPath)
        {
            ToolInfo tool;
            tool.Name = name;
            while (!tool.Available && !searchPath.empty())
            {
                const auto end = searchPath.find(kPathSeparator);
                const fs::path directory{ std::string{
                    searchPath.substr(0, end) } };
                searchPath.remove_prefix(end == std::string_view::npos
                                             ? searchPath.size()
                                             : end + 1);
                if (directory.empty())
                {
                    continue;
                }
                for (const auto extension : kExtensions)
                {
                    const auto file
                        = directory
                          / (std::string{ name } + std::string{ extension });
                    if (IsProgram(file))
                    {
                        tool.Path = file.string();
                        tool.Available = true;
                        break;
                    }
                }
            }
            return tool;
        }

        /// What `git <arguments>` writes in the current directory, or nothing
        /// when it fails. A shell reads the command, so it never holds a path.
        [[nodiscard]] std::optional<std::string>
        Git(std::string_view arguments)
        {
            const auto command
                = "git " + std::string{ arguments } + std::string{ kNoErrors };
#if defined(_WIN32)
            auto *pipe = _popen(command.c_str(), "r");
#else
            auto *pipe = popen(command.c_str(), "r");
#endif
            if (pipe == nullptr)
            {
                return std::nullopt;
            }
            std::string output;
            std::array<char, 512> buffer{};
            std::size_t count = 0;
            while ((count = std::fread(buffer.data(), 1, buffer.size(), pipe))
                   != 0)
            {
                output.append(buffer.data(), count);
            }
#if defined(_WIN32)
            const int status = _pclose(pipe);
#else
            const int status = pclose(pipe);
#endif
            if (status != 0)
            {
                return std::nullopt;
            }
            return std::string{ Trimmed(output) };
        }

        void
        AddText(yyjson_mut_doc *document,
                yyjson_mut_val *object,
                const char *key,
                const std::string &value)
        {
            if (!value.empty())
            {
                yyjson_mut_obj_add_strn(document,
                                        object,
                                        key,
                                        value.data(),
                                        value.size());
            }
        }

        struct FreeDocument final
        {
            void
            operator()(yyjson_mut_doc *document) const
            {
                yyjson_mut_doc_free(document);
            }
        };

        struct FreeText final
        {
            void
            operator()(char *text) const
            {
                std::free(text);
            }
        };
    } // namespace

    RepositoryReport
    CollectRepositoryReport(const fs::path &root)
    {
        RepositoryReport report;
        report.Root = root.string();
        report.Os = OperatingSystem();
        report.Architecture = Architecture();

        Problems ignored;
        report.Version = CurrentVersion(root, ignored).value_or("");
        report.EngineVersion
            = ParseEngineVersion(
                  ReadText(root / "src/Progmasoft/Catch3/EngineVersion.cpp")
                      .value_or(""))
                  .value_or("");

        const auto searchPath = ReadEnvironment("PATH").value_or("");
        for (const auto name : kTools)
        {
            report.Tools.push_back(Find(name, searchPath));
        }

        std::error_code error;
        fs::current_path(root, error);
        const auto branch = Git("branch --show-current");
        const auto revision = Git("rev-parse HEAD");
        const auto status = Git("status --porcelain --untracked-files=no");
        if (!error && branch && revision && status)
        {
            report.Branch = *branch;
            report.Revision = *revision;
            report.Dirty = !status->empty();
        }
        else
        {
            report.GitError = "Git checkout metadata could not be read";
        }
        return report;
    }

    std::string
    FormatReport(const RepositoryReport &report)
    {
        std::string text = "Checkout: " + report.Root + "\n";
        text += "Host: " + report.Os + "/" + report.Architecture + "\n";
        text += "Catch3: " + report.Version + ", on the Catch2 "
                + report.EngineVersion + " engine\n";
        text += "Branch: " + report.Branch + "\n";
        text += "Revision: " + report.Revision + "\n";
        text += std::string{ "Tracked changes: " }
                + (!report.Dirty   ? "unknown"
                   : *report.Dirty ? "true"
                                   : "false")
                + "\n";
        if (!report.GitError.empty())
        {
            text += report.GitError + "\n";
        }
        for (const auto &tool : report.Tools)
        {
            auto name = tool.Name;
            name.resize(std::max<std::size_t>(name.size(), 13), ' ');
            text += name + (tool.Available ? tool.Path : "not found") + "\n";
        }
        return text;
    }

    std::string
    ReportToJson(const RepositoryReport &report)
    {
        const std::unique_ptr<yyjson_mut_doc, FreeDocument> holder{
            yyjson_mut_doc_new(nullptr)
        };
        auto *document = holder.get();
        auto *root = yyjson_mut_obj(document);
        yyjson_mut_doc_set_root(document, root);
        AddText(document, root, "root", report.Root);
        AddText(document, root, "os", report.Os);
        AddText(document, root, "architecture", report.Architecture);
        AddText(document, root, "version", report.Version);
        AddText(document, root, "engineVersion", report.EngineVersion);
        AddText(document, root, "branch", report.Branch);
        AddText(document, root, "revision", report.Revision);
        if (report.Dirty)
        {
            yyjson_mut_obj_add_bool(document, root, "dirty", *report.Dirty);
        }
        else
        {
            yyjson_mut_obj_add_null(document, root, "dirty");
        }
        AddText(document, root, "gitError", report.GitError);
        auto *tools = yyjson_mut_obj_add_arr(document, root, "tools");
        for (const auto &tool : report.Tools)
        {
            auto *entry = yyjson_mut_arr_add_obj(document, tools);
            AddText(document, entry, "name", tool.Name);
            AddText(document, entry, "path", tool.Path);
            yyjson_mut_obj_add_bool(document,
                                    entry,
                                    "available",
                                    tool.Available);
        }
        const std::unique_ptr<char, FreeText> text{
            yyjson_mut_write(document, YYJSON_WRITE_PRETTY_TWO_SPACES, nullptr)
        };
        return text == nullptr ? std::string{}
                               : std::string{ text.get() } + "\n";
    }
} // namespace Progmasoft::Catch3::Tools