navid/veins / commit

728ba26f8c67

Navid M <navidmomtahen@gmail.com> · 2026-11-29

parents: 4c5650fedc43

Browse files at this commit · Download source

all: fix slowness when diffing and format solution
--- a/xveins/src/cli/cli.cpp+++ b/xveins/src/cli/cli.cpp@@ -29,6 +29,30 @@     return !arg.empty() && arg.front() == '-'; } +bool view_ref_named(const View& view, std::string_view name) {+    if (find_bookmark(view, name) || find_tag(view, name))+        return true;+    for (const auto& ref : view.remote_refs) {+        const std::size_t joined = ref.remote.size() + 1 + ref.name.size();+        if (name.size() == joined && name.starts_with(ref.remote) &&+            name[ref.remote.size()] == '/' && name.substr(ref.remote.size() + 1) == ref.name)+            return true;+    }+    return false;+}++bool existing_directory_operand(std::string_view text) {+    if (text.empty() || text.find('\0') != std::string_view::npos)+        return false;+    std::error_code ec;+    fs::path path = path_from_bytes(text);+    if (!path.is_absolute())+        path = fs::current_path(ec) / path;+    if (ec)+        return false;+    return path_is_dir(path);+}+ std::optional<fs::path> open_repo() {     const auto root = discover_repo(".");     if (!root) {@@ -1208,16 +1232,8 @@     const auto root = open_repo();     if (!root)         return 1;-    const int snap = snapshot_run(*root);-    if (snap != 0)-        return snap;     Store store = Store::open_all(*root);     auto view = current_view(*root, store);-    if (!view) {-        error_prefix();-        std::cout << "no operation state\n";-        return 1;-    }      std::vector<std::string> revs = flag_revs;     std::optional<std::string> path_filter;@@ -1237,7 +1253,10 @@             }             revs.push_back(operands[0]);             revs.push_back(operands[1]);-        } else if (resolve_revision(store, *view, operands.front())) {+        } else if (existing_directory_operand(operands.front()) &&+                   (!view || !view_ref_named(*view, operands.front()))) {+            path_filter = operands.front();+        } else if (view && resolve_revision(store, *view, operands.front())) {             revs.push_back(operands.front());         } else {             path_filter = operands.front();@@ -1264,6 +1283,44 @@         path_filter = std::move(*normalized);     } +    bool needs_snapshot = revs.empty();+    std::optional<std::vector<std::optional<Id>>> pre_resolved;+    if (!needs_snapshot) {+        if (!view) {+            needs_snapshot = true;+        } else {+            const auto wc = working_copy_id(*view);+            std::vector<std::optional<Id>> resolved;+            resolved.reserve(revs.size());+            for (const auto& rev : revs) {+                if (rev.empty() || rev == "@") {+                    needs_snapshot = true;+                    break;+                }+                auto id = resolve_revision(store, *view, rev);+                if (!id || (wc && std::memcmp(id->data(), wc->data(), ID_LEN) == 0)) {+                    needs_snapshot = true;+                    break;+                }+                resolved.push_back(*id);+            }+            if (!needs_snapshot)+                pre_resolved = std::move(resolved);+        }+    }+    if (needs_snapshot) {+        const int snap = snapshot_run(*root, store);+        if (snap != 0)+            return snap;+        view = current_view(*root, store);+        pre_resolved.reset();+    }+    if (!view) {+        error_prefix();+        std::cout << "no operation state\n";+        return 1;+    }+     std::optional<Id> old_tree;     std::optional<Id> new_tree;     bool failed = false;@@ -1282,9 +1339,14 @@             return std::nullopt;         return parent->commit.root_tree;     };+    auto resolve_for_diff = [&](std::size_t index) -> std::optional<Id> {+        if (pre_resolved && index < pre_resolved->size())+            return (*pre_resolved)[index];+        return resolve_revision(store, *view, revs[index]);+    };     if (revs.size() == 2) {-        auto a = resolve_revision(store, *view, revs[0]);-        auto b = resolve_revision(store, *view, revs[1]);+        auto a = resolve_for_diff(0);+        auto b = resolve_for_diff(1);         if (!a || !b)             failed = true;         else {@@ -1292,7 +1354,7 @@             new_tree = tree_of(*b);         }     } else if (revs.size() == 1) {-        auto a = resolve_revision(store, *view, revs[0]);+        auto a = resolve_for_diff(0);         if (!a)             failed = true;         else {--- a/xveins/src/git/git_object.cpp+++ b/xveins/src/git/git_object.cpp@@ -690,9 +690,8 @@         if (sec_text[i] < '0' || sec_text[i] > '9')             return std::nullopt;         const std::uint64_t digit = static_cast<std::uint64_t>(sec_text[i] - '0');-        if (magnitude > (static_cast<std::uint64_t>(std::numeric_limits<std::int64_t>::max()) --                         digit) /-                            10)+        if (magnitude >+            (static_cast<std::uint64_t>(std::numeric_limits<std::int64_t>::max()) - digit) / 10)             return std::nullopt;         magnitude = magnitude * 10 + digit;     }--- a/xveins/src/server/server.cpp+++ b/xveins/src/server/server.cpp@@ -209,8 +209,8 @@             collect_object(session, request_id, hasher, entry.target, TYPE_CONFLICT, seen, send,                            failed, depth + 1);         } else if (entry.kind != KIND_SUBMODULE) {-            collect_object(session, request_id, hasher, entry.target, TYPE_BLOB, seen, send,-                           failed, depth + 1);+            collect_object(session, request_id, hasher, entry.target, TYPE_BLOB, seen, send, failed,+                           depth + 1);         }         if (failed)             return;@@ -238,8 +238,8 @@         }         if (blob->form == BlobForm::Interior) {             for (const auto& entry : blob->entries) {-                collect_object(session, request_id, hasher, entry.id, TYPE_BLOB, seen, send,-                               failed, depth + 1);+                collect_object(session, request_id, hasher, entry.id, TYPE_BLOB, seen, send, failed,+                               depth + 1);                 if (failed)                     return;             }--- a/xveins/src/store/store.cpp+++ b/xveins/src/store/store.cpp@@ -2102,6 +2102,20 @@     return true; } +// Change ids use a lowercase base32 alphabet. A path or an arbitrary word can+// be rejected without walking history, which keeps `vn diff <path>` cheap on+// large repositories.+static bool is_change_id_prefix(std::string_view text) {+    if (text.empty() || text.size() > 26)+        return false;+    for (char raw : text) {+        const char c = (raw >= 'A' && raw <= 'Z') ? static_cast<char>(raw + 32) : raw;+        if (!((c >= 'a' && c <= 'z') || (c >= '2' && c <= '7')))+            return false;+    }+    return true;+}+ std::optional<Id> resolve_revision(Store& store, const View& view, std::string_view rev) {     if (rev.empty() || rev == "@")         return working_copy_id(view);@@ -2112,9 +2126,25 @@     if (auto id = remote_target(view, rev))         return id; +    // A full commit hash can be checked directly. Walking every reachable+    // commit just to rediscover an exact hash made `vn diff <hash>` scale with+    // total history even though the answer was already addressable.+    if (rev.size() == ID_LEN * 2) {+        if (auto full = parse_id(rev)) {+            if (load_commit(store, *full))+                return full;+        }+        return std::nullopt;+    }++    const bool hex_prefix = is_hex_prefix(rev);+    const bool change_prefix = is_change_id_prefix(rev);+    if (!hex_prefix && !change_prefix)+        return std::nullopt;+     const auto ids = all_commits(store, view);     std::vector<Id> matches;-    if (is_hex_prefix(rev)) {+    if (hex_prefix) {         for (const auto& id : ids) {             const std::string full = hex_encode(id);             if (base32_prefix_matches(full, rev))@@ -2121,7 +2151,7 @@                 matches.push_back(id);         }     }-    if (matches.empty()) {+    if (matches.empty() && change_prefix) {         for (const auto& id : ids) {             auto loaded = load_commit(store, id);             if (!loaded)@@ -2266,6 +2296,10 @@  std::vector<TreeChange> compare_trees(Store& store, std::optional<Id> old_tree,                                       std::optional<Id> new_tree) {+    if (!old_tree && !new_tree)+        return {};+    if (old_tree && new_tree && id_equal(*old_tree, *new_tree))+        return {};     const auto old_entries = flatten_or_empty(store, old_tree);     const auto new_entries = flatten_or_empty(store, new_tree);     std::vector<TreeChange> out;--- a/xveins/tests/integration/history_tests.cpp+++ b/xveins/tests/integration/history_tests.cpp@@ -139,10 +139,26 @@      f.vn({"go", "-m", "second"});     const std::string second = f.commit_for_message("second");-    const std::string revision_diff = f.vn({"diff", "-rev", second, "one.txt"}).out;+    const std::string second_full = f.hash(second);+    const std::string revision_diff = f.vn({"diff", "-rev", second_full, "one.txt"}).out;     t.expect_contains(revision_diff, "+one changed");     t.expect_not_contains(revision_diff, "+two changed");     t.expect_contains(f.vn({"diff"}).out, "no diff to show");++    // A historical revision diff must not snapshot (and therefore must not+    // rewrite) the working copy just to print its result.+    const fs::path heads = f.repo / ".veins" / "op-heads";+    const std::size_t heads_before = list_directory(heads).size();+    f.write("one.txt", "one dirty\n");+    const std::string dirty_historical = f.vn({"diff", "-rev", second_full, "one.txt"}).out;+    t.expect_contains(dirty_historical, "+one changed");+    t.expect_not_contains(dirty_historical, "+one dirty");+    t.expect_eq(list_directory(heads).size(), heads_before);++    // A bookmark that points at the working copy still includes uncommitted+    // changes, so resolving it must trigger a snapshot.+    f.vn({"bmk", "set", "master"});+    t.expect_contains(f.vn({"diff", "master"}).out, "+one dirty");     t.expect_contains(f.vn({"help", "diff"}).out, "-a, -all"); }