navid/veins / commit
7b5672594b66
Navid M <navidmomtahen@gmail.com> · 2026-11-29
parents: 20ddbcdc2ae0
Browse files at this commit · Download source
--- a/docs/SPECIFICATION_V1.adoc+++ b/docs/SPECIFICATION_V1.adoc@@ -1143,7 +1143,7 @@ | `vn undo`, `vn op log`, `vn op restore <op>`, `vn op revert <op>` | Operation log commands. | `vn bookmark set\|list\|delete`, `vn tag set\|ls\|rm\|latest` | Manage named pointers. | `vn remote add\|set-url\|list\|remove ...` | Manage native remotes stored in local config.-| `vn fetch [<remote>]`, `vn push [<remote>] [<bookmark>]` | Native protocol. Both use the active remote when none is given; `push` uses the current bookmark when omitted.+| `vn fetch [<remote>]`, `vn pull [<remote>]`, `vn push [<remote>] [<bookmark>]` | Native protocol. All use the active remote when none is given; `push` uses the current bookmark when omitted. `pull` fast-forwards the current bookmark and checks it out. | `vn git import [-source:<gitdir>] [-remote:<url>] [-remote-name:<name>] [path]`, `vn git export`, `vn git materialize [path]`, `vn git fetch`, `vn git push`, `vn git remote` | Git bridge. | `vn serve -stdio <repo>`, `vn serve -listen <addr:port> -root <dir> ...` | Run the native server over stdio or TLS. | `vn key generate\|list\|show\|import` | Manage client keys.@@ -1158,7 +1158,9 @@ `vn co <bookmark>` and `vn bmk set <name>` set the current bookmark, stored as `bookmark.current` in local config. `vn clone` and `vn git import` initialize it from the default branch (the checked-out or imported `HEAD`). Snapshots, `describe`, and `go` advance the current bookmark, and `vn push <remote>` uses it when no bookmark argument is given. -`vn fetch` and `vn push` use the active remote when no remote is named: `remote.current`, then `remote.default`, then `o`, then the first configured remote. A native clone records its source as remote `o` and sets `remote.default = o`, so `vn psh o` and bare `vn psh` work immediately after cloning.+`vn fetch`, `vn pull`, and `vn push` use the active remote when no remote is named: `remote.current`, then `remote.default`, then `o`, then the first configured remote. A native clone records its source as remote `o` and sets `remote.default = o`, so `vn psh o` and bare `vn psh` work immediately after cloning.++`vn pull` fetches the active remote, then fast-forwards the current bookmark to its remote-tracking ref and checks it out. It refuses to run when the working copy has uncommitted changes or unresolved conflicts, and reports an error when the local and remote histories have diverged; in that case use `vn merge` or `vn rebase`. `vn pl` is an alias. `vn log` prints commits newest first in aligned columns with no borders. Column titles are shown by default and can be omitted with `-no-col-titles`. `-lN` limits the output to the last N commits (newest N), while `-fN` shows the first N commits (oldest N) in oldest-first order. The separated forms `-l N` and `-f N`, and the bare forms `lN` and `fN`, are also accepted. `-p` pages the output through `$PAGER` (or `less -R` when unset) and shows the full history unless `-l` or `-f` is given. Output is colored when standard output is a terminal; setting `NO_COLOR` disables color. --- a/xveins/src/cli/cli.cpp+++ b/xveins/src/cli/cli.cpp@@ -2440,6 +2440,7 @@ std::cout << " remote manage native remotes\n"; std::cout << " clone clone a repository over the native protocol\n"; std::cout << " fetch fetch from a native remote\n";+ std::cout << " pull fetch and fast-forward the current bookmark; alias: pl\n"; std::cout << " gg fetch the active remote, or clone a URL\n"; std::cout << " push push a bookmark to a native remote\n"; std::cout << " serve run the native server over stdio or TLS\n";@@ -3026,6 +3027,8 @@ return clone_run(args); if (cmd == "fetch") return fetch_run(args);+ if (cmd == "pull" || cmd == "pl")+ return pull_run(args); if (cmd == "gg") return gg_run(args); if (cmd == "push" || cmd == "psh")--- a/xveins/src/cli/net_commands.cpp+++ b/xveins/src/cli/net_commands.cpp@@ -803,35 +803,42 @@ return 0; } -int fetch_run(const std::vector<std::string>& args) {+struct FetchOptions { std::string remote_name; std::string path = "."; std::string explicit_key; std::string explicit_pin; bool have_remote = false;+};++bool parse_fetch_options(const std::vector<std::string>& args, FetchOptions& options) { for (std::size_t i = 2; i < args.size(); ++i) { const std::string& arg = args[i]; if (starts_with(arg, "-key:")) {- explicit_key = arg.substr(5);- if (starts_with(explicit_key, "ed25519:")) {+ options.explicit_key = arg.substr(5);+ if (starts_with(options.explicit_key, "ed25519:")) { error_prefix(); std::cout << "-key: expects a local key name, not a public key\n";- return 1;+ return false; } } else if (starts_with(arg, "-pin:")) {- explicit_pin = arg.substr(5);+ options.explicit_pin = arg.substr(5); } else if (is_flag(arg)) { error_prefix(); std::cout << "unknown flag: " << arg << "\n";- return 1;- } else if (!have_remote) {- remote_name = arg;- have_remote = true;+ return false;+ } else if (!options.have_remote) {+ options.remote_name = arg;+ options.have_remote = true; } else {- path = arg;+ options.path = arg; } }- auto repo = discover_repo(path_from_bytes(path));+ return true;+}++int fetch_command(FetchOptions& options) {+ auto repo = discover_repo(path_from_bytes(options.path)); if (!repo) { error_prefix(); std::cout << "not a veins repository\n";@@ -838,9 +845,9 @@ return 1; } const fs::path root = *repo;- if (!have_remote)- remote_name = active_remote(root);- auto url = config_value(root, remote_key(remote_name, "url"));+ if (!options.have_remote)+ options.remote_name = active_remote(root);+ auto url = config_value(root, remote_key(options.remote_name, "url")); if (!url) { error_prefix(); std::cout << "unknown remote\n";@@ -847,10 +854,10 @@ return 1; } std::string key;- if (!explicit_key.empty()) {- key = explicit_key;+ if (!options.explicit_key.empty()) {+ key = options.explicit_key; } else {- auto configured = config_value(root, remote_key(remote_name, "key"));+ auto configured = config_value(root, remote_key(options.remote_name, "key")); if (configured && auth::local_key_exists(*configured)) key = *configured; if (key.empty()) {@@ -859,9 +866,9 @@ } } std::string pin;- if (!explicit_pin.empty()) {- pin = explicit_pin;- } else if (auto configured = config_value(root, remote_key(remote_name, "tls-pin"))) {+ if (!options.explicit_pin.empty()) {+ pin = options.explicit_pin;+ } else if (auto configured = config_value(root, remote_key(options.remote_name, "tls-pin"))) { pin = *configured; } const std::string repo_name = proto::url_repo_name(*url);@@ -900,13 +907,150 @@ return 1; } }- if (!apply_refs(root, refs, remote_name, false)) {+ if (!apply_refs(root, refs, options.remote_name, false)) { error_prefix(); std::cout << "cannot write repository state\n"; return 1; } info_prefix();- std::cout << "fetched from " << remote_name << "\n";+ std::cout << "fetched from " << options.remote_name << "\n";+ return 0;+}++int fetch_run(const std::vector<std::string>& args) {+ FetchOptions options;+ if (!parse_fetch_options(args, options))+ return 1;+ return fetch_command(options);+}++bool commit_is_ancestor(Store& store, const Id& ancestor, const Id& descendant) {+ if (same_id(ancestor, descendant))+ return true;+ const std::vector<Id> commits = collect_commits(store, std::vector<Id>{descendant});+ for (const auto& id : commits) {+ if (same_id(ancestor, id))+ return true;+ }+ return false;+}++int pull_run(const std::vector<std::string>& args) {+ FetchOptions options;+ if (!parse_fetch_options(args, options))+ return 1;+ if (fetch_command(options) != 0)+ return 1;++ auto repo = discover_repo(path_from_bytes(options.path));+ if (!repo) {+ error_prefix();+ std::cout << "not a veins repository\n";+ return 1;+ }+ const fs::path root = *repo;+ Store store = Store::open_all(root);+ const int snap = snapshot_run(root, store);+ if (snap != 0)+ return snap;++ auto bookmark_name = current_bookmark_name(root);+ if (!bookmark_name) {+ error_prefix();+ std::cout << "no current bookmark; check out a bookmark before pulling\n";+ return 1;+ }+ auto view = current_view(root, store);+ if (!view) {+ error_prefix();+ std::cout << "no operation state\n";+ return 1;+ }+ const auto index = find_bookmark(*view, *bookmark_name);+ if (!index) {+ error_prefix();+ std::cout << "current bookmark is missing: " << *bookmark_name << "\n";+ return 1;+ }+ const auto& bookmark = view->bookmarks[*index];+ if (bookmark.targets.adds.size() != 1 || !bookmark.targets.adds.front()) {+ error_prefix();+ std::cout << "current bookmark is conflicted: " << *bookmark_name << "\n";+ return 1;+ }+ const Id local_target = *bookmark.targets.adds.front();++ std::optional<Id> remote_target;+ for (const auto& ref : view->remote_refs) {+ if (ref.remote == options.remote_name && ref.name == *bookmark_name) {+ remote_target = ref.target;+ break;+ }+ }+ const std::string remote_ref = options.remote_name + "/" + *bookmark_name;+ if (!remote_target) {+ error_prefix();+ std::cout << "no " << remote_ref << " on remote " << options.remote_name << "\n";+ return 1;+ }++ if (auto wc = working_copy_id(*view)) {+ auto loaded = load_commit(store, *wc);+ if (!loaded) {+ error_prefix();+ std::cout << "cannot read working-copy commit\n";+ return 1;+ }+ if (tree_conflicted(store, loaded->commit.root_tree)) {+ error_prefix();+ std::cout << "working copy has unresolved conflicts\n";+ return 1;+ }+ if (has_changes(store, *loaded)) {+ error_prefix();+ std::cout << "working copy has uncommitted changes; commit them before pulling\n";+ return 1;+ }+ }++ if (same_id(local_target, *remote_target)) {+ info_prefix();+ std::cout << "already up to date with " << remote_ref << "\n";+ return checkout_run(root, *bookmark_name);+ }+ if (!commit_is_ancestor(store, local_target, *remote_target)) {+ if (commit_is_ancestor(store, *remote_target, local_target)) {+ info_prefix();+ std::cout << "local " << *bookmark_name << " is ahead of " << remote_ref << "\n";+ return 0;+ }+ error_prefix();+ std::cout << "local " << *bookmark_name << " and " << remote_ref+ << " have diverged; use 'vn merge " << remote_ref << "' or 'vn rebase'\n";+ return 1;+ }++ auto tx = Txn::begin(root, store);+ if (!tx) {+ error_prefix();+ std::cout << "cannot load repository state\n";+ return 1;+ }+ set_bookmark_target(tx->view(), *bookmark_name, *remote_target);+ rebuild_heads(tx->view());+ const auto status = tx->finish("pull " + *bookmark_name + " from " + options.remote_name);+ if (!status) {+ error_prefix();+ std::cout << "cannot write repository state\n";+ return 1;+ }+ store.refresh(root);++ const int checkout = checkout_run(root, *bookmark_name);+ if (checkout != 0)+ return checkout;+ info_prefix();+ std::cout << "pulled " << *bookmark_name << " from " << options.remote_name << "\n"; return 0; } @@ -1461,7 +1605,6 @@ std::cout << "fingerprint: " << base32_text(bytes_of(fingerprint)) << "\n"; return 0; }- bool net_help(std::string_view command) { if (command == "remote") {@@ -1496,6 +1639,16 @@ "remote\n\n"; std::cout << "without <remote>, the active remote is used\n"; std::cout << "auth falls back to remote.<name>.key, then 'vn auth use'\n";+ return true;+ }+ if (command == "pull" || command == "pl") {+ std::cout << "usage: vn pull [-key:<name>] [<remote>] [path]\n\n";+ std::cout << "fetch the active remote and fast-forward the current "+ "bookmark\n\n";+ std::cout << "the working copy is checked out after a fast-forward\n";+ std::cout << "when local and remote history have diverged, use 'vn merge' "+ "or 'vn rebase'\n";+ std::cout << "alias: pl\n"; return true; } if (command == "gg") {--- a/xveins/src/cli/net_commands.h+++ b/xveins/src/cli/net_commands.h@@ -9,6 +9,7 @@ int remote_run(const std::vector<std::string>& args); int clone_run(const std::vector<std::string>& args); int fetch_run(const std::vector<std::string>& args);+int pull_run(const std::vector<std::string>& args); int gg_run(const std::vector<std::string>& args); int push_run(const std::vector<std::string>& args); int serve_run(const std::vector<std::string>& args);--- a/xveins/tests/integration/protocol_tests.cpp+++ b/xveins/tests/integration/protocol_tests.cpp@@ -71,6 +71,51 @@ t.expect_contains(f.vn({"log", "-rev", "o/master"}, clone_a).out, "clone b change"); f.vn({"fetch"}, clone_a); t.expect_contains(f.vn({"log", "-rev", "o/master"}, clone_a).out, "clone b change");+}++void protocol_pull_fast_forwards_and_detects_divergence(test::Context& t) {+ VnFixture f;+ make_source_repository(f);+ const fs::path source = f.root / "source";+ const std::string key_info = f.vn({"key", "generate", "integration"}, source).out;+ std::string public_key;+ for (const std::string& line : split_lines(key_info)) {+ if (line.starts_with("public key: ")) {+ public_key = line.substr(std::string("public key: ").size());+ break;+ }+ }+ t.expect(!public_key.empty());+ f.vn({"access", "add", "-role", "write", "-key", public_key, "-name", "integration"}, source);++ const fs::path clone_a = f.root / "clone-a";+ const fs::path clone_b = f.root / "clone-b";+ f.vn({"clone", "file://" + source.string(), clone_a.string()}, f.root);+ f.vn({"clone", "file://" + source.string(), clone_b.string()}, f.root);+ f.vn({"cfg", "remote.o.key", "integration"}, clone_a);+ f.vn({"cfg", "remote.o.key", "integration"}, clone_b);++ f.write("b.txt", "from b\n", clone_b);+ f.commit("b change", clone_b);+ f.vn({"psh", "o", "master"}, clone_b);++ const CommandResult pulled = f.vn({"pl"}, clone_a);+ t.expect_contains(pulled.out, "pulled master from o");+ t.expect(f.exists("b.txt", clone_a));+ t.expect_eq(f.read("b.txt", clone_a), "from b\n");+ t.expect_contains(f.vn({"stat"}, clone_a).out, "working copy clean");+ t.expect_contains(f.vn({"pull"}, clone_a).out, "already up to date");++ f.write("a.txt", "dirty\n", clone_a);+ f.failed({"pull"}, "uncommitted changes", clone_a);+ f.commit("local change", clone_a);++ f.write("b2.txt", "from b again\n", clone_b);+ f.commit("b second change", clone_b);+ f.vn({"psh", "o", "master"}, clone_b);++ f.failed({"pull"}, "diverged", clone_a);+ t.expect(!f.exists("b2.txt", clone_a)); } void protocol_bad_inputs(test::Context& t) {@@ -98,6 +143,8 @@ { {"clone_whoami", protocol_clone_whoami}, {"push_fetch", protocol_push_fetch},+ {"pull_fast_forwards_and_detects_divergence",+ protocol_pull_fast_forwards_and_detects_divergence}, {"bad_inputs", protocol_bad_inputs}, }}; }