#pragma once #include "common.h" #include "object.h" #include "store.h" #include #include #include #include #include #include namespace vn::git { using Oid = std::array; 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 body; }; struct Pack { fs::path path; std::vector data; std::vector oids; std::vector offsets; }; struct Repo { fs::path git_dir; std::vector packs; }; std::string oid_hex(const Oid &oid); std::optional oid_from_hex(std::string_view hex); std::optional object_hash(std::uint8_t kind, Bytes body); std::optional open_repo(const fs::path &git_dir); std::optional read_object(Repo &repo, const Oid &oid, bool verify = false); std::shared_ptr cached_repo(const fs::path &git_dir); struct RawTreeEntry { std::string mode; std::string name; Oid oid{}; }; struct ParsedTree { std::vector entries; bool canonical = true; }; struct ParsedTag { Oid target{}; std::uint8_t target_kind = 0; }; struct ParsedCommit { Oid tree{}; std::vector parents; Sig author; Sig committer; std::string message; std::vector extras; std::optional change_id; bool canonical = true; }; std::optional parse_sig(std::string_view value); std::optional parse_commit(Bytes body); std::vector serialize_commit(const ParsedCommit &commit); std::optional parse_tree(Bytes body); std::string mode_for_kind(std::uint8_t kind); std::uint8_t mode_kind(std::string_view mode); std::optional parse_tag(Bytes body); struct IdMap { std::map items; std::optional get(const Oid &oid) const; void put(const Oid &oid, const Id &id); std::optional 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> load_raw(const fs::path &root, const Id &id); struct Ref { std::string name; Oid oid{}; }; struct RefSet { std::vector branches; std::vector tags; std::vector remotes; std::optional head; }; RefSet read_refs(const fs::path &git_dir); Id lazy_blob_id(const Oid &oid); std::optional 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 list_remotes(const fs::path &root); std::optional import_tree(Writer &writer, Repo &repo, IdMap &trees, IdMap &blobs, const Oid &oid, std::vector>> &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 &native, const Id &id, Oid &out, std::size_t depth = 0); } // namespace vn::git