navid/veins / commit

625628f882b9

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

parents: 96f30f16c431

Browse files at this commit · Download source

all: use relative paths in stat output
--- a/xveins/src/cli/cli.cpp+++ b/xveins/src/cli/cli.cpp@@ -554,10 +554,59 @@     return 0; } +std::string status_path(std::string_view path, std::string_view cwd_rel) {+    if (cwd_rel.empty())+        return std::string(path);+    constexpr std::string_view parent = "../";+    std::size_t c = 0;+    std::size_t p = 0;+    std::size_t common_end = 0;+    while (c < cwd_rel.size() && p < path.size()) {+        std::size_t ce = cwd_rel.find('/', c);+        if (ce == std::string_view::npos)+            ce = cwd_rel.size();+        std::size_t pe = path.find('/', p);+        if (pe == std::string_view::npos)+            pe = path.size();+        if (cwd_rel.substr(c, ce - c) != path.substr(p, pe - p))+            break;+        common_end = ce;+        c = ce + 1;+        p = pe + 1;+        if (ce == cwd_rel.size() || pe == path.size())+            break;+    }+    std::string_view suffix = path;+    std::size_t parents = 0;+    if (common_end == cwd_rel.size()) {+        if (path.size() == common_end)+            return std::string(path);+        suffix = path.substr(common_end + 1);+    } else if (common_end == 0) {+        parents = 1;+        for (std::size_t i = 1; i < cwd_rel.size(); ++i)+            if (cwd_rel[i] == '/')+                ++parents;+    } else {+        parents = 1;+        for (std::size_t i = common_end + 1; i < cwd_rel.size(); ++i)+            if (cwd_rel[i] == '/')+                ++parents;+        suffix = common_end < path.size() ? path.substr(common_end + 1) : std::string_view{};+    }+    std::string out;+    out.reserve(parents * parent.size() + suffix.size());+    for (std::size_t i = 0; i < parents; ++i)+        out.append(parent);+    out.append(suffix);+    return out;+}+ int run_status() {     const auto root = open_repo();     if (!root)         return 1;+    const auto cwd_rel = cwd_relative(*root);     Store store = Store::open_all(*root);     const int snap = snapshot_run(*root, store);     if (snap != 0)@@ -601,21 +650,23 @@         old_tree && new_tree && std::memcmp(old_tree->data(), new_tree->data(), ID_LEN) == 0;     const auto changes =         trees_equal ? std::vector<TreeChange>{} : compare_trees(store, old_tree, new_tree);+    const std::string_view cwd = cwd_rel ? std::string_view(*cwd_rel) : std::string_view{};     std::size_t conflicts = 0;     for (const auto& change : changes) {+        const std::string display_path = status_path(change.path, cwd);         if (change.old_kind == KIND_MISSING) {             print_colored("A", Color::BrightGreen, false, false);-            std::cout << " " << change.path << "\n";+            std::cout << " " << display_path << "\n";         } else if (change.new_kind == KIND_MISSING) {             print_colored("D", Color::Red, false, false);-            std::cout << " " << change.path << "\n";+            std::cout << " " << display_path << "\n";         } else if (change.new_kind == KIND_CONFLICT) {             print_colored("C", Color::BrightRed, false, false);-            std::cout << " " << change.path << "\n";+            std::cout << " " << display_path << "\n";             ++conflicts;         } else {             print_colored("M", Color::Cyan, false, false);-            std::cout << " " << change.path << "\n";+            std::cout << " " << display_path << "\n";         }     }     if (changes.empty()) {--- a/xveins/tests/integration_tests.cpp+++ b/xveins/tests/integration_tests.cpp@@ -429,6 +429,29 @@     f.vn({"desc", "-m", "draft message"});     t.expect_contains(f.vn({"show", "@"}).out, "draft message");     t.expect_contains(f.vn({"stat"}).out, "A a.txt");+}++void core_status_paths_relative_to_cwd(test::Context& t) {+    VnFixture f;+    f.init();+    f.write("a.txt", "alpha\n");+    f.write("dir/b.txt", "beta\n");+    f.write("dir/sub/c.txt", "gamma\n");++    const std::string root_status = f.vn({"stat"}).out;+    t.expect_contains(root_status, "A a.txt");+    t.expect_contains(root_status, "A dir/b.txt");++    const std::string dir_status = f.vn({"stat"}, f.repo / "dir").out;+    t.expect_contains(dir_status, "A ../a.txt");+    t.expect_contains(dir_status, "A b.txt");+    t.expect_contains(dir_status, "A sub/c.txt");+    t.expect_not_contains(dir_status, "A dir/b.txt");++    const std::string sub_status = f.vn({"stat"}, f.repo / "dir" / "sub").out;+    t.expect_contains(sub_status, "A ../../a.txt");+    t.expect_contains(sub_status, "A ../b.txt");+    t.expect_contains(sub_status, "A c.txt"); }  void core_commit_log_show_hash(test::Context& t) {@@ -1298,6 +1321,7 @@              {"init_version_help", core_init_version_help},              {"outside_repository", core_outside_repository},              {"status_list_diff_describe", core_status_list_diff_describe},+             {"status_paths_relative_to_cwd", core_status_paths_relative_to_cwd},              {"commit_log_show_hash", core_commit_log_show_hash},              {"log_limits_revisions", core_log_limits_revisions},              {"checkout_materializes_and_new", core_checkout_materializes_and_new},