|
| 1 | +#include <vix/cli/project/native/GitPackageMetadataSource.hpp> |
| 2 | + |
| 3 | +#include <vix/cli/project/native/PackageContentDigest.hpp> |
| 4 | +#include <vix/cli/project/native/TomlParser.hpp> |
| 5 | +#include <vix/cli/project/native/Validation.hpp> |
| 6 | + |
| 7 | +#include <algorithm> |
| 8 | +#include <atomic> |
| 9 | +#include <chrono> |
| 10 | +#include <cstdint> |
| 11 | +#include <filesystem> |
| 12 | +#include <string_view> |
| 13 | +#include <system_error> |
| 14 | +#include <type_traits> |
| 15 | +#include <utility> |
| 16 | + |
| 17 | +namespace vix::cli::project::native |
| 18 | +{ |
| 19 | + namespace fs = std::filesystem; |
| 20 | + namespace |
| 21 | + { |
| 22 | + bool valid_full_oid(std::string_view oid) |
| 23 | + { |
| 24 | + return (oid.size() == 40 || oid.size() == 64) && |
| 25 | + std::all_of(oid.begin(), oid.end(), [](char ch) |
| 26 | + { return (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f'); }); |
| 27 | + } |
| 28 | + |
| 29 | + std::string trim_line(std::string value) |
| 30 | + { |
| 31 | + while (!value.empty() && (value.back() == '\n' || value.back() == '\r')) value.pop_back(); |
| 32 | + return value; |
| 33 | + } |
| 34 | + |
| 35 | + GitCommandResult run(GitCommandRunner &runner, std::vector<std::string> arguments) |
| 36 | + { return runner.run({std::move(arguments), {}}); } |
| 37 | + |
| 38 | + bool valid_revision_name(GitCommandRunner &runner, std::string_view namespaceName, |
| 39 | + std::string_view value) |
| 40 | + { |
| 41 | + if (value.empty() || value.find_first_of("\r\n") != std::string_view::npos || |
| 42 | + value.find('\0') != std::string_view::npos) return false; |
| 43 | + const auto checked = run(runner, {"git", "check-ref-format", std::string{namespaceName} + std::string{value}}); |
| 44 | + return checked.started && checked.exitCode == 0; |
| 45 | + } |
| 46 | + |
| 47 | + GitQueryResult failed(std::string message) |
| 48 | + { return {GitQueryStatus::failed, std::nullopt, std::move(message)}; } |
| 49 | + |
| 50 | + GitQueryResult missing(std::string message) |
| 51 | + { return {GitQueryStatus::not_found, std::nullopt, std::move(message)}; } |
| 52 | + |
| 53 | + std::optional<std::string> package_contract_bytes(const CanonicalPackageTree &tree) |
| 54 | + { |
| 55 | + const auto entry = std::find_if(tree.entries.begin(), tree.entries.end(), [](const auto &value) |
| 56 | + { return value.path == "vix.package.toml"; }); |
| 57 | + if (entry == tree.entries.end() || entry->kind != CanonicalPackageEntryKind::file) return std::nullopt; |
| 58 | + std::string bytes; |
| 59 | + bytes.reserve(entry->content.size()); |
| 60 | + for (const auto byte : entry->content) bytes.push_back(static_cast<char>(std::to_integer<unsigned char>(byte))); |
| 61 | + return bytes; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + GitQueryResult GitPackageMetadataSource::resolve_git( |
| 66 | + const GitPackageIdentity &identity, const GitRevisionRequirement &revision) |
| 67 | + { |
| 68 | + if (!identity.repositoryRoot || identity.normalizedRepository.empty() || |
| 69 | + identity.normalizedRepository.find_first_of("\r\n") != std::string::npos || |
| 70 | + identity.normalizedRepository.find('\0') != std::string::npos) |
| 71 | + return failed("Git metadata request has an invalid repository identity or unsupported non-root package identity"); |
| 72 | + |
| 73 | + std::string fullCommit; |
| 74 | + std::string sourceRef; |
| 75 | + std::string destinationRef; |
| 76 | + bool commitRequest = false; |
| 77 | + const bool revisionValid = std::visit([&](const auto &value) |
| 78 | + { |
| 79 | + using Revision = std::decay_t<decltype(value)>; |
| 80 | + if constexpr (std::is_same_v<Revision, CommitRevision>) |
| 81 | + { |
| 82 | + commitRequest = true; |
| 83 | + fullCommit = value.value; |
| 84 | + return valid_full_oid(value.value); |
| 85 | + } |
| 86 | + else if constexpr (std::is_same_v<Revision, BranchRevision>) |
| 87 | + { |
| 88 | + sourceRef = "refs/heads/" + value.value; |
| 89 | + destinationRef = "refs/vix-metadata/heads/" + value.value; |
| 90 | + return valid_revision_name(runner_, "refs/heads/", value.value); |
| 91 | + } |
| 92 | + else |
| 93 | + { |
| 94 | + sourceRef = "refs/tags/" + value.value; |
| 95 | + destinationRef = "refs/vix-metadata/tags/" + value.value; |
| 96 | + return valid_revision_name(runner_, "refs/tags/", value.value); |
| 97 | + } |
| 98 | + }, revision); |
| 99 | + if (!revisionValid) |
| 100 | + return failed("Git revision must be a full commit ID or a valid exact branch/tag name"); |
| 101 | + |
| 102 | + std::error_code ec; |
| 103 | + fs::create_directories(workRoot_, ec); |
| 104 | + if (ec) return failed("cannot create injected Git metadata work root"); |
| 105 | + static std::atomic<std::uint64_t> counter{0}; |
| 106 | + const auto stamp = std::chrono::steady_clock::now().time_since_epoch().count(); |
| 107 | + fs::path workspace = workRoot_ / ("vix-git-metadata-" + std::to_string(stamp) + "-" + |
| 108 | + std::to_string(counter.fetch_add(1, std::memory_order_relaxed))); |
| 109 | + if (!fs::create_directory(workspace, ec) || ec) |
| 110 | + return failed("cannot create isolated Git metadata workspace"); |
| 111 | + struct Cleanup |
| 112 | + { |
| 113 | + fs::path path; |
| 114 | + ~Cleanup() { std::error_code ignored; fs::remove_all(path, ignored); } |
| 115 | + } cleanup{workspace}; |
| 116 | + const auto gitDir = (workspace / "objects.git").string(); |
| 117 | + const auto initialized = run(runner_, {"git", "init", "--bare", "--quiet", gitDir}); |
| 118 | + if (!initialized.started || initialized.exitCode != 0) |
| 119 | + return failed("could not initialize isolated Git metadata object database"); |
| 120 | + |
| 121 | + if (commitRequest) |
| 122 | + { |
| 123 | + auto fetched = runner_.run({{"git", "--git-dir", gitDir, "fetch", "--no-tags", "--depth=1", "--", |
| 124 | + identity.normalizedRepository, fullCommit}, {}}); |
| 125 | + if (!fetched.started) return failed("Git executable unavailable during exact-commit metadata fetch"); |
| 126 | + auto type = run(runner_, {"git", "--git-dir", gitDir, "cat-file", "-t", fullCommit}); |
| 127 | + if (!type.started || type.exitCode != 0) |
| 128 | + { |
| 129 | + fetched = runner_.run({{"git", "--git-dir", gitDir, "fetch", "--no-tags", "--depth=1", "--", |
| 130 | + identity.normalizedRepository, "+refs/*:refs/vix-metadata/all/*"}, {}}); |
| 131 | + if (!fetched.started || fetched.exitCode != 0) |
| 132 | + return missing("the requested full Git commit is unavailable from the configured repository"); |
| 133 | + type = run(runner_, {"git", "--git-dir", gitDir, "cat-file", "-t", fullCommit}); |
| 134 | + } |
| 135 | + if (!type.started || type.exitCode != 0) |
| 136 | + return missing("the requested full Git commit is absent from the configured repository"); |
| 137 | + if (trim_line(std::move(type.standardOutput)) != "commit") |
| 138 | + return failed("the requested full Git object is not a commit"); |
| 139 | + } |
| 140 | + else |
| 141 | + { |
| 142 | + const auto refspec = "+" + sourceRef + ":" + destinationRef; |
| 143 | + const auto fetched = runner_.run({{"git", "--git-dir", gitDir, "fetch", "--no-tags", "--depth=1", "--", |
| 144 | + identity.normalizedRepository, refspec}, {}}); |
| 145 | + if (!fetched.started) return failed("Git executable unavailable while resolving the requested ref"); |
| 146 | + if (fetched.exitCode != 0) |
| 147 | + { |
| 148 | + const auto &diagnostic = fetched.standardError; |
| 149 | + if (diagnostic.find("couldn't find remote ref") != std::string::npos || |
| 150 | + diagnostic.find("no such ref") != std::string::npos) |
| 151 | + return missing("the requested exact Git ref is not present in the configured repository"); |
| 152 | + return failed("Git transport failed while fetching the requested exact ref"); |
| 153 | + } |
| 154 | + const auto peel = run(runner_, {"git", "--git-dir", gitDir, "rev-parse", "--verify", |
| 155 | + destinationRef + "^{commit}"}); |
| 156 | + if (!peel.started || peel.exitCode != 0) |
| 157 | + return failed("the requested Git ref does not resolve to a commit"); |
| 158 | + fullCommit = trim_line(peel.standardOutput); |
| 159 | + if (!valid_full_oid(fullCommit)) return failed("Git returned a malformed full commit ID"); |
| 160 | + } |
| 161 | + |
| 162 | + const auto objectTree = read_git_object_tree(runner_, gitDir, fullCommit); |
| 163 | + if (objectTree.status != GitObjectTreeReadStatus::available || !objectTree.tree) |
| 164 | + return failed(objectTree.diagnostic.empty() ? "could not inspect exact Git commit objects" : objectTree.diagnostic); |
| 165 | + const auto contractBytes = package_contract_bytes(*objectTree.tree); |
| 166 | + if (!contractBytes) return failed("exact Git commit is missing a regular vix.package.toml at repository root"); |
| 167 | + const auto parsedContract = parse_package_contract_text(*contractBytes, "Git vix.package.toml"); |
| 168 | + if (!parsedContract.valid() || !parsedContract.value || !validate_package_contract(*parsedContract.value).valid()) |
| 169 | + return failed("exact Git vix.package.toml is malformed or semantically invalid"); |
| 170 | + if (!std::holds_alternative<GitPackageContractIdentity>(parsedContract.value->identity)) |
| 171 | + return failed("exact Git vix.package.toml declares a non-Git package identity"); |
| 172 | + const auto canonical = canonicalize_package_content_tree(*objectTree.tree); |
| 173 | + if (!canonical.valid() || !canonical.contentId) |
| 174 | + return failed("exact Git package tree cannot be represented by the P10O canonical contract"); |
| 175 | + |
| 176 | + return {GitQueryStatus::found, |
| 177 | + GitResolution{identity.normalizedRepository, revision, std::move(fullCommit), |
| 178 | + canonical.contentId->digest(), *parsedContract.value}, {}}; |
| 179 | + } |
| 180 | +} |
0 commit comments