summaryrefslogtreecommitdiff
path: root/src/buildtool/computed_roots/artifacts_root.cpp
blob: 1fa5ee7ddc42eaae1d3920a2832e8ff2a6880d51 (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
// Copyright 2024 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "src/buildtool/computed_roots/artifacts_root.hpp"

#include <deque>
#include <filesystem>
#include <functional>
#include <memory>
#include <stack>
#include <unordered_map>
#include <utility>
#include <vector>

#include "fmt/core.h"
#include "gsl/gsl"
#include "nlohmann/json.hpp"
#include "src/buildtool/build_engine/expression/expression.hpp"
#include "src/buildtool/common/artifact.hpp"
#include "src/buildtool/common/artifact_digest.hpp"
#include "src/buildtool/file_system/git_repo.hpp"
#include "src/buildtool/file_system/object_type.hpp"
#include "src/utils/cpp/expected.hpp"
#include "src/utils/cpp/hex_string.hpp"

namespace {
void AddEntryRaw(gsl::not_null<GitRepo::tree_entries_t*> const& tree,
                 std::string const& p,
                 std::string const& raw_hash,
                 ObjectType const& ot) {
    if (tree->contains(raw_hash)) {
        (*tree)[raw_hash].emplace_back(p, ot);
    }
    else {
        (*tree)[raw_hash] =
            std::vector<GitRepo::TreeEntry>{GitRepo::TreeEntry{p, ot}};
    }
}

auto AddEntry(gsl::not_null<GitRepo::tree_entries_t*> const& tree,
              std::string const& p,
              std::string const& hash,
              ObjectType const& ot,
              AsyncMapConsumerLoggerPtr const& logger) -> bool {
    auto raw_hash = FromHexString(hash);
    if (not raw_hash) {
        (*logger)(
            fmt::format("Not a hex string {}", nlohmann::json(hash).dump()),
            true);
        return false;
    }
    AddEntryRaw(tree, p, *raw_hash, ot);
    return true;
}

// Structure building a git tree from entries traversed in order so that
// all entries of a subdirectory come next to each other.
struct PartialTree {
    std::filesystem::path current_dir_;
    std::stack<GitRepo::tree_entries_t> partial_trees_;

    PartialTree() { partial_trees_.emplace(); }

    void Down(std::string const& segment) {
        if (segment == "." or segment.empty()) {
            return;
        }
        partial_trees_.emplace();
        current_dir_ = current_dir_ / segment;
    }

    auto Up() -> bool {
        auto name = current_dir_.filename().string();
        auto git_tree = GitRepo::CreateShallowTree(partial_trees_.top());
        if (not git_tree) {
            return false;
        }
        current_dir_ = current_dir_.parent_path();
        partial_trees_.pop();
        AddEntryRaw(
            &partial_trees_.top(), name, git_tree->first, ObjectType::Tree);
        return true;
    }

    auto To(std::filesystem::path const& dir) -> bool {
        auto rel_path = dir.lexically_relative(current_dir_);
        while (*rel_path.begin() == "..") {
            if (not Up()) {
                return false;
            }
            rel_path = dir.lexically_relative(current_dir_);
        }
        for (auto const& segment : rel_path) {
            Down(segment.string());
        }
        return true;
    }

    auto Add(std::string const& ps,
             std::string const& hash,
             ObjectType const& ot,
             AsyncMapConsumerLoggerPtr const& logger) -> bool {
        auto p = std::filesystem::path(ps);
        if (not To(p.parent_path())) {
            (*logger)("Failure in tree computation", true);
            return false;
        }
        return AddEntry(
            &partial_trees_.top(), p.filename().string(), hash, ot, logger);
    }

    auto Done(AsyncMapConsumerLoggerPtr const& logger)
        -> std::optional<std::string> {
        while (not current_dir_.empty()) {
            if (not Up()) {
                (*logger)("Failure computing final git tree", true);
                return std::nullopt;
            }
        }
        auto git_tree = GitRepo::CreateShallowTree(partial_trees_.top());
        if (not git_tree) {
            (*logger)("Failure computing top-level git tree", true);
            return std::nullopt;
        }
        return ToHexString(git_tree->first);
    }
};
}  // namespace

auto ArtifactsRoot(ExpressionPtr const& stage,
                   AsyncMapConsumerLoggerPtr const& logger,
                   std::optional<RehashUtils::Rehasher> const& rehash)
    -> std::optional<std::string> {
    if (not stage->IsMap()) {
        (*logger)(fmt::format("Expected stage to be a map, but found {}",
                              stage->ToString()),
                  true);
        return std::nullopt;
    }
    // We obtain the entries odered by key; in particular, the entries of all
    // subtrees will be next to each other. So, we compute the final tree
    // keeping a stack of partially set up tree while walking.
    auto partial_tree = PartialTree();
    for (auto const& [ps, entry] : stage->Map().Items()) {
        if (not entry->IsArtifact()) {
            (*logger)(fmt::format("Expected stage, but at entry {} found {}",
                                  nlohmann::json(ps).dump(),
                                  entry->ToString()),
                      true);
            return std::nullopt;
        }
        auto const& val = entry->Artifact();
        if (not val.IsKnown()) {
            (*logger)(fmt::format(
                          "Expected evaluated stage, but at entry {} found {}",
                          nlohmann::json(ps).dump(),
                          entry->ToString()),
                      true);
            return std::nullopt;
        }
        auto info_opt = val.ToArtifact().Info();
        if (not info_opt) {
            (*logger)(fmt::format("Failed to determine artifact info of known "
                                  "artifact {}",
                                  entry->ToString()),
                      true);
            return std::nullopt;
        }
        auto info = *info_opt;
        if (rehash) {
            auto rehash_result = rehash->Rehash(info);
            if (not rehash_result) {
                (*logger)(
                    fmt::format("Rehashing failed: {}", rehash_result.error()),
                    true);
                return std::nullopt;
            }
            info = *rehash_result;
        }
        if (not partial_tree.Add(ps, info.digest.hash(), info.type, logger)) {
            return std::nullopt;
        }
    }
    return partial_tree.Done(logger);
}