summaryrefslogtreecommitdiff
path: root/src/buildtool/file_system/git_tree.cpp
blob: 1805961c921741c8f6e961eb9544bba55691b001 (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
#include "src/buildtool/file_system/git_tree.hpp"

#include <sstream>

#include "src/buildtool/logging/logger.hpp"

extern "C" {
#include <git2.h>
}

namespace {

constexpr auto kOIDRawSize{GIT_OID_RAWSZ};

auto const kLoadTreeError =
    std::make_shared<std::optional<GitTree>>(std::nullopt);

[[nodiscard]] auto PermToType(std::string const& perm_str) noexcept
    -> std::optional<ObjectType> {
    constexpr auto kPermBase = 8;
    constexpr auto kTreePerm = 040000;
    constexpr auto kFilePerm = 0100644;
    constexpr auto kExecPerm = 0100755;
    constexpr auto kLinkPerm = 0120000;

    int perm = std::stoi(perm_str, nullptr, kPermBase);

    switch (perm) {
        case kTreePerm:
            return ObjectType::Tree;
        case kFilePerm:
            return ObjectType::File;
        case kExecPerm:
            return ObjectType::Executable;
        case kLinkPerm:
            Logger::Log(LogLevel::Error, "symlinks are not yet supported");
            return std::nullopt;
        default:
            Logger::Log(LogLevel::Error, "unsupported permission {}", perm_str);
            return std::nullopt;
    }
}

auto ParseRawTreeObject(GitCASPtr const& cas,
                        std::string const& raw_tree) noexcept
    -> std::optional<GitTree::entries_t> {
    std::string perm{};
    std::string path{};
    std::string hash(kOIDRawSize, '\0');
    std::istringstream iss{raw_tree};
    GitTree::entries_t entries{};
    // raw tree format is: "<perm> <path>\0<hash>[next entries...]"
    while (std::getline(iss, perm, ' ') and   // <perm>
           std::getline(iss, path, '\0') and  // <path>
           iss.read(hash.data(),              // <hash>
                    static_cast<std::streamsize>(hash.size()))) {
        auto type = PermToType(perm);
        if (not type) {
            return std::nullopt;
        }
        try {
            entries.emplace(path,
                            std::make_shared<GitTreeEntry>(cas, hash, *type));
        } catch (std::exception const& ex) {
            Logger::Log(LogLevel::Error,
                        "parsing git raw tree object failed with:\n{}",
                        ex.what());
            return std::nullopt;
        }
    }
    return entries;
}

// resolve '.' and '..' in path.
[[nodiscard]] auto ResolveRelativePath(
    std::filesystem::path const& path) noexcept -> std::filesystem::path {
    auto normalized = path.lexically_normal();
    return (normalized / "").parent_path();  // strip trailing slash
}

// NOLINTNEXTLINE(misc-no-recursion)
[[nodiscard]] auto LookupEntryPyPath(
    GitTree const& tree,
    std::filesystem::path::const_iterator it,
    std::filesystem::path::const_iterator const& end) noexcept
    -> GitTreeEntryPtr {
    auto segment = *it;
    auto entry = tree.LookupEntryByName(segment);
    if (not entry) {
        return nullptr;
    }
    if (++it != end) {
        if (not entry->IsTree()) {
            return nullptr;
        }
        return LookupEntryPyPath(*entry->Tree(), it, end);
    }
    return entry;
}

}  // namespace

auto GitTree::Read(std::filesystem::path const& repo_path,
                   std::string const& tree_id) noexcept
    -> std::optional<GitTree> {
    auto cas = GitCAS::Open(repo_path);
    if (not cas) {
        return std::nullopt;
    }
    return Read(cas, tree_id);
}

auto GitTree::Read(gsl::not_null<GitCASPtr> const& cas,
                   std::string const& tree_id) noexcept
    -> std::optional<GitTree> {
    auto obj = cas->ReadObject(tree_id, /*is_hex_id=*/true);
    if (not obj) {
        return std::nullopt;
    }
    auto entries = ParseRawTreeObject(cas, *obj);
    if (not entries) {
        return std::nullopt;
    }
    return GitTree{cas, std::move(*entries)};
}

auto GitTree::LookupEntryByName(std::string const& name) const noexcept
    -> GitTreeEntryPtr {
    auto entry_it = entries_.find(name);
    if (entry_it == entries_.end()) {
        Logger::Log(
            LogLevel::Error, "git tree does not contain entry {}", name);
        return nullptr;
    }
    return entry_it->second;
}

auto GitTree::LookupEntryByPath(
    std::filesystem::path const& path) const noexcept -> GitTreeEntryPtr {
    auto resolved = ResolveRelativePath(path);
    return LookupEntryPyPath(*this, resolved.begin(), resolved.end());
}

auto GitTreeEntry::Blob() const noexcept -> std::optional<std::string> {
    if (not IsBlob()) {
        return std::nullopt;
    }
    return cas_->ReadObject(raw_id_);
}

auto GitTreeEntry::Tree() const& noexcept -> std::optional<GitTree> const& {
    auto ptr = tree_cached_.load();
    if (not ptr) {
        if (not tree_loading_.exchange(true)) {
            ptr = kLoadTreeError;
            std::optional<std::string> obj{};
            if (IsTree() and (obj = cas_->ReadObject(raw_id_))) {
                if (auto entries = ParseRawTreeObject(cas_, *obj)) {
                    ptr = std::make_shared<std::optional<GitTree>>(
                        GitTree{cas_, std::move(*entries)});
                }
            }
            tree_cached_.store(ptr);
            tree_cached_.notify_all();
        }
        else {
            tree_cached_.wait(nullptr);
            ptr = tree_cached_.load();
        }
    }
    return *ptr;
}

auto GitTreeEntry::Size() const noexcept -> std::optional<std::size_t> {
    if (auto header = cas_->ReadHeader(raw_id_)) {
        return header->first;
    }
    return std::nullopt;
}