navid/veins / b8f7327bf194b843bd36e1a9bd5d6d3d5d123eea3f36bf4e780cc97678358890 / xveins/src/oplog.cpp
oplog.cpp
← xveins/src · 7162 bytes · raw · download
#include "local_commands.h"
#include "store.h"
#include "worktree.h"
#include <algorithm>
#include <iostream>
namespace vn {
namespace {
struct OpRec {
Id id{};
Operation op;
};
std::vector<OpRec> collect_operations(Store& store, const std::vector<Id>& heads) {
std::vector<OpRec> out;
std::vector<Id> seen;
std::vector<Id> stack(heads.rbegin(), heads.rend());
while (!stack.empty()) {
const Id id = stack.back();
stack.pop_back();
if (contains_id(seen, id))
continue;
seen.push_back(id);
auto op = store.read_operation(id);
if (!op)
continue;
for (const auto& parent : op->parents) {
if (!contains_id(seen, parent))
stack.push_back(parent);
}
out.push_back(OpRec{id, std::move(*op)});
}
return out;
}
void sort_operations(std::vector<OpRec>& recs) {
std::stable_sort(recs.begin(), recs.end(),
[](const OpRec& a, const OpRec& b) { return a.op.end.ms > b.op.end.ms; });
}
std::optional<std::size_t> resolve_operation(const std::vector<OpRec>& recs,
std::string_view text) {
std::optional<std::size_t> found;
std::size_t count = 0;
for (std::size_t i = 0; i < recs.size(); ++i) {
const std::string full = hex_encode(recs[i].id);
if (full.size() >= text.size() && full.compare(0, text.size(), text) == 0) {
found = i;
++count;
}
}
if (count == 1)
return found;
return std::nullopt;
}
std::optional<Id> wc_tree(Store& store, const View& view) {
const auto wc = working_copy_id(view);
if (!wc)
return std::nullopt;
auto loaded = load_commit(store, *wc);
if (!loaded)
return std::nullopt;
return loaded->commit.root_tree;
}
int write_view(const fs::path& root, Store& store, const View& next, std::string_view description) {
auto tx = Txn::begin(root, store);
if (!tx) {
error_prefix();
std::cout << "cannot load repository state\n";
return 1;
}
const auto old_tree = wc_tree(store, tx->view());
const auto new_tree = wc_tree(store, next);
tx->view() = next;
if (!tx->finish(description))
return 1;
return checkout_materialize_optional(root, store, old_tree, new_tree);
}
int op_log(const std::vector<std::string>&) {
const auto root = discover_repo(".");
if (!root) {
error_prefix();
std::cout << "not a veins repository\n";
return 1;
}
Store store = Store::open_all(*root);
const auto heads = read_heads(*root);
auto recs = collect_operations(store, heads);
sort_operations(recs);
for (const auto& rec : recs) {
const std::string marker = contains_id(heads, rec.id) ? "@" : " ";
const std::string stamp = format_utc(rec.op.end.ms / 1000);
std::cout << marker << " " << hex_encode(rec.id).substr(0, 12) << " " << stamp << " "
<< rec.op.description << "\n";
}
return 0;
}
int op_restore(const std::vector<std::string>& args, std::size_t start) {
if (start >= args.size()) {
error_prefix();
std::cout << "op restore requires an operation id\n";
return 1;
}
const std::string& text = args[start];
const auto root = discover_repo(".");
if (!root) {
error_prefix();
std::cout << "not a veins repository\n";
return 1;
}
const int snap = snapshot_run(*root);
if (snap != 0)
return snap;
Store store = Store::open_all(*root);
const auto heads = read_heads(*root);
auto recs = collect_operations(store, heads);
const auto target = resolve_operation(recs, text);
if (!target) {
error_prefix();
std::cout << "unknown operation\n";
return 1;
}
auto target_view = store.read_view(recs[*target].op.view);
if (!target_view) {
error_prefix();
std::cout << "cannot read operation view\n";
return 1;
}
return write_view(*root, store, *target_view, "op restore");
}
int op_revert(const std::vector<std::string>& args, std::size_t start) {
if (start >= args.size()) {
error_prefix();
std::cout << "op revert requires an operation id\n";
return 1;
}
const std::string& text = args[start];
const auto root = discover_repo(".");
if (!root) {
error_prefix();
std::cout << "not a veins repository\n";
return 1;
}
const int snap = snapshot_run(*root);
if (snap != 0)
return snap;
Store store = Store::open_all(*root);
const auto heads = read_heads(*root);
auto recs = collect_operations(store, heads);
const auto target = resolve_operation(recs, text);
if (!target) {
error_prefix();
std::cout << "unknown operation\n";
return 1;
}
const auto& op = recs[*target].op;
View view;
if (!op.parents.empty()) {
auto parent = store.read_operation(op.parents.front());
if (parent) {
auto parent_view = store.read_view(parent->view);
if (parent_view)
view = std::move(*parent_view);
}
}
return write_view(*root, store, view, "op revert");
}
} // namespace
int op_run(const std::vector<std::string>& args) {
if (args.size() <= 2) {
error_prefix();
std::cout << "expected an op subcommand (log, restore, revert)\n";
return 1;
}
const std::string& sub = args[2];
if (sub == "log")
return op_log(args);
if (sub == "restore")
return op_restore(args, 3);
if (sub == "revert")
return op_revert(args, 3);
error_prefix();
std::cout << "unknown op subcommand: " << sub << "\n";
return 1;
}
int undo_run(const std::vector<std::string>&) {
const auto root = discover_repo(".");
if (!root) {
error_prefix();
std::cout << "not a veins repository\n";
return 1;
}
const int snap = snapshot_run(*root);
if (snap != 0)
return snap;
Store store = Store::open_all(*root);
const auto heads = read_heads(*root);
if (heads.empty()) {
error_prefix();
std::cout << "no operations to undo\n";
return 1;
}
auto recs = collect_operations(store, heads);
sort_operations(recs);
if (recs.empty()) {
error_prefix();
std::cout << "cannot resolve undo target\n";
return 1;
}
const auto& latest = recs.front().op;
View view;
if (!latest.parents.empty()) {
auto parent = store.read_operation(latest.parents.front());
if (!parent) {
error_prefix();
std::cout << "cannot resolve undo target\n";
return 1;
}
auto parent_view = store.read_view(parent->view);
if (!parent_view) {
error_prefix();
std::cout << "cannot resolve undo target\n";
return 1;
}
view = std::move(*parent_view);
}
return write_view(*root, store, view, "undo");
}
} // namespace vn