// SPDX-License-Identifier: AGPL-3.0-only // SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® module Crafter.Review:Artifact_impl; import :Artifact; import :Text; import :Crypto; import :Platform; import :Process; import std; namespace fs = std::filesystem; namespace Crafter::Review::Artifact { namespace { std::string ReadBytes(const fs::path& path) { std::optional bytes = Platform::ReadFile(path); if (!bytes) throw std::runtime_error(std::format("{}: cannot read", path.string())); return std::move(*bytes); } // Four decimal digits and a '-' at `at`, as (\d{4})-: the number. std::optional FourDigits(std::string_view name, std::size_t at) { std::size_t end = at; for (std::int32_t i = 0; i < 4; ++i) { if (end >= name.size()) return std::nullopt; const auto [c, used] = Dyn::CodePointAt(name, end); if (!Dyn::IsDecimal(c)) return std::nullopt; end += used; } if (end >= name.size() || name[end] != '-') return std::nullopt; return Dyn::ParseInt(name.substr(at, end - at)); } } std::string Sha256File(const fs::path& path) { return Sha256Hex(ReadBytes(path)); } std::string ArtifactHash(std::string_view mode, const fs::path& path) { if (mode == "reply") return Sha256File(path); std::string all; for (const std::string& name : PatchFiles(path)) all += ReadBytes(path / name); return Sha256Hex(all); } std::vector PatchFiles(const fs::path& path) { std::vector out; for (const fs::directory_entry& entry : fs::directory_iterator(path)) { std::string name = entry.path().filename().string(); if (name.ends_with(".patch")) out.push_back(std::move(name)); } std::ranges::sort(out); return out; } std::optional PatchNumber(std::string_view name) { if (name.starts_with('v')) { const std::size_t digits = Dyn::DecimalRun(name, 1); if (digits > 0 && 1 + digits < name.size() && name[1 + digits] == '-') { if (const std::optional k = FourDigits(name, 2 + digits)) return k; } } return FourDigits(name, 0); } std::map PatchTexts(const fs::path& path) { std::map out; for (const std::string& name : PatchFiles(path)) { const std::optional k = PatchNumber(name); if (!k) throw Dyn::Error("AttributeError", "'NoneType' object has no attribute 'group'"); out[*k] = Dyn::ReadText(path / name); } return out; } std::optional PatchHead(const fs::path& path) { const std::vector files = PatchFiles(path); if (files.empty()) return std::nullopt; // open(errors='replace').readline().split(): the first line, universal // newlines and all. const std::string text = Dyn::UniversalNewlines(Dyn::DecodeReplace(ReadBytes(path / files.back()))); const std::vector first = Dyn::Split(std::string_view(text).substr(0, text.find('\n'))); if (first.size() > 1 && first[0] == "From" && Dyn::Length(first[1]) == 40) return first[1]; return std::nullopt; } std::string Git(const fs::path& tree, std::span args) { std::vector argv = {"git", "-C", tree.string()}; argv.insert(argv.end(), args.begin(), args.end()); const Process::Captured run = Process::Run(argv); if (run.returnCode != 0) { const std::string& said = run.err.empty() ? run.out : run.err; throw Dyn::Error("RuntimeError", std::format("git -C {} {}: {}", tree.string(), Dyn::Join(args, " "), Dyn::Strip(Dyn::UniversalNewlines(Dyn::DecodeReplace(said))))); } return Dyn::UniversalNewlines(Dyn::DecodeReplace(run.out)); } bool IsToplevel(const fs::path& tree) { try { const std::string top = Dyn::Strip(Git(tree, std::array{"rev-parse", "--show-toplevel"})); return fs::weakly_canonical(fs::absolute(top)) == fs::weakly_canonical(fs::absolute(tree)); } catch (const std::exception&) { return false; } } std::string TreeHead(const fs::path& tree) { return Dyn::Strip(Git(tree, std::array{"rev-parse", "HEAD"})); } std::string TreeDirty(const fs::path& tree) { return Dyn::Strip(Git(tree, std::array{"status", "--porcelain", "--untracked-files=no"})); } }