navid/veins / commit
6169bc0f55b5
Navid M <navidmomtahen@gmail.com> · 2026-11-29
parents: b314db4a5767
Browse files at this commit · Download source
--- a/docs/SPECIFICATION_V1.adoc+++ b/docs/SPECIFICATION_V1.adoc@@ -794,6 +794,8 @@ === Refs Bookmarks map to git branches (`refs/heads/<name>`), and tags to git tags (`refs/tags/<name>`). Tags are created natively with `vn tag set` or imported from git. A bookmark's remote tracking state is stored in `remote_refs` of the view and mirrored under `refs/remotes/<remote>/` in the mirror. Fetching moves remote refs, not local bookmarks. Local bookmarks move on explicit command or merge.++`vn git mirror push [<remote>]` exports every bookmark and tag and force-pushes them to the selected git remote. `vn git mirror psh` is an alias. `-prune` deletes remote branches and tags absent from the current view; `-dry-run` previews the push, and `-strict` fails when a ref cannot be exported. `vn git mirror pt <tag> [<remote>]` exports and force-pushes one tag only. Without an explicit remote, the commands use `git.mirror`, then `origin`, then the only configured git remote. === Pull request workflow @@ -1144,7 +1146,7 @@ | `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 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 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 mirror`, `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. | `vn access add\|list\|remove\|anonymous` | Manage a served repository's access file (server side, local only).--- a/xveins/src/cli/cli.cpp+++ b/xveins/src/cli/cli.cpp@@ -2799,6 +2799,7 @@ std::cout << " fetch [<remote>] [path] git fetch into the mirror, then import\n"; std::cout << " push [<remote>] <bookmark> [path]\n"; std::cout << " export and git push a bookmark\n";+ std::cout << " mirror <push|pt> ... mirror all refs or one tag to a git remote\n"; std::cout << " remote <add|list|remove> manage git remotes\n\n"; std::cout << "run 'vn help git <subcommand>' for details\n"; }@@ -2831,6 +2832,23 @@ "operation\n"; } +void show_git_mirror() {+ std::cout << "usage: vn git mirror <push|pt> ...\n\n";+ std::cout << "mirror Veins refs to a git remote\n\n";+ std::cout << "subcommands:\n";+ std::cout << " push, psh [<remote>] [path]\n";+ std::cout << " export and push all bookmarks and tags\n";+ std::cout << " pt <tag> [<remote>] [path]\n";+ std::cout << " export and push one tag\n\n";+ std::cout << "options:\n";+ std::cout << " -dry-run show what would be pushed\n";+ std::cout << " -prune delete remote refs absent from Veins\n";+ std::cout << " -strict fail if any ref cannot be exported\n";+ std::cout << " -no-force do not force-push\n\n";+ std::cout << "the default remote is git.mirror, then origin, then the only\n";+ std::cout << "configured git remote\n";+}+ void show_unimplemented(std::string_view command) { std::cout << "usage: vn " << command << "\n\n"; std::cout << "this command is planned for a later stage of the C++ rewrite\n";@@ -2921,6 +2939,8 @@ std::cout << "usage: vn git fetch [<remote>] [path]\n"; else if (sub == "push") std::cout << "usage: vn git push [<remote>] <bookmark> [path]\n";+ else if (sub == "mirror")+ show_git_mirror(); else if (sub == "remote") std::cout << "usage: vn git remote <add|list|remove> ...\n"; else {--- a/xveins/src/git/git.cpp+++ b/xveins/src/git/git.cpp@@ -414,6 +414,27 @@ } int run_git(const fs::path& dir, const std::vector<std::string>& args) {+ std::string command = "git -C ";+ command += shell_quote(path_to_bytes(dir));+ for (const auto& arg : args) {+ command.push_back(' ');+ command += shell_quote(arg);+ }+ const int rc = std::system(command.c_str());+ if (rc == -1)+ return -1;+#if !defined(_WIN32)+ if (WIFEXITED(rc))+ return WEXITSTATUS(rc);+ return 1;+#else+ return rc;+#endif+}++int run_git_capture(const fs::path& dir, const std::vector<std::string>& args, std::string& out) {+ const fs::path capture =+ fs::temp_directory_path() / ("vn-git-" + std::to_string(random_u64()) + ".out"); std::string command = "git -C "; command += shell_quote(path_to_bytes(dir)); for (const auto& arg : args) {@@ -420,7 +441,14 @@ command.push_back(' '); command += shell_quote(arg); }+ command += " > ";+ command += shell_quote(path_to_bytes(capture)); const int rc = std::system(command.c_str());+ out.clear();+ if (auto data = read_file_opt(capture)) {+ out.assign(reinterpret_cast<const char*>(data->data()), data->size());+ }+ (void)remove_file(capture); if (rc == -1) return -1; #if !defined(_WIN32)@@ -436,7 +464,28 @@ const fs::path dir = git_dir(root); if (!path_is_dir(dir)) return false;- (void)run_git(dir, {"remote", "remove", std::string(remote)});+ std::string remotes;+ if (run_git_capture(dir, {"remote"}, remotes) != 0)+ return false;+ bool exists = false;+ std::size_t start = 0;+ while (start <= remotes.size()) {+ std::size_t end = remotes.find('\n', start);+ if (end == std::string::npos)+ end = remotes.size();+ std::string_view line(remotes.data() + start, end - start);+ if (!line.empty() && line.back() == '\r')+ line.remove_suffix(1);+ if (line == remote) {+ exists = true;+ break;+ }+ if (end == remotes.size())+ break;+ start = end + 1;+ }+ if (exists)+ return run_git(dir, {"remote", "set-url", std::string(remote), std::string(url)}) == 0; return run_git(dir, {"remote", "add", std::string(remote), std::string(url)}) == 0; } @@ -1292,6 +1341,311 @@ return 1; } std::cout << "pushed " << bookmark << " to " << remote << "\n";+ return 0;+}++namespace {++void collect_text_lines(std::string_view text, std::set<std::string>& out) {+ std::size_t start = 0;+ while (start <= text.size()) {+ std::size_t end = text.find('\n', start);+ if (end == std::string_view::npos)+ end = text.size();+ std::string_view line = text.substr(start, end - start);+ if (!line.empty() && line.back() == '\r')+ line.remove_suffix(1);+ if (!line.empty())+ out.emplace(line);+ if (end == text.size())+ break;+ start = end + 1;+ }+}++void collect_remote_refs(std::string_view text, std::set<std::string>& out) {+ std::size_t start = 0;+ while (start <= text.size()) {+ std::size_t end = text.find('\n', start);+ if (end == std::string_view::npos)+ end = text.size();+ std::string_view line = text.substr(start, end - start);+ if (!line.empty() && line.back() == '\r')+ line.remove_suffix(1);+ const std::size_t tab = line.find('\t');+ if (tab != std::string_view::npos) {+ std::string_view ref = line.substr(tab + 1);+ if (!ref.ends_with("^{}"))+ out.emplace(ref);+ }+ if (end == text.size())+ break;+ start = end + 1;+ }+}++struct MirrorRefs {+ std::vector<std::string> heads;+ std::vector<std::string> tags;+ std::set<std::string> present;+ std::vector<std::string> skipped;+};++MirrorRefs collect_mirror_refs(Store& store, const View& view) {+ MirrorRefs refs;+ for (const auto& bookmark : view.bookmarks) {+ if (bookmark.targets.adds.empty() || !bookmark.targets.adds.front())+ continue;+ const std::string ref = "refs/heads/" + bookmark.name;+ refs.present.insert(ref);+ if (bookmark.targets.adds.size() != 1 ||+ !exportable_target(store, *bookmark.targets.adds.front())) {+ refs.skipped.push_back(ref);+ continue;+ }+ refs.heads.push_back(bookmark.name);+ }+ for (const auto& tag : view.tags) {+ const std::string ref = "refs/tags/" + tag.name;+ refs.present.insert(ref);+ if (!exportable_target(store, tag.target)) {+ refs.skipped.push_back(ref);+ continue;+ }+ refs.tags.push_back(tag.name);+ }+ return refs;+}++bool capture_local_refs(const fs::path& root, std::set<std::string>& out) {+ std::string text;+ if (run_git_capture(git_dir(root),+ {"for-each-ref", "--format=%(refname)", "refs/heads", "refs/tags"},+ text) != 0)+ return false;+ collect_text_lines(text, out);+ return true;+}++void report_skipped_refs(const std::vector<std::string>& skipped) {+ for (const auto& ref : skipped) {+ warning_prefix();+ std::cout << "skipped " << ref << " (not exportable)\n";+ }+}++} // namespace++int git_mirror_push(const fs::path& root, std::string_view remote,+ const GitMirrorOptions& options) {+ const auto url = repo_config_value(root, git::remote_key(remote, "url"));+ if (!url) {+ error_prefix();+ std::cout << "unknown git remote: " << remote << "\n";+ return 1;+ }+ if (!git::ensure_mirror(root)) {+ error_prefix();+ std::cout << "cannot create git mirror\n";+ return 1;+ }+ if (!configure_mirror(root, remote, *url)) {+ error_prefix();+ std::cout << "cannot configure git mirror\n";+ return 1;+ }+ const int exported = git_export(root);+ if (exported != 0)+ return exported;++ Store store = Store::open_all(root);+ auto view = current_view(root, store);+ if (!view) {+ error_prefix();+ std::cout << "no operation state\n";+ return 1;+ }+ MirrorRefs refs = collect_mirror_refs(store, *view);+ if (options.strict && !refs.skipped.empty()) {+ report_skipped_refs(refs.skipped);+ error_prefix();+ std::cout << "cannot mirror every ref\n";+ return 1;+ }+ report_skipped_refs(refs.skipped);++ std::set<std::string> local_refs;+ if (!capture_local_refs(root, local_refs)) {+ error_prefix();+ std::cout << "cannot inspect git mirror refs\n";+ return 1;+ }++ std::vector<std::string> missing;+ auto keep_present = [&](std::vector<std::string>& names, std::string_view prefix) {+ names.erase(std::remove_if(names.begin(), names.end(),+ [&](const std::string& name) {+ if (local_refs.count(std::string(prefix) + name))+ return false;+ missing.emplace_back(std::string(prefix) + name);+ return true;+ }),+ names.end());+ };+ keep_present(refs.heads, "refs/heads/");+ keep_present(refs.tags, "refs/tags/");+ if (options.strict && !missing.empty()) {+ for (const auto& ref : missing) {+ warning_prefix();+ std::cout << "missing " << ref << " in git mirror\n";+ }+ error_prefix();+ std::cout << "cannot mirror every ref\n";+ return 1;+ }+ for (const auto& ref : missing) {+ warning_prefix();+ std::cout << "missing " << ref << " in git mirror\n";+ }++ std::vector<std::string> args{"push"};+ if (options.dry_run)+ args.push_back("--dry-run");+ args.push_back(std::string(remote));+ std::size_t refspecs = 0;+ auto add_refspec = [&](std::string_view ref) {+ std::string spec;+ if (options.force)+ spec.push_back('+');+ spec += ref;+ spec.push_back(':');+ spec += ref;+ args.push_back(std::move(spec));+ ++refspecs;+ };+ for (const auto& name : refs.heads)+ add_refspec("refs/heads/" + name);+ for (const auto& name : refs.tags)+ add_refspec("refs/tags/" + name);++ std::size_t pruned = 0;+ if (options.prune) {+ std::string remote_text;+ if (run_git_capture(git_dir(root), {"ls-remote", "--heads", "--tags", std::string(remote)},+ remote_text) != 0) {+ error_prefix();+ std::cout << "cannot list refs on git remote: " << remote << "\n";+ return 1;+ }+ std::set<std::string> remote_refs;+ collect_remote_refs(remote_text, remote_refs);+ for (const auto& ref : remote_refs) {+ if (ref.starts_with("refs/heads/") || ref.starts_with("refs/tags/")) {+ if (refs.present.count(ref) != 0)+ continue;+ args.push_back(":" + ref);+ ++pruned;+ }+ }+ }++ if (refspecs == 0 && pruned == 0) {+ info_prefix();+ std::cout << "nothing to mirror\n";+ return 0;+ }+ std::cout.flush();+ if (run_git(git_dir(root), args) != 0) {+ error_prefix();+ std::cout << "git push failed\n";+ return 1;+ }+ info_prefix();+ if (options.dry_run)+ std::cout << "would mirror ";+ else+ std::cout << "mirrored ";+ std::cout << refs.heads.size() << " bookmark(s) and " << refs.tags.size() << " tag(s) to "+ << remote;+ if (pruned > 0)+ std::cout << ", pruning " << pruned << " ref(s)";+ std::cout << "\n";+ return 0;+}++int git_mirror_tag(const fs::path& root, std::string_view remote, std::string_view tag,+ const GitMirrorOptions& options) {+ const auto url = repo_config_value(root, git::remote_key(remote, "url"));+ if (!url) {+ error_prefix();+ std::cout << "unknown git remote: " << remote << "\n";+ return 1;+ }+ Store store = Store::open_all(root);+ auto view = current_view(root, store);+ if (!view) {+ error_prefix();+ std::cout << "no operation state\n";+ return 1;+ }+ const auto index = find_tag(*view, tag);+ if (!index) {+ error_prefix();+ std::cout << "unknown tag: " << tag << "\n";+ return 1;+ }+ if (!exportable_target(store, view->tags[*index].target)) {+ error_prefix();+ std::cout << "cannot export tag: " << tag << "\n";+ return 1;+ }+ if (!git::ensure_mirror(root)) {+ error_prefix();+ std::cout << "cannot create git mirror\n";+ return 1;+ }+ if (!configure_mirror(root, remote, *url)) {+ error_prefix();+ std::cout << "cannot configure git mirror\n";+ return 1;+ }+ const int exported = git_export(root);+ if (exported != 0)+ return exported;++ std::set<std::string> local_refs;+ if (!capture_local_refs(root, local_refs)) {+ error_prefix();+ std::cout << "cannot inspect git mirror refs\n";+ return 1;+ }+ const std::string ref = "refs/tags/" + std::string(tag);+ if (local_refs.count(ref) == 0) {+ error_prefix();+ std::cout << "tag was not exported: " << tag << "\n";+ return 1;+ }++ std::vector<std::string> args{"push"};+ if (options.dry_run)+ args.push_back("--dry-run");+ args.push_back(std::string(remote));+ std::string spec;+ if (options.force)+ spec.push_back('+');+ spec += ref;+ spec.push_back(':');+ spec += ref;+ args.push_back(std::move(spec));+ std::cout.flush();+ if (run_git(git_dir(root), args) != 0) {+ error_prefix();+ std::cout << "git push failed\n";+ return 1;+ }+ info_prefix();+ std::cout << (options.dry_run ? "would mirror tag " : "mirrored tag ") << tag << " to "+ << remote << "\n"; return 0; } @@ -1503,6 +1857,128 @@ return 1; } return git_push(*root, remote, *bookmark);+}++namespace {++std::optional<std::string> resolve_mirror_remote(const fs::path& root,+ const std::string& requested) {+ auto configured = [&](std::string_view name) {+ return repo_config_value(root, git::remote_key(name, "url")).has_value();+ };+ if (!requested.empty()) {+ if (configured(requested))+ return requested;+ error_prefix();+ std::cout << "unknown git remote: " << requested << "\n";+ return std::nullopt;+ }+ if (auto mirror = repo_config_value(root, "git.mirror"); mirror && !mirror->empty()) {+ if (configured(*mirror))+ return *mirror;+ error_prefix();+ std::cout << "git.mirror refers to an unknown remote: " << *mirror << "\n";+ return std::nullopt;+ }+ const auto names = git::list_remotes(root);+ if (std::find(names.begin(), names.end(), "origin") != names.end())+ return std::string("origin");+ if (names.size() == 1)+ return names.front();+ error_prefix();+ if (names.empty()) {+ std::cout << "no git remotes configured; use 'vn git remote add <name> <url>'\n";+ } else {+ std::cout << "multiple git remotes; specify one or set git.mirror\n";+ for (const auto& name : names)+ std::cout << " " << name << "\n";+ }+ return std::nullopt;+}++void git_mirror_usage() {+ std::cout << "usage: vn git mirror <push|pt> ...\n\n";+ std::cout << "mirror Veins refs to a git remote\n\n";+ std::cout << "subcommands:\n";+ std::cout << " push, psh [<remote>] [path]\n";+ std::cout << " export and push all bookmarks and "+ "tags\n";+ std::cout << " pt <tag> [<remote>] [path]\n";+ std::cout << " export and push one tag\n\n";+ std::cout << "options:\n";+ std::cout << " -dry-run show what would be pushed\n";+ std::cout << " -prune delete remote refs absent from Veins\n";+ std::cout << " -strict fail if any ref cannot be exported\n";+ std::cout << " -no-force do not force-push\n";+}++} // namespace++int git_mirror_cli(const std::vector<std::string>& args) {+ if (args.size() < 4) {+ git_mirror_usage();+ return 1;+ }+ const std::string& sub = args[3];+ const bool mirror_push = sub == "push" || sub == "psh";+ const bool mirror_tag = sub == "pt";+ if (!mirror_push && !mirror_tag) {+ error_prefix();+ std::cout << "unknown git mirror subcommand: " << sub << "\n";+ return 1;+ }++ GitMirrorOptions options;+ std::string tag;+ std::string remote;+ std::string repo_path = ".";+ bool have_tag = false;+ bool have_remote = false;+ bool end_flags = false;+ for (std::size_t i = 4; i < args.size(); ++i) {+ const std::string& arg = args[i];+ if (!end_flags && arg == "--") {+ end_flags = true;+ } else if (!end_flags && arg == "-dry-run") {+ options.dry_run = true;+ } else if (!end_flags && arg == "-no-force") {+ options.force = false;+ } else if (!end_flags && mirror_push && arg == "-prune") {+ options.prune = true;+ } else if (!end_flags && mirror_push && arg == "-strict") {+ options.strict = true;+ } else if (!end_flags && !arg.empty() && arg.front() == '-') {+ error_prefix();+ std::cout << "unknown flag: " << arg << "\n";+ return 1;+ } else if (mirror_tag && !have_tag) {+ tag = arg;+ have_tag = true;+ } else if (!have_remote) {+ remote = arg;+ have_remote = true;+ } else {+ repo_path = arg;+ }+ }+ if (mirror_tag && tag.empty()) {+ error_prefix();+ std::cout << "git mirror pt requires a tag\n";+ return 1;+ }++ const auto root = discover_repo(path_from_bytes(repo_path));+ if (!root) {+ error_prefix();+ std::cout << "not a veins repository\n";+ return 1;+ }+ const auto resolved = resolve_mirror_remote(*root, remote);+ if (!resolved)+ return 1;+ if (mirror_tag)+ return git_mirror_tag(*root, *resolved, tag, options);+ return git_mirror_push(*root, *resolved, options); } int git_remote_cli(const std::vector<std::string>& args) {@@ -1575,6 +2051,8 @@ return git_fetch_cli(args); if (sub == "push") return git_push_cli(args);+ if (sub == "mirror")+ return git_mirror_cli(args); if (sub == "remote") return git_remote_cli(args); error_prefix();--- a/xveins/src/git/git.h+++ b/xveins/src/git/git.h@@ -15,6 +15,18 @@ int git_materialize(const fs::path& root); int git_fetch(const fs::path& root, std::string_view remote); int git_push(const fs::path& root, std::string_view remote, std::string_view bookmark);++struct GitMirrorOptions {+ bool force = true;+ bool prune = false;+ bool dry_run = false;+ bool strict = false;+};++int git_mirror_push(const fs::path& root, std::string_view remote, const GitMirrorOptions& options);+int git_mirror_tag(const fs::path& root, std::string_view remote, std::string_view tag,+ const GitMirrorOptions& options);+ int git_prepare_mirror(const fs::path& root, std::string_view source); std::optional<std::vector<std::uint8_t>> git_read_lazy_blob(const fs::path& root, const Id& id); int git_cli_run(const std::vector<std::string>& args);--- a/xveins/tests/integration/git_tests.cpp+++ b/xveins/tests/integration/git_tests.cpp@@ -88,6 +88,69 @@ via_import); t.expect_contains(f.vn({"remote", "list"}, via_import).out, "o\tfile:///tmp/veins-import"); t.expect_contains(f.vn({"cfg", "remote.default"}, via_import).out, "o");+#endif+}++void git_mirror_push_and_tag(test::Context& t) {+#if defined(_WIN32)+ t.expect(true);+#else+ if (!git_available()) {+ t.expect(true);+ return;+ }+ VnFixture f;+ f.init();+ f.write("a.txt", "one\n");+ f.vn({"bmk", "set", "master"});+ f.commit("first");++ const fs::path mirror = f.root / "mirror.git";+ fs::create_directories(mirror);+ require_shell(t, run_shell({"git", "init", "--bare", mirror.string()}, f.root),+ "git init bare");+ f.vn({"git", "remote", "add", "mirror", mirror.string()});++ const CommandResult pushed = f.vn({"git", "mirror", "psh"}, f.repo);+ t.expect_contains(pushed.out, "mirrored 1 bookmark(s)");+ CommandResult heads =+ run_shell({"git", "--git-dir", mirror.string(), "show-ref", "--heads"}, f.root);+ require_shell(t, heads, "git show-ref heads");+ t.expect_contains(heads.out, "refs/heads/master");++ f.vn({"tag", "set", "v0.0.1"});+ f.vn({"tag", "set", "v0.0.2"});+ f.vn({"git", "mirror", "pt", "v0.0.1", "mirror"});+ CommandResult tags =+ run_shell({"git", "--git-dir", mirror.string(), "show-ref", "--tags"}, f.root);+ require_shell(t, tags, "git show-ref tags");+ t.expect_contains(tags.out, "refs/tags/v0.0.1");+ t.expect_not_contains(tags.out, "refs/tags/v0.0.2");++ f.vn({"tag", "set", "v0.0.3"});+ f.vn({"git", "mirror", "pt", "v0.0.3", "mirror", "-dry-run"});+ tags = run_shell({"git", "--git-dir", mirror.string(), "show-ref", "--tags"}, f.root);+ require_shell(t, tags, "git show-ref tags after dry run");+ t.expect_not_contains(tags.out, "refs/tags/v0.0.3");++ f.vn({"git", "mirror", "push", "mirror"});+ tags = run_shell({"git", "--git-dir", mirror.string(), "show-ref", "--tags"}, f.root);+ require_shell(t, tags, "git show-ref tags after full push");+ t.expect_contains(tags.out, "refs/tags/v0.0.2");+ t.expect_contains(tags.out, "refs/tags/v0.0.3");++ f.vn({"tag", "rm", "v0.0.2"});+ f.vn({"git", "mirror", "push", "mirror"});+ tags = run_shell({"git", "--git-dir", mirror.string(), "show-ref", "--tags"}, f.root);+ require_shell(t, tags, "git show-ref tags without prune");+ t.expect_contains(tags.out, "refs/tags/v0.0.2");++ f.vn({"git", "mirror", "push", "mirror", "-prune"});+ tags = run_shell({"git", "--git-dir", mirror.string(), "show-ref", "--tags"}, f.root);+ require_shell(t, tags, "git show-ref tags after prune");+ t.expect_not_contains(tags.out, "refs/tags/v0.0.2");++ f.failed({"git", "mirror", "pt", "missing", "mirror"}, "unknown tag"); #endif } @@ -99,6 +162,7 @@ {"import_materialize_export", git_bridge_import_materialize_export}, {"invalid_import", git_bridge_invalid_import}, {"import_remote_options", git_bridge_import_remote_options},+ {"mirror_push_and_tag", git_mirror_push_and_tag}, }}; } } // namespace vn::integration