navid/veins / master / xveins/src/git/git_internal.h
git_internal.h
← xveins/src/git · 4551 bytes · raw · download
#pragma once
#include "core/common.h"
#include "object/object.h"
#include "store/store.h"
#include <array>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <vector>
namespace vn::git {
using Oid = std::array<std::uint8_t, 20>;
// A single git object is materialized in memory during import/export. Bound it
// so a hostile pack or loose object cannot trigger an unbounded allocation.
constexpr std::size_t MAX_GIT_OBJECT_BODY = 1ull * 1024ull * 1024ull * 1024ull;
// Packs are currently read in full. Bound the file size for the same reason.
constexpr std::uint64_t MAX_GIT_PACK_BYTES = 8ull * 1024ull * 1024ull * 1024ull;
constexpr std::size_t MAX_GIT_DELTA_DEPTH = 128;
struct OidLess {
bool operator()(const Oid& a, const Oid& b) const {
return std::memcmp(a.data(), b.data(), a.size()) < 0;
}
};
enum Kind : std::uint8_t {
KIND_NONE = 0,
KIND_COMMIT = 1,
KIND_TREE = 2,
KIND_BLOB = 3,
KIND_TAG = 4,
KIND_OFS_DELTA = 6,
KIND_REF_DELTA = 7,
};
struct Object {
std::uint8_t kind = 0;
std::vector<std::uint8_t> body;
};
struct Pack {
fs::path path;
std::vector<std::uint8_t> data;
std::vector<Oid> oids;
std::vector<std::uint64_t> offsets;
};
struct Repo {
fs::path git_dir;
std::vector<Pack> packs;
};
std::string oid_hex(const Oid& oid);
std::optional<Oid> oid_from_hex(std::string_view hex);
std::optional<Oid> object_hash(std::uint8_t kind, Bytes body);
std::optional<Repo> open_repo(const fs::path& git_dir);
std::optional<Object> read_object(Repo& repo, const Oid& oid, bool verify = false,
std::size_t depth = 0);
std::shared_ptr<Repo> cached_repo(const fs::path& git_dir);
struct RawTreeEntry {
std::string mode;
std::string name;
Oid oid{};
};
struct ParsedTree {
std::vector<RawTreeEntry> entries;
bool canonical = true;
};
struct ParsedTag {
Oid target{};
std::uint8_t target_kind = 0;
};
struct ParsedCommit {
Oid tree{};
std::vector<Oid> parents;
Sig author;
Sig committer;
std::string message;
std::vector<KeyValue> extras;
std::optional<ChangeId> change_id;
bool canonical = true;
};
std::optional<Sig> parse_sig(std::string_view value);
std::optional<ParsedCommit> parse_commit(Bytes body);
std::vector<std::uint8_t> serialize_commit(const ParsedCommit& commit);
std::optional<ParsedTree> parse_tree(Bytes body);
std::string mode_for_kind(std::uint8_t kind);
std::uint8_t mode_kind(std::string_view mode);
std::optional<ParsedTag> parse_tag(Bytes body);
struct IdMap {
std::map<Oid, Id, OidLess> items;
std::optional<Id> get(const Oid& oid) const;
void put(const Oid& oid, const Id& id);
std::optional<Oid> reverse(const Id& id) const;
};
bool load_map(const fs::path& root, std::string_view name, IdMap& out);
bool save_map(const fs::path& root, std::string_view name, const IdMap& map);
bool save_raw(const fs::path& root, const Id& id, Bytes body);
std::optional<std::vector<std::uint8_t>> load_raw(const fs::path& root, const Id& id);
struct Ref {
std::string name;
Oid oid{};
};
struct RefSet {
std::vector<Ref> branches;
std::vector<Ref> tags;
std::vector<Ref> remotes;
std::optional<Oid> head;
};
RefSet read_refs(const fs::path& git_dir);
Id lazy_blob_id(const Oid& oid);
std::optional<Oid> lazy_blob_oid(const Id& id);
bool ensure_mirror(const fs::path& root);
int prepare_mirror(const fs::path& root, const fs::path& source);
bool write_loose(const fs::path& git_dir, std::uint8_t kind, const Oid& oid, Bytes body);
bool write_ref(const fs::path& git_dir, std::string_view name, const Oid& oid);
std::string remote_key(std::string_view remote, std::string_view field);
std::vector<std::string> list_remotes(const fs::path& root);
std::optional<Id> import_tree(Writer& writer, Repo& repo, IdMap& trees, IdMap& blobs,
const Oid& oid,
std::vector<std::pair<Id, std::vector<std::uint8_t>>>& raw_saves,
std::size_t& new_trees, std::size_t& new_blobs,
std::size_t depth = 0);
bool export_tree(const fs::path& root, Store& store, IdMap& trees, IdMap& blobs, const Id& id,
Oid& out, std::size_t depth = 0);
bool export_commit(const fs::path& root, Store& store, IdMap& commits, IdMap& trees, IdMap& blobs,
const std::vector<Id>& native, const Id& id, Oid& out, std::size_t depth = 0);
} // namespace vn::git