navid/veins / 20d450df87bd0aaa912e365df1bf63afaf9a58ee0d14370879e8dae8f8ba5750 / xveins/src/object/object.h

object.h

← xveins/src/object · 6963 bytes · raw · download

#pragma once

#include "core/common.h"

#include <optional>
#include <string>
#include <vector>

namespace vn {

class Store;

constexpr std::uint8_t TYPE_CHUNK = 1;
constexpr std::uint8_t TYPE_BLOB = 2;
constexpr std::uint8_t TYPE_TREE = 3;
constexpr std::uint8_t TYPE_COMMIT = 4;
constexpr std::uint8_t TYPE_CONFLICT = 5;
constexpr std::uint8_t TYPE_OPERATION = 6;
constexpr std::uint8_t TYPE_VIEW = 7;

std::string_view type_name(std::uint8_t type);
bool type_valid(std::uint8_t type);

struct RawObject {
    std::uint8_t type = 0;
    std::vector<std::uint8_t> body;
    std::optional<Id> id;
};

struct KeyValue {
    std::string key;
    std::string value;
};

// ---------------------------------------------------------------------------
// Names
// ---------------------------------------------------------------------------

bool valid_name(std::string_view name);
bool valid_path(std::string_view path);

// ---------------------------------------------------------------------------
// Blobs
// ---------------------------------------------------------------------------

enum class BlobForm : std::uint8_t { Inline = 0, Leaf = 1, Interior = 2 };

struct BlobEntry {
    Id id{};
    std::uint64_t length = 0;
};

struct Blob {
    BlobForm form = BlobForm::Inline;
    std::vector<std::uint8_t> data;
    std::vector<BlobEntry> entries;
};

Result<Blob> decode_blob(Bytes body);
Result<std::vector<std::uint8_t>> encode_inline_blob(Bytes data);

std::size_t fastcdc_next(Bytes data);
const std::array<std::uint64_t, 256>& fastcdc_gear();

struct BlobBuild {
    Id root{};
    std::vector<RawObject> objects;
};

BlobBuild build_blob(Bytes data);

// ---------------------------------------------------------------------------
// Trees
// ---------------------------------------------------------------------------

constexpr std::uint8_t KIND_FILE = 1;
constexpr std::uint8_t KIND_EXECUTABLE = 2;
constexpr std::uint8_t KIND_SYMLINK = 3;
constexpr std::uint8_t KIND_SUBTREE = 4;
constexpr std::uint8_t KIND_SUBMODULE = 5;
constexpr std::uint8_t KIND_CONFLICT = 6;
constexpr std::uint8_t KIND_MISSING = 0;

struct TreeEntry {
    std::string name;
    std::uint8_t kind = 0;
    Id target{};
};

struct Tree {
    std::vector<TreeEntry> entries;
    std::vector<std::uint8_t> body;
};

Result<Tree> decode_tree(Bytes body);
Result<std::vector<std::uint8_t>> encode_tree(const std::vector<TreeEntry>& entries);

// ---------------------------------------------------------------------------
// Commits
// ---------------------------------------------------------------------------

constexpr std::uint8_t RENAMES_UNKNOWN = 0;
constexpr std::uint8_t RENAMES_RECORDED = 1;

struct Rename {
    std::uint64_t parent_index = 0;
    std::uint8_t kind = 0;
    std::string from_path;
    std::string to_path;
};

struct Commit {
    std::uint8_t version = 1;
    std::vector<Id> parents;
    Id root_tree{};
    ChangeId change_id{};
    Sig author;
    Sig committer;
    std::string message;
    std::uint8_t renames_state = RENAMES_RECORDED;
    std::vector<Rename> renames;
    std::vector<KeyValue> extra;
};

Result<Commit> decode_commit(Bytes body);
std::vector<std::uint8_t> encode_commit(const Commit& commit);

// ---------------------------------------------------------------------------
// Conflicts
// ---------------------------------------------------------------------------

struct ConflictValue {
    std::uint8_t kind = 0;
    std::optional<Id> target;
};

struct Conflict {
    std::vector<ConflictValue> adds;
    std::vector<ConflictValue> removes;
};

Result<Conflict> decode_conflict(Bytes body);
std::vector<std::uint8_t> encode_conflict(const Conflict& conflict);

// ---------------------------------------------------------------------------
// Operations and views
// ---------------------------------------------------------------------------

struct TargetSet {
    std::vector<std::optional<Id>> adds;
    std::vector<std::optional<Id>> removes;
};

struct Bookmark {
    std::string name;
    TargetSet targets;
};

struct TagRef {
    std::string name;
    Id target{};
};

struct RemoteRef {
    std::string remote;
    std::string name;
    Id target{};
};

struct WorkspaceRef {
    std::string workspace;
    Id target{};
};

struct View {
    std::vector<Id> heads;
    std::vector<Bookmark> bookmarks;
    std::vector<TagRef> tags;
    std::vector<RemoteRef> remote_refs;
    std::vector<WorkspaceRef> working_copies;
};

struct Operation {
    std::vector<Id> parents;
    Id view{};
    Reader::TimeMs start;
    Reader::TimeMs end;
    std::string hostname;
    std::string username;
    std::string description;
    std::vector<KeyValue> tags;
};

Result<View> decode_view(Bytes body);
std::vector<std::uint8_t> encode_view(const View& view);
Result<Operation> decode_operation(Bytes body);
std::vector<std::uint8_t> encode_operation(const Operation& op);

// ---------------------------------------------------------------------------
// Graph, tree walking, and content
// ---------------------------------------------------------------------------

struct LoadedCommit {
    Commit commit;
    std::vector<std::uint8_t> body;
};

struct LogOrderNode {
    Id id{};
    std::int64_t seconds = 0;
    std::vector<Id> parents;
};

std::optional<LoadedCommit> load_commit(Store& store, const Id& id);
bool has_changes(Store& store, const LoadedCommit& loaded);
std::vector<Id> collect_commits(Store& store, std::span<const Id> start);
std::vector<Id> all_commits(Store& store, const View& view);
std::vector<Id> view_roots(const View& view);
std::vector<std::size_t> order_log_nodes(const std::vector<LogOrderNode>& nodes);
std::optional<Id> resolve_revision(Store& store, const View& view, std::string_view rev);
std::optional<Id> working_copy_id(const View& view);

bool contains_id(const std::vector<Id>& ids, const Id& id);
bool target_contains(const std::vector<std::optional<Id>>& ids, const Id& id);

struct FlatEntry {
    std::string path;
    std::uint8_t kind = 0;
    Id target{};
};

std::vector<FlatEntry> flatten_tree(Store& store, const Id& tree_id);
Result<std::vector<FlatEntry>> flatten_tree_checked(Store& store, const Id& tree_id);
std::vector<FlatEntry> flatten_or_empty(Store& store, std::optional<Id> tree_id);
const FlatEntry* find_flat(const std::vector<FlatEntry>& entries, std::string_view path);
int find_tree_name(const Tree& tree, std::string_view name);
bool tree_conflicted(Store& store, const Id& tree_id);

struct TreeChange {
    std::string path;
    std::uint8_t old_kind = KIND_MISSING;
    std::uint8_t new_kind = KIND_MISSING;
    std::optional<Id> old_id;
    std::optional<Id> new_id;
};

std::vector<TreeChange> compare_trees(Store& store, std::optional<Id> old_tree,
                                      std::optional<Id> new_tree);

std::optional<std::vector<std::uint8_t>> read_blob(Store& store, const Id& id);
bool is_lazy_blob(const Id& id);

} // namespace vn